EVOLUTION-MANAGER
Edit File: callr-package.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Call R from R</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for callr-package {callr}"><tr><td>callr-package {callr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Call R from R</h2> <h3>Description</h3> <p>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. </p> <h3>callr</h3> <h4>Features</h4> <ul> <li><p> Calls an R function, with arguments, in a subprocess. </p> </li> <li><p> Copies function arguments to the subprocess and copies the return value of the function back, seamlessly. </p> </li> <li><p> Copies error objects back from the subprocess, including a stack trace. </p> </li> <li><p> Shows and/or collects the standard output and standard error of the subprocess. </p> </li> <li><p> Supports both one-off and persistent R subprocesses. </p> </li> <li><p> Calls the function synchronously or asynchronously (in the background). </p> </li> <li><p> Can call <code style="white-space: pre;">R CMD</code> commands, synchronously or asynchronously. </p> </li> <li><p> Can call R scripts, synchronously or asynchronously. </p> </li> <li><p> Provides extensible <code>r_process</code>, <code>rcmd_process</code> and <code>rscript_process</code> R6 classes, based on <code>processx::process</code>. </p> </li></ul> <h4>Installation</h4> <p>Install the stable version from CRAN: </p> <div class="sourceCode r"><pre>install.packages("callr") </pre></div> <h4>Synchronous, one-off R processes</h4> <p>Use <code>r()</code> to run an R function in a new R process. The results are passed back seamlessly: </p> <div class="sourceCode r"><pre>callr::r(function() var(iris[, 1:4])) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> Sepal.Length Sepal.Width Petal.Length Petal.Width #> Sepal.Length 0.6856935 -0.0424340 1.2743154 0.5162707 #> Sepal.Width -0.0424340 0.1899794 -0.3296564 -0.1216394 #> Petal.Length 1.2743154 -0.3296564 3.1162779 1.2956094 #> Petal.Width 0.5162707 -0.1216394 1.2956094 0.5810063 </pre></div> <h5>Passing arguments</h5> <p>You can pass arguments to the function by setting <code>args</code> 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: </p> <div class="sourceCode r"><pre>mycars <- cars callr::r(function() summary(mycars)) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> <span style="font-weight: bold;;color: #B58900">Error</span>: #> <span style="color: #B58900">!</span> error in callr subprocess #> <span style="font-weight: bold;">Caused by error</span> in `summary(mycars)`: #> <span style="color: #B58900">!</span> object 'mycars' not found #> <span style="color: #002B36">Type .Last.error to see the more details.</span> </pre></div> <p>But this does: </p> <div class="sourceCode r"><pre>mycars <- cars callr::r(function(x) summary(x), args = list(mycars)) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> speed dist #> Min. : 4.0 Min. : 2.00 #> 1st Qu.:12.0 1st Qu.: 26.00 #> Median :15.0 Median : 36.00 #> Mean :15.4 Mean : 42.98 #> 3rd Qu.:19.0 3rd Qu.: 56.00 #> Max. :25.0 Max. :120.00 </pre></div> <p>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. </p> <h5>Using packages</h5> <p>You can use any R package in the child process, just make sure to refer to it explicitly with the <code>::</code> operator. For example, the following code creates an <a href="https://github.com/igraph/rigraph">igraph</a> graph in the child, and calculates some metrics of it. </p> <div class="sourceCode r"><pre>callr::r(function() { g <- igraph::sample_gnp(1000, 4/1000); igraph::diameter(g) }) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [1] 11 </pre></div> <h5>Error handling</h5> <p>callr copies errors from the child process back to the main R session: </p> <div class="sourceCode r"><pre>callr::r(function() 1 + "A") </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> <span style="font-weight: bold;;color: #B58900">Error</span>: #> <span style="color: #B58900">!</span> error in callr subprocess #> <span style="font-weight: bold;">Caused by error</span> in `1 + "A"`: #> <span style="color: #B58900">!</span> non-numeric argument to binary operator #> <span style="color: #002B36">Type .Last.error to see the more details.</span> </pre></div> 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. <div class="sourceCode r"><pre>.Last.error </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> <span style="color: #268BD2"><callr_error/rlib_error_3_0/rlib_error/error></span> #> <span style="font-weight: bold;;color: #B58900">Error</span>: #> <span style="color: #B58900">!</span> error in callr subprocess #> <span style="font-weight: bold;">Caused by error</span> in `1 + "A"`: #> <span style="color: #B58900">!</span> non-numeric argument to binary operator #> --- #> Backtrace: #> <span style="color: #002B36">1. </span>callr::<span style="color: #2AA198">r</span><span style="color: #B58900">(</span><span style="color: #DC322F">function</span><span style="color: #268BD2">()</span> <span style="color: #268BD2">1</span> <span style="color: #859900">+</span> <span style="color: #B58900">"A")</span> #> <span style="color: #002B36">2. </span>callr:::<span style="color: #2AA198">get_result</span><span style="color: #B58900">(</span>output = out, options<span style="color: #B58900">)</span><span style="color: #002B36"> at eval.R:189:3</span> #> <span style="color: #002B36">3. </span>callr:::<span style="color: #2AA198">throw</span><span style="color: #B58900">(</span><span style="color: #2AA198">callr_remote_error</span><span style="color: #268BD2">(</span>remerr<span style="color: #268BD2">)</span><span style="color: #B58900">)</span><span style="color: #002B36"> at result.R:67:5</span> #> <span style="color: #002B36">4. </span>callr:::<span style="color: #2AA198">callr_remote_error</span><span style="color: #B58900">(</span>remerr<span style="color: #B58900">)</span><span style="color: #002B36"> at result.R:67:5</span> #> <span style="color: #002B36">5. </span>callr:::<span style="color: #2AA198">throw</span><span style="color: #B58900">(</span>err, parent = remerr<span style="color: #268BD2">[[3]]</span><span style="color: #B58900">)</span><span style="color: #002B36"> at error.R:44:3</span> #> --- #> Subprocess backtrace: #> <span style="color: #002B36">1. </span>base::.handleSimpleError(function (e) … #> <span style="color: #002B36">2. </span>global <span style="color: #2AA198">h</span><span style="color: #B58900">(</span><span style="color: #2AA198">simpleError</span><span style="color: #268BD2">(</span>msg, call<span style="color: #268BD2">)</span><span style="color: #B58900">)</span> </pre></div> <p>The error objects has two parts. The first belongs to the main process, and the second belongs to the subprocess. </p> <p><code>.Last.error</code> also includes a stack trace, that includes both the main R process and the subprocess: </p> <p>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. </p> <h5>Standard output and error</h5> <p>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: </p> <div class="sourceCode r"><pre>x <- callr::r(function() { print("hello world!"); message("hello again!") }, stdout = "/tmp/out", stderr = "/tmp/err" ) readLines("/tmp/out") </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [1] "[1] \"hello world!\"" </pre></div> <div class="sourceCode r"><pre>readLines("/tmp/err") </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [1] "hello again!" </pre></div> <p>With the <code>stdout</code> option, the standard output is collected and can be examined once the child process finished. The <code>show = TRUE</code> options will also show the output of the child, as it is printed, on the console of the parent. </p> <h4>Background R processes</h4> <p><code>r_bg()</code> is similar to <code>r()</code> but it starts the R process in the background. It returns an <code>r_process</code> R6 object, that provides a rich API: </p> <div class="sourceCode r"><pre>rp <- callr::r_bg(function() Sys.sleep(.2)) rp </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> PROCESS 'R', running, pid 57164. </pre></div> <p>This is a list of all <code>r_process</code> methods: </p> <div class="sourceCode r"><pre>ls(rp) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [1] "as_ps_handle" "clone" "finalize" #> [4] "format" "get_cmdline" "get_cpu_times" #> [7] "get_error_connection" "get_error_file" "get_exe" #> [10] "get_exit_status" "get_input_connection" "get_input_file" #> [13] "get_memory_info" "get_name" "get_output_connection" #> [16] "get_output_file" "get_pid" "get_poll_connection" #> [19] "get_result" "get_start_time" "get_status" #> [22] "get_username" "get_wd" "has_error_connection" #> [25] "has_input_connection" "has_output_connection" "has_poll_connection" #> [28] "initialize" "interrupt" "is_alive" #> [31] "is_incomplete_error" "is_incomplete_output" "is_supervised" #> [34] "kill" "kill_tree" "poll_io" #> [37] "print" "read_all_error" "read_all_error_lines" #> [40] "read_all_output" "read_all_output_lines" "read_error" #> [43] "read_error_lines" "read_output" "read_output_lines" #> [46] "resume" "signal" "supervise" #> [49] "suspend" "wait" "write_input" </pre></div> <p>These include all methods of the <code>processx::process</code> superclass and the new <code>get_result()</code> method, to retrieve the R object returned by the function call. Some of the handiest methods are: </p> <ul> <li> <p><code>get_exit_status()</code> to query the exit status of a finished process. </p> </li> <li> <p><code>get_result()</code> to collect the return value of the R function call. </p> </li> <li> <p><code>interrupt()</code> to send an interrupt to the process. This is equivalent to a <code>CTRL+C</code> key press, and the R process might ignore it. </p> </li> <li> <p><code>is_alive()</code> to check if the process is alive. </p> </li> <li> <p><code>kill()</code> to terminate the process. </p> </li> <li> <p><code>poll_io()</code> to wait for any standard output, standard error, or the completion of the process, with a timeout. </p> </li> <li> <p><code style="white-space: pre;">read_*()</code> to read the standard output or error. </p> </li> <li> <p><code>suspend()</code> and <code>resume()</code> to stop and continue a process. </p> </li> <li> <p><code>wait()</code> to wait for the completion of the process, with a timeout. </p> </li></ul> <h4>Multiple background R processes and <code>poll()</code></h4> <p>Multiple background R processes are best managed with the <code>processx::poll()</code> 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. </p> <div class="sourceCode r"><pre>rp1 <- callr::r_bg(function() { Sys.sleep(1/2); "1 done" }) rp2 <- callr::r_bg(function() { Sys.sleep(1/1000); "2 done" }) processx::poll(list(rp1, rp2), 1000) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [[1]] #> output error process #> "silent" "silent" "silent" #> #> [[2]] #> output error process #> "ready" "ready" "ready" #> </pre></div> <div class="sourceCode r"><pre>rp2$get_result() </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [1] "2 done" </pre></div> <div class="sourceCode r"><pre>processx::poll(list(rp1), 1000) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [[1]] #> output error process #> "ready" "ready" "ready" #> </pre></div> <div class="sourceCode r"><pre>rp1$get_result() </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [1] "1 done" </pre></div> <h4>Persistent R sessions</h4> <p><code>r_session</code> is another <code>processx::process</code> subclass that represents a persistent background R session: </p> <div class="sourceCode r"><pre>rs <- callr::r_session$new() rs </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> R SESSION, alive, idle, pid 57198. </pre></div> <p><code>r_session$run()</code> is a synchronous call, that works similarly to <code>r()</code>, but uses the persistent session. <code>r_session$call()</code> starts the function call and returns immediately. The <code>r_session$poll_process()</code> method or <code>processx::poll()</code> can then be used to wait for the completion or other events from one or more R sessions, R processes or other <code>processx::process</code> objects. </p> <p>Once an R session is done with an asynchronous computation, its <code>poll_process()</code> method returns <code>"ready"</code> and the <code>r_session$read()</code> method can read out the result. </p> <div class="sourceCode r"><pre>rs <- callr::r_session$new() rs$run(function() runif(10)) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [1] 0.9694117 0.1148252 0.7002372 0.5020423 0.6733614 0.2834034 0.4684455 #> [8] 0.2777103 0.3983423 0.3311824 </pre></div> <div class="sourceCode r"><pre>rs$call(function() rnorm(10)) rs </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> R SESSION, alive, busy, pid 57202. </pre></div> <div class="sourceCode r"><pre>rs$poll_process(2000) </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> [1] "ready" </pre></div> <div class="sourceCode r"><pre>rs$read() </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> $code #> [1] 200 #> #> $message #> [1] "done callr-rs-result-df023a1ea239" #> #> $result #> [1] -0.120592200 1.064599579 0.411464719 0.003468047 1.217852929 #> [6] 0.619740451 -0.384256042 -0.727031228 -1.054600334 -0.605265817 #> #> $stdout #> [1] "" #> #> $stderr #> [1] "" #> #> $error #> NULL #> #> attr(,"class") #> [1] "callr_session_result" </pre></div> <h4>Running <code style="white-space: pre;">R CMD</code> commands</h4> <p>The <code>rcmd()</code> function calls an <code style="white-space: pre;">R CMD</code> command. For example, you can call <code style="white-space: pre;">R CMD INSTALL</code>, <code style="white-space: pre;">R CMD check</code> or <code style="white-space: pre;">R CMD config</code> this way: </p> <div class="sourceCode r"><pre>callr::rcmd("config", "CC") </pre></div> <div class="asciicast" style="color: #172431;font-family: 'Fira Code',Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;line-height: 1.300000"><pre> #> $status #> [1] 0 #> #> $stdout #> [1] "clang -arch arm64\n" #> #> $stderr #> [1] "" #> #> $timeout #> [1] FALSE #> #> $command #> [1] "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/bin/R" #> [2] "CMD" #> [3] "config" #> [4] "CC" #> </pre></div> <p>This returns a list with three components: the standard output, the standard error, and the exit (status) code of the <code style="white-space: pre;">R CMD</code> command. </p> <h4>Code of Conduct</h4> <p>Please note that the callr project is released with a <a href="https://callr.r-lib.org/CODE_OF_CONDUCT.html">Contributor Code of Conduct</a>. By contributing to this project, you agree to abide by its terms. </p> <h4>License</h4> <p>MIT © Mango Solutions, RStudio </p> <h3>Author(s)</h3> <p><strong>Maintainer</strong>: Gábor Csárdi <a href="mailto:csardi.gabor@gmail.com">csardi.gabor@gmail.com</a> (<a href="https://orcid.org/0000-0001-7098-9676">ORCID</a>) [copyright holder] </p> <p>Authors: </p> <ul> <li><p> Winston Chang </p> </li></ul> <p>Other contributors: </p> <ul> <li><p> RStudio [copyright holder, funder] </p> </li> <li><p> Mango Solutions [copyright holder, funder] </p> </li></ul> <h3>See Also</h3> <p>Useful links: </p> <ul> <li> <p><a href="https://callr.r-lib.org">https://callr.r-lib.org</a> </p> </li> <li> <p><a href="https://github.com/r-lib/callr#readme">https://github.com/r-lib/callr#readme</a> </p> </li> <li><p> Report bugs at <a href="https://github.com/r-lib/callr/issues">https://github.com/r-lib/callr/issues</a> </p> </li></ul> <hr /><div style="text-align: center;">[Package <em>callr</em> version 3.7.2 <a href="00Index.html">Index</a>]</div> </body></html>