run_r_process: Asynchronous call to an R function, in a background R process

View source: R/r-process.R

run_r_processR Documentation

Asynchronous call to an R function, in a background R process

Description

Start a background R process and evaluate a function call in it. It uses callr::r_process internally.

Usage

run_r_process(
  func,
  args = list(),
  libpath = .libPaths(),
  repos = c(getOption("repos"), c(CRAN = "https://cloud.r-project.org")),
  cmdargs = c("--no-site-file", "--slave", "--no-save", "--no-restore"),
  system_profile = FALSE,
  user_profile = FALSE,
  env = callr::rcmd_safe_env()
)

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 :: notation. By default the environment of the function is set to .GlobalEnv before passing it to the child process. (See the package option if you want to keep the environment.) 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 .libPaths is defined in a special environment, but

r(function() .libPaths())

works just fine.

args

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

libpath

The library path.

repos

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.

cmdargs

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.

system_profile

Whether to use the system profile file.

user_profile

Whether to use the user's profile file. If this is "project", then only the profile from the working directory is used, but the R_PROFILE_USER environment variable and the user level profile are not. See also "Security considerations" below.

env

Environment variables to set for the child process.

Examples

## Not run: 
afun <- function() {
  run_r_process(function() Sys.getpid())
}
synchronise(afun())

## End(Not run)

r-lib/async documentation built on Nov. 19, 2024, 12:08 p.m.