lib.execute_using_packagelist: Perform operation with a certain set of packages.

View source: R/execute_with_packages.R

lib.execute_using_packagelistR Documentation

Perform operation with a certain set of packages.

Description

This function can be used to perform R operations with a configured set of packages to load in a separate R process. The package callr is required to use this functionality. It will start a new process, then load the provided packages and execute your function. The callr package may be provided via the R_MV_library or your standard library, in which case it must be in a library where .libPaths is pointing to.

Usage

lib.execute_using_packagelist(
  packages_to_load = c(),
  func_handle,
  ...,
  .lib_location = lib.location(),
  .pick_last = FALSE,
  .also_load_from_temp_lib = FALSE,
  .wait_for_response = TRUE,
  .run_quietly = FALSE,
  .callr_arguments = list()
)

Arguments

packages_to_load

An array indicating which packages must be loaded like "c(dplyr = '0.5.0', ggplot2 = '', tidyr = '> 1.2.3')".

func_handle

A function object or the function name as a character string.

...

Provide all the remaining arguments which will be arguments for the function handle. Note that every argument must be named and must match an argument in your func_handle.

.lib_location

The location of the version controlled library. Defaults to lib.location(), which is the directory provided by the environment variable.

.pick_last

Passed to lib.load(packages_to_load, ...) inside the fired callr process.

.also_load_from_temp_lib

Passed to lib.load(packages_to_load, ...) inside the fired callr process.

.wait_for_response

If false, it will fire and forget and return immediately using callr::r_process, otherwise will use callr::r.

.run_quietly

Controls the 'show' parameter of callr::r or callr::r_process.

.callr_arguments

List specifying additional arguments for callr::r or callr::r_process (depending on the .wait_for_response value). Note that func, args, show and libpath are already in use. Every parameter must be named.

Details

The additional arguments to callr: .callr_arguments, can for example be used to keep a log of a detached process. By including the following .callr_arguments for example:

lib.execute_using_packagelist(
    ...,
    .callr_arguments   = list(stdout = paste0('./execution_', gsub('\s|-|:', '_', format(Sys.time())), '.log'), stderr = "2>&1")
)

See the example below for a complete example. When you do this, it somehow swallows the first character of every stderr that is directly returned (also from message calls) when run_quietly = FALSE, but the log file seems intact.

Value

Will return the outcome of your func_handle.

Example

If you would like to log the outcomes, provide the .callr_arguments:

lib.execute_using_packagelist(
    packages_to_load   = c(package.a =  '0.1.0'),
    func_handle        = function() {an_important_value(); package_a1(5, 10)},
    .wait_for_response = TRUE,
    .callr_arguments   = list(stdout = paste0('./execution_', gsub('\s|-|:', '_', format(Sys.time())), '.log'), stderr = "2>&1"),
    .run_quietly       = TRUE
)

Another more simple example:

lib.execute_using_packagelist(
    packages_to_load   = c(dplyr =  ''),
    func_handle        = function() {mtcars}
)

multiversion documentation built on March 22, 2022, 1:07 a.m.