Evaluate an expression in another R session
r(func, args = list(), libpath = .libPaths(), repos = getOption("repos"),
stdout = NULL, stderr = NULL, error = c("error", "stack", "debugger"),
cmdargs = "--slave", show = FALSE, callback = NULL,
system_profile = TRUE, user_profile = TRUE, env = character())
Function object to call in the new R process.
The function should be self-contained and only refer to
other functions and use variables explicitly from other packages
using the ::
notation. The environment of the function
is set to .GlobalEnv
before passing it to the child process.
Because of this, it is good practice to create an anonymous
function and pass that to callr
, instead of passing
a function object from a (base or other) package. In particular
r(.libPaths)
does not work, because it is defined in a special environment, but
r(function() .libPaths())
works just fine.
Arguments to pass to the function. Must be a list. vector.
The library path.
The ‘repos’ option. If NULL
, then no
repos
option is set. This options is only used if
user_profile
or system_profile
is set to FALSE
,
as it is set using the system or the user profile.
The name of the file the standard output of
the child R process will be written to.
If the child process runs with the --slave
option (the default),
then the commands are not echoed and will not be shown
in the standard output. Also note that you need to call `print()`
explicitly to show the output of the command(s).
The name of the file the standard error of
the child R process will be written to.
In particular message()
sends output to the standard
error. If nothing was sent to the standard error, then this file
will be empty.
What to do if the remote process throws an error. See details below.
Command line arguments to pass to the R process.
Note that c("-f", rscript)
is appended to this, rscript
is the name of the script file to run. This contains a call to the
supplied function and some error handling code.
Logical, whether to show the standard output on the screen
while the child process is running. Note that this is independent
of the stdout
and stderr
arguments. The standard
error is not shown currently.
A function to call for each line of the standard
output from the child process. It works together with the show
option; i.e. if show = TRUE
, and a callback is provided, then
the output is shown of the screen, and the callback is also called.
Whether to use the system profile file.
Whether to use the user's profile file.
Environment variables to set for the child process.
Value of the evaluated expression.
callr
handles errors properly. If the child process throws an
error, then callr
throws an error with the same error message
in the parent process.
The ‘error’ expert option may be used to specify a different behavior on error. The following values are possible:
‘error’ is the default behavior: throw an error in the parent, with the same error message. In fact the same error object is thrown again.
‘stack’ also throws an error in the parent, but the error
is of a special kind, class callr_condition
, and it contains
both the original error object, and the call stack of the child,
as written out by dump.frames
.
‘debugger’ is similar to ‘stack’, but in addition
to returning the complete call stack, it also start up a debugger
in the child call stack, via debugger
.
# NOT RUN {
# Workspace is empty
r(function() ls())
# library path is the same by default
r(function() .libPaths())
.libPaths()
# }
Run the code above in your browser using DataCamp Workspace