From 794f99c3e4648be2364dfb279ab339126b9c418d Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Wed, 18 Oct 2017 16:26:47 -0700 Subject: [PATCH 1/7] add skeleton.dcf --- inst/rstudio/templates/skeleton.dcf | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 inst/rstudio/templates/skeleton.dcf diff --git a/inst/rstudio/templates/skeleton.dcf b/inst/rstudio/templates/skeleton.dcf new file mode 100644 index 00000000..55f173ab --- /dev/null +++ b/inst/rstudio/templates/skeleton.dcf @@ -0,0 +1,3 @@ +Title: R Package using htmlwidgets +Binding: project_template +OpenFiles: R/main.R, java/main.scala From a44a51e764135381513e8ed846c598d974e5e034 Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Wed, 18 Oct 2017 17:19:00 -0700 Subject: [PATCH 2/7] move skeleton into projects --- inst/rstudio/templates/project/skeleton.dcf | 3 +++ inst/rstudio/templates/skeleton.dcf | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 inst/rstudio/templates/project/skeleton.dcf delete mode 100644 inst/rstudio/templates/skeleton.dcf diff --git a/inst/rstudio/templates/project/skeleton.dcf b/inst/rstudio/templates/project/skeleton.dcf new file mode 100644 index 00000000..c21d3cfe --- /dev/null +++ b/inst/rstudio/templates/project/skeleton.dcf @@ -0,0 +1,3 @@ +Title: R Package using htmlwidgets +Binding: projectTemplate +OpenFiles: README.Rmd, inst/htmlwidgets/widget.js, R/main.R, java/main.scala diff --git a/inst/rstudio/templates/skeleton.dcf b/inst/rstudio/templates/skeleton.dcf deleted file mode 100644 index 55f173ab..00000000 --- a/inst/rstudio/templates/skeleton.dcf +++ /dev/null @@ -1,3 +0,0 @@ -Title: R Package using htmlwidgets -Binding: project_template -OpenFiles: R/main.R, java/main.scala From e6bc7f61b391d4b3c18523b0f1b1c71b1670f9f3 Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Wed, 18 Oct 2017 17:19:08 -0700 Subject: [PATCH 3/7] add project generation code --- R/project.R | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 R/project.R diff --git a/R/project.R b/R/project.R new file mode 100644 index 00000000..354a81b7 --- /dev/null +++ b/R/project.R @@ -0,0 +1,127 @@ +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", + " };", + " }", + "});" + ) + + description_file <- c( + paste("Package: ", project_name, sep = ""), + "Type: Package", + "Title: What the Package Does (Title Case)", + "Version: 0.1.0", + "Author: Who wrote it", + "Maintainer: The package maintainer ", + "Description: More about what it does (maybe more than one line)", + " Use four spaces when indenting paragraphs within the Description.", + "License: What license is it under?", + "Encoding: UTF-8", + "LazyData: true", + "Depends:", + " R (>= 3.1.2)", + "Imports:", + " htmlwidgets" + ) + + namespace_file <- c( + paste("export(", project_name, ")", sep = ""), + "import(sparklyr)" + ) + + readme_file <- c( + "---", + "title: \"What the Package Does (Title Case)\"", + "output:", + " github_document:", + " fig_width: 9", + " fig_height: 5", + "---", + "", + "## Getting Started", + "", + "Build the package then launch this `htmlwidget` as follows:", + "", + "```{r}", + paste("library(", project_name, ")", sep = ""), + paste(project_name, "()", 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 = "."))) + + 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")) + + TRUE +} From 746bab4dd73f7138b69c4bea941fccb9b93bc083 Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Wed, 18 Oct 2017 17:24:32 -0700 Subject: [PATCH 4/7] disable knitting htmlwidget into github readme --- R/project.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/project.R b/R/project.R index 354a81b7..dc798316 100644 --- a/R/project.R +++ b/R/project.R @@ -102,7 +102,7 @@ projectTemplate <- function(path, ...) { "", "Build the package then launch this `htmlwidget` as follows:", "", - "```{r}", + "```{r eval=FALSE}", paste("library(", project_name, ")", sep = ""), paste(project_name, "()", sep = ""), "```" From 82b8f2e3bd8e83ed9192e4f0065e9c813591a99d Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Thu, 14 Dec 2017 16:42:52 -0800 Subject: [PATCH 5/7] 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 From 0cb752d0d84d2ab5b2be41f57a520302f8bf65be Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Thu, 14 Dec 2017 16:46:15 -0800 Subject: [PATCH 6/7] tweak project template bower label --- inst/rstudio/templates/project/skeleton.dcf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/rstudio/templates/project/skeleton.dcf b/inst/rstudio/templates/project/skeleton.dcf index adad132c..5277dba2 100644 --- a/inst/rstudio/templates/project/skeleton.dcf +++ b/inst/rstudio/templates/project/skeleton.dcf @@ -4,4 +4,4 @@ OpenFiles: README.Rmd Parameter: bower Widget: TextInput -Label: Bower Dependencies +Label: Bower package From 9f051bce0fa81beedcf647a605b1d62caaf8d747 Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Tue, 20 Mar 2018 14:27:50 -0700 Subject: [PATCH 7/7] export project creation file and rebuild docs --- NAMESPACE | 1 + R/project.R | 6 ++++++ man/projectTemplate.Rd | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 man/projectTemplate.Rd diff --git a/NAMESPACE b/NAMESPACE index c37ce80f..4109486d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,6 +10,7 @@ export(getDependency) export(onRender) export(onStaticRenderComplete) export(prependContent) +export(projectTemplate) export(saveWidget) export(scaffoldWidget) export(setWidgetIdSeed) diff --git a/R/project.R b/R/project.R index 6a6d39b3..f362d916 100644 --- a/R/project.R +++ b/R/project.R @@ -1,3 +1,9 @@ +#' Creates an RStudio Project Template +#' +#' @param path Destination path where this project will be created. +#' @param ... Additional parameters, currently not used. +#' +#' @export projectTemplate <- function(path, ...) { project_name <- tolower(basename(path)) diff --git a/man/projectTemplate.Rd b/man/projectTemplate.Rd new file mode 100644 index 00000000..a9a92bca --- /dev/null +++ b/man/projectTemplate.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/project.R +\name{projectTemplate} +\alias{projectTemplate} +\title{Creates an RStudio Project Template} +\usage{ +projectTemplate(path, ...) +} +\arguments{ +\item{path}{Destination path where this project will be created.} + +\item{...}{Additional parameters, currently not used.} +} +\description{ +Creates an RStudio Project Template +}