Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

Rscript_call

Call a function in a new R session via Rscript()


Description

Save the argument values of a function in a temporary RDS file, open a new R session via Rscript(), read the argument values, call the function, and read the returned value back to the current R session.

Usage

Rscript_call(
  fun,
  args = list(),
  options = NULL,
  ...,
  wait = TRUE,
  fail = sprintf("Failed to run '%s' in a new R session.",
    deparse(substitute(fun))[1])
)

Arguments

fun

A function, or a character string that can be parsed and evaluated to a function.

args

A list of argument values.

options

A character vector of options to passed to Rscript, e.g., "--vanilla".

..., wait

Arguments to be passed to system2().

fail

The desired error message when an error occurred in calling the function.

Value

The returned value of the function in the new R session.

Examples

factorial(10)
# should return the same value
xfun::Rscript_call("factorial", list(10))

# the first argument can be either a character string or a function
xfun::Rscript_call(factorial, list(10))

# Run Rscript starting a vanilla R session
xfun::Rscript_call(factorial, list(10), options = c("--vanilla"))

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.