Starts evaluating an R function call in a background R process, and returns immediately.

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 = FALSE,
  env = rcmd_safe_env(), supervise = FALSE, ...)



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


does not work, because .libPaths is defined in a special environment, but

r(function() .libPaths())

works just fine.


Arguments to pass to the function. Must be a list.


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 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. This argument can be the same file as stdout, in which case they will be correctly interleaved. If this is the string "2>&1", then standard error is redictd to standard output.


Whether to have a control connection to the process. This is used to transmit messages from the subprocess to the parent.


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.


Whether to use the system profile file.


Whether to use the user's profile file.


Environment variables to set for the child process.


Whether to register the process with a supervisor. If TRUE, the supervisor will ensure that the process is killed when the R process exits.


Extra arguments are passed to the processx::process constructor.


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.


rx <- r_bg(function() 1 + 2)

# wait until it is done
# }