exec_r: Execute R from R

Description Usage Arguments Details See Also Examples

Description

Convenience wrappers for exec_wait and exec_internal that shell out to R itself: R.home("bin/R").

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
r_wait(
  args = "--vanilla",
  std_out = stdout(),
  std_err = stderr(),
  std_in = NULL
)

r_internal(args = "--vanilla", std_in = NULL, error = TRUE)

r_background(args = "--vanilla", std_out = TRUE, std_err = TRUE, std_in = NULL)

Arguments

args

command line arguments for R

std_out

if and where to direct child process STDOUT. Must be one of TRUE, FALSE, filename, connection object or callback function. See section on Output Streams below for details.

std_err

if and where to direct child process STDERR. Must be one of TRUE, FALSE, filename, connection object or callback function. See section on Output Streams below for details.

std_in

a file to send to stdin, usually an R script (see examples).

error

automatically raise an error if the exit status is non-zero.

Details

This is a simple but robust way to invoke R commands in a separate process. Use the callr package if you need more sophisticated control over (multiple) R process jobs.

See Also

Other sys: exec

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Hello world
r_wait("--version")

# Run some code
r_wait(c('--vanilla', '-q', '-e', 'sessionInfo()'))

# Run a script via stdin
tmp <- tempfile()
writeLines(c("x <- rnorm(100)", "mean(x)"), con = tmp)
r_wait(std_in = tmp)

Example output

System failure for: open() set_input not equal to STDIN_FILENO (Permission denied)
R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
http://www.gnu.org/licenses/.

[1] 0
System failure for: open() set_input not equal to STDIN_FILENO (Permission denied)
Fatal error: creating temporary file for '-e' failed
[1] 2

R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> x <- rnorm(100)
> mean(x)
[1] 0.03919752
> 
sh: 1: rm: Permission denied
[1] 0

sys documentation built on July 23, 2020, 9:07 a.m.

Related to exec_r in sys...