| 1 |
# This script is executed via the command line `Rscript call-fun.R arg1 arg2 |
| 2 |
# arg3`, where arg1 is a path to an .rds file, which contains the function and |
| 3 |
# its arguments saved as a list; arg2 is a path to an .rds file to which the |
| 4 |
# returned value of the function call is saved; arg3 saves the error message. |
| 5 |
|
| 6 |
local({ |
| 7 |
if (length(a <- commandArgs(TRUE)) != 3) |
| 8 |
stop('The number of arguments passed to Rscript should be 3.') |
| 9 |
# save the error message on exit if an error occurred |
| 10 |
on.exit(if (!file.exists(a[2])) saveRDS(geterrmessage(), a[3])) |
| 11 |
x = readRDS(a[1]) # list(fun, args) |
| 12 |
f = x[[1]] |
| 13 |
if (is.character(f)) f = eval(parse(text = f), envir = globalenv()) |
| 14 |
r = do.call(f, x[[2]], envir = globalenv()) |
| 15 |
saveRDS(r, a[2]) |
| 16 |
}) |