|
| 1 | +#' Create implementation scaffolding for a React.js-based HTML widget |
| 2 | +#' |
| 3 | +#' Add the minimal code required to implement a React.js-based HTML widget to an |
| 4 | +#' R package. |
| 5 | +#' |
| 6 | +#' @param name Name of widget |
| 7 | +#' @param npmPkg Optional \href{https://npmjs.com/}{NPM} package upon which this |
| 8 | +#' widget is based, a named list with two elements: \code{name} and |
| 9 | +#' \href{https://docs.npmjs.com/files/package.json#dependencies}{version}. If |
| 10 | +#' you specify this parameter the package will be added to the |
| 11 | +#' \code{dependency} section of the generated \code{package.json}. |
| 12 | +#' @param edit Automatically open the widget's JavaScript source file after |
| 13 | +#' creating the scaffolding. |
| 14 | +#' |
| 15 | +#' @note This function must be executed from the root directory of the package |
| 16 | +#' you wish to add the widget to. |
| 17 | +#' |
| 18 | +#' @export |
| 19 | +scaffoldReactWidget <- function(name, npmPkg = NULL, edit = interactive()){ |
| 20 | + if (!file.exists('DESCRIPTION')){ |
| 21 | + stop( |
| 22 | + "You need to create a package to house your widget first!", |
| 23 | + call. = F |
| 24 | + ) |
| 25 | + } |
| 26 | + if (!file.exists('inst')){ |
| 27 | + dir.create('inst') |
| 28 | + } |
| 29 | + package <- read.dcf('DESCRIPTION')[[1,"Package"]] |
| 30 | + addWidgetConstructor(name, package, edit) |
| 31 | + addWidgetYAML(name, edit) |
| 32 | + addPackageJSON(toDepJSON(npmPkg)) |
| 33 | + addWebpackConfig(name) |
| 34 | + 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") |
| 38 | +} |
| 39 | + |
| 40 | +toDepJSON <- function(npmPkg) { |
| 41 | + if (is.null(npmPkg)) { |
| 42 | + "" |
| 43 | + } else { |
| 44 | + sprintf('"%s": "%s"', npmPkg$name, npmPkg$version) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +slurp <- function(file) { |
| 49 | + paste(readLines( |
| 50 | + system.file(file, package = 'reactR') |
| 51 | + ), collapse = "\n") |
| 52 | +} |
| 53 | + |
| 54 | +# Perform a series of pattern replacements on str. |
| 55 | +# Example: renderTemplate("foo ${x} bar ${y} baz ${x}", list(x = 1, y = 2)) |
| 56 | +# Produces: "foo 1 bar 2 baz 1" |
| 57 | +renderTemplate <- function(str, substitutions) { |
| 58 | + Reduce(function(str, name) { |
| 59 | + gsub(paste0("\\$\\{", name, "\\}"), substitutions[[name]], str) |
| 60 | + }, names(substitutions), str) |
| 61 | +} |
| 62 | + |
| 63 | +capName = function(name){ |
| 64 | + paste0(toupper(substring(name, 1, 1)), substring(name, 2)) |
| 65 | +} |
| 66 | + |
| 67 | +addWidgetConstructor <- function(name, package, edit){ |
| 68 | + tpl <- slurp('templates/widget_r.txt') |
| 69 | + if (!file.exists(file_ <- sprintf("R/%s.R", name))){ |
| 70 | + cat( |
| 71 | + renderTemplate(tpl, list(name = name, package = package, capName = capName(name))), |
| 72 | + file = file_ |
| 73 | + ) |
| 74 | + message('Created boilerplate for widget constructor ', file_) |
| 75 | + } else { |
| 76 | + message(file_, " already exists") |
| 77 | + } |
| 78 | + if (edit) fileEdit(file_) |
| 79 | +} |
| 80 | + |
| 81 | +addWidgetYAML <- function(name, edit){ |
| 82 | + tpl <- "# (uncomment to add a dependency) |
| 83 | +# dependencies: |
| 84 | +# - name: |
| 85 | +# version: |
| 86 | +# src: |
| 87 | +# script: |
| 88 | +# stylesheet: |
| 89 | +" |
| 90 | + if (!file.exists('inst/htmlwidgets')){ |
| 91 | + dir.create('inst/htmlwidgets') |
| 92 | + } |
| 93 | + if (!file.exists(file_ <- sprintf('inst/htmlwidgets/%s.yaml', name))){ |
| 94 | + cat(tpl, file = file_) |
| 95 | + message('Created boilerplate for widget dependencies at ', |
| 96 | + sprintf('inst/htmlwidgets/%s.yaml', name) |
| 97 | + ) |
| 98 | + } else { |
| 99 | + message(file_, " already exists") |
| 100 | + } |
| 101 | + if (edit) fileEdit(file_) |
| 102 | +} |
| 103 | + |
| 104 | +addPackageJSON <- function(npmPkg) { |
| 105 | + tpl <- renderTemplate(slurp('templates/widget_package.json.txt'), list(npmPkg = npmPkg)) |
| 106 | + if (!file.exists('package.json')) { |
| 107 | + cat(tpl, file = 'package.json') |
| 108 | + message('Created package.json') |
| 109 | + } else { |
| 110 | + message("package.json already exists") |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +addWebpackConfig <- function(name) { |
| 115 | + tpl <- renderTemplate(slurp('templates/widget_webpack.config.js.txt'), list(name = name)) |
| 116 | + if (!file.exists('webpack.config.js')) { |
| 117 | + cat(tpl, file = 'webpack.config.js') |
| 118 | + message('Created webpack.config.js') |
| 119 | + } else { |
| 120 | + message("webpack.config.js already exists") |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +addWidgetJS <- function(name, edit){ |
| 125 | + tpl <- paste(readLines( |
| 126 | + system.file('templates/widget_js.txt', package = 'reactR') |
| 127 | + ), collapse = "\n") |
| 128 | + if (!file.exists('srcjs')){ |
| 129 | + dir.create('srcjs') |
| 130 | + } |
| 131 | + if (!file.exists(file_ <- sprintf('srcjs/%s.js', name))){ |
| 132 | + cat(renderTemplate(tpl, list(name = name)), file = file_) |
| 133 | + message('Created boilerplate for widget javascript bindings at ', |
| 134 | + sprintf('srcjs/%s.js', name) |
| 135 | + ) |
| 136 | + } else { |
| 137 | + message(file_, " already exists") |
| 138 | + } |
| 139 | + if (edit) fileEdit(file_) |
| 140 | +} |
| 141 | + |
| 142 | +addExampleApp <- function(name) { |
| 143 | + tpl <- renderTemplate(slurp('templates/widget_app.R.txt'), list(name = name, capName = capName(name))) |
| 144 | + if (!file.exists('app.R')) { |
| 145 | + cat(tpl, file = 'app.R') |
| 146 | + message('Created example app.R') |
| 147 | + } else { |
| 148 | + message("app.R already exists") |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +# invoke file.edit in a way that will bind to the RStudio editor |
| 153 | +# when running inside RStudio |
| 154 | +fileEdit <- function(file) { |
| 155 | + fileEditFunc <- eval(parse(text = "file.edit"), envir = globalenv()) |
| 156 | + fileEditFunc(file) |
| 157 | +} |
0 commit comments