launch.R
873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
source(file.path(Sys.getenv("DIRNAME"), "needs.R"))
needs(jsonlite)
run <- function(dataIn) {
# set up environment
input <- unname(dataIn[[1]])
.e <- as.environment(list(
path = dataIn[[2]],
out = modifyList(list(x = NULL, auto_unbox = T),
dataIn[[3]], keep.null = T)
))
lockBinding(".e", environment())
# run source, capture output
captured <- tryCatch(capture.output({
temp <- source(.e$path, local = T)$value
}), error = function(err) err)
unlockBinding(".e", environment())
# process and return
if (inherits(captured, "error")) {
msg <- conditionMessage(captured)
cat("Error in R script", .e$path, "\n", sQuote(msg), file = stderr())
return(invisible(F))
}
.e$out$x <- if (is.null(temp)) {
""
} else {
temp
}
do.call(toJSON, .e$out)
}
suppressWarnings(
run(fromJSON(Sys.getenv("input")))
)