Skip to content

Commit 2602d41

Browse files
committed
Working input scaffold; dedup package.json and webpack templates
1 parent b48c208 commit 2602d41

File tree

12 files changed

+122
-61
lines changed

12 files changed

+122
-61
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export(html_dependency_corejs)
1212
export(html_dependency_react)
1313
export(html_dependency_reacttools)
1414
export(reactMarkup)
15+
export(scaffoldReactInput)
1516
export(scaffoldReactWidget)
1617
importFrom(htmltools,htmlDependency)

R/reacttools.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ createReactInput <- function(inputId,
119119
container(id = inputId, class = class),
120120
htmltools::tags$script(id = sprintf("%s_value", inputId),
121121
type = "application/json",
122-
jsonlite::toJSON(value, auto_unbox = FALSE)),
122+
jsonlite::toJSON(value, auto_unbox = TRUE)),
123123
htmltools::tags$script(id = sprintf("%s_configuration", inputId),
124124
type = "application/json",
125-
jsonlite::toJSON(configuration, auto_unbox = FALSE)),
125+
jsonlite::toJSON(configuration, auto_unbox = TRUE)),
126126
html_dependency_react(),
127127
html_dependency_reacttools(),
128128
dependencies

R/scaffold_input.R

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,61 @@
1616
#'
1717
#' @export
1818
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"]]
19+
package <- getPackage()
2720

28-
# Add input constructor
29-
renderFile(
21+
file <- renderFile(
3022
sprintf("R/%s.R", name),
3123
"templates/input_r.txt",
3224
"boilerplate for input constructor",
3325
list(
3426
name = name,
35-
package = package,
36-
capName = capitalize(name)
27+
package = package
28+
)
29+
)
30+
if (edit) fileEdit(file)
31+
32+
renderFile(
33+
'package.json',
34+
'templates/package.json.txt',
35+
'project metadata',
36+
list(npmPkgs = toDepJSON(npmPkgs))
37+
)
38+
39+
renderFile(
40+
'webpack.config.js',
41+
'templates/webpack.config.js.txt',
42+
'webpack configuration',
43+
list(
44+
name = name,
45+
outputPath = sprintf("inst/www/%s/%s", package, name)
46+
)
47+
)
48+
49+
renderFile(
50+
sprintf('srcjs/%s.jsx', name),
51+
'templates/input_js.txt',
52+
'JavaScript implementation',
53+
list(
54+
name = name,
55+
package = package
3756
)
3857
)
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")
58+
59+
renderFile(
60+
'app.R',
61+
'templates/input_app.R.txt',
62+
'example app',
63+
list(
64+
name = name,
65+
package = package
66+
)
67+
)
68+
69+
usethis::use_build_ignore(c("node_modules", "srcjs"))
70+
usethis::use_git_ignore(c("node_modules"))
71+
lapply(c("htmltools", "htmlwidgets", "reactR"), usethis::use_package)
72+
73+
message("To install dependencies from npm run: yarn install")
74+
message("To build JavaScript run: yarn run webpack --mode=development")
5175
}
5276

R/scaffold_utils.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ renderFile <- function(outputFile, templateFile, description = '', substitutions
4646
outputFile
4747
}
4848

49+
getPackage <- function() {
50+
if (!file.exists('DESCRIPTION')) {
51+
stop("You need to create a package to house your widget first!", call. = FALSE)
52+
}
53+
read.dcf('DESCRIPTION')[[1,"Package"]]
54+
}

R/scaffold_widget.R

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@
1616
#'
1717
#' @export
1818
scaffoldReactWidget <- 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-
}
19+
package <- getPackage()
2520

26-
package <- read.dcf('DESCRIPTION')[[1,"Package"]]
2721
addWidgetConstructor(name, package, edit)
2822
addWidgetYAML(name, edit)
2923
addPackageJSON(toDepJSON(npmPkgs))
@@ -65,7 +59,7 @@ addWidgetYAML <- function(name, edit){
6559
addPackageJSON <- function(npmPkgs) {
6660
renderFile(
6761
'package.json',
68-
'templates/widget_package.json.txt',
62+
'templates/package.json.txt',
6963
'project metadata',
7064
list(npmPkgs = npmPkgs)
7165
)
@@ -74,9 +68,12 @@ addPackageJSON <- function(npmPkgs) {
7468
addWebpackConfig <- function(name) {
7569
renderFile(
7670
'webpack.config.js',
77-
'templates/widget_webpack.config.js.txt',
71+
'templates/webpack.config.js.txt',
7872
'webpack configuration',
79-
list(name = name)
73+
list(
74+
name = name,
75+
outputPath = 'inst/htmlwidgets'
76+
)
8077
)
8178
}
8279

inst/templates/input_app.R.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library(shiny)
2+
library(${package})
3+
4+
ui <- fluidPage(
5+
titlePanel("reactR Input Example"),
6+
${name}Input("textInput"),
7+
textOutput("textOutput")
8+
)
9+
10+
server <- function(input, output, session) {
11+
output$textOutput <- renderText({
12+
sprintf("You entered: %s", input$textInput)
13+
})
14+
}
15+
16+
shinyApp(ui, server)

inst/templates/input_js.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { reactInput } from 'reactR';
2+
3+
const TextInput = ({ configuration, value, setValue }) => {
4+
return <input type='text' value={value} onChange={e => setValue(e.target.value)}/>;
5+
};
6+
7+
reactInput('.${name}', '${package}.${name}', TextInput);

inst/templates/input_r.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ ${name}Input <- function(inputId, default = "") {
1515
htmlDependency(
1616
name = "${name}-input",
1717
version = "1.0.0",
18-
src = "www/${name}",
19-
package = "${name}",
18+
src = "www/${package}/${name}",
19+
package = "${package}",
2020
script = "${name}.js"
2121
),
2222
value,

inst/templates/widget_webpack.config.js.txt renamed to inst/templates/webpack.config.js.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ var path = require('path');
22

33
module.exports = {
44
mode: 'development',
5-
entry: path.join(__dirname, 'srcjs', '${name}.js'),
5+
entry: path.join(__dirname, 'srcjs', '${name}.jsx'),
66
output: {
7-
path: path.join(__dirname, 'inst', 'htmlwidgets'),
7+
path: path.join(__dirname, 'inst', 'www', '${package}', '${name}'),
8+
path: path.join(__dirname, '${outputPath}'),
89
filename: '${name}.js'
910
},
1011
module: {
1112
rules: [
1213
{
13-
test: /\.js$/,
14+
test: /\.jsx?$/,
1415
loader: 'babel-loader',
1516
options: {
1617
presets: ['@babel/preset-env', '@babel/preset-react']

0 commit comments

Comments
 (0)