Skip to content

Commit 6c7940c

Browse files
committed
WIP input scaffold
1 parent 6c2414c commit 6c7940c

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

R/scaffold_input.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#' Create implementation scaffolding for a React.js-based Shiny input.
2+
#'
3+
#' Add the minimal code required to implement a React.js-based Shiny input to an
4+
#' R package.
5+
#'
6+
#' @param name Name of input
7+
#' @param npmPkgs Optional \href{https://npmjs.com/}{NPM} packages upon which
8+
#' this input is based which will be used to populate \code{package.json}.
9+
#' Should be a named list of names to
10+
#' \href{https://docs.npmjs.com/files/package.json#dependencies}{versions}.
11+
#' @param edit Automatically open the input's source files after creating the
12+
#' scaffolding.
13+
#'
14+
#' @note This function must be executed from the root directory of the package
15+
#' you wish to add the input to.
16+
#'
17+
#' @export
18+
scaffoldReactInput <- function(name, npmPkgs = NULL, edit = interactive()) {
19+
if (!file.exists('DESCRIPTION')){
20+
stop(
21+
"You need to create a package to house your widget first!",
22+
call. = F
23+
)
24+
}
25+
26+
package <- read.dcf('DESCRIPTION')[[1,"Package"]]
27+
28+
# Add input constructor
29+
renderFile(
30+
sprintf("R/%s.R", name),
31+
"templates/input_r.txt",
32+
"boilerplate for input constructor",
33+
list(
34+
name = name,
35+
package = package,
36+
capName = capitalize(name)
37+
)
38+
)
39+
# addInputConstructor(name, package, edit)
40+
# addInputJSON(toDepJSON(npmPkgs))
41+
# addWebpackConfig(name)
42+
# addWidgetJS(name, edit)
43+
# addExampleApp(name)
44+
#
45+
# usethis::use_build_ignore(c("node_modules", "srcjs"))
46+
# usethis::use_git_ignore(c("node_modules"))
47+
# lapply(c("htmltools", "htmlwidgets", "reactR"), usethis::use_package)
48+
#
49+
# message("To install dependencies from npm run: yarn install")
50+
# message("To build JavaScript run: yarn run webpack --mode=development")
51+
}
52+

inst/templates/input_r.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' <Add Title>
2+
#'
3+
#' <Add Description>
4+
#'
5+
#' @importFrom shiny restoreInput
6+
#' @importFrom reactR createReactInput
7+
#' @importFrom htmltools htmlDependency tags
8+
#'
9+
#' @export
10+
${name}Input <- function(inputId, default = "") {
11+
value <- restoreInput(id = inputId, default = default)
12+
createReactInput(
13+
inputId,
14+
"${name}",
15+
htmlDependency(
16+
name = "${name}-input",
17+
version = "1.0.0",
18+
src = "www/${name}",
19+
package = "${name}",
20+
script = "${name}.js"
21+
),
22+
value,
23+
list(),
24+
tags$span
25+
)
26+
}

0 commit comments

Comments
 (0)