-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples.html
161 lines (133 loc) · 14.4 KB
/
examples.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Lukasz A. Bartnik" />
<meta name="date" content="2017-06-09" />
<title>Examples for Third-Party Packages</title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link href="data:text/css;charset=utf-8,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20700px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%201%2E35%3B%0A%7D%0A%23header%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%2010px%3B%0Apadding%3A%204px%3B%0Awidth%3A%20400px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%20%7B%0Amargin%3A%201em%20auto%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%200%2E25em%200%2E75em%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0Awhite%2Dspace%3A%20pre%2Dwrap%3B%20%0A%7D%0Apre%20%7B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200px%2010px%200px%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0Afont%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%200px%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%3A%201px%20solid%20%23DDDDDD%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3A%2040px%3B%0A%7D%0Ah2%20%7B%0Aborder%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Apadding%2Dbottom%3A%202px%3B%0Afont%2Dsize%3A%20145%25%3B%0A%7D%0Ah3%20%7B%0Aborder%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Ah4%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0Amargin%2Dleft%3A%208px%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Ah5%2C%20h6%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23ccc%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Aa%20%7B%0Acolor%3A%20%230033dd%3B%0Atext%2Ddecoration%3A%20none%3B%0A%7D%0Aa%3Ahover%20%7B%0Acolor%3A%20%236666ff%3B%20%7D%0Aa%3Avisited%20%7B%0Acolor%3A%20%23800080%3B%20%7D%0Aa%3Avisited%3Ahover%20%7B%0Acolor%3A%20%23BB00BB%3B%20%7D%0Aa%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0Aa%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%20code%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%0A" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 class="title toc-ignore">Examples for Third-Party Packages</h1>
<h4 class="author"><em>Lukasz A. Bartnik</em></h4>
<h4 class="date"><em>2017-06-09</em></h4>
<div id="deferred-and-remote" class="section level2">
<h2>Deferred and Remote</h2>
<p>In the <a href="intro.html">Introduction</a> vignette we explain the steps to create a simple <strong>deferred function wrapper</strong>. Here we will show how it can be combined with other packages to implement a simple RPC mechanism.</p>
</div>
<div id="preparing-the-wrapper" class="section level2">
<h2>Preparing the Wrapper</h2>
<p>Unlike in the <a href="intro.html">Introduction</a>, to avoid clouding the message, this time we will work with a much simplified example. Here’s the function we want to run in a remote R session:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">fun <-<span class="st"> </span>function(x, y) x ^<span class="st"> </span>y</code></pre></div>
<p>Now we need to wrap it using <code>defer()</code>. Rrunning the wrapper is still straightforward.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(defer)
deferred_fun <-<span class="st"> </span><span class="kw">defer</span>(fun)
<span class="kw">deferred_fun</span>(<span class="dv">9</span>, <span class="dv">2</span>)
<span class="co">#> [1] 81</span></code></pre></div>
<p>Because it is easier to run an argument-less function, our first step will be to <strong>augment</strong> our deferred function wrapper with a value for both of its arguments.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">deferred_fun <-<span class="st"> </span><span class="kw">augment</span>(deferred_fun, <span class="dt">x =</span> <span class="dv">7</span>, <span class="dt">y =</span> <span class="dv">3</span>)</code></pre></div>
<p>Now we can call <code>deferred_fun()</code> completely ommitting arguments (see <code>?defer::augment</code> for more details).</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">deferred_fun</span>()
<span class="co">#> [1] 343</span></code></pre></div>
</div>
<div id="opencpu" class="section level2">
<h2>OpenCPU</h2>
<p>Our first example will be <a href="www.opencpu.org">OpenCPU</a>. We will also need two other packages: <a href="https://cran.r-project.org/package=jsonlite">jsonlite</a> to serialize the wrapper and <a href="https://cran.r-project.org/package=httr">httr</a> to talk to OpenCPU’s HTTP-based API. <code>jsonlite</code> is a prerequisite of <code>opencpu</code> so we can be sure it will be available.</p>
<div id="making-the-call" class="section level3">
<h3>Making the Call</h3>
<p>With <code>OpenCPU</code> we can run any R function available in any of the packages installed on the remote R server. <code>OpenCPU</code> also knows how to handle an uploaded file, thus we will call <code>base::source()</code> and send a very simple script, <code>local_script_path</code>, as its argument. We will call a public <code>OpenCPU</code> server, <strong>cloud.opencpu.org</strong>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(httr)
<span class="kw">library</span>(jsonlite)
public_opencpu_url <-<span class="st"> "https://cloud.opencpu.org/ocpu/library/base/R/source/json"</span>
local_script_path <-<span class="st"> </span><span class="kw">tempfile</span>(<span class="dt">fileext =</span> <span class="st">".R"</span>)</code></pre></div>
<p>Here is how we invoke the remote <code>OpenCPU</code> server throgh <code>httr</code>:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">http_result <-<span class="st"> </span>httr::<span class="kw">POST</span>(public_opencpu_url,
<span class="dt">body =</span> <span class="kw">list</span>(<span class="dt">file =</span> <span class="kw">upload_file</span>(local_script_path)))</code></pre></div>
</div>
<div id="the-script" class="section level3">
<h3>The Script</h3>
<p>The script contains <code>deferred_fun</code> serialized into a base64-encoded string. It instructs the remote R session to decode the string, unserialize the wrapper and then run it in that remote R session.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">serialized_wrapper <-<span class="st"> </span>jsonlite::<span class="kw">base64_enc</span>(<span class="kw">serialize</span>(deferred_fun, <span class="ot">NULL</span>))
<span class="kw">cat</span>(<span class="kw">paste0</span>(<span class="st">"wrapper <- unserialize(jsonlite::base64_dec('"</span>, serialized_wrapper, <span class="st">"'))</span><span class="ch">\n</span><span class="st">"</span>,
<span class="st">"wrapper()</span><span class="ch">\n</span><span class="st">"</span>),
<span class="dt">file =</span> local_script_path)</code></pre></div>
</div>
<div id="running-the-opencpu-example" class="section level3">
<h3>Running the OpenCPU Example</h3>
<p>Now that everything is set, we can run the HTTP call and collect the result.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">http_result <-<span class="st"> </span>httr::<span class="kw">POST</span>(public_opencpu_url,
<span class="dt">body =</span> <span class="kw">list</span>(<span class="dt">file =</span> <span class="kw">upload_file</span>(local_script_path)))
<span class="kw">fromJSON</span>(<span class="kw">content</span>(http_result, <span class="st">'text'</span>))
<span class="co">#> No encoding supplied: defaulting to UTF-8.</span>
<span class="co">#> $value</span>
<span class="co">#> [1] 343</span>
<span class="co">#> </span>
<span class="co">#> $visible</span>
<span class="co">#> [1] TRUE</span></code></pre></div>
<p><code>source()</code> returns a <code>list</code> with two elements: <code>value</code> is the value of the last statement in the file - which is 7 to the power of 3, as expected; <code>visible</code> is an attribute that tells us if <code>value</code> was intended to be printed out.</p>
</div>
</div>
<div id="foreach" class="section level2">
<h2>foreach</h2>
</div>
<div id="rserve" class="section level2">
<h2>Rserve</h2>
</div>
<div id="sparkr" class="section level2">
<h2>SparkR</h2>
</div>
<div id="planned-extension-for-dplyrdo" class="section level2">
<h2>(planned) extension for dplyr::do</h2>
</div>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>