R {tools} | R Documentation |
Call Function in Inferior R Process
Description
Call a function with given arguments in a new (“inferior”) R process.
Usage
R(fun, args = list(), opts = "--no-save --no-restore",
env = character(), arch = "", drop = TRUE, timeout = 0)
Arguments
fun |
a function to call in the inferior R process. |
args |
a list of arguments to the function call. |
opts |
a character vector of command line options to use when starting the inferior R process. |
env |
a character vector of name=value strings to set environment variables to use when starting the inferior R process. |
arch |
a character string giving a sub-architecture to use for the inferior R process. |
drop |
a logical controlling what gets returned in case of
“success” (when there was no error running the inferior R
process and the value of the function call is not a condition
object). By default, in this case only that value is returned.
If |
timeout |
a timeout in seconds for the inferior R process, ignored if 0. |
Details
The given function and argument list are serialized into a
tempfile.
Then, an inferior R process is run via (a wrapper for)
system2()
as controlled by arguments opts
,
env
, arch
and timeout
, the input of which loads
the tempfile and calls the function with the argument list.
It is ensured that an appropriate CRAN mirror is set.
Value
In case of “success” (see above), the value of the function
call if drop = TRUE
(default), or a named list with the value
and the status, stdout and stderr of running the inferior R process.
Otherwise, currently an error condition object inheriting from
"inferiorCallError"
with the result (status, stdout and stderr)
of running the inferior R process, and in case this was successfully
run but the value of the function call is a condition object, that
object in the value
element.
What gets returned in the non-success case may change in the future, perhaps using two different classes for the two cases of “failure” (i.e., failure in running the inferior R process or calling the function).
Examples
## Compute cos(0) in an inferior R process.
## By default, only return the value of the function call.
R(cos, list(0))
## If 'drop = FALSE', we also get status, stdout and stderr.
R(cos, list(0), drop = FALSE)
## A call giving an error:
(e <- tryCatch(R(stop, list("FOOBAR")), error = identity))
## The inferior R process ran successfully:
e$status
## The function call gave an error:
e$value