Call R from R
It is sometimes useful to perform a computation in a separate R process, without affecting the current R process at all. This packages does exactly that.
R CMDcommands, synchronously or asynchronously.
rscript_processR6 classes, based on
Install the stable version from CRAN:
r() to run an R function in a new R process. The results are passed back seamlessly:
You can pass arguments to the function by setting
args to the list of arguments. This is often necessary as these arguments are explicitly copied to the child process, whereas the evaluated function cannot refer to variables in the parent. For example, the following does not work:
But this does:
Note that the arguments will be serialized and saved to a file, so if they are large R objects, it might take a long time for the child process to start up.
You can use any R package in the child process, just make sure to refer to it explicitly with the
:: operator. For example, the following code creates an igraph graph in the child, and calculates some metrics of it.
callr` copies errors from the child process back to the main R session:
r(function() 1 + "A")
callr sets the
.Last.error variable, and after an error you can inspect this for more details about the error, including stack traces both from the main R process and the subprocess.
The error objects has two parts. The first belongs to the main process, and the second belongs to the subprocess.
.Last.error also includes a stack trace, that includes both the main R process and the subprocess:
#> #> ERROR TRACE for callr_status_error, callr_error, rlib_error #> #> Process 31908: #> 38. callr:::r(function() 1 + "A") #> 39. callr:::get_result(output = out, options) #> R/eval.R:149:3 #> 40. callr:::throw(new_callr_error(output, msg), parent = err[]) #> R/result.R:73:5 #> #> x callr subprocess failed: non-numeric argument to binary operator #> #> Process 32006: #> 52. (function () ... #> 53. base:::.handleSimpleError(function (e) ... #> R/<text>:1:3 #> 54. h(simpleError(msg, call)) #> #> x non-numeric argument to binary operator
The top part of the trace contains the frames in the main process, and the bottom part contains the frames in the subprocess, starting with the anonymous function.
By default, the standard output and error of the child is lost, but you can request callr to redirect them to files, and then inspect the files in the parent:
stdout option, the standard output is collected and can be examined once the child process finished. The
show = TRUE options will also show the output of the child, as it is printed, on the console of the parent.
This is a list of all
#>  "as_ps_handle" "clone" #>  "finalize" "format" #>  "get_cmdline" "get_cpu_times" #>  "get_error_connection" "get_error_file" #>  "get_exe" "get_exit_status" #>  "get_input_connection" "get_input_file" #>  "get_memory_info" "get_name" #>  "get_output_connection" "get_output_file" #>  "get_pid" "get_poll_connection" #>  "get_result" "get_start_time" #>  "get_status" "get_username" #>  "get_wd" "has_error_connection" #>  "has_input_connection" "has_output_connection" #>  "has_poll_connection" "initialize" #>  "interrupt" "is_alive" #>  "is_incomplete_error" "is_incomplete_output" #>  "is_supervised" "kill" #>  "kill_tree" "poll_io" #>  "print" "read_all_error" #>  "read_all_error_lines" "read_all_output" #>  "read_all_output_lines" "read_error" #>  "read_error_lines" "read_output" #>  "read_output_lines" "resume" #>  "signal" "supervise" #>  "suspend" "wait" #>  "write_input"
get_exit_status()to query the exit status of a finished process.
get_result()to collect the return value of the R function call.
intertupt()to send an interrupt to the process. This is equivalent to a
CTRL+Ckey press, and the R process might ignore it.
is_alive()to check if the process is alive.
kill()to terminate the process.
poll_io()to wait for any standard output, standard error, or the completion of the process, with a timeout.
read_*()to read the standard output or error.
resume()to stop and continue a process.
wait()to wait for the completion of the process, with a timeout.
Multiple background R processes are best managed with the
processx::poll() function that waits for events (standard output/error or termination) from multiple processes. It returns as soon as one process has generated an event, or if its timeout has expired. The timeout is in milliseconds.
r_session is another
processx::process subclass that represents a persistent background R session:
r_session$run() is a synchronous call, that works similarly to
r(), but uses the persistent session.
r_session$call() starts the function call and returns immediately. The
r_session$poll_process() method or
processx::poll() can then be used to wait for the completion or other events from one or more R sessions, R processes or other
Once an R session is done with an asynchronous computation, its
poll_process() method returns
"ready" and the
r_session$read() method can read out the result.
#> $code #>  200 #> #> $message #>  "done file7ca436de78be" #> #> $result #>  -1.0339966 -0.7015408 1.2220644 -0.9691123 -0.6991386 1.2698753 #>  0.2595250 1.0871667 0.5148076 0.4015015 #> #> $stdout #>  "" #> #> $stderr #>  "" #> #> $error #> NULL #> #> attr(,"class") #>  "callr_session_result"
rcmd() function calls an
R CMD command. For example, you can call
R CMD INSTALL,
R CMD check or
R CMD config this way:
This returns a list with three components: the standard output, the standard error, and the exit (status) code of the
R CMD command.