rcode.RdCompiles an RSP document and returns the generated source code script.
# S3 method for default
rcode(..., file=NULL, path=NULL, output=NULL, workdir=NULL, envir=parent.frame(),
args="*", verbose=FALSE)character strings with RSP markup.
Alternatively, a file, a URL or a connection from
with the strings are read.
If a file, the path is prepended to the file, iff given.
A character string or a connection specifying where
output should be directed.
The default is a file with a filename where the file extension(s)
(typically ".*.rsp") has been replaced by ".R"
in the directory given by the workdir argument.
The working directory to use after parsing and
preprocessing.
If argument output specifies an absolute pathname,
then the directory of output is used, otherwise the
current directory is used.
The environment in which the RSP string is
preprocessed and evaluated.
A named list of arguments assigned to the environment
in which the RSP string is parsed and evaluated.
See cmdArgs.
See Verbose.
Returns an RspFileProduct if possible,
otherwise an RspSourceCode.
# RSP-embedded text to an R script
code <- rcode("Current time is <%=Sys.time()%>\n")
cat(code, sep="\n")
#> ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
#> ## This is a self-contained R script generated from an RSP document.
#> ## It may be evaluated using source() as is.
#> ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
#>
#> ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#> ## Local RSP utility functions
#> ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#> ## RSP metadata function
#> .rmeta <- list(
#> )
#>
#> rmeta <- function(...) {
#> args <- list(...)
#> if (length(args) == 0) return(.rmeta)
#> names <- names(args)
#> if (length(names) > 0) {
#> for (name in names) .rmeta[[name]] <<- args[[name]]
#> } else {
#> names <- unlist(args, use.names=FALSE)
#> if (length(names) == 1) .rmeta[[names]] else .rmeta[names]
#> }
#> }
#>
#> ## Look up 'base' function once (faster)
#> if (getRversion() < "2.15.0") {
#> .base_paste <- base::paste
#> .base_paste0 <- function(...) .base_paste(..., sep="")
#> } else {
#> .base_paste0 <- base::paste0
#> }
#> .base_cat <- base::cat
#>
#> ## RSP output function
#> .rout <- function(x) .base_cat(.base_paste0(x))
#>
#> ## RSP output function for inline RSP constructs
#> .rout0 <- function(x) .base_cat(rpaste(x))
#>
#> ## The output of inline RSP constructs is controlled by
#> ## generic function rpaste().
#> rpaste <- function(...) UseMethod("rpaste")
#>
#> setInlineRsp <- function(class, fun, envir=parent.frame()) {
#> name <- sprintf("rpaste.%s", class)
#> assign(name, fun, envir=envir)
#> ## FIXME: How to register an S3 method at run-time? /HB 2018-04-06
#> ## registerS3method("rpaste", class = class, method = name, envir = envir)
#> }
#>
#> ## The default is to coerce to character and collapse without
#> ## a separator. It is possible to override the default in an
#> ## RSP code expression.
#> setInlineRsp("default", function(x, ...) .base_paste0(x, collapse=""))
#>
#> ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#> ## RSP source code script [BEGIN]
#> ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#> .rout("Current time is ")
#> .rout0(Sys.time())
#> .rout("\n")
#> ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#> ## RSP source code script [END]
#> ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# RSP-embedded text to a shell script
code <- rcode("Current time is <%=date%>\n", language="sh")
cat(code, sep="\n")
#>
#> printf "Current time is "
#> printf "date"
#> printf "\n"
#>