<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/deferred-remote-function-execution-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://random-remarks.net/2017/06/03/deferred-remote-function-execution-in-r/"> R – Random Remarks</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>What would you say if you could automatically wrap your R pipeline – which consists of numerous functions and variables – into a single function? What would you say if you could do it repeatedly, with no extra effort regardless of its complexity? Would you store it to document your progress at that particular moment in time? Would you serialize and send it to a remote R session to offload computations? Well, it’s kind-of possible.</p>
<p><code><a rel="nofollow" href="https://github.com/lbartnik/defer" target="_blank">defer</a></code> brings a new capability to R – packaging, as a single R function, a number of inter-connected functions and variables which exist in the current R session. It is something I found useful during fast-prototyping phases of my own work and wanted to share for quite some time now – and at this point I’m interested in seeing if it turns out useful for someone else besides me.</p>
<p>Here’s a simplistic example where <code>defer</code> is given a top-level function <code>h()</code> that requires two other functions (<code>f()</code> and <code>g()</code>), a variable (<code>C</code>) and a library call (<code>base::mean</code>).</p>
<pre>C &lt;- 10

f &lt;- function(x) x*x + C
g &lt;- function(y) (f(y) + 10) * f(y+3)
h &lt;- function(z) mean(c(g(z), g(z+1), g(z+2)))

wrapper &lt;- defer(h)
#> Found functions:
#>   g, f
#> variables:
#>   C
#> library calls:
#>   base::mean

rm(C, f, g, h)

wrapper(10)
#> [1] 29688.67</pre>
<p>Here is another example where that same <code>wrapper()</code> can be run in a remote R session (via <code>opencpu</code>) without installing or deploying any extra code on the remote server. First, we serialize <code>wrapper</code> as a base64-encoded string.</p>
<pre>library(base64enc)

buffer &lt;- rawConnection(raw(0), 'w')
saveRDS(wrapper, buffer)
serialized &lt;- base64enc::base64encode(rawConnectionValue(buffer))</pre>
<p>Then, we write that string into a temporary file together with a few lines of code to deserialize and run it in a remote R session.</p>
<pre>local_script_path &lt;- tempfile(fileext = &quot;.R&quot;)

cat(paste0(&quot;base64 &lt;- '&quot;, serialized, &quot;'\n&quot;,
           &quot;library(base64enc)\n&quot;,
           &quot;bytes &lt;- rawConnection(base64enc::base64decode(base64), 'r')\n&quot;,
           &quot;deserialized &lt;- readRDS(bytes)\n&quot;,
           &quot;deserialized(10)\n&quot;),
    file = local_script_path)</pre>
<p>Finally, we upload the file on the <code>opencpu</code> server and run it via <code>base::source</code>.</p>
<pre>library(httr)
result &lt;- httr::POST(public_opencpu_url,
                     body = list(file = upload_file(local_script_path)))
content(result, 'text')
#> [1] &quot;$value\n[1] 29688.67\n\n$visible\n[1] TRUE\n\n&quot;</pre>
<p>And that seems like a number we’ve seen already, right?</p>
<p>The package is currently available only from <a rel="nofollow" href="https://github.com/lbartnik/defer" target="_blank">GitHub</a> as it’s way not ready for CRAN.</p>
<p> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/randomremarksblog.wordpress.com/733/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/comments/randomremarksblog.wordpress.com/733/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://random-remarks.net/2017/06/03/deferred-remote-function-execution-in-r/"> R – Random Remarks</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...

	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/IZ4WWh6Hc_g" height="1"></a></div>