r_bg {callr} | R Documentation |
Evaluate an expression in another R session, in the background
Description
Starts evaluating an R function call in a background R process, and
returns immediately.
Use p$get_result()
to collect the result or to throw an error
if the background computation failed.
Usage
r_bg(
func,
args = list(),
libpath = .libPaths(),
repos = default_repos(),
stdout = "|",
stderr = "|",
poll_connection = TRUE,
error = getOption("callr.error", "error"),
cmdargs = c("--slave", "--no-save", "--no-restore"),
system_profile = FALSE,
user_profile = "project",
env = rcmd_safe_env(),
supervise = FALSE,
package = FALSE,
arch = "same",
...
)
Arguments
func |
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 r(.libPaths) does not work, because r(function() .libPaths()) works just fine. |
args |
Arguments to pass to the function. Must be a list. |
libpath |
The library path. |
repos |
The |
stdout |
The name of the file the standard output of
the child R process will be written to.
If the child process runs with the |
stderr |
The name of the file the standard error of
the child R process will be written to.
In particular |
poll_connection |
Whether to have a control connection to the process. This is used to transmit messages from the subprocess to the main process. |
error |
What to do if the remote process throws an error. See details below. |
cmdargs |
Command line arguments to pass to the R process.
Note that |
system_profile |
Whether to use the system profile file. |
user_profile |
Whether to use the user's profile file.
If this is |
env |
Environment variables to set for the child process. |
supervise |
Whether to register the process with a supervisor. If |
package |
Whether to keep the environment of
|
arch |
Architecture to use in the child process, for multi-arch
builds of R. By default the same as the main process. See
|
... |
Extra arguments are passed to the processx::process constructor. |
Value
An r_process
object, which inherits from process,
so all process
methods can be called on it, and in addition it also
has a get_result()
method to collect the result.
Security considerations
callr
makes a copy of the user's .Renviron
file and potentially of
the local or user .Rprofile
, in the session temporary
directory. Avoid storing sensitive information such as passwords, in
your environment file or your profile, otherwise this information will
get scattered in various files, at least temporarily, until the
subprocess finishes. You can use the keyring package to avoid passwords
in plain files.
Examples
rx <- r_bg(function() 1 + 2)
# wait until it is done
rx$wait()
rx$is_alive()
rx$get_result()