From 01f5a04d20d45a6f0776bb3ca7205f848620d19b Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Thu, 14 Dec 2017 16:42:52 -0800 Subject: [PATCH] add bower support in project template --- R/project.R | 86 +++------------------ inst/rstudio/templates/project/skeleton.dcf | 6 +- 2 files changed, 14 insertions(+), 78 deletions(-) diff --git a/R/project.R b/R/project.R index dc798316..6a6d39b3 100644 --- a/R/project.R +++ b/R/project.R @@ -1,70 +1,8 @@ projectTemplate <- function(path, ...) { project_name <- tolower(basename(path)) - htmlwidgets_path <- file.path(path, "inst/htmlwidgets") - htmlwidgets_lib_path <- file.path(path, "inst/htmlwidgets/lib") - - r_path <- file.path(path, "R") - - dependencies_yaml <- c( - "dependencies:", - paste(" - name: ", project_name, sep = ""), - " version: 1.0.0", - " src: htmlwidgets/lib", - " script:", - " - empty.js" - ) - - main_r <- c( - "#' @import htmlwidgets", - "#' @export", - paste(project_name, " <- function(width = NULL, height = NULL) {", sep = ""), - " # pass the data and settings using 'x'", - " x <- list(", - " data = \"1 2 3\"", - " )", - "", - paste(" htmlwidgets::createWidget(\"", project_name, "\", x, width = width, height = height)", sep = ""), - "}", - "", - "#' @export", - paste(project_name, "Output <- function(outputId, width = \"100%\", height = \"400px\") {", sep = ""), - paste(" shinyWidgetOutput(outputId, \"", project_name, "\", width, height, package = \"", project_name, "\")", sep = ""), - "}", - "", - "#' @export", - paste( - "render", - toupper(substr(project_name, 1, 1)), - substr(project_name, 2, nchar(project_name)), - " <- function(expr, env = parent.frame(), quoted = FALSE) {", - sep = "" - ), - " if (!quoted) { expr <- substitute(expr) } # force quoted", - paste(" shinyRenderWidget(expr, ", project_name, "Output, env, quoted = TRUE)", sep = ""), - "}" - ) - - main_js <- c( - "HTMLWidgets.widget({", - paste(" name: \"", project_name, "\",", sep = ""), - " type: \"output\",", - " factory: function(el, width, height) {", - " var div = document.createElement(\"div\");", - " el.appendChild(div);", - "", - " return {", - " renderValue: function(x) {", - " div.innerText = x.data;", - " },", - " resize: function(width, heigh) {", - " },", - " // make the object available as a property on the widget", - " div: div", - " };", - " }", - "});" - ) + dots <- list(...) + bower <- if (nchar(dots$bower) == 0) NULL else dots$bower description_file <- c( paste("Package: ", project_name, sep = ""), @@ -85,8 +23,7 @@ projectTemplate <- function(path, ...) { ) namespace_file <- c( - paste("export(", project_name, ")", sep = ""), - "import(sparklyr)" + paste("export(", project_name, ")", sep = "") ) readme_file <- c( @@ -104,24 +41,19 @@ projectTemplate <- function(path, ...) { "", "```{r eval=FALSE}", paste("library(", project_name, ")", sep = ""), - paste(project_name, "()", sep = ""), + paste(project_name, "(\"Hello World\")", sep = ""), "```" ) - dir.create(htmlwidgets_path, recursive = TRUE, showWarnings = FALSE) - dir.create(htmlwidgets_lib_path, recursive = TRUE, showWarnings = FALSE) - - dir.create(r_path, recursive = TRUE, showWarnings = FALSE) - - writeLines(dependencies_yaml, con = file.path(htmlwidgets_path, paste(project_name, "yaml", sep = "."))) - writeLines("", con = file.path(htmlwidgets_lib_path, "empty.js")) - - writeLines(main_r, con = file.path(r_path, paste(project_name, "R", sep = "."))) - writeLines(main_js, con = file.path(htmlwidgets_path, paste(project_name, "js", sep = "."))) + dir.create(path, recursive = TRUE) + dir.create(file.path(path, "R"), recursive = TRUE) writeLines(description_file, con = file.path(path, "DESCRIPTION")) writeLines(namespace_file, con = file.path(path, "NAMESPACE")) writeLines(readme_file, con = file.path(path, "README.Rmd")) + setwd(project_name) + scaffoldWidget(name = project_name, edit = FALSE, bowerPkg = bower) + TRUE } diff --git a/inst/rstudio/templates/project/skeleton.dcf b/inst/rstudio/templates/project/skeleton.dcf index c21d3cfe..adad132c 100644 --- a/inst/rstudio/templates/project/skeleton.dcf +++ b/inst/rstudio/templates/project/skeleton.dcf @@ -1,3 +1,7 @@ Title: R Package using htmlwidgets Binding: projectTemplate -OpenFiles: README.Rmd, inst/htmlwidgets/widget.js, R/main.R, java/main.scala +OpenFiles: README.Rmd + +Parameter: bower +Widget: TextInput +Label: Bower Dependencies