Read files one by one, and optionally add text before/after the content. Then combine all content into one character vector.
read_all(files, before = function(f, x) NULL, after = function(f, x) NULL)
A character vector.
# two files in this package
fs = system.file("scripts", c("call-fun.R", "child-pids.sh"), package = "xfun")
xfun::read_all(fs)
#> # This script is executed via the command line `Rscript call-fun.R arg1 arg2
#> # arg3`, where arg1 is a path to an .rds file, which contains the function and
#> # its arguments saved as a list; arg2 is a path to an .rds file to which the
#> # returned value of the function call is saved; arg3 saves the error message.
#>
#> local({
#> if (length(a <- commandArgs(TRUE)) != 3)
#> stop('The number of arguments passed to Rscript should be 3.')
#> # save the error message on exit if an error occurred
#> on.exit(if (!file.exists(a[2])) saveRDS(geterrmessage(), a[3]))
#> x = readRDS(a[1]) # list(fun, args)
#> f = x[[1]]
#> if (is.character(f)) f = eval(parse(text = f), envir = globalenv())
#> r = do.call(f, x[[2]], envir = globalenv())
#> saveRDS(r, a[2])
#> })
#> # given a PID, output all its child PIDs recursively
#> list_children() {
#> for j in $(pgrep -P $1); do
#> echo $j
#> echo $(list_children $j)
#> done
#> }
#>
#> for i in $@; do
#> list_children $i
#> done
# add file paths before file content and an empty line after content
xfun::read_all(fs, before = function(f) paste("#-----", f, "-----"), after = "")
#> #----- /tmp/RtmplGg3Jy/temp_libpath39927d15f364fe/xfun/scripts/call-fun.R -----
#> # This script is executed via the command line `Rscript call-fun.R arg1 arg2
#> # arg3`, where arg1 is a path to an .rds file, which contains the function and
#> # its arguments saved as a list; arg2 is a path to an .rds file to which the
#> # returned value of the function call is saved; arg3 saves the error message.
#>
#> local({
#> if (length(a <- commandArgs(TRUE)) != 3)
#> stop('The number of arguments passed to Rscript should be 3.')
#> # save the error message on exit if an error occurred
#> on.exit(if (!file.exists(a[2])) saveRDS(geterrmessage(), a[3]))
#> x = readRDS(a[1]) # list(fun, args)
#> f = x[[1]]
#> if (is.character(f)) f = eval(parse(text = f), envir = globalenv())
#> r = do.call(f, x[[2]], envir = globalenv())
#> saveRDS(r, a[2])
#> })
#>
#> #----- /tmp/RtmplGg3Jy/temp_libpath39927d15f364fe/xfun/scripts/child-pids.sh -----
#> # given a PID, output all its child PIDs recursively
#> list_children() {
#> for j in $(pgrep -P $1); do
#> echo $j
#> echo $(list_children $j)
#> done
#> }
#>
#> for i in $@; do
#> list_children $i
#> done
#>
# add constants
xfun::read_all(fs, before = "/*", after = c("*/", ""))
#> /*
#> # This script is executed via the command line `Rscript call-fun.R arg1 arg2
#> # arg3`, where arg1 is a path to an .rds file, which contains the function and
#> # its arguments saved as a list; arg2 is a path to an .rds file to which the
#> # returned value of the function call is saved; arg3 saves the error message.
#>
#> local({
#> if (length(a <- commandArgs(TRUE)) != 3)
#> stop('The number of arguments passed to Rscript should be 3.')
#> # save the error message on exit if an error occurred
#> on.exit(if (!file.exists(a[2])) saveRDS(geterrmessage(), a[3]))
#> x = readRDS(a[1]) # list(fun, args)
#> f = x[[1]]
#> if (is.character(f)) f = eval(parse(text = f), envir = globalenv())
#> r = do.call(f, x[[2]], envir = globalenv())
#> saveRDS(r, a[2])
#> })
#> */
#>
#> /*
#> # given a PID, output all its child PIDs recursively
#> list_children() {
#> for j in $(pgrep -P $1); do
#> echo $j
#> echo $(list_children $j)
#> done
#> }
#>
#> for i in $@; do
#> list_children $i
#> done
#> */
#>