R Under development (unstable) (2024-01-28 r85838 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("R.rsp") R.rsp v0.46.0 successfully loaded. See ?R.rsp for help. > > # RSP document > pathname <- system.file("exData", "slowcounting.txt.rsp", package="R.rsp") > > # Compile to RSP source code script file > metadata <- list(foo="ABC", bar="123") > pathnameR <- rcode(file=pathname, metadata=metadata) > print(pathnameR) RspFileProduct: Pathname: d:/RCompile/CRANincoming/R-devel/R.rsp.Rcheck/tests/slowcounting.txt.R File size: 2.36 kB (2417 bytes) Content type: application/R Metadata 'foo': 'ABC' Metadata 'bar': '123' Has processor: FALSE > # Assert correct filename > stopifnot(basename(pathnameR) == "slowcounting.txt.R") > > > # Compile to RSP source code object > metadata <- list(foo="ABC", bar="123") > code <- rcode(file=pathname, metadata=metadata, output=RspSourceCode()) > print(code) ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ## This is a self-contained R script generated from an RSP document. ## It may be evaluated using source() as is. ## ## Metadata: ## 'foo': 'ABC' ## 'bar': '123' ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Local RSP utility functions ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## RSP metadata function .rmeta <- list( foo = "ABC", bar = "123" ) 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("Counting:") for (i in 1:3) { Sys.sleep(0.5) .rout(" ") .rout0(i) } .rout(".") ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## RSP source code script [END] ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > > code <- rcode(file=pathname, metadata=metadata, output=RspSourceCode(), verbose = TRUE) rcode() for default... Input file: D:/RCompile/CRANincoming/R-devel/lib/R.rsp/exData/slowcounting.txt.rsp Length of RSP string: 72 rcode() for RspString... Parse RSP string to RSP document... Parse environment: R_GlobalEnv Parsing RSP string... Compile until: '*' Return as: 'RspDocument' Dropping 'empty' RSP comments... Length of RSP string before: 72 RspDocument: Source: D:/RCompile/CRANincoming/R-devel/lib/R.rsp/exData/slowcounting.txt.rsp Total number of RSP constructs: 3 Number of RspComment(s): 1 Number of RspText(s): 2 Content type: NA Metadata 'foo': 'ABC' Metadata 'bar': '123' RspDocument: Source: D:/RCompile/CRANincoming/R-devel/lib/R.rsp/exData/slowcounting.txt.rsp Total number of RSP constructs: 1 Number of RspText(s): 1 Content type: NA Metadata 'foo': 'ABC' Metadata 'bar': '123' Number of 'empty' RSP comments dropped: 1 Length of RSP string after: 67 Dropping 'empty' RSP comments...done Dropping 'paired' RSP comments... Length of RSP string before: 67 No 'paired' RSP comments found. Dropping 'paired' RSP comments...done Processing RSP preprocessing directives... Length of RSP string before: 67 No RSP preprocessing directives found. Processing RSP preprocessing directives...done Processing RSP expressions... Length of RSP string before: 67 Number of (unparsed) RSP expressions found: 4 Length of RSP string after: 60 Processing RSP expressions...done Parsing RSP string...done RspDocument: Source: D:/RCompile/CRANincoming/R-devel/lib/R.rsp/exData/slowcounting.txt.rsp Total number of RSP constructs: 7 Number of RspCode(s): 3 Number of RspCodeChunk(s): 1 Number of RspText(s): 3 Content type: NA Metadata 'foo': 'ABC' Metadata 'bar': '123' Parse RSP string to RSP document...done rcode() for RspDocument... Coerce RSP document to source code... Language: R Generated source code:... ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ## This is a self-contained R script generated from an RSP document. ## It may be evaluated using source() as is. ## ## Metadata: ## 'foo': 'ABC' ## 'bar': '123' ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - [...] .rout("Counting:") for (i in 1:3) { Sys.sleep(0.5) .rout(" ") .rout0(i) } .rout(".") ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## RSP source code script [END] ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Generated source code:...done Coerce RSP document to source code...done rcode() for RspDocument...done rcode() for RspString...done rcode() for default...done > print(code) ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ## This is a self-contained R script generated from an RSP document. ## It may be evaluated using source() as is. ## ## Metadata: ## 'foo': 'ABC' ## 'bar': '123' ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Local RSP utility functions ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## RSP metadata function .rmeta <- list( foo = "ABC", bar = "123" ) 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("Counting:") for (i in 1:3) { Sys.sleep(0.5) .rout(" ") .rout0(i) } .rout(".") ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## RSP source code script [END] ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > > proc.time() user system elapsed 0.70 0.14 0.81