Skip to content

Commit

Permalink
Merge pull request #22 from react-R/input-binding
Browse files Browse the repository at this point in the history
Easy React-based input bindings
  • Loading branch information
timelyportfolio committed Apr 9, 2019
2 parents c0c08fd + 38aca8b commit 449d3c3
Show file tree
Hide file tree
Showing 30 changed files with 1,365 additions and 439 deletions.
16 changes: 8 additions & 8 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
^pkgdown$
^assets$
^.*\.Rproj$
^\.Rproj\.user$
^\build
^CONDUCT\.md$
^cran-comments\.md$
^docs$
^README\.Rmd$
^\.Rproj\.user$
^\.travis\.yml$
^\assets
^build$
^docs$
^cran-comments\.md$
^karma\.conf\.js$
^logo.svg$
^package\.json$
^webpack\.config\.js$
^yarn\.lock$
^README\.Rmd$
logo.svg
webpack.config.js
^logo\.svg$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Meta
doc
.Rproj.user
.Rhistory
.RData
Expand All @@ -7,3 +9,4 @@ node_modules
reactR.Rcheck
reactR_*.tar.gz
*.swp
.DS_Store
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: reactR
Type: Package
Title: React Helpers
Version: 0.3.1
Date: 2019-01-26
Version: 0.4.0
Date: 2019-04-10
Authors@R: c(
person(
"Facebook", "Inc"
Expand Down Expand Up @@ -39,6 +39,7 @@ Suggests:
shiny,
V8,
knitr,
usethis
usethis,
jsonlite
RoxygenNote: 6.1.1
VignetteBuilder: knitr
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ S3method("[[<-",react_component_builder)
export(React)
export(babel_transform)
export(component)
export(createReactShinyInput)
export(html_dependency_corejs)
export(html_dependency_react)
export(html_dependency_reacttools)
export(reactMarkup)
export(scaffoldReactShinyInput)
export(scaffoldReactWidget)
importFrom(htmltools,htmlDependency)
60 changes: 60 additions & 0 deletions R/reacttools.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# A robust name string is a valid
# - CSS class
# - JavaScript variable name
# - R variable name
robustName <- "^[[:alpha:]_][[:alnum:]_]*$"

isUpper <- function(s) {
grepl("^[[:upper:]]+$", s)
}
Expand Down Expand Up @@ -92,3 +98,57 @@ reactMarkup <- function(tag) {
list(tag = tag, class = "reactR_markup")
}

#' Create a React-based input
#'
#' @param inputId The \code{input} slot that will be used to access the value.
#' @param class Space-delimited list of CSS class names that should identify
#' this input type in the browser.
#' @param dependencies HTML dependencies to include in addition to those
#' supporting React. Must contain at least one dependency, that of the input's
#' implementation.
#' @param default Initial value.
#' @param configuration Static configuration data.
#' @param container Function to generate an HTML element to contain the input.
#'
#' @return Shiny input suitable for inclusion in a UI.
#' @export
#'
#' @examples
#' myInput <- function(inputId, default = "") {
#' # The value of createReactShinyInput should be returned from input constructor functions.
#' createReactShinyInput(
#' inputId,
#' "myinput",
#' # At least one htmlDependency must be provided -- the JavaScript implementation of the input.
#' htmlDependency(
#' name = "my-input",
#' version = "1.0.0",
#' src = "www/mypackage/myinput",
#' package = "mypackage",
#' script = "myinput.js"
#' ),
#' default
#' )
#' }
createReactShinyInput <- function(inputId,
class,
dependencies,
default = NULL,
configuration = list(),
container = htmltools::tags$div) {
if(length(dependencies) < 1) stop("Must include at least one HTML dependency.")
value <- shiny::restoreInput(id = inputId, default = default)
htmltools::tagList(
html_dependency_corejs(),
html_dependency_react(),
html_dependency_reacttools(),
container(id = inputId, class = class),
htmltools::tags$script(id = sprintf("%s_value", inputId),
type = "application/json",
jsonlite::toJSON(value, auto_unbox = TRUE)),
htmltools::tags$script(id = sprintf("%s_configuration", inputId),
type = "application/json",
jsonlite::toJSON(configuration, auto_unbox = TRUE)),
dependencies
)
}
163 changes: 0 additions & 163 deletions R/scaffold.R

This file was deleted.

78 changes: 78 additions & 0 deletions R/scaffold_input.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#' Create implementation scaffolding for a React.js-based Shiny input.
#'
#' Add the minimal code required to implement a React.js-based Shiny input to an
#' R package.
#'
#' @param name Name of input
#' @param npmPkgs Optional \href{https://npmjs.com/}{NPM} packages upon which
#' this input is based which will be used to populate \code{package.json}.
#' Should be a named list of names to
#' \href{https://docs.npmjs.com/files/package.json#dependencies}{versions}.
#' @param edit Automatically open the input's source files after creating the
#' scaffolding.
#'
#' @note This function must be executed from the root directory of the package
#' you wish to add the input to.
#'
#' @export
scaffoldReactShinyInput <- function(name, npmPkgs = NULL, edit = interactive()) {
assertNameValid(name)
package <- getPackage()

file <- renderFile(
sprintf("R/%s.R", name),
"templates/input_r.txt",
"boilerplate for input constructor",
list(
name = name,
capName = capitalize(name),
package = package
)
)
if (edit) fileEdit(file)

renderFile(
'package.json',
'templates/package.json.txt',
'project metadata',
list(npmPkgs = toDepJSON(npmPkgs))
)

renderFile(
'webpack.config.js',
'templates/webpack.config.js.txt',
'webpack configuration',
list(
name = name,
outputPath = sprintf("inst/www/%s/%s", package, name)
)
)

renderFile(
sprintf('srcjs/%s.jsx', name),
'templates/input_js.txt',
'JavaScript implementation',
list(
name = name,
package = package
)
)

renderFile(
'app.R',
'templates/input_app.R.txt',
'example app',
list(
name = name,
package = package
)
)

usethis::use_build_ignore(c("node_modules", "srcjs", "app.R", "package.json", "webpack.config.js", "yarn.lock"))
usethis::use_git_ignore(c("node_modules"))
lapply(c("htmltools", "shiny", "reactR"), usethis::use_package)

message("To install dependencies from npm run: yarn install")
message("To build JavaScript run: yarn run webpack --mode=development")
}

Loading

0 comments on commit 449d3c3

Please sign in to comment.