Skip to content

Commit 8c26d12

Browse files
committed
Add example app.R during scaffolding
1 parent 2b96174 commit 8c26d12

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

R/scaffold.R

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ scaffoldReactWidget <- function(name, npmPkg = NULL, edit = interactive()){
3232
addPackageJSON(toDepJSON(npmPkg))
3333
addWebpackConfig(name)
3434
addWidgetJS(name, edit)
35+
addExampleApp(name)
36+
message("To install dependencies from npm run: yarn install")
37+
message("To build JavaScript run: yarn run webpack --mode=development")
3538
}
3639

3740
toDepJSON <- function(npmPkg) {
@@ -48,12 +51,12 @@ slurp <- function(file) {
4851
), collapse = "\n")
4952
}
5053

54+
capName = function(name){
55+
paste0(toupper(substring(name, 1, 1)), substring(name, 2))
56+
}
57+
5158
addWidgetConstructor <- function(name, package, edit){
5259
tpl <- slurp('templates/widget_r.txt')
53-
54-
capName = function(name){
55-
paste0(toupper(substring(name, 1, 1)), substring(name, 2))
56-
}
5760
if (!file.exists(file_ <- sprintf("R/%s.R", name))){
5861
cat(
5962
sprintf(tpl, name, name, package, name, name, name, name, name, name, package, name, capName(name), name, name, name),
@@ -93,6 +96,7 @@ addPackageJSON <- function(npmPkg) {
9396
tpl <- sprintf(slurp('templates/widget_package.json.txt'), npmPkg)
9497
if (!file.exists('package.json')) {
9598
cat(tpl, file = 'package.json')
99+
message('Created package.json')
96100
} else {
97101
message("package.json already exists")
98102
}
@@ -102,6 +106,7 @@ addWebpackConfig <- function(name) {
102106
tpl <- sprintf(slurp('templates/widget_webpack.config.js.txt'), name, name)
103107
if (!file.exists('webpack.config.js')) {
104108
cat(tpl, file = 'webpack.config.js')
109+
message('Created webpack.config.js')
105110
} else {
106111
message("webpack.config.js already exists")
107112
}
@@ -125,6 +130,16 @@ addWidgetJS <- function(name, edit){
125130
if (edit) fileEdit(file_)
126131
}
127132

133+
addExampleApp <- function(name) {
134+
tpl <- sprintf(slurp('templates/widget_app.R.txt'), name, name, capName(name), name)
135+
if (!file.exists('app.R')) {
136+
cat(tpl, file = 'app.R')
137+
message('Created example app.R')
138+
} else {
139+
message("app.R already exists")
140+
}
141+
}
142+
128143
# invoke file.edit in a way that will bind to the RStudio editor
129144
# when running inside RStudio
130145
fileEdit <- function(file) {

inst/templates/widget_app.R.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
library(shiny)
2+
library(%s)
3+
4+
ui <- fluidPage(
5+
titlePanel("reactR HTMLWidget Example"),
6+
%sOutput('widgetOutput')
7+
)
8+
9+
server <- function(input, output, session) {
10+
output$widgetOutput <- render%s(
11+
%s("Hello world!")
12+
)
13+
}
14+
15+
shinyApp(ui, server)

0 commit comments

Comments
 (0)