<p>Here’s a new package that brings to R new API to handle child processes – similar to how <a target="_blank" rel="nofollow" href="https://docs.python.org/3/library/subprocess.html">Python handles</a> them.</p>
<p>Unlike the already available <code><a target="_blank" rel="nofollow" href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/system.html">system()</a></code> and <code><a target="_blank" rel="nofollow" href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/system2.html">system2()</a></code> calls from the <code>base</code> package or the <code><a target="_blank" rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/parallel/html/mclapply.html">mclapply()</a></code> function from the <code>parallel</code> package, this new API is aimed at handling long-lived child processes that can be controlled by the parent R process in a programmatic way. Here’s an example:</p>
<pre>handle &lt;- spawn_process(&quot;/usr/bin/sshpass&quot;,
                        c(&quot;ssh&quot;, &quot;-T&quot;, &quot;user@domain.com&quot;))
process_write(handle, &quot;password&quot;)
process_write(handle, &quot;ls\n&quot;)
process_read(handle, &quot;stdout&quot;)
<em>#> &quot;bin&quot; &quot;public_html&quot; &quot;www-backup&quot;</em></pre>
<p>This of course can be done with <code>system(&quot;ssh&quot;, c(&quot;user@domain.com&quot;, &quot;ls&quot;))</code> as well (at least as long as password-less ssh connectivity is enabled). However, if there is a need to make a number of subsequent calls in response to user’s input, keeping a single connection open can save some time. Otherwise you need to wait for <code>ssh</code> to establish a new connection each time a new command is to be executed.</p>
<p>Perhaps a bit more silly example is working with a local (or remote, for that matter) Spark session. Imagine there is no package dedicated to Spark (which might well be the case with the next new thing that you find under your Christmas tree this year). The simplest approach could be to open Spark console and keep it alive while sending commands on its standard input and parsing the text output. However naive, this approach can save some prototyping time.</p>
<pre>handle &lt;- spawn_process(&quot;/usr/bin/spark-shell&quot;)
process_write(handle, 'val textFile = sc.textFile(&quot;README.md&quot;)\n')
process_write(handle, 'textFile.count()\n')
process_read(handle)
[1] &quot;textFile: org.apache.spark.rdd.RDD[String] = README.md MapPart
itionsRDD[1] at textFile at &lt;console>:25&quot;
[2] &quot;res0: Long = 126&quot;</pre>
<p>The new <code>subprocess</code> package is available <a target="_blank" rel="nofollow" href="https://github.com/lbartnik/subprocess">from my GitHub account</a> and <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=subprocess">CRAN</a>. All functions can be run in both Linux and Windows and the few OS-specific details (like signals) are described in respective manual pages. There is also an <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/subprocess/vignettes/intro.html">introductory vignette</a>.</p>
<p>I should also say that this package has been designed with <a target="_blank" rel="nofollow" href="https://docs.python.org/3/library/subprocess.html">Python’s subprocess module</a> in mind, which (both package <em>and</em> language) I greatly admire. Its R equivalent is now in version 0.7.4 which is there to indicate that it’s a perfect equivalent. More (simultaneous wait on <em>stdout</em> and <em>stderr</em>) are still to come.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/randomremarksblog.wordpress.com/190/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/randomremarksblog.wordpress.com/190/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Yxfq0Gnkr1I">