# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Example 1
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cat("=== Example 1 ================================================
")
foo <- function(file, ...) {
cat("Local objects before calling sourceTo():
")
print(ls())
res <- sourceTo(file, ...)
cat("Local objects after calling sourceTo():
")
print(ls())
}
cat("Global objects before calling foo():
")
lsBefore <- NA
lsBefore <- ls()
foo(file=textConnection(c('a <- 1', 'b <- 2')))
cat("Global objects after calling foo():
")
stopifnot(length(setdiff(ls(), lsBefore)) == 0)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Example 2 - with VComments preprocessor
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cat("=== Example 2 ================================================
")
preprocessor <- function(lines, ...) {
cat("-----------------------------------------
")
cat("Source code before preprocessing:
")
displayCode(code=lines, pager="console")
cat("-----------------------------------------
")
cat("Source code after preprocessing:
")
lines <- VComments$compile(lines)
displayCode(code=lines, pager="console")
cat("-----------------------------------------
")
lines
}
oldHooks <- getHook("sourceTo/onPreprocess")
setHook("sourceTo/onPreprocess", preprocessor, action="replace")
code <- c(
'x <- 2',
'#V1# threshold=-1',
'#Vc# A v-comment log message',
'print("Hello world")'
)
fh <- textConnection(code)
sourceTo(fh)
setHook("sourceTo/onPreprocess", oldHooks, action="replace")
Run the code above in your browser using DataLab