|
| 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, as a two-element character vector of name and |
| 9 | +#' \href{https://docs.npmjs.com/files/package.json#dependencies}{version |
| 10 | +#' range}. If 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 | +} |
| 36 | + |
| 37 | +toDepJSON <- function(npmPkg) { |
| 38 | + if (is.null(npmPkg)) { |
| 39 | + "" |
| 40 | + } else { |
| 41 | + do.call(sprintf, as.list(c('"%s": "%s"', npmPkg))) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +slurp <- function(file) { |
| 46 | + paste(readLines( |
| 47 | + system.file(file, package = 'reactR') |
| 48 | + ), collapse = "\n") |
| 49 | +} |
| 50 | + |
| 51 | +addWidgetConstructor <- function(name, package, edit){ |
| 52 | + tpl <- slurp('templates/widget_r.txt') |
| 53 | + |
| 54 | + capName = function(name){ |
| 55 | + paste0(toupper(substring(name, 1, 1)), substring(name, 2)) |
| 56 | + } |
| 57 | + if (!file.exists(file_ <- sprintf("R/%s.R", name))){ |
| 58 | + cat( |
| 59 | + sprintf(tpl, name, name, package, name, name, name, name, name, name, package, name, capName(name), name, name, name), |
| 60 | + file = file_ |
| 61 | + ) |
| 62 | + message('Created boilerplate for widget constructor ', file_) |
| 63 | + } else { |
| 64 | + message(file_, " already exists") |
| 65 | + } |
| 66 | + if (edit) fileEdit(file_) |
| 67 | +} |
| 68 | + |
| 69 | +addWidgetYAML <- function(name, edit){ |
| 70 | + tpl <- "# (uncomment to add a dependency) |
| 71 | +# dependencies: |
| 72 | +# - name: |
| 73 | +# version: |
| 74 | +# src: |
| 75 | +# script: |
| 76 | +# stylesheet: |
| 77 | +" |
| 78 | + if (!file.exists('inst/htmlwidgets')){ |
| 79 | + dir.create('inst/htmlwidgets') |
| 80 | + } |
| 81 | + if (!file.exists(file_ <- sprintf('inst/htmlwidgets/%s.yaml', name))){ |
| 82 | + cat(tpl, file = file_) |
| 83 | + message('Created boilerplate for widget dependencies at ', |
| 84 | + sprintf('inst/htmlwidgets/%s.yaml', name) |
| 85 | + ) |
| 86 | + } else { |
| 87 | + message(file_, " already exists") |
| 88 | + } |
| 89 | + if (edit) fileEdit(file_) |
| 90 | +} |
| 91 | + |
| 92 | +addPackageJSON <- function(npmPkg) { |
| 93 | + tpl <- sprintf(slurp('templates/widget_package.json.txt'), npmPkg) |
| 94 | + if (!file.exists('package.json')) { |
| 95 | + cat(tpl, file = 'package.json') |
| 96 | + } else { |
| 97 | + message("package.json already exists") |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +addWebpackConfig <- function(name) { |
| 102 | + tpl <- sprintf(slurp('templates/widget_webpack.config.js.txt'), name, name) |
| 103 | + if (!file.exists('webpack.config.js')) { |
| 104 | + cat(tpl, file = 'webpack.config.js') |
| 105 | + } else { |
| 106 | + message("webpack.config.js already exists") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +addWidgetJS <- function(name, edit){ |
| 111 | + tpl <- paste(readLines( |
| 112 | + system.file('templates/widget_js.txt', package = 'reactR') |
| 113 | + ), collapse = "\n") |
| 114 | + if (!file.exists('srcjs')){ |
| 115 | + dir.create('srcjs') |
| 116 | + } |
| 117 | + if (!file.exists(file_ <- sprintf('srcjs/%s.js', name))){ |
| 118 | + cat(sprintf(tpl, name), file = file_) |
| 119 | + message('Created boilerplate for widget javascript bindings at ', |
| 120 | + sprintf('srcjs/%s.js', name) |
| 121 | + ) |
| 122 | + } else { |
| 123 | + message(file_, " already exists") |
| 124 | + } |
| 125 | + if (edit) fileEdit(file_) |
| 126 | +} |
| 127 | + |
| 128 | +# invoke file.edit in a way that will bind to the RStudio editor |
| 129 | +# when running inside RStudio |
| 130 | +fileEdit <- function(file) { |
| 131 | + fileEditFunc <- eval(parse(text = "file.edit"), envir = globalenv()) |
| 132 | + fileEditFunc(file) |
| 133 | +} |
0 commit comments