Created
March 13, 2019 21:42
-
-
Save alandipert/7a489e16af391ea3a8822e7e2acedf12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
slurp <- function(file) { | |
paste(readLines( | |
system.file(file, package = 'reactR') | |
), collapse = "\n") | |
} | |
# invoke file.edit in a way that will bind to the RStudio editor | |
# when running inside RStudio | |
fileEdit <- function(file) { | |
fileEditFunc <- eval(parse(text = "file.edit"), envir = globalenv()) | |
fileEditFunc(file) | |
} | |
startsWith <- function(s1, s2) { | |
substr(s1, 0, nchar(s2)) == s2 | |
} | |
renderTemplateIter <- function(str, substitutions) { | |
if (length(substitutions) == 0) return(str) | |
output <- "" | |
repeat { | |
if (nchar(str) == 0) return(output) | |
matched <- FALSE | |
for (name in names(substitutions)) { | |
placeholder <- paste0('${', name, '}') | |
if (startsWith(str, placeholder)) { | |
matched <- TRUE | |
output <- paste0(output, substitutions[[name]]) | |
str <- substr(str, nchar(name)+4, nchar(str)) | |
break | |
} | |
} | |
if (!matched) { | |
output <- paste0(output, substr(str, 1, 1)) | |
str <- substr(str, 2, nchar(str)) | |
} | |
} | |
output | |
} | |
matches <- function(str, pat) { | |
stopifnot(length(str) == 1) | |
match <- gregexpr(pat, str)[[1]] | |
if (any(match == -1)) return() | |
df <- data.frame( | |
start = match, | |
end = match + attr(match, 'match.length') - 1 | |
) | |
df[order(df$start),] | |
} | |
gfsub <- function(str, pat, func) { | |
if (nchar(str) == 0) return(str) | |
df <- matches(str, pat) | |
output <- "" | |
i <- 1 | |
repeat { | |
if (nrow(df) == 0 && nchar(str) > i) { | |
return(paste0(output, substr(str, i, nchar(str)))) | |
} else if (nrow(df) == 0) { | |
return(output) | |
} else { | |
h <- head(df) | |
output <- paste0(output, substr(str, i, h$start - 1)) | |
output <- paste0(output, func(substr(str, h$start, h$end))) | |
i <- h$end + 1 | |
df <- tail(df, -1) | |
} | |
} | |
output | |
} | |
renderTemplateGfsub <- function(str, substitutions) { | |
gfsub(str, '\\$\\{\\w+\\}', function(placeholder) { | |
substitutions[[substr(placeholder, 3, nchar(placeholder)-1)]] | |
}) | |
} | |
# Perform a series of pattern replacements on str. | |
# Example: renderTemplate("foo ${x} bar ${y} baz ${x}", list(x = 1, y = 2)) | |
# Produces: "foo 1 bar 2 baz 1" | |
renderTemplate <- function(str, substitutions) { | |
Reduce(function(str, name) { | |
gsub(paste0("\\$\\{", name, "\\}"), substitutions[[name]], str) | |
}, names(substitutions), str) | |
} | |
capitalize <- function(s) { | |
gsub("^(.)", perl = TRUE, replacement = '\\U\\1', s) | |
} | |
toDepJSON <- function(npmPkgs) { | |
if (is.null(npmPkgs)) { | |
"" | |
} else if (!length(names(npmPkgs))) { | |
stop("Must specify npm package names in the names attributes of npmPkgs") | |
} else { | |
paste0(sprintf('"%s": "%s"', names(npmPkgs), npmPkgs), collapse = ",\n") | |
} | |
} | |
# Wraps renderTemplate for convenient use from scaffold functions. | |
renderFile <- function(outputFile, templateFile, description = '', substitutions = list()) { | |
if (!file.exists(outputFile)) { | |
dir.create(dirname(outputFile), recursive = TRUE, showWarnings = FALSE) | |
cat(renderTemplate(slurp(templateFile), substitutions), file = outputFile) | |
message("Created ", description, " ", outputFile) | |
} else { | |
message(outputFile, " already exists") | |
} | |
outputFile | |
} | |
getPackage <- function() { | |
if (!file.exists('DESCRIPTION')) { | |
stop("You need to create a package to house your widget first!", call. = FALSE) | |
} | |
read.dcf('DESCRIPTION')[[1,"Package"]] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment