<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Colin Fay</title>
<link>https://colinfay.me/</link>
<atom:link href="https://colinfay.me/index.xml" rel="self" type="application/rss+xml"/>
<description>Mostly R, data, and code.</description>
<generator>quarto-1.6.40</generator>
<lastBuildDate>Fri, 14 Jun 2024 00:00:00 GMT</lastBuildDate>
<item>
  <title>webrcli &amp; spidyr: What’s new</title>
  <link>https://colinfay.me/posts/webrcli-and-spidyr-whats-new.html</link>
  <description><![CDATA[ 




<p>Find all my posts about webR <a href="https://colinfay.me/categories/#webr">here</a>.</p>
<blockquote class="blockquote">
<p>Note: the first post of this series explaining roughly what <code>webR</code> is, I won’t introduce it again here.</p>
</blockquote>
<p>In this blogpost, you’ll find some updates about some recent changes made in <code>webrcli</code> &amp; <code>spidyr</code>.</p>
<section id="what-webrcli-spidy-wants-to-achieve" class="level2">
<h2 class="anchored" data-anchor-id="what-webrcli-spidy-wants-to-achieve">What webrcli &amp; spidy wants to achieve</h2>
<p>In a nutshell, the goal of <code>webrcli</code> &amp; <code>spidyr</code> is to provide an interface for bringing your R functions inside a NodeJS runtime.</p>
<p>Think of it as something that wants to achieve the following interation:</p>
<ul>
<li><p>An R dev writes, builds and shares a package called <code>{rfuns}</code>, which contains a function called <code>hello_world()</code></p></li>
<li><p>A NodeJS dev takes this package, can call it with <code>rfuns.hello_world()</code>, access and manipulate the results with JavaScript.</p></li>
</ul>
<p><img src="https://colinfay.me/assets/img/js-call-r.png" class="img-fluid"></p>
</section>
<section id="an-important-note" class="level2">
<h2 class="anchored" data-anchor-id="an-important-note">An important note</h2>
<p>With <code>webR</code> &amp; the tools described below, there is no R session running while the NodeJS app is running. <code>spidyr</code> imports an R function <strong>converted to a native JS</strong>. <code>rfuns.hello_world</code> does not call an R session running somewhere else.</p>
<p>That also means that if you build a container containing the app, you don’t need to install R in it. It’s a NodeJS app.</p>
<p>Below are some updates recently made to both <code>webrcli</code> &amp; <code>spidyr</code>.</p>
</section>
<section id="making-the-template-code-smaller" class="level2">
<h2 class="anchored" data-anchor-id="making-the-template-code-smaller">Making the template code smaller</h2>
<p>When drafting the first code of <code>webrcli</code> &amp; <code>spidyr</code>, I focused on providing a minimal template for an <code>index.js</code> loading your R functions, contained in a subdirectory inside your node project.</p>
<p>This worked, but might have been a bit complex to grasp. Provided you don’t change the default names of the folders (in that case you’ll need to modify the default parameters), the new default <code>index.js</code> look like this:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> {</span>
<span id="cb1-2">  initSpidyr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-3">  mountLocalPackage</span>
<span id="cb1-4">} <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'spidyr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"></span>
<span id="cb1-6">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb1-7"></span>
<span id="cb1-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">initSpidyr</span>()</span>
<span id="cb1-9"></span>
<span id="cb1-10">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> rfuns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mountLocalPackage</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./rfuns"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-11"></span>
<span id="cb1-12">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> hw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> rfuns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hello_world</span>()</span>
<span id="cb1-13"></span>
<span id="cb1-14">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(hw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-15"></span>
<span id="cb1-16">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ Everything is ready!"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-17">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>This minimal template makes it easier to read and understand what the code does. Note that you’ll get this template if you run <code>webrcli init myproject</code>.</p>
<p>Let’s decompose what this code does :</p>
<ul>
<li><p><code>await initSpidyr()</code> will create an object called <code>spidyr_webR</code> stored as <code>globalThis.spidyr_webR</code>, <code>init()</code> it (as in <code>webr.init()</code>), and load the packages contained in the <code>./webr_packages</code> folder inside your project dir, the one containing the packages installed with <code>webrcli install</code>.</p></li>
<li><p><code>const rfuns = await mountLocalPackage("./rfuns");</code> will take the R package contained in <code>./rfuns</code>, load it into the webR instance (<code>spidyr_webR</code>), recursively get all the exported functions from your package, and store them inside the <code>rfuns</code> object.</p></li>
<li><p><code>const hw = await rfuns.hello_world()</code> will call the <code>hello_world()</code> function from <code>{rfuns}</code>. The values are then <code>console.log</code>ed.</p></li>
</ul>
</section>
<section id="new-library-is-now-library" class="level2">
<h2 class="anchored" data-anchor-id="new-library-is-now-library"><code>new Library()</code> is now <code>library()</code></h2>
<p>Calling a package is now <code>const spongebob = library("spongebob")</code> instead of the previous approach which was <code>const spongebob = new Library("spongebob");await spongebob.load(globalThis.webR);</code></p>
</section>
<section id="better-installation-from-package.json-description" class="level2">
<h2 class="anchored" data-anchor-id="better-installation-from-package.json-description">Better installation from package.json &amp; <code>DESCRIPTION</code></h2>
<p>The previous version of <code>webrcli</code> did not stored nor re-installed the R package installed in the project, meaning that if you git cloned a project, you had to guess what R packages to re-install.</p>
<p>Now, a project initiated with <code>webrcli</code> will store all the packages installed with <code>webrcli install</code> inside the <code>package.json</code> file. Then, when running the <code>npm install</code> command, the R packages will also be redownloaded.</p>
<p>You can also installed a series of packages based on a <code>DESCRIPTION</code> file with <code>webrcli installFromDesc</code>.</p>
</section>
<section id="other-tools" class="level2">
<h2 class="anchored" data-anchor-id="other-tools">Other tools</h2>
<ul>
<li><code>shareEnv</code> allows to <strong>copy</strong> one, several or all env var from node to the webR instance.</li>
</ul>
</section>
<section id="workflow-summary" class="level2">
<h2 class="anchored" data-anchor-id="workflow-summary">Workflow summary</h2>
<ul>
<li><p><code>webrcli init myproject</code> will create a project skeleton at <code>./myproject</code></p></li>
<li><p><code>webrcli install spongebob</code> will download the version of <code>{spongebob}</code> compiled for <code>webR</code>, and add it to <code>./webr_packages</code>.</p></li>
<li><p>You can then modify you R code with your own functions.</p></li>
<li><p>Call your R function from <code>rfuns.xyz()</code> in Node</p></li>
<li><p>Run with <code>npm start</code></p></li>
</ul>
<p><img src="https://colinfay.me/assets/img/webrcli-init.png" class="img-fluid"></p>
</section>
<section id="please-do-give-it-a-shot" class="level2">
<h2 class="anchored" data-anchor-id="please-do-give-it-a-shot">Please do give it a shot</h2>
<p>I think I’ve been the only user for now 😅, so please do try these and let me know what you think.</p>
</section>
<section id="future-works" class="level2">
<h2 class="anchored" data-anchor-id="future-works">Future works</h2>
<p>Here are what I plan on working in the upcoming weeks:</p>
<ul>
<li><p><code>webrcli init</code> should allow for specifying a <code>webr</code> version from npm <a href="https://github.com/ColinFay/webrcli/issues/17">issue 17</a></p></li>
<li><p><code>initSpidyr</code> should allow to pass params to <code>webR.init()</code> <a href="https://github.com/ColinFay/spidyr/issues/12">issue 12</a></p></li>
<li><p>Allow to install from <code>r-universe</code>, something like <code>webrcli installFromRUniverse</code> <a href="https://github.com/ColinFay/webrcli/issues/16">issue 16</a>. Probably in the future it will be merged to <code>webrcli install</code> and we’ll have a way to guess what the user wants based on the input.</p></li>
<li><p>A small converter from R named list to JS objects <a href="https://github.com/ColinFay/spidyr/issues/3">Issue 3</a></p></li>
<li><p>More documentation, and probably documenting the conversion of an existing <code>{shiny}</code> app to a <code>spidyr</code> based one.</p></li>
</ul>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/webrcli-and-spidyr-whats-new/";
  this.page.identifier = "/webrcli-and-spidyr-what-new";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>webr</category>
  <guid>https://colinfay.me/posts/webrcli-and-spidyr-whats-new.html</guid>
  <pubDate>Fri, 14 Jun 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>webrcli &amp; spidyr: A starter pack for building NodeJS projects with webR inside</title>
  <link>https://colinfay.me/posts/webrcli-and-spidyr.html</link>
  <description><![CDATA[ 




<p>Find all my posts about webR <a href="https://colinfay.me/categories/#webr">here</a>.</p>
<blockquote class="blockquote">
<p>Note: the first post of this series explaining roughly what <code>webR</code> is, I won’t introduce it again here.</p>
</blockquote>
<section id="again-a-new-project" class="level2">
<h2 class="anchored" data-anchor-id="again-a-new-project">Again, a new project?</h2>
<p>Most of the previous posts have been about poking around with <code>webR</code> inside NodeJS, but nothing stable on how to build an app.</p>
<p>That’s something I’m trying to change. Based on the experience gathered building apps in R (in <code>{shiny}</code> with <code>{golem}</code>) and in JavaScript, I’ve tried to come up with a solution for a starter kit for building what I think will be the perfect skeleton for <code>webR</code>/<code>NodeJS</code> project.</p>
<p>The idea is to have one project with inside:</p>
<ul>
<li>One R package</li>
<li>One JS app</li>
<li>A tool that brings the R package into the NodeJS app and allow to use the R functions inside JavaScript</li>
</ul>
<p>This skeleton needed a toolkit to perform the following task:</p>
<p>1️⃣ From a cli point of view:</p>
<ul>
<li>init the project with a specific skeleton</li>
<li>allow to install packages from the <code>webr</code>CRAN to a local <code>webr</code> library, in the spirit of <a href="https://colinfay.me/rethinking-packages-and-functions-preloading-in-webr-0.2.2/">previous post</a>.</li>
<li>allow to install the deps from a DESCRIPTION file</li>
</ul>
<p>2️⃣ From a NodeJS point of view:</p>
<ul>
<li>Load the packages downloaded in a local <code>webr</code> library</li>
<li>Create an interface to load and manipulate the functions from a package downloaded from CRAN, and use them in JS.</li>
<li>Same but with a local package folder</li>
</ul>
<p>For the function manipulation mechanism, I wanted something that would allow to think in terms of <code>pkg::fun()</code> but in JavaScript, which would translate as <code>pkg.fun()</code> in your Node app. Something I had implemented in another old NodeJS module called <a href="https://github.com/ColinFay/hordes">hordes</a>, which I can now safely deprecate in favor of the following tools.</p>
<p>The reasoning being: the R dev writes a standalone package that exports an <code>xyz</code> function, then the web team can load this package, and launch <code>xyz()</code> without writing any R code.</p>
</section>
<section id="webrcli-spidyr" class="level2">
<h2 class="anchored" data-anchor-id="webrcli-spidyr">webrcli &amp; spidyr</h2>
<p>So, here comes <a href="https://www.npmjs.com/package/webrcli">webrcli</a> and <a href="https://www.npmjs.com/package/spidyr">spidyr</a>.</p>
<p>❗️ Please note that these tools are work in progress and has been used very few times and only for example apps, will need a lot of bug fixes and documentation, so if ever you plan on using it be indulgent, and report any bug or feature request ❗️</p>
<section id="project-init" class="level3">
<h3 class="anchored" data-anchor-id="project-init">Project init</h3>
<p>These packages are made to be used together, and here is an example of how to use them:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global installing webrcli</span></span>
<span id="cb1-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> install <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-g</span> webrcli</span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Init a webrcli project</span></span>
<span id="cb1-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> /tmp</span>
<span id="cb1-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">webrcli</span> init webrspongebob</span></code></pre></div>
<pre><code>👉 Initializing project ----
(This may take some time, please be patient)
👉 Copying template ----
👉 Installing {pkgload} ----
✅ {pkgload} downloaded and extracted ----

✅ {cli} downloaded and extracted ----

✅ {crayon} downloaded and extracted ----

✅ {desc} downloaded and extracted ----

✅ {fs} downloaded and extracted ----

✅ {glue} downloaded and extracted ----

✅ {pkgbuild} downloaded and extracted ----

✅ {rlang} downloaded and extracted ----

✅ {rprojroot} downloaded and extracted ----

✅ {withr} downloaded and extracted ----

✅ {R6} downloaded and extracted ----

✅ {callr} downloaded and extracted ----

✅ {processx} downloaded and extracted ----

✅ {ps} downloaded and extracted ----</code></pre>
<p>The project is now created, let’s move into it.</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> ./webrspongebob</span>
<span id="cb3-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">tree</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-L</span> 1</span></code></pre></div>
<pre><code>.
├── index.js
├── node_modules
├── package-lock.json
├── package.json
├── rfuns
└── webr_packages

4 directories, 3 files</code></pre>
<p>Here is how it’s organized:</p>
<ul>
<li><code>index.js</code> is the main file for the app, <code>node_modules</code> the standard folder for the node deps</li>
<li><code>package-lock.json</code> / <code>package.json</code> are standard Node metadata files</li>
<li><code>rfuns</code> contains the R package that will be added to the NodeJS app</li>
<li><code>webr_packages</code> contains the R dependencies</li>
</ul>
<p>We can launch the app with <code>node index.js</code> and it will output:</p>
<pre><code>node index.js
👉 Loading WebR ----
👉 Loading R packages ----
ℹ Loading rfuns
[ 'Hello, world!' ]
✅ Everything is ready</code></pre>
<p>Let’s dive a bit inside the <code>index.js</code>:</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'path'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { WebR } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { loadPackages<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> LibraryFromLocalFolder } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'spidyr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> rfuns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">LibraryFromLocalFolder</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rfuns"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-6"></span>
<span id="cb6-7"></span>
<span id="cb6-8">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb6-9"></span>
<span id="cb6-10">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"👉 Loading WebR ----"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-11">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WebR</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-13"></span>
<span id="cb6-14">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"👉 Loading R packages ----"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-15"></span>
<span id="cb6-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(</span>
<span id="cb6-17">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-18">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr_packages'</span>)</span>
<span id="cb6-19">  )</span>
<span id="cb6-20"></span>
<span id="cb6-21">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> rfuns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mountAndLoad</span>(</span>
<span id="cb6-22">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-23">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rfuns'</span>)</span>
<span id="cb6-24">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-25"></span>
<span id="cb6-26">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> hw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> rfuns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hello_world</span>()</span>
<span id="cb6-27"></span>
<span id="cb6-28">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(hw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-29"></span>
<span id="cb6-30">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ Everything is ready!"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-31"></span>
<span id="cb6-32">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>Here are the bits that are specific to a <code>spidyr</code> project:</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb7-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> rfuns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">LibraryFromLocalFolder</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rfuns"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>This function will take a local folder containing an R package, and load the functions from this package into the <code>rfuns</code> object. Here, for example, our R package contains one R function, <code>hello_world()</code>, it will then be available in NodeJS as <code>rfuns.hello_world()</code> once the <code>mountAndLoad</code> function is called.</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb8-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(</span>
<span id="cb8-2">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-3">  path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr_packages'</span>)</span>
<span id="cb8-4">)</span>
<span id="cb8-5"></span>
<span id="cb8-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> rfuns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mountAndLoad</span>(</span>
<span id="cb8-7">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-8">  path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rfuns'</span>)</span>
<span id="cb8-9">)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>The first bit loads the <code>webr_packages</code> folder, containing all the R dependencies, then the second bit mount the local folder into the <code>webR</code> file system and load the functions.</p>
<p>Finally, <code>const hw = await rfuns.hello_world()</code> calls the function from the R package, and its value is <code>console.log</code>ed just after that.</p>
</section>
<section id="with-a-cran-package" class="level3">
<h3 class="anchored" data-anchor-id="with-a-cran-package">With a CRAN package</h3>
<p>And now, how do I load a CRAN package? For example, let’s say I want to use <code>{spongebob}</code> in my app?</p>
<p>First, let’s install <code>{spongebob}</code> via <code>webrcli</code> :</p>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">webrcli</span> install spongebob</span></code></pre></div>
<pre><code>✅ {spongebob} downloaded and extracted ----</code></pre>
<p>Then, let’s update our <code>index.js</code>:</p>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'path'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { WebR } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { loadPackages<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> LibraryFromLocalFolder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> Library } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'spidyr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-4"></span>
<span id="cb11-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> rfuns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">LibraryFromLocalFolder</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rfuns"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> spongebob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spongebob"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-7"></span>
<span id="cb11-8">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb11-9"></span>
<span id="cb11-10">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"👉 Loading WebR ----"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-11">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WebR</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-13"></span>
<span id="cb11-14">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"👉 Loading R packages ----"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-15"></span>
<span id="cb11-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(</span>
<span id="cb11-17">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-18">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr_packages'</span>)</span>
<span id="cb11-19">  )</span>
<span id="cb11-20"></span>
<span id="cb11-21">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> rfuns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mountAndLoad</span>(</span>
<span id="cb11-22">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-23">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rfuns'</span>)</span>
<span id="cb11-24">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-25"></span>
<span id="cb11-26">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> spongebob<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(</span>
<span id="cb11-27">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span></span>
<span id="cb11-28">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-29"></span>
<span id="cb11-30">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> hw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> rfuns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hello_world</span>()</span>
<span id="cb11-31"></span>
<span id="cb11-32">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(hw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-33"></span>
<span id="cb11-34">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> said <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> spongebob<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tospongebob</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello from spongebob"</span>)</span>
<span id="cb11-35"></span>
<span id="cb11-36">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(said<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span>)</span>
<span id="cb11-37"></span>
<span id="cb11-38">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ Everything is ready!"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;;</span></span>
<span id="cb11-39"></span>
<span id="cb11-40">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>Here:</p>
<ul>
<li><code>const spongebob = new Library("spongebob");</code> will create a lib, ready to mount a package</li>
<li><code>await spongebob.load(globalThis.webR)</code> will load all the functions from the <code>{spongebob}</code> package</li>
<li><code>const said = await spongebob.tospongebob("hello from spongebob")</code> will run the <code>spongebob::tospongebob()</code> R function</li>
</ul>
<p>Then :</p>
<div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">node</span> index.js</span></code></pre></div>
<pre><code>👉 Loading WebR ----
👉 Loading R packages ----
ℹ Loading rfuns
[ 'Hello, world!' ]
[ 'helLo fROm spONgEbOb' ]
✅ Everything is ready!</code></pre>
</section>
</section>
<section id="some-random-notes" class="level2">
<h2 class="anchored" data-anchor-id="some-random-notes">Some random notes</h2>
<p>The <code>webrspongebob</code> example is available at <a href="https://github.com/ColinFay/webrspongebob" class="uri">https://github.com/ColinFay/webrspongebob</a></p>
<section id="about-the-current-implementation" class="level3">
<h3 class="anchored" data-anchor-id="about-the-current-implementation">About the current implementation</h3>
<ul>
<li><p>Right now I’m not really sure how this handles the infix function like <code>%&gt;%</code> for example. That being said, this might not be a function you’d want to use in the current way of building things.</p></li>
<li><p>The exported functions are read via <code>getNamespaceExports("pkg")</code> and <code>getExportedValue("pkg", "fun")</code>, if ever this is not the perfect way to do this in base R but I’ll be happy to find some other ways to do this.</p></li>
<li><p><code>webrcli</code> and <code>spidyr</code> both encapsulate code run in <code>webR</code>, especially <code>webrcli</code> which has an <a href="https://github.com/ColinFay/webrcli/blob/main/rpkg/R/get_list_of_tar_gz_dependencies_for_package.R">R script</a> which is sourced and run in a <code>webR</code> instance.</p></li>
</ul>
</section>
<section id="future" class="level3">
<h3 class="anchored" data-anchor-id="future">Future</h3>
<p>Please do try these tools. I would be very happy to have your feedback on the philosophy, and on the general workflow.</p>
<p>If ever you find a bug or have an idea, feel free to open issues at :</p>
<ul>
<li><p>https://github.com/ColinFay/webrcli</p></li>
<li><p>https://github.com/ColinFay/spidyr</p></li>
</ul>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/webrcli-and-spidyr/";
  this.page.identifier = "/webrcli-and-spidyr";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>webr</category>
  <guid>https://colinfay.me/posts/webrcli-and-spidyr.html</guid>
  <pubDate>Wed, 06 Mar 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Rethinking packages &amp; functions preloading in webR 0.2.2</title>
  <link>https://colinfay.me/posts/rethinking-packages-and-functions-preloading-in-webr-0.2.2.html</link>
  <description><![CDATA[ 




<p>Find all my posts about webR <a href="https://colinfay.me/categories/#webr">here</a>.</p>
<blockquote class="blockquote">
<p>Note: the first post of this series explaining roughly what <code>webR</code> is, I won’t introduce it again here.</p>
</blockquote>
<p>When I wrote my blogpost about <a href="../preloading-your-r-packages-in-webr-in-an-express-js-api/">Preloading your R packages in webR in an Express JS API</a>, I mentioned that there was no native way to preload things in the webR filesystem — meaning that you had to reinstall all the R packages whenever the app was launched (and reported it in a <a href="https://github.com/r-wasm/webr/issues/260">Github issue</a>). This also meant that I couldn’t easily take my own functions and run them in the webR environment, as described in <a href="../using-own-functions-in-webr-node-js">Using my own R functions in webR in an Express JS API, and thoughts on building web apps with Node &amp; webR</a>. These needs made me write the <a href="https://www.npmjs.com/package/webrtools">webrtools</a> NodeJS package, to do just that:</p>
<ul>
<li>Download the R packages compiled for webR, to a local folder</li>
<li>Bundle them in the webR lib (by reading the folder tree and reproducing it in webR filesystem)</li>
<li>Load your local package to access its functions in webR</li>
</ul>
<p>The last webR version now exposes Emscripten’s <code>FS.mount</code>, so this makes the process easier, as there is now no need to read the file tree and recreate it: the folder from the server can be mounted into webR filesystem.</p>
<p>That implied some rework (now integrated to <code>webrtools</code>) :</p>
<ul>
<li>Use <code>webR.FS.mkdir</code> to create a local lib: the module can’t mount the package library into the libPath as is, because it would overwrite the already bundled library (in other words, if you mount into a folder that is not empty, the content from the server overwrites the content from webR).</li>
<li>Use <code>webR.FS.mount</code> to mount the local directory into the newly created library</li>
<li>Ensure that this new library is in the libPath()</li>
</ul>
<p>Here is the new code for <code>loadPackages</code>, bundled into <code>webrtools</code>:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> dirPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> libName <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"webr_packages"</span>) {</span>
<span id="cb1-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Create a custom lib so that we don't have to worry about</span></span>
<span id="cb1-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// overwriting any packages that are already installed.</span></span>
<span id="cb1-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`/usr/lib/R/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>libName<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb1-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Mount the custom lib</span></span>
<span id="cb1-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mount</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NODEFS"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">root</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> dirPath }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`/usr/lib/R/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>libName<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Add the custom lib to the R search path</span></span>
<span id="cb1-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`.libPaths(c('/usr/lib/R/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>libName<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">', .libPaths()))`</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-9">}</span></code></pre></div>
<p>I’ve also decided to deprecate the <code>loadFolder</code> function, as it is now native with <code>webR.FS.mkdir</code> + <code>webR.FS.mount</code>.</p>
<p>So here is a rewrite of the app from <a href="../using-own-functions-in-webr-node-js/">Using my own R functions in webR in an Express JS API, and thoughts on building web apps with Node &amp; webR</a>.</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'express'</span>)()</span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'path'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { loadPackages } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webrtools'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { WebR } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-5"></span>
<span id="cb2-6">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-7">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WebR</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-9"></span>
<span id="cb2-10">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"🚀 webR is ready 🚀"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-11"></span>
<span id="cb2-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(</span>
<span id="cb2-13">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-14">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr_packages'</span>)</span>
<span id="cb2-15">  )</span>
<span id="cb2-16"></span>
<span id="cb2-17">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/home/rfuns"</span>)</span>
<span id="cb2-18"></span>
<span id="cb2-19">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mount</span>(</span>
<span id="cb2-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NODEFS"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-21">    {</span>
<span id="cb2-22">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">root</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rfuns'</span>)</span>
<span id="cb2-23">    }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-24">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/home/rfuns"</span></span>
<span id="cb2-25">  )</span>
<span id="cb2-26"></span>
<span id="cb2-27">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"📦 Packages written to webR 📦"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-28"></span>
<span id="cb2-29">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// see https://github.com/r-wasm/webr/issues/292</span></span>
<span id="cb2-30">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"options(expressions=1000)"</span>)</span>
<span id="cb2-31">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pkgload::load_all('/home/rfuns')"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-32"></span>
<span id="cb2-33">  app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listen</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0.0.0.0'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-34">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'http://localhost:3000'</span>)</span>
<span id="cb2-35">  })</span>
<span id="cb2-36"></span>
<span id="cb2-37">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-38"></span>
<span id="cb2-39">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-40">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(</span>
<span id="cb2-41">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'unique_species()'</span></span>
<span id="cb2-42">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-43">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span> {</span>
<span id="cb2-44">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> js_res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toJs</span>()</span>
<span id="cb2-45">    res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">send</span>(js_res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span>)</span>
<span id="cb2-46">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">finally</span> {</span>
<span id="cb2-47">    webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">destroy</span>(result)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-48">  }</span>
<span id="cb2-49"></span>
<span id="cb2-50">})</span>
<span id="cb2-51"></span>
<span id="cb2-52">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/:n'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-53">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(</span>
<span id="cb2-54">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'star_wars_by_species(n)'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-55">    { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">env</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n</span> } }</span>
<span id="cb2-56">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-57">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span> {</span>
<span id="cb2-58">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> result_js <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toJs</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-59">    res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">send</span>(result_js)</span>
<span id="cb2-60">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">finally</span> {</span>
<span id="cb2-61">    webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">destroy</span>(result)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-62">  }</span>
<span id="cb2-63">})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>Full code is at <a href="https://github.com/ColinFay/webr-examples/tree/main/webr-preload-funs">ColinFay/webr-examples/</a>.</p>
<p>Further exploration to be done: webR now bundles a way to package a file system in a file, which can then be downloaded and mounted into the runtime, as described <a href="https://docs.r-wasm.org/webr/latest/mounting.html#emscripten-filesystem-images">here</a>. This might come handy for our current structure, but I’ll have to explore it a bit more.</p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/rethinking-packages-and-functions-preloading-in-webr-0.2.2/";
  this.page.identifier = "/simple-package-and-functions-in-webr";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>webr</category>
  <guid>https://colinfay.me/posts/rethinking-packages-and-functions-preloading-in-webr-0.2.2.html</guid>
  <pubDate>Tue, 21 Nov 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Using my own R functions in webR in an Express JS API, and thoughts on building web apps with Node &amp; webR</title>
  <link>https://colinfay.me/posts/using-own-functions-in-webr-node-js.html</link>
  <description><![CDATA[ 




<p>Find all my posts about webR <a href="https://colinfay.me/categories/#webr">here</a>.</p>
<blockquote class="blockquote">
<p>Note: the first post of this series explaining roughly what <code>webR</code> is, I won’t introduce it again here.</p>
</blockquote>
<blockquote class="blockquote">
<p>Note 2: webR and the tools I’ve been writing are moving fast and if you’re reading this from the future, some of the things here might be obsolete.</p>
</blockquote>
<section id="the-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>Ok, so now that we have our webR / NodeJS machinery up and running, let’s try something more interesting: use our own R functions inside <code>webR</code>.</p>
<p>How can I do that?</p>
<p>There are at least three ways I could think of:</p>
<p>1️⃣ Writing a function inside the JS code to define a function, something like :</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb1-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"my_fun &lt;- function(x){...}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>But that doesn’t check what I would expect from something I’ll use in prod and I’m pretty sure you don’t need me to detail why 😅</p>
<ul>
<li>❌ Well organized</li>
<li>❌ Documented</li>
<li>❌ Tested</li>
<li>❌ Safely installable</li>
</ul>
<p>2️⃣ Simply create an R script and source it. Something like:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> fs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'fs'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'path'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> script <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'script.R'</span>)</span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFileSync</span>(script)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeFile</span>(</span>
<span id="cb2-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/home/web_user/script.R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-7">  data</span>
<span id="cb2-8">)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source('/home/web_user/script.R')"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>That’s a bit better, we can at least organize our code in a script and it will be:</p>
<ul>
<li>✅ Well organized (more or less)</li>
<li>✅ Documented (more or less)</li>
<li>❌ Tested</li>
<li>❌ Safely installable</li>
</ul>
<p>3️⃣ I bet you saw me coming, the best way let’s put stuff into an R package, so that we can check all the boxes.</p>
<ul>
<li>✅ Well organized</li>
<li>✅ Documented</li>
<li>✅ Tested</li>
<li>✅ Safely installable</li>
</ul>
<p>Jeroen has written a <a href="https://github.com/r-universe-org/build-wasm">Docker image</a> to compile an R package to WASM, but I was looking for something that wouldn’t involve compiling via a docker container every time I make a change on my R package (even if that does sound appealing, I’m pretty sure this wouldn’t make for a seamless workflow).</p>
<p>So here is what I’m thinking should be a well structured NodeJS / WebR app:</p>
<ul>
<li>Putting all the web stuff inside the NodeJS app, because, well, NodeJS is really good at doing that.</li>
<li>Putting all the “business logic”, data-crunching, modeling stuff (and everything R is really good at) into an R package.</li>
<li>load webR, write my R package to the webR file system, and <code>pkgload::load_all()</code> it into webR.</li>
</ul>
<p>That way, I can enjoy the best of both worlds:</p>
<ul>
<li>NodeJS is really good at doing web related things, and there are plenty of ways to test and deploy the code.</li>
<li>And same goes for the R package: if you’re reading my blog I’m pretty sure I don’t need to convince you of why packages are the perfect tool for sharing production code.</li>
</ul>
</section>
<section id="the-how" class="level2">
<h2 class="anchored" data-anchor-id="the-how">The how</h2>
<p>Let’s start by creating our project:</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> webr-preload-funs</span>
<span id="cb3-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> webr-preload-funs</span>
<span id="cb3-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> init <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-y</span></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> index.js</span>
<span id="cb3-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> install express webr</span>
<span id="cb3-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">R</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"usethis::create_package('rfuns', rstudio = FALSE)"</span></span></code></pre></div>
<p>Let’s now create a simple function :</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_r</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sw"</span>)</span></code></pre></div>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @title Star Wars by Species</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @description Return a tibble of Star Wars characters by species</span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @import dplyr</span></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param species character</span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return tibble</span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @examples</span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' star_wars_by_species("Human")</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' star_wars_by_species("Droid")</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' star_wars_by_species("Wookiee")</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' star_wars_by_species("Rodian")</span></span>
<span id="cb5-12">star_wars_by_species <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(species){</span>
<span id="cb5-13">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>starwars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> {{species}})</span>
<span id="cb5-15">}</span></code></pre></div>
<p>We can now add <code>{dplyr}</code> and <code>{pkgload}</code> to our <code>DESCRIPTION</code> (we’ll need <code>{pkgload}</code> to <code>load_all()</code> the package).</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_package</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>)</span>
<span id="cb6-2">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_package</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pkgload"</span>)</span>
<span id="cb6-3">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">document</span>()</span></code></pre></div>
<p>Now that we have a package skeleton, we’ll have to upload it to webR.</p>
<p>As described in the previous post, I’ve started a <code>webrtools</code> NodeJS module, which will contains function to play with webR. Before this post, it had one function, <code>loadPackages</code>, that was used to build a webR dependency library (see <a href="https://colinfay.me/preloading-your-r-packages-in-webr-in-an-express-js-api/">Preloading your R packages in webR in an Express JS API</a> for more info).</p>
<p>We’ll need to add two features :</p>
<ul>
<li>Install deps from DESCRIPTION (not just a package name), so a wrapper around the <code>Rscript ./node_modules/webrtools/r/install.R dplyr</code> from before</li>
<li>Copy the package folder in NodeJS, so a more generic version of <code>loadPackages</code>, that can load any folder to the webR filesystem.</li>
</ul>
<p>First, in R, we’ll need to read the <code>DESCRIPTION</code> and build the lib:</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">download_packs_and_deps_from_desc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> (</span>
<span id="cb7-2">  description,</span>
<span id="cb7-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path_to_installation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./webr_packages"</span></span>
<span id="cb7-4">)</span>
<span id="cb7-5">{</span>
<span id="cb7-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.exists</span>(description)) {</span>
<span id="cb7-7">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESCRIPTION file not found"</span>)</span>
<span id="cb7-8">    }</span>
<span id="cb7-9">    deps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> desc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc_get_deps</span>(description)</span>
<span id="cb7-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (pak <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> deps<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>package) {</span>
<span id="cb7-11">        webrtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download_packs_and_deps</span>(pak, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path_to_installation =</span> path_to_installation)</span>
<span id="cb7-12">    }</span>
<span id="cb7-13">}</span></code></pre></div>
<blockquote class="blockquote">
<p>Note: the code of <code>webrtools::download_packs_and_deps()</code> is a wrapper around the R code described in <a href="../preloading-your-r-packages-in-webr-in-an-express-js-api/">Preloading your R packages in webR in an Express JS API</a></p>
</blockquote>
<p>And in Node, we’ll rework our <code>loadPackages</code> and split it into two functions — one to load into any folder, and one to load into the package library.</p>
<p><strong>Note that this function is bundled in the <a href="https://www.npmjs.com/package/webrtools">webrtools</a> npm module</strong>.</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadFolder</span>(webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> dirPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> outputdir <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/usr/lib/R/library"</span>) {</span>
<span id="cb8-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// function from webrtools</span></span>
<span id="cb8-3">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> files <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getDirectoryTree</span>(</span>
<span id="cb8-4">    dirPath</span>
<span id="cb8-5">  )</span>
<span id="cb8-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">of</span> files) {</span>
<span id="cb8-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span>) {</span>
<span id="cb8-8">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span>(</span>
<span id="cb8-9">        <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>outputdir<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-10">      )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-11">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb8-12">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFileSync</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>dirPath<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-13">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeFile</span>(</span>
<span id="cb8-14">        <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>outputdir<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-15">        data</span>
<span id="cb8-16">      )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-17">    }</span>
<span id="cb8-18">  }</span>
<span id="cb8-19">}</span>
<span id="cb8-20"></span>
<span id="cb8-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> dirPath) {</span>
<span id="cb8-22">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadFolder</span>(webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> dirPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> outputdir <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/usr/lib/R/library"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-23">}</span></code></pre></div>
</section>
<section id="the-end-app" class="level2">
<h2 class="anchored" data-anchor-id="the-end-app">The end app</h2>
<p>We now have everything we need!</p>
<pre><code>npm i webrtools@0.0.2
Rscript ./node_modules/webrtools/r/install_from_desc.R $(pwd)/rfuns/DESCRIPTION</code></pre>
<p>And now, to our index.js</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'express'</span>)()</span>
<span id="cb10-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'path'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { loadPackages<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> loadFolder } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webrtools'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { WebR } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-5"></span>
<span id="cb10-6">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb10-7">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WebR</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-9"></span>
<span id="cb10-10">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"🚀 webR is ready 🚀"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-11"></span>
<span id="cb10-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(</span>
<span id="cb10-13">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-14">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr_packages'</span>)</span>
<span id="cb10-15">  )</span>
<span id="cb10-16"></span>
<span id="cb10-17">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadFolder</span>(</span>
<span id="cb10-18">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-19">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rfuns'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/home/web_user"</span></span>
<span id="cb10-21">  )</span>
<span id="cb10-22"></span>
<span id="cb10-23">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"📦 Packages written to webR 📦"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-24"></span>
<span id="cb10-25">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// see https://github.com/r-wasm/webr/issues/292</span></span>
<span id="cb10-26">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"options(expressions=1000)"</span>)</span>
<span id="cb10-27">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pkgload::load_all('/home/web_user')"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-28"></span>
<span id="cb10-29">  app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listen</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0.0.0.0'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb10-30">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'http://localhost:3000'</span>)</span>
<span id="cb10-31">  })</span>
<span id="cb10-32"></span>
<span id="cb10-33">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-34"></span>
<span id="cb10-35">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb10-36">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(</span>
<span id="cb10-37">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'unique(dplyr::starwars$species)'</span></span>
<span id="cb10-38">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-39">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> js_res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toJs</span>()</span>
<span id="cb10-40">  res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">send</span>(js_res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span>)</span>
<span id="cb10-41">})</span>
<span id="cb10-42"></span>
<span id="cb10-43"></span>
<span id="cb10-44">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/:n'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb10-45">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(</span>
<span id="cb10-46">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'star_wars_by_species(n)'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-47">    { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">env</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n</span> } }</span>
<span id="cb10-48">    )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-49">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span> {</span>
<span id="cb10-50">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> result_js <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toJs</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-51">    res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">send</span>(result_js)</span>
<span id="cb10-52">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">finally</span> {</span>
<span id="cb10-53">    webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">destroy</span>(result)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-54">  }</span>
<span id="cb10-55">})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>Let’s now try from another terminal:</p>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">curl</span> http://localhost:3000</span></code></pre></div>
<pre><code>["Human","Droid","Wookiee","Rodian","Hutt","Yoda's species","Trandoshan","Mon Calamari","Ewok","Sullustan","Neimodian","Gungan",null,"Toydarian","Dug","Zabrak","Twi'lek","Vulptereen","Xexto","Toong","Cerean","Nautolan","Tholothian","Iktotchi","Quermian","Kel Dor","Chagrian","Geonosian","Mirialan","Clawdite","Besalisk","Kaminoan","Aleena","Skakoan","Muun","Togruta","Kaleesh","Pau'an"]</code></pre>
<div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">curl</span> http://localhost:3000/Rodian</span></code></pre></div>
<pre><code>{"type":"list","names":["name","height","mass","hair_color","skin_color","eye_color","birth_year","sex","gender","homeworld","species","films","vehicles","starships"],"values":[{"type":"character","names":null,"values":["Greedo"]},{"type":"integer","names":null,"values":[173]},{"type":"double","names":null,"values":[74]},{"type":"character","names":null,"values":[null]},{"type":"character","names":null,"values":["green"]},{"type":"character","names":null,"values":["black"]},{"type":"double","names":null,"values":[44]},{"type":"character","names":null,"values":["male"]},{"type":"character","names":null,"values":["masculine"]},{"type":"character","names":null,"values":["Rodia"]},{"type":"character","names":null,"values":["Rodian"]},{"type":"list","names":null,"values":[{"type":"character","names":null,"values":["A New Hope"]}]},{"type":"list","names":null,"values":[{"type":"character","names":null,"values":[]}]},{"type":"list","names":null,"values":[{"type":"character","names":null,"values":[]}]}]}</code></pre>
<p>Yeay 🎉 .</p>
<p>You can find the code <a href="https://github.com/ColinFay/webr-examples/tree/main/webr-preload-funs">here</a>, and see it live at <a href="https://srv.colinfay.me/webr-preload-funs/Rodian">srv.colinfay.me/webr-preload-funs/</a>.</p>
<p>You can also try it with</p>
<div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-it</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 3000:3000 colinfay/webr-preload-funs</span></code></pre></div>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/using-own-functions-in-webr-node-js/";
  this.page.identifier = "/adding-my-r-function-to-webr";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>webr</category>
  <guid>https://colinfay.me/posts/using-own-functions-in-webr-node-js.html</guid>
  <pubDate>Tue, 17 Oct 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Preloading your R packages in webR in an Express JS API</title>
  <link>https://colinfay.me/posts/preloading-your-r-packages-in-webr-in-an-express-js-api.html</link>
  <description><![CDATA[ 




<p>Find all my posts about webR <a href="https://colinfay.me/categories/#webr">here</a>.</p>
<blockquote class="blockquote">
<p>Note: the first post of this series explaining roughly what <code>webR</code> is, I won’t introduce it again here.</p>
</blockquote>
<blockquote class="blockquote">
<p>Note 2 (or Disclaimer): this is kind of a long post that get into the weeds of package installation and library structure</p>
</blockquote>
<blockquote class="blockquote">
<p>Note 3: webR and the tools I’ve been writing are moving fast and if you’re reading this from the future, some of the things here might be obsolete.</p>
</blockquote>
<section id="the-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>In the two last posts, I’ve been deploying two REST APIs that use functions from the base distribution of R:</p>
<ul>
<li><code>tools::toTitleCase()</code> in <a href="https://srv.colinfay.me/express-webr-hello-world" target="_blank">express-webr-hello-world</a></li>
<li><code>cut()</code> &amp; <code>faithful</code> for <a href="https://srv.colinfay.me/express-webr-old-faithful/" target="_blank">express-webr-old-faithful</a></li>
</ul>
<p>One of the strengths of the R ecosystem is its extensive list of contributed packages: a wide collection of code written by R nerds from all over the world. It would be unfortunate to use <code>webR</code> to create a web applications or API without taking advantage of this extensive library of packages: R wouldn’t be R without its packages.</p>
<p>Yet, bringing packages to <code>webR</code> is not that simple: it’s a distribution of R compiled for WASM, and you can’t just install a package from any CRAN repo.</p>
<p>Thankfully we don’t need to scratch our head too much anymore: the <code>webR</code> team has precompiled more than 18000 (at the time of writing this post) packages that we can install by simply calling <code>webr::install()</code> inside the <code>webR</code> instance.</p>
<p>That’s awesome, but there’s an issue with that: you can only install it from the <code>webR</code> JavaScript object As far as I know, there is no way to download them beforehand and upload them to the <code>webR</code> filesystem, as I reported in <a href="https://github.com/r-wasm/webr/issues/260" target="_blank">this issue</a>. (Note: at the time of writing these lines, there has been some discussion on the GH issue, but there is at the moment no native, efficient way to do what I want to do 😅 — there might be one though if you’re reading this post in the future).</p>
<p>That means that every time the API is launched, <code>webR</code> has to download and reinstall the packages, as shown by this simple example:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'express'</span>)()</span>
<span id="cb1-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { WebR } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3"></span>
<span id="cb1-4">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb1-5">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WebR</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">installPackages</span>([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt'</span>])</span>
<span id="cb1-8">  app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listen</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0.0.0.0'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb1-9">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'http://localhost:3000'</span>)</span>
<span id="cb1-10">  })</span>
<span id="cb1-11">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12"></span>
<span id="cb1-13"></span>
<span id="cb1-14">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb1-15">  res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">send</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello"</span>)</span>
<span id="cb1-16">})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>If I launch the app twice, the packages are downloaded twice 👇</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> node index.js</span>
<span id="cb2-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">webR</span> is ready</span>
<span id="cb2-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Downloading</span> webR package: rlang</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Downloading</span> webR package: attempt</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">http://localhost:3000</span></span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> node index.js</span>
<span id="cb2-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">webR</span> is ready</span>
<span id="cb2-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Downloading</span> webR package: rlang</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Downloading</span> webR package: attempt</span>
<span id="cb2-14"></span>
<span id="cb2-15"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">http://localhost:3000</span></span></code></pre></div>
<p>This approach is definitely suboptimal: imagine having to reinstall packages every time you <code>source()</code> an R script, instead of benefiting from pre-installed packages in your lib. That’s kind of what is happening right now in this <code>webR</code> example.</p>
<p>Sure, it works, but it means that:</p>
<ul>
<li>I’m downloading the (same) packages every time the app is launched, which feels weird</li>
<li>If I have a lot of packages, it would take a lot of time every time</li>
<li>I can’t deploy this on a service that has no access to internet</li>
<li>I can’t build my app just as any other language does: by relying on pre-downloaded /pre-installed modules</li>
<li>I can’t benefit from caching the package installation in a Docker layer</li>
</ul>
<p>So, faced with something I needed but didn’t find, I behave like any other software engineer: I built my own solution to pre-load R packages in <code>webR</code>, without having to re-download them every time. The rest of this blogpost explains this process.</p>
</section>
<section id="preloading-r-packages-in-my-expressjswebr-api" class="level2">
<h2 class="anchored" data-anchor-id="preloading-r-packages-in-my-expressjswebr-api">Preloading R packages in my ExpressJS/webR API</h2>
<section id="building-the-r-lib" class="level3">
<h3 class="anchored" data-anchor-id="building-the-r-lib">Building the R lib</h3>
<p>If you look at the internal of the folder on your computer that contains your packages (which is at <code>.libPaths()</code>), you’ll see that it’s a series of folders with a bunch of files. Whenever you load R and call <code>library()</code>, it looks there to load the code.</p>
<p>Inside these folders (you can have several lib), you’ll find unzipped archives, downloaded from a repository (or installed from git/developed locally). These packages, at installation time, possibly performed some build and compilation, depending on the package and the OS you’re on. I won’t go into the details of this here, as the packages for webR have been pre-compiled, meaning that you simply have to download the tar file, and unarchive it at the correct place. In other words, the content of this archive is exactly what you’ll find in the library folder, and there is nothing to compile/install again.</p>
<p>Keeping this in mind, the first thing to do then is to create a folder that will contain all the folders and files necessary to load a package. In other words, if I want to use <code>{dplyr}</code>, I need to find a way to download the pre-compiled version of <code>{dplyr}</code> and all its dependencies from <code>https://repo.r-wasm.org/</code>, and untar them into the folder I wanna use inside my project.</p>
<p>R has a weird way of managing packages, and if ever you want to hear me monologue, catch me at a conference and ask me to talk about this for 30 minutes (just kidding, it will probably last one hour). I won’t go into details about how it works and why it’s weird, but here are some issues I’ve been facing for my current task:</p>
<ul>
<li><p>By default R will not reinstall the dependencies if they are already installed on the machine. Which is great, but not in my case (I want the destination folder to contain the entire dependency tree). In other words, if I do <code>install.packages("dplyr", lib="webr_packages")</code>, R will only download the packages that are not already on my computer.</p></li>
<li><p>There is no (sane) way to avoid the default library. So basically unless you’re ready to write hacky code that will override the library environments and stuff, R will always look into the default lib. There is an old issue on the <a href="https://github.com/r-lib/withr/issues/122" target="_blank"><code>{withr}</code> repo </a> if you want to look into this, but basically the idea is that avoiding default lib is quite a challenge.</p></li>
<li><p>That means that if I try to do something as simple as <code>install.packages("dplyr", lib="webr_packages", repos = "https://repo.r-wasm.org/")</code>, it will only install <code>{dplyr}</code>, given that all the other dependencies already exist on my machine :</p></li>
</ul>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setwd</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempdir</span>())</span>
<span id="cb3-2"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"webr_packages"</span>)</span>
<span id="cb3-3"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lib=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"webr_packages"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repos =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://repo.r-wasm.org/"</span>)</span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [...TRUNCATED]</span></span>
<span id="cb3-5"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">DONE</span> (dplyr)</span>
<span id="cb3-6"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list.files</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"webr_packages/"</span>)</span>
<span id="cb3-7">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span></span></code></pre></div>
<p>I’ve tried the following:</p>
<ul>
<li><p>Hacking <code>.lib.loc</code> as suggest by Andrie <a href="https://stackoverflow.com/a/36873741">here</a>, but it comes with a series of challenges like not having access to other packages and having to reinstall them and load them to the webR FileSystem which is not what I wanted.</p></li>
<li><p>Using <code>{renv}</code> but it feels a bit overkill for what I’m trying to do: download a bunch of zip files from the internet and unzip them in the correct folder. Also, <code>{renv}</code> failed to parse the dep tree in a NodeJS project for some reasons.</p></li>
</ul>
<p>So I’ve ended up with my own R script, inspired by the internal of <code>install.packages</code> and <code>webr::install</code> (<a href="https://blog.codinghorror.com/learn-to-read-the-source-luke/">Read the source, Luke</a>). I’ve first explored a version with <code>{pak}</code> and <code>{pkgdepends}</code>, but ended up using a base solution so that I don’t have to install anything in the Docker container.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/env Rscript</span></span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This script only works if you pass it at least one arg</span></span>
<span id="cb4-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">commandArgs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trailingOnly =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])) {</span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This script requires at least one argument"</span>)</span>
<span id="cb4-6">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb4-7">  pk_to_install <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">commandArgs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trailingOnly =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb4-8">}</span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If no second value, defaulting to webr_packages</span></span>
<span id="cb4-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">commandArgs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trailingOnly =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])) {</span>
<span id="cb4-12">  path_to_installation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./webr_packages"</span></span>
<span id="cb4-13">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb4-14">  path_to_installation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">commandArgs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trailingOnly =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb4-15">}</span>
<span id="cb4-16"></span>
<span id="cb4-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build the repos url</span></span>
<span id="cb4-18">repos <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(</span>
<span id="cb4-19">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://repo.r-wasm.org/bin/emscripten/contrib/%s.%s"</span>,</span>
<span id="cb4-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get major R version</span></span>
<span id="cb4-21">  R.version<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>major,</span>
<span id="cb4-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substr</span>(R.version<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>minor, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-23">)</span>
<span id="cb4-24"></span>
<span id="cb4-25">deps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(</span>
<span id="cb4-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(</span>
<span id="cb4-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">use.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb4-28">    tools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">package_dependencies</span>(</span>
<span id="cb4-29">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">recursive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb4-30">      pk_to_install</span>
<span id="cb4-31">    )</span>
<span id="cb4-32">  )</span>
<span id="cb4-33">)</span>
<span id="cb4-34"></span>
<span id="cb4-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now the package list</span></span>
<span id="cb4-36">pkg_deps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb4-37">  pk_to_install,</span>
<span id="cb4-38">  deps</span>
<span id="cb4-39">)</span>
<span id="cb4-40"></span>
<span id="cb4-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I don't want to re-download things so I'm listing all the already</span></span>
<span id="cb4-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># downloaded folders</span></span>
<span id="cb4-43">already_installed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list.files</span>(</span>
<span id="cb4-44">  path_to_installation</span>
<span id="cb4-45">)</span>
<span id="cb4-46"></span>
<span id="cb4-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Getting the list of available package</span></span>
<span id="cb4-48">info <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> utils<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">available.packages</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contriburl =</span> repos)</span>
<span id="cb4-49"></span>
<span id="cb4-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now we can install</span></span>
<span id="cb4-51"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (pkg <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> pkg_deps){</span>
<span id="cb4-52">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Don't redownload things</span></span>
<span id="cb4-53">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (pkg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> already_installed) {</span>
<span id="cb4-54">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Package "</span>, pkg, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" already installed"</span>)</span>
<span id="cb4-55">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">next</span></span>
<span id="cb4-56">  }</span>
<span id="cb4-57">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Not available: either it's not on the repo or it's included</span></span>
<span id="cb4-58">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in the base distribution of R</span></span>
<span id="cb4-59">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(pkg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> info[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Package"</span>])) {</span>
<span id="cb4-60">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Package {"</span>, pkg, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"} not found in repo (unavailable or is base package)"</span>)</span>
<span id="cb4-61">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">next</span></span>
<span id="cb4-62">  }</span>
<span id="cb4-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Installing "</span>, pkg)</span>
<span id="cb4-64">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Name of the targz, should be on the form of pkg_version.this.that.tgz</span></span>
<span id="cb4-65">  targz_file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(pkg, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_"</span>, info[info[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Package"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> pkg, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Version"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".tgz"</span>)</span>
<span id="cb4-66">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Location of file on the repo</span></span>
<span id="cb4-67">  url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(repos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>, targz_file)</span>
<span id="cb4-68">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download the file and untar the archive on the local lib</span></span>
<span id="cb4-69">  tmp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempfile</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fileext =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".tgz"</span>)</span>
<span id="cb4-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(url, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> tmp)</span>
<span id="cb4-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">untar</span>(tmp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exdir =</span> path_to_installation)</span>
<span id="cb4-72">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlink</span>(tmp)</span>
<span id="cb4-73">}</span>
<span id="cb4-74"></span>
<span id="cb4-75"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Done"</span>)</span></code></pre></div>
<p>And now, let’s try this:</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> webr_packages</span>
<span id="cb5-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">script</span> tools/installR.r attempt</span>
<span id="cb5-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">✔</span> Loading metadata database ... done</span>
<span id="cb5-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Installing</span> attempt</span>
<span id="cb5-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">trying</span> URL <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://repo.r-wasm.org/bin/emscripten/contrib/4.3/attempt_0.3.1.tgz'</span></span>
<span id="cb5-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Content</span> type <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'application/x-tar'</span> length 93561 bytes <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">91</span> KB<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="cb5-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">==================================================</span></span>
<span id="cb5-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">downloaded</span> 91 KB</span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Installing</span> rlang</span>
<span id="cb5-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">trying</span> URL <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://repo.r-wasm.org/bin/emscripten/contrib/4.3/rlang_1.1.1.tgz'</span></span>
<span id="cb5-12"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Content</span> type <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'application/x-tar'</span> length 1062482 bytes <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">1.0</span> MB<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="cb5-13"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">==================================================</span></span>
<span id="cb5-14"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">downloaded</span> 1.0 MB</span>
<span id="cb5-15"></span>
<span id="cb5-16"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Done</span></span>
<span id="cb5-17"></span>
<span id="cb5-18"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Rscript</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fs::dir_tree('webr_packages')"</span></span>
<span id="cb5-19"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">webr_packages</span></span>
<span id="cb5-20"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> attempt</span>
<span id="cb5-21"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── DESCRIPTION</span>
<span id="cb5-22"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── INDEX</span>
<span id="cb5-23"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── LICENSE</span>
<span id="cb5-24"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── Meta</span>
<span id="cb5-25"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── Rd.rds</span>
<span id="cb5-26"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── features.rds</span>
<span id="cb5-27"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── hsearch.rds</span>
<span id="cb5-28"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── links.rds</span>
<span id="cb5-29"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── nsInfo.rds</span>
<span id="cb5-30"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── package.rds</span>
<span id="cb5-31"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   └── vignette.rds</span>
<span id="cb5-32"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── NAMESPACE</span>
<span id="cb5-33"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── NEWS.md</span>
<span id="cb5-34"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── R</span>
<span id="cb5-35"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── attempt</span>
<span id="cb5-36"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── attempt.rdb</span>
<span id="cb5-37"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   └── attempt.rdx</span>
<span id="cb5-38"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── doc</span>
<span id="cb5-39"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── a_intro_attempt.R</span>
<span id="cb5-40"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── a_intro_attempt.Rmd</span>
<span id="cb5-41"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── a_intro_attempt.html</span>
<span id="cb5-42"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── b_try_catch.R</span>
<span id="cb5-43"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── b_try_catch.Rmd</span>
<span id="cb5-44"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── b_try_catch.html</span>
<span id="cb5-45"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── c_adverbs.R</span>
<span id="cb5-46"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── c_adverbs.Rmd</span>
<span id="cb5-47"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── c_adverbs.html</span>
<span id="cb5-48"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── d_if.R</span>
<span id="cb5-49"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── d_if.Rmd</span>
<span id="cb5-50"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── d_if.html</span>
<span id="cb5-51"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── e_conditions.R</span>
<span id="cb5-52"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── e_conditions.Rmd</span>
<span id="cb5-53"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── e_conditions.html</span>
<span id="cb5-54"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   └── index.html</span>
<span id="cb5-55"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── help</span>
<span id="cb5-56"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── AnIndex</span>
<span id="cb5-57"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── aliases.rds</span>
<span id="cb5-58"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── attempt.rdb</span>
<span id="cb5-59"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── attempt.rdx</span>
<span id="cb5-60"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   └── paths.rds</span>
<span id="cb5-61"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── html</span>
<span id="cb5-62"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>       ├── 00Index.html</span>
<span id="cb5-63"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>       └── R.css</span>
<span id="cb5-64"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> rlang</span>
<span id="cb5-65">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> DESCRIPTION</span>
<span id="cb5-66">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> INDEX</span>
<span id="cb5-67">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> LICENSE</span>
<span id="cb5-68">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> Meta</span>
<span id="cb5-69">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── Rd.rds</span>
<span id="cb5-70">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── features.rds</span>
<span id="cb5-71">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── hsearch.rds</span>
<span id="cb5-72">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── links.rds</span>
<span id="cb5-73">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── nsInfo.rds</span>
<span id="cb5-74">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── package.rds</span>
<span id="cb5-75">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> NAMESPACE</span>
<span id="cb5-76">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> NEWS.md</span>
<span id="cb5-77">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> R</span>
<span id="cb5-78">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── rlang</span>
<span id="cb5-79">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── rlang.rdb</span>
<span id="cb5-80">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── rlang.rdx</span>
<span id="cb5-81">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> backtrace-ver</span>
<span id="cb5-82">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> help</span>
<span id="cb5-83">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── AnIndex</span>
<span id="cb5-84">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── aliases.rds</span>
<span id="cb5-85">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── figures</span>
<span id="cb5-86">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-archived.svg</span>
<span id="cb5-87">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-defunct.svg</span>
<span id="cb5-88">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-deprecated.svg</span>
<span id="cb5-89">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-experimental.svg</span>
<span id="cb5-90">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-maturing.svg</span>
<span id="cb5-91">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-questioning.svg</span>
<span id="cb5-92">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-retired.svg</span>
<span id="cb5-93">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-soft-deprecated.svg</span>
<span id="cb5-94">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-stable.svg</span>
<span id="cb5-95">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   ├── lifecycle-superseded.svg</span>
<span id="cb5-96">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   │   └── logo.png</span>
<span id="cb5-97">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── paths.rds</span>
<span id="cb5-98">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── rlang.rdb</span>
<span id="cb5-99">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── rlang.rdx</span>
<span id="cb5-100">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> html</span>
<span id="cb5-101">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── 00Index.html</span>
<span id="cb5-102">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── R.css</span>
<span id="cb5-103">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> libs</span>
<span id="cb5-104">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> rlang.so</span></code></pre></div>
<p>Woot woot, seems to work 🎉.</p>
</section>
<section id="moving-everything-to-the-webr-lib-when-the-api-is-launched" class="level3">
<h3 class="anchored" data-anchor-id="moving-everything-to-the-webr-lib-when-the-api-is-launched">Moving everything to the webR lib when the API is launched</h3>
<p>And now, challenge number two: rebuilding this library in the <code>webR</code> filesystem.</p>
<p><code>webR</code> has an interface to WASM filesystem (aka Emscripten Virtual File System), exposed via <code>FS</code> (<a href="https://docs.r-wasm.org/webr/latest/api/js/interfaces/WebR.WebRFS.html">doc is here</a>). This interface allows to create a directory and write a file, which are the two things we want to do here:</p>
<ul>
<li>Create the package folders/subfolders</li>
<li>Copy the files from the local machine (contained in a <code>./webr_packages</code> folder) into the <code>webR</code> filesystem when launching the Node API.</li>
</ul>
<section id="creating-the-directory-tree" class="level4">
<h4 class="anchored" data-anchor-id="creating-the-directory-tree">Creating the directory tree</h4>
<p>Creating a directory tree is not an easy task — it implies recursive function, keeping track of parents, and other weird things. R has <code>list.files(recursive = TRUE)</code> and we should be VERY happy about it: I looked on the internet and apparently there is no native way to do that in Node, you have to do it by hand.</p>
<p>As any sane modern JavaScript developer would do, I asked chatGPT to do it for me. It took me several iterations before getting the exact answer I was looking for but here it is.</p>
<p><strong>Note that the JS functions below are bundled in the <a href="https://www.npmjs.com/package/webrtools">webrtools</a> npm module</strong>.</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> fs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'fs'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'path'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// comments are mine</span></span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Recursive function to get files, thank you chatGPT</span></span>
<span id="cb6-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getDirectoryTree</span>(dirPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> currentPath <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>) {</span>
<span id="cb6-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Get a list of all the files/folders in the currently inspected folder</span></span>
<span id="cb6-9">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> items <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readdirSync</span>(path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(dirPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> currentPath))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// output</span></span>
<span id="cb6-11">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> tree <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-12"></span>
<span id="cb6-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Looping on all the files/folders</span></span>
<span id="cb6-14">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> item <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">of</span> items) {</span>
<span id="cb6-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Inspecting the item</span></span>
<span id="cb6-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> itemPath <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(dirPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> currentPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> item)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> isDirectory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">statSync</span>(itemPath)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isDirectory</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> relativePath <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(currentPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> item)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-19"></span>
<span id="cb6-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (isDirectory) {</span>
<span id="cb6-21">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If it's a directory, we'll go inside and repeat the</span></span>
<span id="cb6-22">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// current mechanism</span></span>
<span id="cb6-23">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> subtree <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getDirectoryTree</span>(dirPath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> relativePath)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-24">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// pushing the current item, and its subtree</span></span>
<span id="cb6-25">      tree<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">push</span>({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> relativePath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-26">      tree<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">push</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>subtree)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-27">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb6-28">      tree<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">push</span>({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> relativePath<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-29">    }</span>
<span id="cb6-30">  }</span>
<span id="cb6-31">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Getting the tree</span></span>
<span id="cb6-32">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> tree<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-33">}</span></code></pre></div>
<p>Here is what it looks like on our <code>./webr_packages</code> folder from before:</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb7-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getDirectoryTree</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"webr_packages"</span>)</span>
<span id="cb7-2">[</span>
<span id="cb7-3">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-4">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/DESCRIPTION'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-5">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/INDEX'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-6">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/LICENSE'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-7">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/Meta'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-8">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/Meta/Rd.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-9">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/Meta/features.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-10">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/Meta/hsearch.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-11">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/Meta/links.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-12">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/Meta/nsInfo.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-13">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/Meta/package.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-14">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/Meta/vignette.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-15">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/NAMESPACE'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-16">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/NEWS.md'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-17">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/R'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-18">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/R/attempt'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-19">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/R/attempt.rdb'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-20">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/R/attempt.rdx'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-21">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-22">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/a_intro_attempt.R'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-23">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/a_intro_attempt.Rmd'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-24">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/a_intro_attempt.html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-25">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/b_try_catch.R'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-26">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/b_try_catch.Rmd'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-27">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/b_try_catch.html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-28">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/c_adverbs.R'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-29">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/c_adverbs.Rmd'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-30">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/c_adverbs.html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-31">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/d_if.R'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-32">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/d_if.Rmd'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-33">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/d_if.html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-34">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/e_conditions.R'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-35">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/e_conditions.Rmd'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-36">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/e_conditions.html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-37">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/doc/index.html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-38">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/help'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-39">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/help/AnIndex'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-40">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/help/aliases.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-41">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/help/attempt.rdb'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-42">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/help/attempt.rdx'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-43">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/help/paths.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-44">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-45">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/html/00Index.html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-46">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'attempt/html/R.css'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-47">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-48">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/DESCRIPTION'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-49">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/INDEX'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-50">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/LICENSE'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-51">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/Meta'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-52">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/Meta/Rd.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-53">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/Meta/features.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-54">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/Meta/hsearch.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-55">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/Meta/links.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-56">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/Meta/nsInfo.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-57">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/Meta/package.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-58">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/NAMESPACE'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-59">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/NEWS.md'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-60">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/R'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-61">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/R/rlang'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-62">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/R/rlang.rdb'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-63">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/R/rlang.rdx'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-64">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/backtrace-ver'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-65">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-66">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/AnIndex'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-67">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/aliases.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-68">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-69">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-archived.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-70">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-defunct.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-71">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-deprecated.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-72">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-experimental.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-73">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-maturing.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-74">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-questioning.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-75">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-retired.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-76">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-soft-deprecated.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-77">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-stable.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-78">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/lifecycle-superseded.svg'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-79">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/figures/logo.png'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-80">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/paths.rds'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-81">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/rlang.rdb'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-82">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/help/rlang.rdx'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-83">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-84">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/html/00Index.html'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-85">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/html/R.css'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-86">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/libs'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span> }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-87">  { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rlang/libs/rlang.so'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> }</span>
<span id="cb7-88">]</span></code></pre></div>
<blockquote class="blockquote">
<p>Note: I need all the folders and subfolders, for example rlang/libs and rlang/libs/rlang.so, because I haven’t found a way to do a recursive mkdir in webR, and had to create all children level by level.</p>
</blockquote>
<p>🚀 Yeay 🚀</p>
<p>Now I have a JavaScript object I can use to move my <code>./webr_packages</code> library to a <code>webR</code> instance library.</p>
<p>Let’s write a function for that (no need for chatGPT here 😅):</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(webR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> dirPath){</span>
<span id="cb8-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Our function from before</span></span>
<span id="cb8-3">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> files <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getDirectoryTree</span>(</span>
<span id="cb8-4">    dirPath</span>
<span id="cb8-5">  )</span>
<span id="cb8-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> file <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">of</span> files) {</span>
<span id="cb8-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If the entry is a directory, we'll</span></span>
<span id="cb8-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// call mkdir</span></span>
<span id="cb8-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">===</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'directory'</span>) {</span>
<span id="cb8-10">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span>(</span>
<span id="cb8-11">        <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`/usr/lib/R/library/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-12">      )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-13">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb8-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// case 2, the entry is a file, then we can use fs.readFileSync</span></span>
<span id="cb8-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// That will return a ArrayBuffer that can be directly passed</span></span>
<span id="cb8-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// to webR.FS.writeFile</span></span>
<span id="cb8-17">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFileSync</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`webr_packages/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-18">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeFile</span>(</span>
<span id="cb8-19">        <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`/usr/lib/R/library/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-20">        data</span>
<span id="cb8-21">      )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-22">    }</span>
<span id="cb8-23">  }</span>
<span id="cb8-24">}</span></code></pre></div>
<p>Now we have our whole process:</p>
<ul>
<li>downloading the precompiled packages</li>
<li>listing the dir tree in Node</li>
<li>bringing everything into the <code>webR</code> filesystem</li>
</ul>
<p>🚀 Yeay (bis) 🚀</p>
<p>To make things simple, I have put these into a node package, at <a href="https://www.npmjs.com/package/webrtools" target="_blank">npmjs.com/package/webrtools</a>.</p>
</section>
</section>
<section id="bringing-everything-together" class="level3">
<h3 class="anchored" data-anchor-id="bringing-everything-together">Bringing everything together</h3>
<p>Workflow for building the app:</p>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> webr-example</span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> webr-preloadr</span>
<span id="cb9-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> webr-preloadr</span>
<span id="cb9-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> init <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-y</span></span>
<span id="cb9-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> install express webr webrtools</span>
<span id="cb9-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Rscript</span> ./node_modules/webrtools/r/install.R dplyr</span>
<span id="cb9-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> index.js</span></code></pre></div>
<p>Then :</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'express'</span>)()</span>
<span id="cb10-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'path'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { loadPackages } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webrtools'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { WebR } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-5"></span>
<span id="cb10-6">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb10-7">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WebR</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-9"></span>
<span id="cb10-10">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"🚀 webR is ready 🚀"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-11"></span>
<span id="cb10-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadPackages</span>(</span>
<span id="cb10-13">    globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb10-14">    path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'webr_packages'</span>)</span>
<span id="cb10-15">  )</span>
<span id="cb10-16"></span>
<span id="cb10-17">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"📦 Packages written to webR 📦"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-18"></span>
<span id="cb10-19">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FS</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lookupPath</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/usr/lib/R/library"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-20">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(res)</span>
<span id="cb10-21"></span>
<span id="cb10-22">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"library(dplyr)"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-23">  app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listen</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0.0.0.0'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb10-24">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'http://localhost:3000'</span>)</span>
<span id="cb10-25">  })</span>
<span id="cb10-26">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-27"></span>
<span id="cb10-28"></span>
<span id="cb10-29">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb10-30">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sample_n(mtcars, 10)'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-31">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toJs</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-32">  res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">send</span>(output<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span>)</span>
<span id="cb10-33">})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">node</span> index.js</span></code></pre></div>
<p>And in another console:</p>
<div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> curl http://localhost:3000</span>
<span id="cb12-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[21</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">24.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">14.3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">32.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">17.8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">21.4]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[6</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[160</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">146.7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">78.7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">360</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">167.6</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">108</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">460</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">75.7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">121]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[110</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">62</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">245</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">66</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">175</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">93</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">215</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">109]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[3.9</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.69</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.21</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.08</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.15</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.92</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.85</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.93</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.11]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[2.62</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.19</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.57</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.44</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.44</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.424</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.615</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.78]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[16.46</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">15.84</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.47</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">17.02</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.9</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.61</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">17.82</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.52</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.6]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"double"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:null</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">2]}</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">]</span></span></code></pre></div>
<p>🚀 Yeay (ter) 🚀</p>
</section>
<section id="and-now-for-the-dockerfile" class="level3">
<h3 class="anchored" data-anchor-id="and-now-for-the-dockerfile">And now for the Dockerfile</h3>
<div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb13-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> r-base:4.3.1</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create app directory</span></span>
<span id="cb13-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WORKDIR</span> /usr/src/app</span>
<span id="cb13-5"></span>
<span id="cb13-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> update <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb13-7">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> install nodejs npm <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-y</span></span>
<span id="cb13-8"></span>
<span id="cb13-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COPY</span> package*.json ./</span>
<span id="cb13-10"></span>
<span id="cb13-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> install</span>
<span id="cb13-12"></span>
<span id="cb13-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Rscript</span> ./node_modules/webrtools/r/install.R dplyr</span>
<span id="cb13-14"></span>
<span id="cb13-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COPY</span> . .</span>
<span id="cb13-16"></span>
<span id="cb13-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXPOSE</span> 3000</span>
<span id="cb13-18"></span>
<span id="cb13-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CMD</span> [ <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"node"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index.js"</span> ]</span></code></pre></div>
<p>You can find the code <a href="https://github.com/ColinFay/webr-examples/tree/main/webr-preloadr">here</a>, and see it live at <a href="https://srv.colinfay.me/webr-preloadr/">srv.colinfay.me/webr-preloadr</a>.</p>
<p>You can also try it with</p>
<div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb14-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-it</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 3000:3000 colinfay/webr-preloadr</span></code></pre></div>
</section>
</section>
<section id="final-notes" class="level2">
<h2 class="anchored" data-anchor-id="final-notes">Final notes</h2>
<section id="should-i-use-it-in-prod" class="level3">
<h3 class="anchored" data-anchor-id="should-i-use-it-in-prod">Should I use it in prod?</h3>
<p>Probably not — for now it’s more of an experiment than a real bullet-proof approach. But if you’re a reader from the future, many things may have happen, and maybe the <code>webrtools</code> node module is now prod ready. Who knows!</p>
</section>
<section id="further-work" class="level3">
<h3 class="anchored" data-anchor-id="further-work">Further work</h3>
<p>After some discussion on the issue on the <code>webR</code> repo, it seems that a promising path to do what I’d like to do is to use the <a href="https://emscripten.org/docs/api_reference/Filesystem-API.html#FS.mount" target="_blank"><code>FS.mount</code> interface from WASM</a>, but it needs to be expose by <code>webR</code>. Keep an eye on the repo!</p>
<p>Finally, I’m a bit displeased with writing the installR fun in R and having to install R in the <code>Dockerfile</code> to do it, so I should probably re-write it in full NodeJS. We could read the package tree in R with <code>webR</code> and then download/untar with Node.</p>
<p>Food for thoughts.</p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/preloading-your-r-packages-in-webr-in-an-express-js-api/";
  this.page.identifier = "/preloading-your-r-packages-in-webr-in-an-express-js-api";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>webr</category>
  <guid>https://colinfay.me/posts/preloading-your-r-packages-in-webr-in-an-express-js-api.html</guid>
  <pubDate>Mon, 04 Sep 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The Old Faithful Geyser Data shiny app with webR, Bootstrap &amp; ExpressJS</title>
  <link>https://colinfay.me/posts/old-faithful-express-bootstrap-webr.html</link>
  <description><![CDATA[ 




<p>Find all my posts about webR <a href="https://colinfay.me/categories/#webr">here</a>.</p>
<blockquote class="blockquote">
<p>Note: the first post of this series explaining roughly what <code>webR</code> is, I won’t introduce it again here.</p>
</blockquote>
<blockquote class="blockquote">
<p>Note 2: webR and the tools I’ve been writing are moving fast and if you’re reading this from the future, some of the things here might be obsolete.</p>
</blockquote>
<p>In this post, I’ll attempt to recreate a version of the famous <em>Old Faithful Geyser Data</em> <code>{shiny}</code> app using <code>webR</code>, <code>Bootstrap</code> &amp; <code>ExpressJS</code>. (If you really don’t know which app I’m talking about, it’s <a href="https://gallery.shinyapps.io/001-hello/">this one</a>.)</p>
<section id="introductory-notes" class="level2">
<h2 class="anchored" data-anchor-id="introductory-notes">Introductory notes</h2>
<p>Before starting, here are two notes regarding the app built in this post:</p>
<ul>
<li><p>this app could have been build in full “web mode” (no server required, just an HTML page), but this is not the approach I’m currently experimenting with. Going full browser-based doesn’t work for all cases, and most of the time with production apps you’ll need some part of your code to be computed by the server (because of resources, because you’ll connect to API with token, because you need access to DB with passwords, because you don’t want the full data to be available in the brower, or many other good reason…).</p></li>
<li><p>In order to recreate the app, my first approach was to try to draw the histogram in base R and send it back to the browser. <a href="https://rud.is/b/2023/03/18/the-road-to-ggplot2-in-webr-part-1-the-road-is-paved-with-good-base-r-plots/">boB has a great blogpost</a> about how to do exactly that, but after a lot of bad code and good 4 letter words, I realized there was no sane reason for me to display a base R plot instead of a JavaScript based one. That’s why I chose to return the data for the barplot (using the <code>cut()</code> function from R) and draw with <a href="https://www.chartjs.org/">chart.js</a>, instead of trying to display the “not so user friendly” base plot.</p></li>
</ul>
</section>
<section id="project-init" class="level2">
<h2 class="anchored" data-anchor-id="project-init">Project init</h2>
<p>Let’s start by creating a new Express app:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> express-webr-old-faithful</span>
<span id="cb1-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> express-webr-old-faithful</span>
<span id="cb1-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> init <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-y</span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Installing the deps we'll need</span></span>
<span id="cb1-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> i @r-wasm/webr express</span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creating a server, and the front page</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> index.js</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> index.html</span></code></pre></div>
</section>
<section id="server" class="level2">
<h2 class="anchored" data-anchor-id="server">Server</h2>
<p>Let’s move to the server side first (<code>index.js</code> ). We’ll start by taking the file from the previous blog post, and modify it to:</p>
<ul>
<li>serve index.html on <code>/</code></li>
<li>create a route that returns the data of the bins for our histogram</li>
</ul>
<p>Let’s start with our code to init <code>webR</code>.</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb2-1"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'use strict'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> express <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"express"</span>)</span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// For serving the html</span></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path"</span>)</span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">express</span>()</span>
<span id="cb2-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { WebR } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'@r-wasm/webr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-7"></span>
<span id="cb2-8">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-9">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WebR</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-10">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Given that we will reuse this value,</span></span>
<span id="cb2-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// we assign it at launch</span></span>
<span id="cb2-13">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x &lt;- faithful[, 2]'</span>)</span>
<span id="cb2-14">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"webR is ready"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-15">  app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listen</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0.0.0.0'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb2-16">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'http://localhost:3000'</span>)</span>
<span id="cb2-17">  })</span>
<span id="cb2-18">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>Then, an endpoint that serves <code>index.html</code>:</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb3-1">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb3-2">  res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sendFile</span>(path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">__dirname</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index.html"</span>))</span>
<span id="cb3-3">})</span></code></pre></div>
<p>And an endpoint that sends the bins for our plot, using ExpressJS parameter notation (<code>path/:n</code>). The value is sent to webR via the <code>env</code> option of <code>evalR</code></p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb4-1">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/hist-data/:n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb4-2">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(</span>
<span id="cb4-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'table(cut(x, seq(min(x), max(x), length.out = n + 1)))'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-4">    { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">env</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> { <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n</span>) } }</span>
<span id="cb4-5">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-6">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toJs</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-7">  res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">send</span>(output)</span>
<span id="cb4-8">})</span></code></pre></div>
<p>Now that our backend is ready, let’s check that we can now call it from the command line:</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">curl</span> http://localhost:3000/hist-data/10</span>
<span id="cb5-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"names"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(43,48.3]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(48.3,53.6]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(53.6,58.9]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(58.9,64.2]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(64.2,69.5]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(69.5,74.8]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(74.8,80.1]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(80.1,85.4]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(85.4,90.7]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(90.7,96]"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"values"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">:[15</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">26</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">62</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">6]}</span></span></code></pre></div>
</section>
<section id="front" class="level2">
<h2 class="anchored" data-anchor-id="front">Front</h2>
<p>Now, time to build the front.</p>
<p>We’ll start with the Boostrap boilerplate from <a href="https://getbootstrap.com/docs/5.3/getting-started/introduction/#quick-start" class="uri">https://getbootstrap.com/docs/5.3/getting-started/introduction/#quick-start</a>.</p>
<blockquote class="blockquote">
<p>Note also that I’ve chosen (for simplicity’s sake) to use the CDN version of the external deps, instead of installing them in my Node project, which would be what I would do in a normal context.</p>
</blockquote>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb6-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;!doctype</span> html<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">html</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> lang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"en"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">head</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-4">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">meta</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> charset</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"utf-8"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">meta</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"viewport"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> content</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"width=device-width, initial-scale=1"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">title</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Bootstrap demo<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">title</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">link</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> href</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> rel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stylesheet"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> integrity</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> crossorigin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anonymous"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">head</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">body</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">h1</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Hello, world!<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">h1</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> src</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> integrity</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> crossorigin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anonymous"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-12">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">body</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-13"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">html</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div>
<p>Let’s change the title, and add:</p>
<ul>
<li>the range input</li>
<li>a div to receive the chart</li>
</ul>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb7-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"container"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">h1</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Old Faithful Geyser Data<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">h1</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;!-- Bootstrap grid system --&gt;</span></span>
<span id="cb7-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row align-items-start"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col-4"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-6">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">label</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customRange1"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"form-label"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Number of bins:<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">label</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-7">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">input</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"range"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"form-range"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customRange1"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> min</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> max</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">30</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">10</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-8">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bins"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Selected: 10<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-9">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> class</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col-8"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-11">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-12">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;!-- Where we'll get the chart drawn --&gt;</span></span>
<span id="cb7-13">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">canvas</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"myChart"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">canvas</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-14">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-16">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb7-17"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div>
<p>In order to draw the graph, I’ll rely on a dead simple (yet powerful) JavaScript lib called <a href="https://www.chartjs.org/">Chart.js</a>. It has a great <a href="https://www.chartjs.org/docs/latest/charts/bar.html">bar chart</a> graph that will work perfectly for our case.</p>
<p>Let’s start by adding the lib with <code>&lt;script src="https://cdn.jsdelivr.net/npm/chart.js"&gt;&lt;/script&gt;</code>.</p>
<p>We then need some JavaScript to:</p>
<ul>
<li>Initiate the chart at launch</li>
</ul>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// https://colinfay.me/api-from-client-shiny/</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Default to 10 bins</span></span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fetch</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hist-data/10"</span>)</span>
<span id="cb8-4">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">then</span>((data) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span>{</span>
<span id="cb8-5">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Convert the data to json and</span></span>
<span id="cb8-6">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// create a chart</span></span>
<span id="cb8-7">        data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">json</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">then</span>((res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb8-8">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Keeping a global object with the chart</span></span>
<span id="cb8-9">          globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chart</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Chart</span>(</span>
<span id="cb8-10">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// This is where the chart will go</span></span>
<span id="cb8-11">          <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">document</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getElementById</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'myChart'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {</span>
<span id="cb8-12">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bar'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-13">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb8-14">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// webR returns the names</span></span>
<span id="cb8-15">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">labels</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-16">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">datasets</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [{</span>
<span id="cb8-17">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram of waiting times"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-18">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span></span>
<span id="cb8-19">            }]</span>
<span id="cb8-20">          }</span>
<span id="cb8-21">        })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-22">        })</span>
<span id="cb8-23">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">catch</span>((error) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb8-24">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">alert</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Error catchin result from R"</span>)</span>
<span id="cb8-25">        })</span>
<span id="cb8-26">      })</span>
<span id="cb8-27">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">catch</span>((error) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb8-28">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">alert</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Error catchin result from R"</span>)</span>
<span id="cb8-29">      })</span></code></pre></div>
<ul>
<li>Update the chart when the slider is moved</li>
</ul>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb9-1">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> update <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> (n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) {</span>
<span id="cb9-2">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fetch</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`hist-data/</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">then</span>((data) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb9-3">        data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">json</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">then</span>((res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb9-4">          globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chart</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-5">          globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chart</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">datasets</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forEach</span>(dataset <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb9-6">            dataset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-7">          })</span>
<span id="cb9-8">          globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chart</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-9">        })</span>
<span id="cb9-10">      })</span>
<span id="cb9-11">      <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">document</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">querySelector</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#bins'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">innerHTML</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`Selected: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-12"></span>
<span id="cb9-13">    }</span>
<span id="cb9-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">document</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">querySelector</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#customRange1'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addEventListener</span>(</span>
<span id="cb9-15">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'change'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb9-16">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() { <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> }</span>
<span id="cb9-17"> )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>And here it is! You can see the app live at <a href="https://srv.colinfay.me/express-webr-old-faithful/">srv.colinfay.me/express-webr-old-faithful</a>. You can find the code <a href="https://github.com/ColinFay/webr-examples/tree/main/express-webr-old-faithful">here</a>.</p>
<p>You can also try it with:</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-it</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 3000:3000 colinfay/express-webr-old-faithful</span></code></pre></div>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/old-faithful-express-bootstrap-webr/";
  this.page.identifier = "/old-faithful-express-bootstrap-webr";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>webr</category>
  <guid>https://colinfay.me/posts/old-faithful-express-bootstrap-webr.html</guid>
  <pubDate>Tue, 25 Jul 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Using webR in an Express JS REST API</title>
  <link>https://colinfay.me/posts/calling-webr-from-expressjs.html</link>
  <description><![CDATA[ 




<p>Find all my posts about webR <a href="https://colinfay.me/categories/#webr">here</a>.</p>
<blockquote class="blockquote">
<p>Note: webR and the tools I’ve been writing are moving fast and if you’re reading this from the future, some of the things here might be obsolete.</p>
</blockquote>
<section id="webr-wat-again" class="level2">
<h2 class="anchored" data-anchor-id="webr-wat-again">webR? wat again?</h2>
<p>As described in the <a href="https://docs.r-wasm.org/webr/latest/">doc</a>:</p>
<blockquote class="blockquote">
<p>WebR is a version of the statistical language R compiled for the browser and Node.js using WebAssembly, via Emscripten.</p>
</blockquote>
<p>In this post, I won’t go into details into what <code>webR</code> is, as if you want to know more, you’ll probably get a better understanding by going through the documentation.</p>
<p>But if like a normal person you don’t read the doc, let’s sum up what <code>webR</code> is. Simply put, <code>WebAssembly</code> (shorten wasm) is a format that can be used to encode a programming language to something that can then be called from a JavaScript runtime (in the browser or in NodeJS). Schematically, you take one language, you translate it to wasm, and the output can be read and used from JavaScript.</p>
<p>If you still have no idea what wasm is but want to get a better grasp on what it is, I suggest reading <a href="https://www.jesuisundev.com/en/understand-webassembly-in-5-minutes/">this blogpost</a> that really sums it well, especially this quote:</p>
<blockquote class="blockquote">
<p>WASM is a way to use non-Javascript code and run it in your browser.</p>
</blockquote>
<p>I also suggest reading the blogposts by <a href="https://rud.is/b/">boB</a> on the subject.</p>
<p>The rest of this post assume that you understand what wasm &amp; <code>webR</code> are.</p>
</section>
<section id="playing-with-webr" class="level2">
<h2 class="anchored" data-anchor-id="playing-with-webr">Playing with webR</h2>
<p>I’ve been interested with WebAssembly for a while now, and can remember talking about it late during the night at R conferences around 2018 and 2019… but that’s a story for another time 😅</p>
<p>Today’s story is about exploring how to build tools in JavaScript that will call <code>webR</code>.</p>
<p>I’ll try to document my experimentations, and this post is the first one of a series that I hope I’ll find time to continue over the summer.</p>
</section>
<section id="hello-world---creating-an-api-where-we-call-webr" class="level2">
<h2 class="anchored" data-anchor-id="hello-world---creating-an-api-where-we-call-webr">Hello World! - Creating an API where we call webR</h2>
<p>I’ll start with a simple <a href="https://expressjs.com/">Express JS</a> REST API that uses webR to run R code.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initiating a projet and opening it with VSCode</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> webrexpresshelloworld</span>
<span id="cb1-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">code</span> webrexpresshelloworld</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Quick init of a node project</span></span>
<span id="cb1-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> init <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-y</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> index.js</span>
<span id="cb1-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> install express</span></code></pre></div>
<p>We’ll now install the webR nodejs package</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">npm</span> i @r-wasm/webr</span></code></pre></div>
<p>And now, let’s move to our index.js file. We’ll start with the basics of Express JS: creating the <code>app</code> object.</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Creating the express app</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'express'</span>)()</span></code></pre></div>
<p>Then, we’ll import <code>WebR</code> from <code>@r-wasm/webr</code>.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// importing the `WebR` class inside the runtime</span></span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> { WebR } <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'@r-wasm/webr'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>We’ll now be initiating the webR session by: - creating an instance of webR - binding it to <code>globalThis</code> so that it’s available everywhere - calling the <code>init</code> method - Once <code>webR</code> is ready, we’ll launch the <code>app</code></p>
<p>As webR communicates via the worker thread asynchronously, we need to use <code>async</code>/<code>await</code>.</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb5-1">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb5-2">  globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WebR</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-4">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"webR is ready"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Starting the express app</span></span>
<span id="cb5-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// only after webR is ready</span></span>
<span id="cb5-7">  app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">listen</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> () <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb5-8">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">console</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'http://localhost:3000'</span>)</span>
<span id="cb5-9">  })</span>
<span id="cb5-10">})()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>Finally, we add a route that returns an hello world:</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Creating a route for the express app</span></span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// that will return a titleCase hello world from r</span></span>
<span id="cb6-3">app<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">async</span> (req<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> res) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> {</span>
<span id="cb6-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Evaluating the R code inside the R worker</span></span>
<span id="cb6-5">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> globalThis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">webR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evalR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tools::toTitleCase("hello from r!")'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Converting the result to a JS object</span></span>
<span id="cb6-7">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toJs</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Sending the result back to the client,</span></span>
<span id="cb6-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// (webR outputs an object with types/names/values</span></span>
<span id="cb6-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// and here we only want the values)</span></span>
<span id="cb6-11">  res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">send</span>(output<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values</span>)</span>
<span id="cb6-12">})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>And now, let’s run our app!</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">node</span> index.js</span></code></pre></div>
<p>You can find the code <a href="https://github.com/ColinFay/webr-examples/tree/main/express-webr-hello-world">here</a>, and see it live at <a href="https://srv.colinfay.me/express-webr-hello-world">srv.colinfay.me/express-webr-hello-world</a>.</p>
<p>You can also try it with</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">docker</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-it</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 3000:3000 colinfay/express-webr-hello-world</span></code></pre></div>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/calling-webr-from-expressjs/";
  this.page.identifier = "/webr-in-expressjs";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>webr</category>
  <guid>https://colinfay.me/posts/calling-webr-from-expressjs.html</guid>
  <pubDate>Tue, 04 Jul 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>[Keynote] Production is like ultra running: brutal, ungrateful, but worth every step</title>
  <link>https://colinfay.me/posts/keynote-shiny-conf-2023.html</link>
  <description><![CDATA[ 




<p>Hey folks, my keynote at the <a href="https://shinyconf.appsilon.com/">shinyconf 2023</a> called “Production is like ultra running: brutal, ungrateful, but worth every step” is now online!</p>
<p>In this talk, I reflect on how ultra running has helped me become a better software engineer, and how building software has helped me learning how to run long distances.</p>
<p>Giving this talk has been something quite hard for me (probably harder than running 50 miles 😅) — I usually give very technical talks, but for one time decided I should try telling a bit more about me and my story, about why I love running long distances and building software that are used in production. I hope watching this one will inspire some of you, and feel free to share your thoughts about it with me!</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/wMbhxTJNrmw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="">
</iframe>
<p>Thanks again to the Apsillon team for inviting me to talk, to Vincent and Diane from ThinkR for their support, and to <a href="https://www.maximetarcher.com/">Maxime</a> for coaching me and pushing me toward being more open about myself.</p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/keynote-shiny-conf-2023/";
  this.page.identifier = "/keynote-shiny-conf";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <guid>https://colinfay.me/posts/keynote-shiny-conf-2023.html</guid>
  <pubDate>Fri, 05 May 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2021-02 with R &amp; JavaScript</title>
  <link>https://colinfay.me/posts/aoc-2021-02.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2021-02 with R and JavaScript.</p>
<p>Like yesterday, trying to write JS code that looks like R code.</p>
<section id="disclaimer" class="level2">
<h2 class="anchored" data-anchor-id="disclaimer">Disclaimer</h2>
<p>Disclaimer Obviously, this post contains a big spoiler about Advent of Code, as it gives solutions for solving 2021-02.</p>
<p>[Disclaimer bis] I’m no JavaScript expert so this might not be the perfect solution. TBH, that’s also the case for the R solution.</p>
</section>
<section id="about-the-javascript-code" class="level2">
<h2 class="anchored" data-anchor-id="about-the-javascript-code">About the JavaScript code</h2>
<p>The JavaScript code has been written in the same RMarkdown as the R code. It runs thanks to the <code>{bubble}</code> package: <a href="https://github.com/ColinFay/bubble" class="uri">https://github.com/ColinFay/bubble</a>.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">bubble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_node_engine</span>()</span></code></pre></div>
</section>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<section id="one" class="level3">
<h3 class="anchored" data-anchor-id="one">One</h3>
<ul>
<li>We start at a position [0, 0] (horizontal, depth), then
<ul>
<li>forward X increases the horizontal position by X units</li>
<li>down X increases the depth by X units</li>
<li>up X decreases the depth by X units</li>
</ul></li>
</ul>
<p>Result is horizontal * depth.</p>
</section>
<section id="two" class="level3">
<h3 class="anchored" data-anchor-id="two">Two</h3>
<ul>
<li>We start at a position [0, 0, 0] (horizontal, depth,), then
<ul>
<li>down X increases your aim by X units</li>
<li>up X decreases your aim by X units</li>
<li>forward X does two things :
<ul>
<li>It increases your horizontal position by X units</li>
<li>It increases your depth by your aim multiplied by X</li>
</ul></li>
</ul></li>
</ul>
<p>Result is horizontal * depth.</p>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2021/day/2" class="uri">https://adventofcode.com/2021/day/2</a>.</p>
</section>
</section>
<section id="reading-inputs" class="level2">
<h2 class="anchored" data-anchor-id="reading-inputs">Reading inputs</h2>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read</span></span>
<span id="cb2-2">ipt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.delim</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-02-aoc.txt"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1</span></code></pre></div>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> fs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'fs'</span>)</span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">var</span> ipt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFileSync</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-02-aoc.txt"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf8'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
</section>
<section id="part-one" class="level2">
<h2 class="anchored" data-anchor-id="part-one">Part one</h2>
<section id="r-solution" class="level3">
<h3 class="anchored" data-anchor-id="r-solution">R solution</h3>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Horizontal and depth</span></span>
<span id="cb4-2">start_pos <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">horizontal =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (moves <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> ipt) {</span>
<span id="cb4-4">  move <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(moves, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb4-5"></span>
<span id="cb4-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up"</span>) {</span>
<span id="cb4-7">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb4-8">  }</span>
<span id="cb4-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"down"</span>) {</span>
<span id="cb4-10">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb4-11">  }</span>
<span id="cb4-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forward"</span>) {</span>
<span id="cb4-13">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb4-14">  }</span>
<span id="cb4-15">}</span>
<span id="cb4-16"></span>
<span id="cb4-17">start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span></code></pre></div>
<pre><code>## horizontal
##    1727835</code></pre>
</section>
<section id="js-solution" class="level3">
<h3 class="anchored" data-anchor-id="js-solution">JS solution</h3>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">var</span> start_pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Gotcha: R for(x in y) is JS for(x of y)</span></span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (moves <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">of</span> ipt) {</span>
<span id="cb6-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// In JS, object have methods, so the split function</span></span>
<span id="cb6-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// is containes inside the string object</span></span>
<span id="cb6-8">  move <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> moves<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span>)</span>
<span id="cb6-9"></span>
<span id="cb6-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Small gotcha here, JS i 0 based index</span></span>
<span id="cb6-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up"</span>) {</span>
<span id="cb6-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// R as.numeric is parseInt()</span></span>
<span id="cb6-13">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb6-14">  }</span>
<span id="cb6-15">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"down"</span>) {</span>
<span id="cb6-16">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb6-17">  }</span>
<span id="cb6-18">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forward"</span>) {</span>
<span id="cb6-19">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb6-20">  }</span>
<span id="cb6-21">}</span></code></pre></div>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb7-1">start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span></code></pre></div>
<pre><code>## 1727835</code></pre>
</section>
</section>
<section id="part-two" class="level2">
<h2 class="anchored" data-anchor-id="part-two">Part two</h2>
<section id="r-solution-1" class="level3">
<h3 class="anchored" data-anchor-id="r-solution-1">R solution</h3>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Horizontal, depth, and aim</span></span>
<span id="cb9-2">start_pos <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">horizontal =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">depth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb9-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (moves <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> ipt) {</span>
<span id="cb9-4">  move <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(moves, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb9-5"></span>
<span id="cb9-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up"</span>) {</span>
<span id="cb9-7">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb9-8">  }</span>
<span id="cb9-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"down"</span>) {</span>
<span id="cb9-10">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb9-11">  }</span>
<span id="cb9-12">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forward"</span>) {</span>
<span id="cb9-13">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb9-14">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span>
<span id="cb9-15">  }</span>
<span id="cb9-16">}</span>
<span id="cb9-17"></span>
<span id="cb9-18">start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span></code></pre></div>
<pre><code>## horizontal
## 1544000595</code></pre>
</section>
<section id="js-solution-1" class="level3">
<h3 class="anchored" data-anchor-id="js-solution-1">JS solution</h3>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">var</span> start_pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (moves <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">of</span> ipt) {</span>
<span id="cb11-4">  move <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> moves<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span>)</span>
<span id="cb11-5">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"up"</span>) {</span>
<span id="cb11-6">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb11-7">  }</span>
<span id="cb11-8">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"down"</span>) {</span>
<span id="cb11-9">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb11-10">  }</span>
<span id="cb11-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forward"</span>) {</span>
<span id="cb11-12">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb11-13">    start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(move[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb11-14">  }</span>
<span id="cb11-15">}</span></code></pre></div>
<div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb12-1">start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> start_pos[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span></code></pre></div>
<pre><code>## 1544000595</code></pre>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2021-02/";
  this.page.identifier = "/aoc-02";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2021-02.html</guid>
  <pubDate>Thu, 02 Dec 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2021-01 with R &amp; JavaScript</title>
  <link>https://colinfay.me/posts/aoc-2021-01.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2021-01 with R and JavaScript.</p>
<p>I’ll try in this post to write R code that looks like the JavaScript one, using <code>{purrr}</code>, to show how both syntaxes can look alike. The idea is to highlight how you can leverage your <code>{purrr}</code> knowledge when writting JavaScript code.</p>
<section id="disclaimer" class="level2">
<h2 class="anchored" data-anchor-id="disclaimer">Disclaimer</h2>
<p>Disclaimer Obviously, this post contains a big spoiler about Advent of Code, as it gives solutions for solving day 1.</p>
<p>[Disclaimer bis] I’m no JavaScript expert so this might not be the perfect solution. TBH, that’s also the case for the R solution.</p>
</section>
<section id="about-the-javascript-code" class="level2">
<h2 class="anchored" data-anchor-id="about-the-javascript-code">About the JavaScript code</h2>
<p>The JavaScript code has been written in the same RMarkdown as the R code. It runs thanks to the <code>{bubble}</code> package: <a href="https://github.com/ColinFay/bubble" class="uri">https://github.com/ColinFay/bubble</a></p>
</section>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<ul>
<li><p>Count the number of times n is larger that n-1</p></li>
<li><p>Count the number of times n + n-1 + n-2 is larger that n-1 + n-2 + n-3</p></li>
</ul>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2021/day/1" class="uri">https://adventofcode.com/2021/day/1</a>.</p>
</section>
<section id="reading-inputs" class="level2">
<h2 class="anchored" data-anchor-id="reading-inputs">Reading inputs</h2>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb1-3">ipt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.delim</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-01-aoc.txt"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1</span></code></pre></div>
<blockquote class="blockquote">
<p>// Reading the file and converting it to int</p>
</blockquote>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> fs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'fs'</span>)</span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">var</span> ipt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readFileSync</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-01-aoc.txt"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf8'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-3">ipt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ipt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parseInt</span>(x))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
</section>
<section id="part-one" class="level2">
<h2 class="anchored" data-anchor-id="part-one">Part one</h2>
<section id="r-solution" class="level3">
<h3 class="anchored" data-anchor-id="r-solution">R solution</h3>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(ipt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(</span>
<span id="cb3-3">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ipt[.x] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ipt[.x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-4">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">keep</span>(</span>
<span id="cb3-6">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb3-7">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>()</span></code></pre></div>
<pre><code>## [1] 1154</code></pre>
</section>
<section id="js-solution" class="level3">
<h3 class="anchored" data-anchor-id="js-solution">JS solution</h3>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb5-1">ipt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(</span>
<span id="cb5-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// inside map() in JS, the second argument to the anonymous</span></span>
<span id="cb5-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// function is the index inside the array.</span></span>
<span id="cb5-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// It mimics what we have in map(2:length(ipt)) in R</span></span>
<span id="cb5-5">  (x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ipt[y] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ipt[y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb5-6">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb5-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// filter() will keep the element that matches the anonymous</span></span>
<span id="cb5-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// predicate function</span></span>
<span id="cb5-9">    x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb5-10">    )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<pre><code>## 1154</code></pre>
</section>
</section>
<section id="part-two" class="level2">
<h2 class="anchored" data-anchor-id="part-two">Part two</h2>
<section id="r-solution-1" class="level3">
<h3 class="anchored" data-anchor-id="r-solution-1">R solution</h3>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">tmp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(ipt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(</span>
<span id="cb7-3">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ipt[.x] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ipt[.x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ipt[.x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb7-4">  )</span>
<span id="cb7-5"></span>
<span id="cb7-6"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(tmp) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(</span>
<span id="cb7-8">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> tmp[.x] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> tmp[.x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb7-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">keep</span>(</span>
<span id="cb7-11">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb7-12">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>()</span></code></pre></div>
<pre><code>## [1] 1127</code></pre>
</section>
<section id="js-solution-1" class="level3">
<h3 class="anchored" data-anchor-id="js-solution-1">JS solution</h3>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode javascript code-with-copy"><code class="sourceCode javascript"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">var</span> tmp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ipt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(</span>
<span id="cb9-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Creating n + n-1 + n-2</span></span>
<span id="cb9-4">    (x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> ipt[y] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ipt[y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ipt[y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb9-5">    )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb9-6"></span>
<span id="cb9-7">tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span></span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(</span>
<span id="cb9-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Same as part 1</span></span>
<span id="cb9-10">    (x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> tmp[y] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> tmp[y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb9-11">    )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span></span>
<span id="cb9-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb9-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// keeping the ones that have raised</span></span>
<span id="cb9-14">    x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb9-15">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span></span>
<span id="cb9-16">  length<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<pre><code>## undefined
## undefined
## 1127</code></pre>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2021-01/";
  this.page.identifier = "/aoc-01";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2021-01.html</guid>
  <pubDate>Wed, 01 Dec 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The ‘Engineering Production-Grade Shiny Apps’ book is available in print!</title>
  <link>https://colinfay.me/posts/engineering-shiny-print.html</link>
  <description><![CDATA[ 




<p>I’m so, so excited to announce that the <em>‘Engineering Production-Grade Shiny Apps’</em> book is now available in print! This book is the result of two years of work, and I couldn’t be more happy to see it available in print.</p>
<div data-align="center">
<h3 style="font-size:1.5em;margin-bottom:1.5em;" class="anchored">
<p><a href="https://www.routledge.com/Engineering-Production-Grade-Shiny-Apps/Fay-Rochette-Guyader-Girard/p/book/9780367466022">📚 Get a copy 📚</a></p>
</h3>
</div>
<div>

</div>
<p>As its title suggests, <em>‘Engineering Production-Grade Shiny Apps’</em> is a book about building <code>{shiny}</code> apps that will be sent to production. It goes deep into <code>{golem}</code>, of course, but it’s not a book about <code>{golem}</code> only — it tries to cover a large panel of topics, and I’m pretty sure that if you are a <code>{shiny}</code> developer, there is something for you in this book: project management, code organization, team work, best practices for front end development, CI and CD, deployment, code optimization…</p>
<p>This book will remain available online for free at <a href="https://engineering-shiny.org/" class="uri">https://engineering-shiny.org/</a> (and I want to thank Chapman and Hall/CRC for this), but if you want to grab a copy, you can go on <a href="https://www.routledge.com/Engineering-Production-Grade-Shiny-Apps/Fay-Rochette-Guyader-Girard/p/book/9780367466022">routledge.com</a> and order one. I know it can feel counter-intuitive to buy a print copy of a book which is available online, but if you want this to continue to happen, please continue to support the publisher by buying a copy of this book, or of any other from Chapman and Hall/CRC — they have been doing a massive work when it comes to organizing, editing and reviewing.</p>
<p>I want to personally thank everybody that has been contributing to this book, with some special thanks to <a href="https://twitter.com/theRcast">Eric Nantz</a> which has been a <code>{golem}</code> early adapter and an all time supporter of everything from the <code>golemverse</code>, to <a href="https://twitter.com/divadnojnarg">David Granjon</a> for all his feedback on the book, and to <a href="https://twitter.com/chrisderv">Christophe Dervieux</a> for his precious help during my battles with Markdown. Thanks also to <a href="https://twitter.com/crcgrubbsd">David Grubbs</a> from Chapman and Hall/CRC for making this happen.</p>
<p>That being said, I still can’t believe I’ve added my name to the list of authors in “The R Series” books 😱</p>
<p>I hope you’ll enjoy it!</p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/engineering-shiny-print/";
  this.page.identifier = "/engineering-shiny";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <guid>https://colinfay.me/posts/engineering-shiny-print.html</guid>
  <pubDate>Mon, 04 Oct 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Add POST requests to {shiny} with {brochure}</title>
  <link>https://colinfay.me/posts/post-request-shiny-app-brochure.html</link>
  <description><![CDATA[ 




<p>[Disclaimer] The package <strong>presented in this blog post is still experimental</strong> at the time of writing these lines (2021-02-28), so it might face some API changes in the future.</p>
<p>Yesterday Jacqueline’s tweeted about her need to add <code>POST</code> requests to a <code>{shiny}</code> application.</p>
<div data-align="center">
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
</p><p>I’m astounded that it doesn’t seem like a Shiny app is able to accept POST requests. I have data I wanna pass to my server :/</p>
<p></p>
<p>— Dr.&nbsp;Jacqueline Nolis (<span class="citation" data-cites="skyetetra">@skyetetra</span>) <a href="https://twitter.com/skyetetra/status/1365785536049291264?ref_src=twsrc%5Etfw">February 27, 2021</a></p>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
<p>This is actually one of the things I had in mind when building <code>{brochure}</code>: more flexibility with the HTTP requests and how they are handled.</p>
<p>So here is a working example of adding a <code>POST</code> handler to a <code>{shiny}</code> app, using <code>{brochure}</code>.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(shiny)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(brochure)</span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We'll start by defining a home page</span></span>
<span id="cb1-4">home <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(</span>
<span id="cb1-5">  httr_code</span>
<span id="cb1-6">){</span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,</span>
<span id="cb1-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simple UI, no server side</span></span>
<span id="cb1-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb1-11">      tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello world!"</span>), </span>
<span id="cb1-12">      tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open a new R console and run:"</span>),</span>
<span id="cb1-13">      tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pre</span>(</span>
<span id="cb1-14">        httr_code</span>
<span id="cb1-15">      )</span>
<span id="cb1-16">    )</span>
<span id="cb1-17">  )</span>
<span id="cb1-18">}</span>
<span id="cb1-19"></span>
<span id="cb1-20">postpage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb1-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb1-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/post"</span>,</span>
<span id="cb1-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We'll handle POST requests via a request handler</span></span>
<span id="cb1-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">req_handlers =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb1-25">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(req){</span>
<span id="cb1-26">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is where the magic happens</span></span>
<span id="cb1-27">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Our req object contains a `REQUEST_METHOD` </span></span>
<span id="cb1-28">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># entry that contains the HTTP verb used </span></span>
<span id="cb1-29">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to perform the request</span></span>
<span id="cb1-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>( req<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>REQUEST_METHOD <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"POST"</span> ){</span>
<span id="cb1-31">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"In POST!"</span>)</span>
<span id="cb1-32">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Because we want the HTTP request to be </span></span>
<span id="cb1-33">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># completed here, we return an httpResponse object here. </span></span>
<span id="cb1-34">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># httpResponse() is exported since {shiny} 1.6.0, </span></span>
<span id="cb1-35">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># otherwise you'll have to ::: (shiny:::httpResponse)</span></span>
<span id="cb1-36">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(</span>
<span id="cb1-37">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">httpResponse</span>(</span>
<span id="cb1-38">              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 201 is the HTTP code you'll send back when </span></span>
<span id="cb1-39">              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># you have created a resource on the server</span></span>
<span id="cb1-40">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">status =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">201</span>, </span>
<span id="cb1-41">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">content =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ok"</span></span>
<span id="cb1-42">            )</span>
<span id="cb1-43">          )</span>
<span id="cb1-44">        } </span>
<span id="cb1-45">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Whenever we're not in a POST, we'll simply return </span></span>
<span id="cb1-46">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># req, which will the move to standard {shiny} handling, </span></span>
<span id="cb1-47">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># i.e. calling ui and server.</span></span>
<span id="cb1-48">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(req)</span>
<span id="cb1-49">      }</span>
<span id="cb1-50">    ), </span>
<span id="cb1-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb1-52">      tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello from /post!"</span>)</span>
<span id="cb1-53">    )</span>
<span id="cb1-54">  )</span>
<span id="cb1-55">}</span>
<span id="cb1-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For the sake of reproducibility:</span></span>
<span id="cb1-57"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shiny.port =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2811</span>)</span>
<span id="cb1-58"></span>
<span id="cb1-59"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brochureApp</span>(</span>
<span id="cb1-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">home</span>(</span>
<span id="cb1-61">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">httr_code =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"httr::POST('http://127.0.0.1:2811/post')"</span></span>
<span id="cb1-62">  ),</span>
<span id="cb1-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">postpage</span>()</span>
<span id="cb1-64">)</span></code></pre></div>
<p>If you open another terminal, you can run:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> httr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">POST</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://127.0.0.1:2811/post"</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3">Response [http<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span>.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2811</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>post]</span>
<span id="cb2-4">  Date<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2021-02-28</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">47</span></span>
<span id="cb2-5">  Status<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">201</span></span>
<span id="cb2-6">  Content<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> text<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>html; charset<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span></span>
<span id="cb2-7">  Size<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> B</span></code></pre></div>
<p>And your R console running the <code>{shiny}</code> app should print a message:</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">Listening on http<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span>.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2811</span></span>
<span id="cb3-2">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"In POST!"</span></span></code></pre></div>
<p>Of course, what’s interesting with <code>POST</code> is that you can actually send a <code>body</code> along the http request. Using <code>{brochure}</code>, you can access it inside the request handler, via <code>req$.bodyData</code>. It’s an external pointer to a file, so we can read it with <code>readLines</code>.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(req<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.bodyData)</span>
<span id="cb4-2"> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'file'</span> int <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb4-3"> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"conn_id"</span>)<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span>externalptr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> </span></code></pre></div>
<p>Let’s add this to our app:</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">postpage <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb5-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/post"</span>,</span>
<span id="cb5-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">req_handlers =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-5">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(req){</span>
<span id="cb5-6">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>( req<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>REQUEST_METHOD <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"POST"</span> ){</span>
<span id="cb5-7">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reading the req$.bodyData. </span></span>
<span id="cb5-8">          body <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(</span>
<span id="cb5-9">            req<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.bodyData,</span>
<span id="cb5-10">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The file will have no final EOL</span></span>
<span id="cb5-11">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># and we don't want a message to be </span></span>
<span id="cb5-12">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># printed to the console</span></span>
<span id="cb5-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb5-14">          )</span>
<span id="cb5-15">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Here, you can define your own handling </span></span>
<span id="cb5-16">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># logic of the body. </span></span>
<span id="cb5-17">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We'll simply print it to the console </span></span>
<span id="cb5-18">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in this example.</span></span>
<span id="cb5-19">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(body)</span>
<span id="cb5-20">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(</span>
<span id="cb5-21">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">httpResponse</span>(</span>
<span id="cb5-22">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">status =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">201</span>, </span>
<span id="cb5-23">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">content =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Created"</span></span>
<span id="cb5-24">            )</span>
<span id="cb5-25">          )</span>
<span id="cb5-26">        } </span>
<span id="cb5-27">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(req)</span>
<span id="cb5-28">      }</span>
<span id="cb5-29">    ), </span>
<span id="cb5-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb5-31">      tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello from /post!"</span>)</span>
<span id="cb5-32">    )</span>
<span id="cb5-33">  )</span>
<span id="cb5-34">}</span>
<span id="cb5-35"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brochureApp</span>(</span>
<span id="cb5-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">home</span>(</span>
<span id="cb5-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">httr_code =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"httr::POST('http://127.0.0.1:2811/post', body = 'plop')"</span></span>
<span id="cb5-38">  ),</span>
<span id="cb5-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">postpage</span>()</span>
<span id="cb5-40">)</span></code></pre></div>
<p>Now inside another console, send:</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> (resp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> httr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">POST</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://127.0.0.1:2811/post"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">body =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plop"</span>))</span>
<span id="cb6-2">Response [http<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span>.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2811</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>post]</span>
<span id="cb6-3">  Date<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2021-02-28</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">57</span></span>
<span id="cb6-4">  Status<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">201</span></span>
<span id="cb6-5">  Content<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> text<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>html; charset<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span></span>
<span id="cb6-6">  Size<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> B</span></code></pre></div>
<p>The R console running the <code>{shiny}</code> app should print a message:</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">Listening on http<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span>.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2811</span></span>
<span id="cb7-2">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plop"</span></span></code></pre></div>
<p>Back to the other console, the <code>content()</code> of the <code>{httr}</code> <code>resp</code> will be the content defined in the <code>httpResponse</code>:</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> httr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">content</span>(resp)</span>
<span id="cb8-2">{html_document}</span>
<span id="cb8-3"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>html<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb8-4">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>Created<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span>body<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div>
<p>Hope this helps!</p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/post-request-shiny-app-brochure/";
  this.page.identifier = "/post-req-shiny-brochure";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <guid>https://colinfay.me/posts/post-request-shiny-app-brochure.html</guid>
  <pubDate>Sun, 28 Feb 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Multi-page {shiny} Applications with {brochure}</title>
  <link>https://colinfay.me/posts/brochure-r-package.html</link>
  <description><![CDATA[ 




<p>[Disclaimer] The package <strong>presented in this blog post is still experimental</strong> at the time of writing these lines (2021-02-18), so it might face some API changes in the future.</p>
<section id="multi-page-in-shiny-and-random-thoughts-about-designing-web-applications" class="level2">
<h2 class="anchored" data-anchor-id="multi-page-in-shiny-and-random-thoughts-about-designing-web-applications">Multi-page in {shiny}, and random thoughts about designing web applications</h2>
<p>Something that has been bugging me for a while is the inability to really share a specific part of a <code>{shiny}</code> dashboard, at least natively, using a specific path. In other words, I’ve always wanted to be able to do something like <code>my-uberapp.io/contact</code> to share with someone a specific part of the app. And only this part.</p>
<p>Another need I was facing, probably born out of building web applications using NodeJS, is a <strong>natural way to manipulate endpoints, http requests and responses</strong>, so that I could do something pretty common in web application: a home page, and next to it a login page that verifies your identity, and redirects you to another page after setting a cookie. That of course, leads to the necessity of having the <strong>ability to define various endpoints with a specific behavior for each</strong>: <strong>a unique UI, and a 18 server response</strong>. Pure, native, multi-endpoint applications, not one big ball of UI with parts hidden using JavaScript &amp; CSS, and a global server function that might launch computation you don’t need for your page.</p>
<p>A few weeks back, I’ve decided to focus on this question. Multi-page in <code>{shiny}</code> is not new: I’ve found both <a href="https://appsilon.com/shiny-router-020/"><code>{shiny.router}</code></a>, and <a href="https://github.com/nteetor/blaze"><code>{blaze}</code></a> that already implement a form of “multi-page”.</p>
<blockquote class="blockquote">
<p>Note: both these packages work perfectly, and they definitely do what they are designed for. There are the product of tremendous work with smart implementations and my goal is in no way to undermine these packages. They just don’t answer the need I was having, hence my new approach to this question. <code>{brochure}</code> will answer the needs for more large and complex applications built with <code>{shiny}</code>, while both the <code>{shiny.router}</code> &amp; <code>{blaze}</code> should be easier to get started with.</p>
</blockquote>
<p>As far as I saw it, these two packages didn’t answer what I was looking for: “real” multi-page. Both <code>{shiny.router}</code> &amp; <code>{blaze}</code> <strong>still produce a form of Single Page Application</strong>, but plays with the URL to make you feel like you’re on a multi-page application.</p>
<p>For example, <code>{shiny.router}</code> hides the parts of the app via JS &amp; CSS – if you browse the source code, you’ll still see the hidden HTML. That also implies that they are potential conflicts between ids: you have to be sure that all your pages have unique id on their ouputs/inputs, otherwise the app doesn’t render, meaning that <strong>a “page” doesn’t have its own server function</strong>. And of course, if you look at the <code>Network</code> tab of your browser developer tool, you’ll see that there is no new <code>GET</code> request made whenever a new “page” is loaded: this new page is still the same application, with parts hidden through JavaScript.</p>
<p>Hiding UI parts is a practice that has always been bothering me in term of performance – on a large <code>{shinydashboard}</code>, for example, <strong>you’ll be transferring the full HTML, CSS and JavaScript for the whole application when the app launches, even if the user never visits all the tabs</strong>. <code>{shiny}</code> is very smart when it comes to transferring CSS and JS files: for example, it will only serve the external resources for the <code>sliderInput()</code> if there is one in the app. But if you’ve got a <code>{shinydashboard}</code> dashboard with 19 pages, and the <code>sliderInput()</code> is on page 19 and will only be seen by 0.1% of the visitors, it will still be transferred to every user, regardless of whether or not they need it. This might result in lowering the performance of the app, as it might be raising the time to ‘First Meaningful Paint’, and of course it is a waste of resources, especially if your application is served to people with low bandwidth, and/or browsing your app using cellular data. I know this is a question you’ve probably never asked yourself before ;) - but <strong>performance is a pretty common question in the web development world, and something to be aware if you’re serving your app at scale</strong>. If this is something you’re interested in, I suggest reading <a href="https://web.dev/why-speed-matters/">Why does speed matter?</a> from the Google dev center, and <a href="https://developer.mozilla.org/en-US/docs/Web/Performance">Web Performance</a> from Mozilla Web Docs. See also <a href="https://engineering-shiny.org/need-for-optimization.html#b.-shiny-front-end">14.2.2 Profiling {shiny}</a> from the <em>Engineering Shiny</em> book.</p>
<p>And, another final thing I wanted is a way to “flush” objects when you change page: in the current implementation of <code>{shinydashboard}</code>, if you create something on <code>tab1</code>, it will still be there in <code>tab2</code>, taking some space in the RAM, even if you don’t need it. It’s rather convenient because you don’t have to think about these things: you start a <code>{shiny}</code> session, and all the objects will still be there as long as the session lives. But that also means that as soon as the session stops, there is no way to get the values back as they only live in the RAM, inside the session.</p>
<p>On the other hand, with an implementation where every page gets its own <code>{shiny}</code> session, you need to find a way to identify the user with a form of <code>id</code>, save the relevant values (potentially write in a DB), then fetch these values again using the <code>id</code> stored in the browser (typically, you have a session cookie in the browser that is used as a key to search the DB). <strong>A process forcing you to think more carefully about the data flow of your app</strong>. What that also means is that as the <code>id</code> is a cookie in the browser, whenever the app crashes, it will be able to reload to the exact same place as long as the cookie is still in the browser (well, not exactly but you understand the idea).</p>
<p>So, back to our first topic – what was I looking for? My goal was to find a way to build <code>{shiny}</code> applications that are <strong>natively</strong> multi-page. In other words, an application that <em>answers</em> to a request on <code>/page2</code>, not an app that silently redirect to <code>/</code>, nor an application that requires playing with <code>/#!/page2</code> in the url. (Reminder, <code>#</code> is traditionally used as an anchor tag in a web page). And, of course, <strong>I wanted each page to have its own shiny session, its own UI and server functions</strong>.</p>
<p>I was also looking for a way to manipulate both the <code>GET</code> request from the server and the <code>httpResponse</code> that is sent back, for example in order to <strong>set <code>httpOnly</code> cookies in the header of the HTTP Response or change the HTTP status code</strong>, the same way you can manipulate these when building an application with NodeJS for example. This is something that you’d find in the <a href="https://github.com/JohnCoene/ambiorix"><code>{ambiorix}</code></a> project by the amazing <a href="https://john-coene.com/">John Coene</a> (who will get some SEO juice thanks to this backlink). It’s an amazing approach that definitely resonated with me as I’ve been a NodeJS &amp; <code>express.js</code> user for some time now, but that’s still far from the way you’d build things in <code>{shiny}</code>, and will probably require a lot of coding to get an application up and running (which is good, because I love coding, but let’s use our <code>{shiny}</code> knowledge for now :) ).</p>
<p>To sum up, I wanted a way to get closer to how you build applications in other languages, but will as little deviation as possible from the <code>{shiny}</code> way of building apps.</p>
</section>
<section id="here-comes-brochure" class="level2">
<h2 class="anchored" data-anchor-id="here-comes-brochure">Here comes <code>{brochure}</code></h2>
<p>As said on the top of the article, this package is still a work in progress, so you might see some changes in the near future :)</p>
<p>[Disclaimer, again] The way you will build applications with <code>{brochure}</code> is different from the way you usually build <code>{shiny}</code> apps, as we no longer operate under the single page app paradigm. Please keep this in mind and everything should be fine.</p>
<section id="installation" class="level3">
<h3 class="anchored" data-anchor-id="installation">Installation</h3>
<p>You can install the development version of <code>{brochure}</code> with:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ColinFay/brochure"</span>)</span></code></pre></div>
</section>
<section id="minimal-brochure-app" class="level3">
<h3 class="anchored" data-anchor-id="minimal-brochure-app">Minimal <code>{brochure}</code> App</h3>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(shiny)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(brochure)</span></code></pre></div>
<pre><code>## 
## Attaching package: 'brochure'

## The following object is masked from 'package:utils':
## 
##     page</code></pre>
<section id="page" class="level4">
<h4 class="anchored" data-anchor-id="page"><code>page()</code></h4>
<p>A <code>brochureApp</code> is a series of <code>page</code>s that are defined by an <code>href</code> (the path/endpoint where the page is available), a UI and a <code>server</code> function. This is conceptually important: <strong>each page has its own shiny session, its own UI, and its own server</strong>.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brochureApp</span>(</span>
<span id="cb4-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First page</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb4-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,</span>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fluidPage</span>(</span>
<span id="cb4-6">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is my first page"</span>), </span>
<span id="cb4-7">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot"</span>)</span>
<span id="cb4-8">    ),</span>
<span id="cb4-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">server =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output, session){</span>
<span id="cb4-10">      output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPlot</span>({</span>
<span id="cb4-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(iris)</span>
<span id="cb4-12">      })</span>
<span id="cb4-13">    }</span>
<span id="cb4-14">  ), </span>
<span id="cb4-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Second page, without any server-side function</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb4-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/page2"</span>, </span>
<span id="cb4-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fluidPage</span>(</span>
<span id="cb4-19">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is my second page"</span>), </span>
<span id="cb4-20">      tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"There is no server function in this one"</span>)</span>
<span id="cb4-21">    )</span>
<span id="cb4-22">  )</span>
<span id="cb4-23">)</span></code></pre></div>
<p>-&gt; In your browser, you can now navigate to <code>/</code>, and to <code>/page2</code>.</p>
</section>
<section id="redirect" class="level4">
<h4 class="anchored" data-anchor-id="redirect"><code>redirect()</code></h4>
<p>Redirections can be used to redirect from one endpoint to the other:</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brochureApp</span>(</span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb5-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,</span>
<span id="cb5-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb5-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is my first page"</span>)</span>
<span id="cb5-6">    )</span>
<span id="cb5-7">  ),</span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">redirect</span>(</span>
<span id="cb5-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/nothere"</span>,</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span></span>
<span id="cb5-11">  ),</span>
<span id="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">redirect</span>(</span>
<span id="cb5-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/colinfay"</span>,</span>
<span id="cb5-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://colinfay.me"</span></span>
<span id="cb5-15">  )</span>
<span id="cb5-16">)</span></code></pre></div>
<p>-&gt; In your browser, if you got to <code>/nothere</code>, and you’ll be redirected to <code>/</code> .</p>
</section>
</section>
</section>
<section id="req_handlers-res_handlers" class="level2">
<h2 class="anchored" data-anchor-id="req_handlers-res_handlers"><code>req_handlers</code> &amp; <code>res_handlers</code></h2>
<section id="sorry-what" class="level3">
<h3 class="anchored" data-anchor-id="sorry-what">Sorry what?</h3>
<p>This is where things get more interesting.</p>
<p>Each page, and the global app, have a <code>req_handlers</code> and <code>res_handlers</code> parameters, that can take a <strong>list of functions</strong>.</p>
<p>An <code>*_handler</code> is a function that takes as parameter(s):</p>
<ul>
<li><p>For <code>req_handlers</code>, <code>req</code>, which is the request object (see below for when these objects are created). For example <code>function(req){     print(req$PATH_INFO); return(req)}</code>.</p></li>
<li><p>For <code>res_handlers</code> , <code>res</code>, the response object, &amp; <code>req</code> , the request object. For example <code>function(res, req){ print(res$content);     return(res)}</code>.</p></li>
</ul>
<p><code>req_handlers</code> <strong>must</strong> return <code>req</code> &amp; <code>res_handlers</code> <strong>must</strong> return <code>res</code>.</p>
<p>They can be used to register log, or to modify the objects, or any kind of things you can think of. If you are familiar with <code>express.js</code>, <strong>you can think of <code>req_handlers</code> as what</strong> <code>express.js</code> <strong>calls “middleware”</strong>. These functions are run when R is building the HTTP response to send to the browser (i.e, no server code has been run yet), following this process:</p>
<ol type="1">
<li>R receives a <code>GET</code> request from the browser, creating a request object, called <code>req</code></li>
<li>The <code>req_handlers</code> are run using this <code>req</code> object, potentially modifying it</li>
<li>R creates an <code>httpResponse</code>, using this <code>req</code> and how you defined the UI</li>
<li>The <code>res_handlers</code> are run on this <code>httpResponse</code> (first app level <code>res_handlers</code>, then page level <code>res_handlers</code>), , potentially modifying it</li>
<li>The <code>httpResponse</code> is sent back to the browser</li>
</ol>
<p>Note that if any <code>req_handlers</code> returns an <code>httpResponse</code> object, it will be returned to the browser immediately, without any further computation. This early <code>httpResponse</code> will not be passed to the <code>res_handlers</code> of the app or the page. This process can for example be used to send custom <code>httpResponse</code>, as shown below with the <code>healthcheck</code> endpoint.</p>
<p>You can use formulas inside your handlers. <code>.x</code> and <code>..1</code> will be <code>req</code> for <code>req_handlers</code>, <code>.x</code> and <code>..1</code> will be <code>res</code> &amp; <code>.y</code> and <code>..2</code> will be <code>req</code> for <code>res_handlers</code>.</p>
<p>Design pattern side-note: you’d probably want to define the handlers outside of the app, for better code organization (as with <code>log_where</code> below).</p>
</section>
<section id="example-logging-with-req_handlers-and-building-a-healthcheck-point" class="level3">
<h3 class="anchored" data-anchor-id="example-logging-with-req_handlers-and-building-a-healthcheck-point">Example: Logging with <code>req_handlers()</code>, and building a healthcheck point</h3>
<p>In this app, we’ll log to the console every page and the time it is called, using the <code>log_where()</code> function.</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">log_where <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(req){</span>
<span id="cb6-2">  cli<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat_rule</span>(</span>
<span id="cb6-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(</span>
<span id="cb6-4">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%s - %s"</span>, </span>
<span id="cb6-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.time</span>(), </span>
<span id="cb6-6">      req<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>PATH_INFO</span>
<span id="cb6-7">    )</span>
<span id="cb6-8">  )</span>
<span id="cb6-9">  req</span>
<span id="cb6-10">}</span></code></pre></div>
<p>For the sake of organization, we’ll create functions that return pages:</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">nav_links <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ul</span>(</span>
<span id="cb7-2">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(</span>
<span id="cb7-3">    tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"home"</span>), </span>
<span id="cb7-4">  ),</span>
<span id="cb7-5">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(</span>
<span id="cb7-6">    tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/page2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"page2"</span>), </span>
<span id="cb7-7">  ),</span>
<span id="cb7-8">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(</span>
<span id="cb7-9">    tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/contact"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contact"</span>), </span>
<span id="cb7-10">  )</span>
<span id="cb7-11">)</span>
<span id="cb7-12"></span>
<span id="cb7-13">page_1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb7-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb7-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,</span>
<span id="cb7-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(request){</span>
<span id="cb7-17">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb7-18">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is my first page"</span>),</span>
<span id="cb7-19">        nav_links,</span>
<span id="cb7-20">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot"</span>)</span>
<span id="cb7-21">      )</span>
<span id="cb7-22">    },</span>
<span id="cb7-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">server =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output, session){</span>
<span id="cb7-24">      output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPlot</span>({</span>
<span id="cb7-25">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(mtcars)</span>
<span id="cb7-26">      })</span>
<span id="cb7-27">    }</span>
<span id="cb7-28">  )</span>
<span id="cb7-29">}</span>
<span id="cb7-30"></span>
<span id="cb7-31">page_2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb7-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb7-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/page2"</span>,</span>
<span id="cb7-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span>  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(request){</span>
<span id="cb7-35">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb7-36">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is my second page"</span>),</span>
<span id="cb7-37">        nav_links,</span>
<span id="cb7-38">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot"</span>)</span>
<span id="cb7-39">      )</span>
<span id="cb7-40">    }, </span>
<span id="cb7-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">server =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output, session){</span>
<span id="cb7-42">      output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPlot</span>({</span>
<span id="cb7-43">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(mtcars)</span>
<span id="cb7-44">      })</span>
<span id="cb7-45">    }</span>
<span id="cb7-46">  )</span>
<span id="cb7-47">}</span>
<span id="cb7-48"></span>
<span id="cb7-49">page_contact <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb7-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb7-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/contact"</span>,</span>
<span id="cb7-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb7-53">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Contact us"</span>),</span>
<span id="cb7-54">      nav_links,</span>
<span id="cb7-55">      tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ul</span>(</span>
<span id="cb7-56">        tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Here"</span>),</span>
<span id="cb7-57">        tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"There"</span>)</span>
<span id="cb7-58">      )</span>
<span id="cb7-59">    )</span>
<span id="cb7-60">  )</span>
<span id="cb7-61">}</span></code></pre></div>
<p>We’ll also build an <code>healthcheck</code> endpoint that simply returns a <code>httpResponse</code> with the 200 HTTP code.</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reusing the pages from before</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brochureApp</span>(</span>
<span id="cb8-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">req_handlers =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb8-4">    log_where</span>
<span id="cb8-5">  ),</span>
<span id="cb8-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pages</span></span>
<span id="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page_1</span>(),</span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page_2</span>(),</span>
<span id="cb8-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page_contact</span>(),</span>
<span id="cb8-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb8-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/healthcheck"</span>,</span>
<span id="cb8-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># As this is a pure backend exchange, </span></span>
<span id="cb8-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We don't need a UI</span></span>
<span id="cb8-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(), </span>
<span id="cb8-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># As this req_handler returns an httpResponse,</span></span>
<span id="cb8-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This response will be returned directly to the browser, </span></span>
<span id="cb8-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># without passing through the usual dance</span></span>
<span id="cb8-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">req_handlers =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb8-19">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If you have shiny &lt; 1.6.0, you'll need to </span></span>
<span id="cb8-20">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># do shiny:::httpResponse (triple `:`) </span></span>
<span id="cb8-21">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># as it is not exported until 1.6.0.</span></span>
<span id="cb8-22">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Otherwise, see ?shiny::httpResponse</span></span>
<span id="cb8-23">      <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> shiny<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">httpResponse</span>( <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">content =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OK"</span>)</span>
<span id="cb8-24">    )</span>
<span id="cb8-25">  )</span>
<span id="cb8-26">)</span></code></pre></div>
<p>If you navigate to each page, you’ll see this in the console:</p>
<pre><code>Listening on http://127.0.0.1:4879
── 2021-02-17 21:52:16 - / ──────────────────────────────
── 2021-02-17 21:52:17 - /page2 ─────────────────────────
── 2021-02-17 21:52:19 - /contact ───────────────────────</code></pre>
<p>If you go to another R session, you can check that you’ve got a 200 on <code>healthcheck</code> :</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> httr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">GET</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://127.0.0.1:4879/healthcheck"</span>)</span>
<span id="cb10-2">Response [http<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span>.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4879</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>healthcheck]</span>
<span id="cb10-3">  Date<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2021-02-17</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span></span>
<span id="cb10-4">  Status<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span></span>
<span id="cb10-5">  Content<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> text<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>html; charset<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span></span>
<span id="cb10-6">  Size<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> B</span></code></pre></div>
</section>
<section id="handling-cookies-using-res_handlers" class="level3">
<h3 class="anchored" data-anchor-id="handling-cookies-using-res_handlers">Handling cookies using <code>res_handlers</code></h3>
<p><code>res_handlers</code> can be used to set cookies, by adding a <code>Set-Cookie</code> header.</p>
<p>Note that you can parse the cookie using <code>parse_cookie_string</code>.</p>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">brochure<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_cookie_string</span>( <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a=12;session=blabla"</span> )</span></code></pre></div>
<pre><code>##        a  session 
##     "12" "blabla"</code></pre>
<p>In the example, we’ll also use <code>brochure::server_redirect("/")</code>, from the server-side to redirect the user after login.</p>
<div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creating a navlink</span></span>
<span id="cb13-2">nav_links <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ul</span>(</span>
<span id="cb13-3">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(</span>
<span id="cb13-4">    tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"home"</span>), </span>
<span id="cb13-5">  ),</span>
<span id="cb13-6">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(</span>
<span id="cb13-7">    tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/login"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"login"</span>), </span>
<span id="cb13-8">  ),</span>
<span id="cb13-9">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(</span>
<span id="cb13-10">    tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/logout"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logout"</span>), </span>
<span id="cb13-11">  )</span>
<span id="cb13-12">)</span>
<span id="cb13-13"></span>
<span id="cb13-14">home <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb13-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb13-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,</span>
<span id="cb13-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb13-18">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is my first page"</span>), </span>
<span id="cb13-19">      tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"It will contain BROCHURECOOKIE depending on the last page you've visited (/login or /logout)"</span>),</span>
<span id="cb13-20">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">verbatimTextOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cookie"</span>),</span>
<span id="cb13-21">      nav_links</span>
<span id="cb13-22">    ),</span>
<span id="cb13-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">server =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output, session){</span>
<span id="cb13-24">      output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cookie <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPrint</span>({</span>
<span id="cb13-25">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_cookie_string</span>(</span>
<span id="cb13-26">          session<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>request<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>HTTP_COOKIE</span>
<span id="cb13-27">        )</span>
<span id="cb13-28">      })</span>
<span id="cb13-29">    }</span>
<span id="cb13-30">  )</span>
<span id="cb13-31">}</span>
<span id="cb13-32"></span>
<span id="cb13-33">login <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb13-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb13-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/login"</span>,</span>
<span id="cb13-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb13-37">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"You've just logged!"</span>),</span>
<span id="cb13-38">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">verbatimTextOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cookie"</span>),</span>
<span id="cb13-39">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">actionButton</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"redirect"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Redirect to the home page"</span>),</span>
<span id="cb13-40">      nav_links</span>
<span id="cb13-41">    ), </span>
<span id="cb13-42">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">server =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output, session){</span>
<span id="cb13-43">      output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cookie <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPrint</span>({</span>
<span id="cb13-44">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_cookie_string</span>(</span>
<span id="cb13-45">          session<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>request<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>HTTP_COOKIE</span>
<span id="cb13-46">        )</span>
<span id="cb13-47">      })</span>
<span id="cb13-48">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">observeEvent</span>( input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>redirect , {</span>
<span id="cb13-49">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using brochure to redirect to another page</span></span>
<span id="cb13-50">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">server_redirect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>)</span>
<span id="cb13-51">      })</span>
<span id="cb13-52">      </span>
<span id="cb13-53">    },</span>
<span id="cb13-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">res_handlers =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb13-55">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We'll add a cookie here</span></span>
<span id="cb13-56">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(res, req){</span>
<span id="cb13-57">        res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>headers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Set-Cookie</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BROCHURECOOKIE=12; HttpOnly;"</span></span>
<span id="cb13-58">        res</span>
<span id="cb13-59">      }</span>
<span id="cb13-60">    )</span>
<span id="cb13-61">  )</span>
<span id="cb13-62">}</span>
<span id="cb13-63"></span>
<span id="cb13-64">logout <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb13-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb13-66">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/logout"</span>,</span>
<span id="cb13-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb13-68">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"You've logged out"</span>),</span>
<span id="cb13-69">      nav_links,</span>
<span id="cb13-70">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">verbatimTextOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cookie"</span>),</span>
<span id="cb13-71">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">actionButton</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"redirect"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Redirect to the home page"</span>),</span>
<span id="cb13-72">    ), </span>
<span id="cb13-73">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">server =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output, session){</span>
<span id="cb13-74">      output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cookie <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPrint</span>({</span>
<span id="cb13-75">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_cookie_string</span>(</span>
<span id="cb13-76">          session<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>request<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>HTTP_COOKIE</span>
<span id="cb13-77">        )</span>
<span id="cb13-78">      })</span>
<span id="cb13-79">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">observeEvent</span>( input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>redirect , {</span>
<span id="cb13-80">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using brochure to redirect to another page</span></span>
<span id="cb13-81">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">server_redirect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>)</span>
<span id="cb13-82">      })</span>
<span id="cb13-83">    },</span>
<span id="cb13-84">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">res_handlers =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb13-85">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We'll add a cookie here</span></span>
<span id="cb13-86">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(res, req){</span>
<span id="cb13-87">        res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>headers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Set-Cookie</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BROCHURECOOKIE=12; Expires=Wed, 21 Oct 1950 07:28:00 GMT"</span></span>
<span id="cb13-88">        res</span>
<span id="cb13-89">      }</span>
<span id="cb13-90">    )</span>
<span id="cb13-91">  )</span>
<span id="cb13-92">}</span>
<span id="cb13-93"></span>
<span id="cb13-94"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brochureApp</span>(</span>
<span id="cb13-95">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pages</span></span>
<span id="cb13-96">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">home</span>(),</span>
<span id="cb13-97">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">login</span>(),</span>
<span id="cb13-98">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logout</span>()</span>
<span id="cb13-99">)</span></code></pre></div>
</section>
</section>
<section id="random-notes-about-design-patterns" class="level2">
<h2 class="anchored" data-anchor-id="random-notes-about-design-patterns">Random notes about design patterns</h2>
<section id="data-persistence" class="level3">
<h3 class="anchored" data-anchor-id="data-persistence">Data persistence</h3>
<p>Every time you open a new page, a <strong>new shiny session is launched</strong>. This is different from what you usually do when you are building a <code>{shiny}</code> app that works as a single page application. This is no longer the case in <code>{brochure}</code>.</p>
<p>What that means is that <strong>there is no data persistence in R when navigating from one page to the other</strong>. That might seem like a downside, but I believe that it will actually be for the best: <strong>it will make developers think more carefully about the data flow of their application, and allow a faster flush of R sessions</strong>.</p>
<p>That being said, how do we keep track of a user though pages, so that if they do something in a page, it’s available on another page?</p>
<p>To do that, you’d need to add a form of session identifier, like a cookie: this can for example be done using the <a href="https://github.com/colinfay/glouton"><code>{glouton}</code></a> package if you want to manage it with JS only. You can also use the cookie example from before.</p>
<p>You’ll also need a form of backend storage (for example with <a href="https://github.com/r-lib/cachem"><code>{cachem}</code></a> here in the example, but you can also use an external DB like SQLite or MongoDB).</p>
<div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glouton)</span>
<span id="cb14-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creating a storage system</span></span>
<span id="cb14-3">cache_system <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cachem<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cache_disk</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempdir</span>())</span>
<span id="cb14-4"></span>
<span id="cb14-5">nav_links <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ul</span>(</span>
<span id="cb14-6">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(</span>
<span id="cb14-7">    tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"home"</span>), </span>
<span id="cb14-8">  ),</span>
<span id="cb14-9">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">li</span>(</span>
<span id="cb14-10">    tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/page2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"page2"</span>), </span>
<span id="cb14-11">  )</span>
<span id="cb14-12">)</span>
<span id="cb14-13"></span>
<span id="cb14-14">cookie_set <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb14-15">  r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactiveValues</span>()</span>
<span id="cb14-16">  </span>
<span id="cb14-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">observeEvent</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, {</span>
<span id="cb14-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fetch the cookies using {glouton}</span></span>
<span id="cb14-19">    r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cook <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fetch_cookies</span>()</span>
<span id="cb14-20">    </span>
<span id="cb14-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If there is no stored cookie for {brochure}, we generate it</span></span>
<span id="cb14-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cook<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>brochure_cookie)){</span>
<span id="cb14-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate a random id</span></span>
<span id="cb14-24">      session_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> digest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sha1</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.time</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(letters, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)))</span>
<span id="cb14-25">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add this id as a cookie</span></span>
<span id="cb14-26">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_cookie</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brochure_cookie"</span>, session_id)</span>
<span id="cb14-27">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store in in the reactiveValues list</span></span>
<span id="cb14-28">      r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cook<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>brochure_cookie <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> session_id</span>
<span id="cb14-29">    }</span>
<span id="cb14-30">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For debugging purpose</span></span>
<span id="cb14-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cook<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>brochure_cookie )</span>
<span id="cb14-32">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">once =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb14-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(r)</span>
<span id="cb14-34">}</span>
<span id="cb14-35"></span>
<span id="cb14-36">page_1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb14-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb14-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,</span>
<span id="cb14-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb14-40">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is my first page"</span>),</span>
<span id="cb14-41">      nav_links,</span>
<span id="cb14-42">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The text enter on page 1 will be available on page 2, using</span></span>
<span id="cb14-43">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a session cookie and a storage system</span></span>
<span id="cb14-44">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">textInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"textenter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Enter a text"</span>), </span>
<span id="cb14-45">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">actionButton</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"save"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Save my text and go to page2"</span>)</span>
<span id="cb14-46">    ),</span>
<span id="cb14-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">server =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output, session){</span>
<span id="cb14-48">      r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cookie_set</span>()</span>
<span id="cb14-49">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">observeEvent</span>( input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>save , {</span>
<span id="cb14-50">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use the session id to save on the cache system</span></span>
<span id="cb14-51">        cache_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set</span>(</span>
<span id="cb14-52">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb14-53">            r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cook<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>brochure_cookie,</span>
<span id="cb14-54">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span></span>
<span id="cb14-55">          ),</span>
<span id="cb14-56">          input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>textenter</span>
<span id="cb14-57">        )</span>
<span id="cb14-58">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">server_redirect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/page2"</span>)</span>
<span id="cb14-59">      })</span>
<span id="cb14-60">    }</span>
<span id="cb14-61">  )</span>
<span id="cb14-62">}</span>
<span id="cb14-63"></span>
<span id="cb14-64">page_2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb14-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page</span>(</span>
<span id="cb14-66">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/page2"</span>,</span>
<span id="cb14-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ui =</span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tagList</span>(</span>
<span id="cb14-68">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h1</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is my second page"</span>),</span>
<span id="cb14-69">      nav_links,</span>
<span id="cb14-70">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The text enter on page 1 will be available here, reading</span></span>
<span id="cb14-71">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the storage system</span></span>
<span id="cb14-72">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">verbatimTextOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"textdisplay"</span>)</span>
<span id="cb14-73">    ), </span>
<span id="cb14-74">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">server =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output, session){</span>
<span id="cb14-75">      r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cookie_set</span>()</span>
<span id="cb14-76">      output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>textdisplay <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPrint</span>({</span>
<span id="cb14-77">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Getting the content value based on the session cookie</span></span>
<span id="cb14-78">        cache_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get</span>(</span>
<span id="cb14-79">          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb14-80">            r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cook<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>brochure_cookie,</span>
<span id="cb14-81">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span></span>
<span id="cb14-82">          )</span>
<span id="cb14-83">        )</span>
<span id="cb14-84">      })</span>
<span id="cb14-85">    }</span>
<span id="cb14-86">  )</span>
<span id="cb14-87">}</span>
<span id="cb14-88"></span>
<span id="cb14-89"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brochureApp</span>(</span>
<span id="cb14-90">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting {glouton} globally</span></span>
<span id="cb14-91">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_glouton</span>(),</span>
<span id="cb14-92">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pages</span></span>
<span id="cb14-93">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page_1</span>(),</span>
<span id="cb14-94">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">page_2</span>()</span>
<span id="cb14-95">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Redirections</span></span>
<span id="cb14-96">)</span></code></pre></div>
</section>
<section id="working-with-golem" class="level3">
<h3 class="anchored" data-anchor-id="working-with-golem">Working with golem</h3>
<p><code>{golem}</code> is a small project that is starting to get some traction in the <code>{shiny}</code> world. I’m still not sure of its relevance, but I’ve heard that some of you are using it for some reasons, so I suppose you might want to make <code>{brochure}</code> work inside a <code>{golem}</code> based app.</p>
<p>To adapt your <code>{golem}</code> based application to <code>{brochure}</code>, here are the two steps to follow:</p>
<ul>
<li><p>Remove the app_server.R file, and the top of app_ui =&gt; You’ll still need <code>golem_add_external_resources()</code>.</p></li>
<li><p>Build the pages inside separate R scripts, following the example from this <code>README</code>.</p></li>
</ul>
<!-- -->
<pre><code>.
├── DESCRIPTION
├── NAMESPACE
├── R
│   ├── app_config.R
│   ├── home.R ### YOUR PAGE 
│   ├── login.R ### YOUR PAGE 
│   ├── logout.R ### YOUR PAGE 
│   └── run_app.R ### YOUR PAGE 
├── dev
│   ├── 01_start.R
│   ├── 02_dev.R
│   ├── 03_deploy.R
│   └── run_dev.R
├── inst
│   ├── app
│   │   └── www
│   │       ├── favicon.ico
│   └── golem-config.yml
├── man
│   └── run_app.Rd</code></pre>
<ul>
<li>Replace <code>shinyApp</code> with <code>brochureApp</code> in <code>run_app()</code>, add the external resources, then your pages.</li>
</ul>
<!-- end list -->
<div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">run_app <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(</span>
<span id="cb16-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">onStart =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb16-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(), </span>
<span id="cb16-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">enableBookmarking =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb16-5">  ...</span>
<span id="cb16-6">) {</span>
<span id="cb16-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with_golem_options</span>(</span>
<span id="cb16-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">app =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brochureApp</span>(</span>
<span id="cb16-9">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Putting the resources here</span></span>
<span id="cb16-10">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">golem_add_external_resources</span>(),</span>
<span id="cb16-11">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">home</span>(),</span>
<span id="cb16-12">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">login</span>(),</span>
<span id="cb16-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logout</span>(),</span>
<span id="cb16-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">onStart =</span> onStart,</span>
<span id="cb16-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> options, </span>
<span id="cb16-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">enableBookmarking =</span> enableBookmarking</span>
<span id="cb16-17">    ), </span>
<span id="cb16-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">golem_opts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(...)</span>
<span id="cb16-19">  )</span>
<span id="cb16-20">}</span></code></pre></div>
</section>
</section>
<section id="improvement-further-work" class="level2">
<h2 class="anchored" data-anchor-id="improvement-further-work">Improvement &amp; further work</h2>
<ul>
<li><p>Testers and apps. If you want to give <code>{brochure}</code> a shot, please do! It’s still a young project that needs more testing, so if you feel like you want to build an app with it, please do. I’d be happy to help you if you have some questions so feel free to reach me on <a href="https://twitter.com/_ColinFay">Twitter</a>, or comment on the post below.</p></li>
<li><p>Functions to manipulate cookies (create and delete them).</p></li>
<li><p>Built-in handlers for <code>req</code> &amp; <code>res</code> that will answer common use cases.</p></li>
<li><p>Allow a way to implement <code>:id:</code> in the URL, the way <code>express.js</code> does: you can set a route as <code>/profile/:id:</code> and the app will answers to <code>/profile/colin</code>, <code>/profile/jean</code>, <code>/profile/whatever</code>, setting the <code>id</code> as a variable that can be reused in the response. In the context of <code>{shiny}</code>, this would of course be available the server.</p></li>
<li><p>Auto build a proxy based on the application. This will allow to create one R session by page, then create a proxy that will plug each endpoint to an R session, allowing a little bit of scalability “for free”.</p></li>
</ul>
<p>If you have any idea or question, please <a href="https://github.com/ColinFay/brochure/issues">open an issue</a> on the GitHub repo!</p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/brochure-r-package/";
  this.page.identifier = "/brochure-r-package";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <guid>https://colinfay.me/posts/brochure-r-package.html</guid>
  <pubDate>Thu, 18 Feb 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2020-09 with R</title>
  <link>https://colinfay.me/posts/aoc-2020-09.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2020-09 with R &amp; Neo4j.</p>
<p>[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.</p>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<p>We have a <code>XMAS</code> code, which:</p>
<ul>
<li><p>Start with 25 numbers</p></li>
<li><p>Any following number should be the sum of one of the possible pairs from the previous 25</p></li>
<li><p>In step 2, find the consecutive set of number that sums to the result of part 1, and add its min and max</p></li>
</ul>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2020/day/9" class="uri">https://adventofcode.com/2020/day/9</a>.</p>
</section>
<section id="r-solution" class="level2">
<h2 class="anchored" data-anchor-id="r-solution">R solution</h2>
<section id="part-one" class="level3">
<h3 class="anchored" data-anchor-id="part-one">Part one</h3>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reading the data</span></span>
<span id="cb1-2">input <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.delim</span>(</span>
<span id="cb1-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>,</span>
<span id="cb1-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-09-aoc.txt"</span>,</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb1-7">)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">26</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1)){</span>
<span id="cb1-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The 25 starting numbers</span></span>
<span id="cb1-11">  to_get <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The 26th number should be between the</span></span>
<span id="cb1-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sum of the two lowest number and the sum</span></span>
<span id="cb1-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># of the two highest</span></span>
<span id="cb1-16">  lower_bond <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(</span>
<span id="cb1-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(</span>
<span id="cb1-18">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1[to_get]),</span>
<span id="cb1-19">      <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-20">    )</span>
<span id="cb1-21">  )</span>
<span id="cb1-22"></span>
<span id="cb1-23">  higher_bond <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(</span>
<span id="cb1-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span>(</span>
<span id="cb1-25">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(</span>
<span id="cb1-26">        input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1[to_get]</span>
<span id="cb1-27">      ),</span>
<span id="cb1-28">      <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-29">    )</span>
<span id="cb1-30">  )</span>
<span id="cb1-31"></span>
<span id="cb1-32">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (</span>
<span id="cb1-33">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1[i], lower_bond, higher_bond)</span>
<span id="cb1-34">  ){</span>
<span id="cb1-35">    output1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;&lt;-</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1[i]</span>
<span id="cb1-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(output1)</span>
<span id="cb1-37">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb1-38"></span>
<span id="cb1-39">  }</span>
<span id="cb1-40">}</span></code></pre></div>
<pre><code>## [1] 530627549</code></pre>
</section>
<section id="part-two" class="level3">
<h3 class="anchored" data-anchor-id="part-two">Part Two</h3>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1)){</span>
<span id="cb3-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We will try to find any sequence in 1:n, then 2:n,</span></span>
<span id="cb3-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># then 3:n, etc, that sums to output1</span></span>
<span id="cb3-4">  selection <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1[i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1)]</span>
<span id="cb3-5">  c_s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(selection) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> output1</span>
<span id="cb3-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (</span>
<span id="cb3-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span>(c_s)</span>
<span id="cb3-8">  ){</span>
<span id="cb3-9">    bounds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> selection[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(c_s)]</span>
<span id="cb3-10"></span>
<span id="cb3-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(</span>
<span id="cb3-12">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(</span>
<span id="cb3-13">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(bounds),</span>
<span id="cb3-14">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(bounds)</span>
<span id="cb3-15">      )</span>
<span id="cb3-16">    )</span>
<span id="cb3-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb3-18">  }</span>
<span id="cb3-19">}</span></code></pre></div>
<pre><code>## [1] 77730285</code></pre>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2020-09/";
  this.page.identifier = "/aoc-09";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2020-09.html</guid>
  <pubDate>Wed, 09 Dec 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2020-08 with R</title>
  <link>https://colinfay.me/posts/aoc-2020-08.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2020-08 with R.</p>
<p>[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.</p>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<p>Input looks like this</p>
<pre><code>nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6</code></pre>
<ul>
<li><p>There is a global var called <code>accumulator</code>, starting at 0</p></li>
<li><p><code>acc n</code> increases or decreases <code>accumulator</code> by <code>n</code>, and goes to next row</p></li>
<li><p><code>jmp n</code> jumps to a its position + <code>n</code></p></li>
<li><p><code>nop</code> does nothing but going to the next execution</p></li>
<li><p>If the program tries to execute a row twice, we stop, and the eturned value is the value just before that</p></li>
</ul>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2020/day/8" class="uri">https://adventofcode.com/2020/day/8</a>.</p>
</section>
<section id="r-solution" class="level2">
<h2 class="anchored" data-anchor-id="r-solution">R solution</h2>
<section id="part-one" class="level3">
<h3 class="anchored" data-anchor-id="part-one">Part one</h3>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reading the data</span></span>
<span id="cb2-2">input <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.delim</span>(</span>
<span id="cb2-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>,</span>
<span id="cb2-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-08-aoc.txt"</span>,</span>
<span id="cb2-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb2-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-7">)</span>
<span id="cb2-8"></span>
<span id="cb2-9">give_me_a_break <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input){</span>
<span id="cb2-10"></span>
<span id="cb2-11">  accumultor <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-12">  current_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-13">  done_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>()</span>
<span id="cb2-14">  exit_code <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-15"></span>
<span id="cb2-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>){</span>
<span id="cb2-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (current_row <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> done_row){</span>
<span id="cb2-18">      exit_code <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-19">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb2-20">    }</span>
<span id="cb2-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (current_row <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(input)){</span>
<span id="cb2-22">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb2-23">    }</span>
<span id="cb2-24">    done_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(done_row, current_row)</span>
<span id="cb2-25">    curr_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> input[current_row, ]</span>
<span id="cb2-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (curr_row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nop"</span>) {</span>
<span id="cb2-27">      current_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> current_row <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-28">    }</span>
<span id="cb2-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (curr_row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acc"</span>) {</span>
<span id="cb2-30">      accumultor <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> accumultor <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> curr_row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V2</span>
<span id="cb2-31">      current_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> current_row <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-32">    }</span>
<span id="cb2-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (curr_row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jmp"</span>) {</span>
<span id="cb2-34">      current_row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> current_row <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> curr_row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V2</span>
<span id="cb2-35">    }</span>
<span id="cb2-36">  }</span>
<span id="cb2-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(</span>
<span id="cb2-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb2-39">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accumultor =</span> accumultor,</span>
<span id="cb2-40">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exit_code =</span> exit_code</span>
<span id="cb2-41">    )</span>
<span id="cb2-42">  )</span>
<span id="cb2-43">}</span>
<span id="cb2-44"></span>
<span id="cb2-45"></span>
<span id="cb2-46"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">give_me_a_break</span>(input)</span></code></pre></div>
<pre><code>## $accumultor
## [1] 1528
##
## $exit_code
## [1] 1</code></pre>
</section>
<section id="part-two" class="level3">
<h3 class="anchored" data-anchor-id="part-two">Part Two</h3>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nop"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jmp"</span>))){</span>
<span id="cb4-2"></span>
<span id="cb4-3">  modified_ipt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> input</span>
<span id="cb4-4"></span>
<span id="cb4-5">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (modified_ipt[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V1'</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nop"</span>){</span>
<span id="cb4-6">    modified_ipt[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V1'</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"jmp"</span></span>
<span id="cb4-7">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb4-8">    modified_ipt[i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V1'</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nop"</span></span>
<span id="cb4-9">  }</span>
<span id="cb4-10"></span>
<span id="cb4-11">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">give_me_a_break</span>(modified_ipt)</span>
<span id="cb4-12"></span>
<span id="cb4-13">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>exit_code <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>){</span>
<span id="cb4-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>accumultor)</span>
<span id="cb4-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb4-16">  }</span>
<span id="cb4-17"></span>
<span id="cb4-18">}</span></code></pre></div>
<pre><code>## [1] 640</code></pre>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2020-08/";
  this.page.identifier = "/aoc-08";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2020-08.html</guid>
  <pubDate>Tue, 08 Dec 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2020-07 with R &amp; Neo4j</title>
  <link>https://colinfay.me/posts/aoc-2020-07.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2020-07 with R &amp; Neo4j.</p>
<p>[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.</p>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2020/day/7" class="uri">https://adventofcode.com/2020/day/7</a>.</p>
</section>
<section id="r-neo4j-solution" class="level2">
<h2 class="anchored" data-anchor-id="r-neo4j-solution">R &amp; Neo4j solution</h2>
<p>As today’s options was a graph challenge, I decided to play a little bit with R and Neo4J.</p>
<section id="part-one" class="level3">
<h3 class="anchored" data-anchor-id="part-one">Part one</h3>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
<pre><code>## ── Attaching packages ───────────── tidyverse 1.3.0 ──

## ✓ ggplot2 3.3.0     ✓ purrr   0.3.4
## ✓ tibble  3.0.1     ✓ dplyr   1.0.2
## ✓ tidyr   1.0.3     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0

## ── Conflicts ──────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()</code></pre>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reading the data</span></span>
<span id="cb3-2">input <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.delim</span>(</span>
<span id="cb3-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-07-aoc.txt"</span>,</span>
<span id="cb3-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb3-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb3-6">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>()</span>
<span id="cb3-7"></span>
<span id="cb3-8"></span>
<span id="cb3-9">clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> input <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extracting the origin</span></span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">origin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(.*) bags .*"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>, V1),</span>
<span id="cb3-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contain =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".* contain (.*) bags*</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">."</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>, V1),</span>
<span id="cb3-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contain =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(contain, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(.x, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>))[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])</span>
<span id="cb3-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(contain) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extracting what they can contain</span></span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_contain =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".*([0-9]+).*"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>, contain),</span>
<span id="cb3-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_contain =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb3-21">      n_contain <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"no other"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>,</span>
<span id="cb3-22">      <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>  n_contain</span>
<span id="cb3-23">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(),</span>
<span id="cb3-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contain =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" bags*"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, contain),</span>
<span id="cb3-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contain =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[0-9]*"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, contain),</span>
<span id="cb3-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contain =</span> stringr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_trim</span>(contain)</span>
<span id="cb3-27">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-28">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keeping only the needed elements</span></span>
<span id="cb3-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(</span>
<span id="cb3-30">    origin, contain, n_contain</span>
<span id="cb3-31">  )</span>
<span id="cb3-32">clean</span></code></pre></div>
<pre><code>## # A tibble: 1,395 x 3
##    origin         contain         n_contain
##    &lt;chr&gt;          &lt;chr&gt;           &lt;chr&gt;
##  1 clear maroon   dull lavender   1
##  2 wavy turquoise vibrant magenta 4
##  3 wavy turquoise light violet    4
##  4 wavy turquoise bright gold     5
##  5 wavy turquoise faded black     2
##  6 wavy beige     plaid magenta   3
##  7 wavy beige     wavy lime       3
##  8 wavy beige     clear turquoise 2
##  9 wavy beige     muted cyan      3
## 10 mirrored black plaid red       1
## # … with 1,385 more rows</code></pre>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Launching the container</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"docker run --rm --name neo4j --env NEO4J_AUTH=neo4j/password --publish=7687:7687 --publish=7474:7474 -v $(pwd):/var/lib/neo4j/import -d neo4j:3.4"</span>)</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(neo4r)</span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Connecting to the Neo4j API</span></span>
<span id="cb5-7">con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> neo4j_api<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>(</span>
<span id="cb5-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://localhost:7474"</span>,</span>
<span id="cb5-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">user =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"neo4j"</span>,</span>
<span id="cb5-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">password =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"password"</span></span>
<span id="cb5-11">)</span>
<span id="cb5-12"></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Waiting for the container to be ready</span></span>
<span id="cb5-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb5-15"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(con<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ping</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>){</span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb5-17">}</span></code></pre></div>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writing CSV for Neo4J</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb6-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nodes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(clean<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>origin, clean<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>contain)</span>
<span id="cb6-4">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aoc07_nodes.csv"</span>)</span>
<span id="cb6-6">clean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aoc07_relationships.csv"</span>)</span></code></pre></div>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adding constraint of uniqueness on names</span></span>
<span id="cb7-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CREATE CONSTRAINT ON (origin:origin) ASSERT origin.name IS UNIQUE;"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call_neo4j</span>(con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_stats =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<pre><code>## No data returned.

## # A tibble: 12 x 2
##    type                  value
##    &lt;chr&gt;                 &lt;dbl&gt;
##  1 contains_updates          1
##  2 nodes_created             0
##  3 nodes_deleted             0
##  4 properties_set            0
##  5 relationships_created     0
##  6 relationship_deleted      0
##  7 labels_added              0
##  8 labels_removed            0
##  9 indexes_added             0
## 10 indexes_removed           0
## 11 constraints_added         1
## 12 constraints_removed       0</code></pre>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CREATE CONSTRAINT ON (contain:contain) ASSERT contain.name IS UNIQUE;"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call_neo4j</span>(con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_stats =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<pre><code>## No data returned.

## # A tibble: 12 x 2
##    type                  value
##    &lt;chr&gt;                 &lt;dbl&gt;
##  1 contains_updates          1
##  2 nodes_created             0
##  3 nodes_deleted             0
##  4 properties_set            0
##  5 relationships_created     0
##  6 relationship_deleted      0
##  7 labels_added              0
##  8 labels_removed            0
##  9 indexes_added             0
## 10 indexes_removed           0
## 11 constraints_added         1
## 12 constraints_removed       0</code></pre>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the nodes</span></span>
<span id="cb11-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'LOAD CSV WITH HEADERS FROM "file:///aoc07_nodes.csv" AS csvLine</span></span>
<span id="cb11-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">MERGE (:bag {name : csvLine.nodes} ) '</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call_neo4j</span>(con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_stats =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<pre><code>## No data returned.

## # A tibble: 12 x 2
##    type                  value
##    &lt;chr&gt;                 &lt;dbl&gt;
##  1 contains_updates          1
##  2 nodes_created           594
##  3 nodes_deleted             0
##  4 properties_set          594
##  5 relationships_created     0
##  6 relationship_deleted      0
##  7 labels_added            594
##  8 labels_removed            0
##  9 indexes_added             0
## 10 indexes_removed           0
## 11 constraints_added         0
## 12 constraints_removed       0</code></pre>
<div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb13-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># # Load the relationships</span></span>
<span id="cb13-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'LOAD CSV WITH HEADERS FROM "file:///aoc07_relationships.csv" AS csvLine</span></span>
<span id="cb13-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">MATCH (c:bag {name : csvLine.contain})</span></span>
<span id="cb13-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">MATCH (o:bag {name : csvLine.origin})</span></span>
<span id="cb13-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">MERGE (o) -[:CAN_CONTAIN {n_contain: csvLine.n_contain}]-&gt;(c);'</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call_neo4j</span>(con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_stats =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<pre><code>## No data returned.

## # A tibble: 12 x 2
##    type                  value
##    &lt;chr&gt;                 &lt;dbl&gt;
##  1 contains_updates          1
##  2 nodes_created             0
##  3 nodes_deleted             0
##  4 properties_set         1385
##  5 relationships_created  1385
##  6 relationship_deleted      0
##  7 labels_added              0
##  8 labels_removed            0
##  9 indexes_added             0
## 10 indexes_removed           0
## 11 constraints_added         0
## 12 constraints_removed       0</code></pre>
<div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Count all the bags that can eventually contain a shiny gold ones</span></span>
<span id="cb15-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MATCH (b:bag)-[:CAN_CONTAIN*1..]-&gt; (:bag {name:"shiny gold"}) RETURN count( DISTINCT b) AS count'</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call_neo4j</span>(con)</span></code></pre></div>
<pre><code>## $count
## # A tibble: 1 x 1
##   value
##   &lt;int&gt;
## 1   126
##
## attr(,"class")
## [1] "neo"  "list"</code></pre>
</section>
<section id="part-two" class="level3">
<h3 class="anchored" data-anchor-id="part-two">Part two</h3>
<div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MATCH (:bag {name:"shiny gold"})-[r:CAN_CONTAIN*1..]-&gt; (:bag) WITH reduce(tot = 1, i IN r| tot * toInteger(i.n_contain)) AS total RETURN sum(total) AS total'</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call_neo4j</span>(con)</span></code></pre></div>
<pre><code>## $total
## # A tibble: 1 x 1
##    value
##    &lt;int&gt;
## 1 220149
##
## attr(,"class")
## [1] "neo"  "list"</code></pre>
<div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Removing the container</span></span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"docker kill neo4j"</span>)</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlink</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aoc07_nodes.csv"</span>)</span>
<span id="cb19-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlink</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aoc07_relationships.csv"</span>)</span></code></pre></div>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2020-07/";
  this.page.identifier = "/aoc-07";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2020-07.html</guid>
  <pubDate>Mon, 07 Dec 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2020-06 with R</title>
  <link>https://colinfay.me/posts/aoc-2020-06.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2020-06 with R.</p>
<p>[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.</p>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2020/day/6" class="uri">https://adventofcode.com/2020/day/6</a>.</p>
</section>
<section id="r-solution" class="level2">
<h2 class="anchored" data-anchor-id="r-solution">R solution</h2>
<section id="part-one" class="level3">
<h3 class="anchored" data-anchor-id="part-one">Part one</h3>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magrittr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read the data</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-06-aoc.txt"</span> ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pasting everything into one big character string</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># splitting where there are two \n\n (new lines)</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-9">  .[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> {</span>
<span id="cb1-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Removing the new lines of each group</span></span>
<span id="cb1-12">    .x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, .x)</span>
<span id="cb1-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Splitting the string</span></span>
<span id="cb1-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(.x, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-15">      .[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-16">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Computing the number of unique answer</span></span>
<span id="cb1-17">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-18">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>()</span>
<span id="cb1-19">  }) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span></code></pre></div>
<pre><code>## [1] 6534</code></pre>
</section>
<section id="part-two" class="level3">
<h3 class="anchored" data-anchor-id="part-two">Part two</h3>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read the data</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-06-aoc.txt"</span> ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pasting everything into one big character string</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># splitting where there are two \n\n (new lines)</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-8">  .[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> {</span>
<span id="cb3-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Removing the new lines of each group</span></span>
<span id="cb3-11">    x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(.x, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb3-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Getting the group size</span></span>
<span id="cb3-13">    ngroup <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)</span>
<span id="cb3-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Splitting the original string</span></span>
<span id="cb3-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(.x, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-16">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-17">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> .</span>
<span id="cb3-18">      ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-19">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Counting the number of occurrences of each letter</span></span>
<span id="cb3-20">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-21">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keeping only the questions that occur in all</span></span>
<span id="cb3-22">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> ngroup) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-23">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>()</span>
<span id="cb3-24">  }) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span></code></pre></div>
<pre><code>## [1] 3402</code></pre>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2020-06/";
  this.page.identifier = "/aoc-06";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2020-06.html</guid>
  <pubDate>Sun, 06 Dec 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2020-05 with R</title>
  <link>https://colinfay.me/posts/aoc-2020-05.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2020-05 with R.</p>
<p>[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.</p>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2020/day/5" class="uri">https://adventofcode.com/2020/day/5</a>.</p>
</section>
<section id="r-solution" class="level2">
<h2 class="anchored" data-anchor-id="r-solution">R solution</h2>
<section id="part-one" class="level3">
<h3 class="anchored" data-anchor-id="part-one">Part one</h3>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magrittr)</span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read the data</span></span>
<span id="cb1-3">ipt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.table</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-05-aoc.txt"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magrittr)</span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For chunk2</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stackoverflow)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb1-9">get_chunks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(vec, ext, nms){</span>
<span id="cb1-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># split the vec in two, set the name, and extract the correct chunk</span></span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chunk2</span>(vec, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</span>(nms) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pluck</span>(ext)</span>
<span id="cb1-14">}</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prefilling the functions with the correct nms</span></span>
<span id="cb1-17">get_bf<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">partial</span>(get_chunks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nms =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>))</span>
<span id="cb1-18">get_lr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">partial</span>(get_chunks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nms =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>))</span>
<span id="cb1-19"></span>
<span id="cb1-20"></span>
<span id="cb1-21">get_all <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(entry){</span>
<span id="cb1-22">  entry <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(entry, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb1-23"></span>
<span id="cb1-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Doing the get_bf on the 7 first letters</span></span>
<span id="cb1-25">  row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_bf</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">127</span>, entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_bf</span>(entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_bf</span>(entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_bf</span>(entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_bf</span>(entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_bf</span>(entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_bf</span>(entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>])</span>
<span id="cb1-32"></span>
<span id="cb1-33">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Doing the get_lr on the 3 last letters</span></span>
<span id="cb1-34">  column <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_lr</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_lr</span>(entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_lr</span>(entry[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>])</span>
<span id="cb1-37"></span>
<span id="cb1-38">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Computing the seat id</span></span>
<span id="cb1-39">  seatid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (row <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> column</span>
<span id="cb1-40"></span>
<span id="cb1-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(seatid)</span>
<span id="cb1-42">}</span>
<span id="cb1-43"></span>
<span id="cb1-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Getting the max</span></span>
<span id="cb1-45"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(ipt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1, get_all) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>()</span></code></pre></div>
<pre><code>## [1] 864</code></pre>
</section>
<section id="part-two" class="level3">
<h3 class="anchored" data-anchor-id="part-two">Part two</h3>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extracting all the ids</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(ipt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V1, get_all) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sorting them in order and turning into a df</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> .</span>
<span id="cb3-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Computing the diff between all ids</span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lag</span>(x),</span>
<span id="cb3-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff =</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y</span>
<span id="cb3-13">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># My passport ID will be the one between the IDS</span></span>
<span id="cb3-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># of diff != 1</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb3-17">    diff <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-18">  )</span></code></pre></div>
<pre><code>##     x   y diff
## 1 740 738    2</code></pre>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2020-05/";
  this.page.identifier = "/aoc-05";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2020-05.html</guid>
  <pubDate>Sat, 05 Dec 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2020-04 with R</title>
  <link>https://colinfay.me/posts/aoc-2020-04.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2020-04 with R.</p>
<p>[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.</p>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<section id="step-1" class="level3">
<h3 class="anchored" data-anchor-id="step-1">Step 1</h3>
<ul>
<li>Inputs are of the form of</li>
</ul>
<!-- end list -->
<pre><code>ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm

iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929

hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm

hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in</code></pre>
<p>Where each “passport” is separated by a new line.</p>
<p>To be considered valid, a passport has to have all of <code>c("byr","iyr","eyr", "hgt", "hcl", "ecl", "pid")</code>.</p>
</section>
<section id="step-2" class="level3">
<h3 class="anchored" data-anchor-id="step-2">Step 2</h3>
<p>To be considered valid, a passport has to have all of <code>c("byr","iyr","eyr", "hgt", "hcl", "ecl", "pid")</code>, plus, each entry has to validate a series of rules.</p>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2020/day/4" class="uri">https://adventofcode.com/2020/day/4</a>.</p>
</section>
</section>
<section id="r-solution" class="level2">
<h2 class="anchored" data-anchor-id="r-solution">R solution</h2>
<section id="part-one" class="level3">
<h3 class="anchored" data-anchor-id="part-one">Part one</h3>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magrittr)</span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read the data</span></span>
<span id="cb2-3">ipt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-04-aoc.txt"</span> ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pasting everything into one big character string</span></span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># splitting where there are two \n\n (new lines)</span></span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-8">  .[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Removing the new lines</span></span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>, .)</span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-13"></span>
<span id="cb2-14">is_north_pole_valid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(</span>
<span id="cb2-15">  x,</span>
<span id="cb2-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">patt =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"byr"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iyr"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eyr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hgt"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hcl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ecl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pid"</span>)</span>
<span id="cb2-17">){</span>
<span id="cb2-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_lgl</span>(</span>
<span id="cb2-19">    patt,</span>
<span id="cb2-20">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(.x, x)</span>
<span id="cb2-21">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>()</span>
<span id="cb2-22">}</span>
<span id="cb2-23"></span>
<span id="cb2-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(</span>
<span id="cb2-25">  ipt, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>{</span>
<span id="cb2-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is_north_pole_valid</span>(.x)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-28">  }</span>
<span id="cb2-29">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span></code></pre></div>
<pre><code>## [1] 204</code></pre>
</section>
<section id="part-two" class="level3">
<h3 class="anchored" data-anchor-id="part-two">Part two</h3>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">ipt <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove the unvalid passport</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">discard</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is_north_pole_valid</span>(.x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split stuff</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>{</span>
<span id="cb4-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Now, the heavy lifting</span></span>
<span id="cb4-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># we have a string of key:value pairs,</span></span>
<span id="cb4-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># split it</span></span>
<span id="cb4-10">    vals <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_chr</span>(.x, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"([^:]*):(.*)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">2"</span>, .x))</span>
<span id="cb4-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(vals) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_chr</span>(.x, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"([^:]*):(.*)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>, .x))</span>
<span id="cb4-12"></span>
<span id="cb4-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># checking that values are inside range</span></span>
<span id="cb4-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"byr"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1920</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2002</span>) ) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iyr"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2010</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>) ) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eyr"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2030</span>) ) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If height is in inch, check the range</span></span>
<span id="cb4-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ( <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"in"</span>, vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hgt"</span>]) ) {</span>
<span id="cb4-19">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"in"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hgt"</span>]), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">59</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">76</span>)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-20">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>, vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hgt"</span>])) {</span>
<span id="cb4-21">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">between</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hgt"</span>]), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">193</span>)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-22">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb4-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># No unit provided</span></span>
<span id="cb4-24">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-25">    }</span>
<span id="cb4-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^#[a-f0-9]{6}$"</span>, vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hcl"</span>])) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span> vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ecl"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amb"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"brn"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gry"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grn"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hzl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"oth"</span>)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-28">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^[0-9]{9}$"</span>, vals[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pid"</span>])) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-30">  }</span>
<span id="cb4-31">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span></code></pre></div>
<pre><code>## [1] 179</code></pre>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2020-04/";
  this.page.identifier = "/aoc-04";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2020-04.html</guid>
  <pubDate>Fri, 04 Dec 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Advent of Code 2020-03 with R</title>
  <link>https://colinfay.me/posts/aoc-2020-03.html</link>
  <description><![CDATA[ 




<p>Solving Advent of Code 2020-03 with R</p>
<p>[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.</p>
<section id="instructions" class="level2">
<h2 class="anchored" data-anchor-id="instructions">Instructions</h2>
<section id="step-1" class="level3">
<h3 class="anchored" data-anchor-id="step-1">Step 1</h3>
<ul>
<li><p>The input is a map (x, y), where <code>.</code> is a square, <code>#</code> is a tree.</p></li>
<li><p>We can move this way: 3 right (x + 3), 1 down (y - 1).</p></li>
<li><p>The y repeats itself indefinitely.</p></li>
<li><p>We start from the top left corner (coordinate x = nrow(map), and y = 1).</p></li>
<li><p>We move until we’ve reached x == 1, i.e by taking nrow(map) steps.</p></li>
<li><p>How many trees <code>#</code> have we met along the way?</p></li>
</ul>
</section>
<section id="step-2" class="level3">
<h3 class="anchored" data-anchor-id="step-2">Step 2</h3>
<p>Repeat this but modify the step schema.</p>
<p>Find the complete instructions at: <a href="https://adventofcode.com/2020/day/3" class="uri">https://adventofcode.com/2020/day/3</a>.</p>
</section>
</section>
<section id="r-solution" class="level2">
<h2 class="anchored" data-anchor-id="r-solution">R solution</h2>
<section id="part-one" class="level3">
<h3 class="anchored" data-anchor-id="part-one">Part one</h3>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb1-2"></span>
<span id="cb1-3">n_trees <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by_x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by_y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb1-6">){</span>
<span id="cb1-7">  ipt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.delim</span>( <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2020-03-aoc.txt"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb1-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Building the path</span></span>
<span id="cb1-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x need to go from 1 to nrow(ipt), 1 by 1</span></span>
<span id="cb1-10">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ipt), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> by_x)</span>
<span id="cb1-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y needs to start at 1, and go by 3 steps until we have length(x) steps</span></span>
<span id="cb1-12">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> by_y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x))</span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb1-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x,</span>
<span id="cb1-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y</span>
<span id="cb1-16">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmap_dbl</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> {</span>
<span id="cb1-18">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Which x step are we in?</span></span>
<span id="cb1-19">      row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ipt[..<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ]</span>
<span id="cb1-20">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If the row isn't wide enough, expand it until it is</span></span>
<span id="cb1-21">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># There is probably a better way to do that but I'm not</span></span>
<span id="cb1-22">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sure I want to spend 5 minutes trying stuff that will</span></span>
<span id="cb1-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save me half a micro second</span></span>
<span id="cb1-24">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nchar</span>(row) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> ..<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>){</span>
<span id="cb1-25">        row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(row, row)</span>
<span id="cb1-26">      }</span>
<span id="cb1-27">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split the damn thing</span></span>
<span id="cb1-28">      row <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(row, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb1-29">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># look for the y, is it a tree?</span></span>
<span id="cb1-30">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (row[..<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb1-31">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (row[..<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#"</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-32">    }</span>
<span id="cb1-33">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb1-34">}</span>
<span id="cb1-35"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n_trees</span>()</span></code></pre></div>
<pre><code>## [1] 278</code></pre>
</section>
<section id="part-two" class="level3">
<h3 class="anchored" data-anchor-id="part-two">Part two</h3>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb3-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-4">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmap_dbl</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n_trees</span>(..<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ..<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">*</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span></code></pre></div>
<pre><code>## [1] 9709761600</code></pre>
</section>
</section>
<section id="js-solution" class="level2">
<h2 class="anchored" data-anchor-id="js-solution">JS solution</h2>
<p>Sorry, no time for JS today ¯\<em>(ツ)</em>/¯</p>
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
  this.page.url = "https://colinfay.me/aoc-2020-03/";
  this.page.identifier = "/aoc-03";
};
(function() {
  var d = document, s = d.createElement('script');
  s.src = 'https://http-colinfay-me.disqus.com/embed.js';
  s.setAttribute('data-timestamp', +new Date());
  (d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-blog-en</category>
  <category>aoc</category>
  <guid>https://colinfay.me/posts/aoc-2020-03.html</guid>
  <pubDate>Thu, 03 Dec 2020 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
