<?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>Joshua Marie</title>
<link>https://joshuamarie.com/</link>
<atom:link href="https://joshuamarie.com/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.8.26</generator>
<lastBuildDate>Mon, 26 Jan 2026 16:00:00 GMT</lastBuildDate>
<item>
  <title>R Reproducibility Stack</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/18-reproducibility-r/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/18-reproducibility-r&amp;text=Reproducibility%20in%20R%20-%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/18-reproducibility-r&amp;title=Reproducibility%20in%20R%20-%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Reproducibility%20in%20R%20-%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/18-reproducibility-r" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/18-reproducibility-r" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/18-reproducibility-r&amp;title=Reproducibility%20in%20R%20-%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/18-reproducibility-r&amp;title=Reproducibility%20in%20R%20-%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/18-reproducibility-r&amp;name=Reproducibility%20in%20R%20-%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Reproducibility in R - Joshua Marie https://joshuamarie.com/posts/18-reproducibility-r'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/18-reproducibility-r%20Reproducibility%20in%20R%20-%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="overall-rationale-why-reproducibility-matters-in-this-date" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Overall Rationale: Why Reproducibility Matters in this date</h1>
<p>Reproducibility in R has derived from theory to practice. After handling numerous data science projects, I have learned that reproducibility is rather fundamentally about maintainability and the ability to return to a project months or years later and have it actually work, not just following best practices. Thus, it matters.</p>
<p>The core problem we face as R practitioners is simple: code that runs perfectly today may fail to other machines or in future executions. Any programming languages you’ll use, not just R, may encounter:</p>
<ol type="1">
<li>Packages update</li>
<li>Dependencies changes</li>
<li>Evolving versions evolve</li>
<li>System libraries get upgraded.</li>
</ol>
<p>What worked flawlessly on your machine might throw cryptic errors on a colleague’s computer. A pipeline that ran successfully six months ago might inexplicably break when you try to reproduce your results.</p>
<p>This post outlines my known reproducibility tools in current 2026, built around five core tools: <a href="https://rstudio.github.io/renv/">renv</a> for package management, Git for version control, <a href="https://docs.ropensci.org/targets/">targets</a> for workflow orchestration, <a href="https://klmr.me/box/">box</a> for code organization, and <a href="https://docs.ropensci.org/rixpress">rixpress</a> (powered by rix) for full-stack reproducibility when standard approaches are insufficient.</p>
<p>I am gonna try to explain what these tools do, and when and why you should use each one, building up from simple projects to complex, long-lived data pipelines.</p>
<p>Just remember: Over-engineering a simple analysis with unnecessary infrastructure wastes time and creates maintenance burden. Conversely, under-engineering a complex project leads to fragility and technical debt.</p>
</section><section id="option-1-renv-git-pak" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> Option 1: {renv} + git + {pak}</h1>
<p>The <a href="https://rstudio.github.io/renv/">renv</a> package got more and more stable after years of development. It is nice that they are listening to people’s mindful criticism about its flaws, and this is the best thing we have when dealing with freezing the working environment. It is so stable, it sync with Git version properly, not perfect but that’s the best thing we had for now. Also, my recommendation is that pair it with <a href="https://pak.r-lib.org/">pak</a> as it resolves the package you want to install fast, and also it syncs well with the initialized <a href="https://rstudio.github.io/renv/">renv</a> project.</p>
<section id="rationale" class="level2" data-number="2.1"><h2 data-number="2.1" class="anchored" data-anchor-id="rationale">
<span class="header-section-number">2.1</span> Rationale</h2>
<p>The single biggest threat to R reproducibility is package version drift. R has little to no issue with backwards compatibility, except R’s package ecosystem updates every time. A package that worked one way last month might behave differently this month after an update — problems involves dependencies changes, functions get deprecated, or default behaviors shift. Of course, a problem arises: your code’s behavior becomes dependent on when you happened to run it, not just what the code says.</p>
<p>Consider a concrete example. You write an analysis using <a href="https://dplyr.tidyverse.org">dplyr</a> v1.0.7. Six months later, <a href="https://dplyr.tidyverse.org">dplyr</a> v1.1.0 is released with breaking changes to certain functions. Potentially, when you or a colleague tries to re-run your analysis, it fails with mysterious errors. Without package version control, you face a frustrating choice: modify your code to work with the new version (potentially changing results) or attempt to manually downgrade packages (tedious and error-prone).</p>
</section><section id="how-renv-solves-this-problem" class="level2" data-number="2.2"><h2 data-number="2.2" class="anchored" data-anchor-id="how-renv-solves-this-problem">
<span class="header-section-number">2.2</span> How {renv} Solves This Problem</h2>
<p>The <a href="https://rstudio.github.io/renv/">renv</a> package creates a project-local library that isolates each project’s packages from your global R library. When you initialize <a href="https://rstudio.github.io/renv/">renv</a> in a project, it creates these core files:</p>
<ul>
<li>A <code>renv.lock</code> file that records the exact version of R version you used, and the exact version of every package used.</li>
<li>A <code>renv/</code> directory containing project-specific package installations.</li>
<li>An <code>activate.R</code> script that automatically loads the project environment. Nothing to worry about this, when <code>renv</code> initialized, <code>.Rprofile</code> is created and automatically executes <code>activate.R</code> script.</li>
</ul>
<p>Here is how I use <a href="https://rstudio.github.io/renv/">renv</a> in a new project:</p>
<ol type="1">
<li>
<p>Install <a href="https://rstudio.github.io/renv/">renv</a> if you haven’t already</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"renv"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</li>
<li>
<p>Initialize <a href="https://rstudio.github.io/renv/">renv</a> in your current project directory</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renv</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rstudio.github.io/renv/reference/init.html">init</a></span><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></code></pre></div></div>
</li>
<li>
<p>Install <a href="https://pak.r-lib.org/">pak</a> package</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renv</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rstudio.github.io/renv/reference/install.html">install</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pak'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</li>
<li>
<p>Now, you can install the packages within the isolated environment with <code><a href="https://pak.r-lib.org/reference/pak.html">pak::pak()</a></code> or <code><a href="https://pak.r-lib.org/reference/pkg_install.html">pak::pkg_install()</a></code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pak</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pak.r-lib.org/reference/pkg_install.html">pkg_install</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidyverse"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</li>
<li>
<p>Then, take a snapshot of the environment to automatically creates / updates the record of your current package versions</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renv</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rstudio.github.io/renv/reference/snapshot.html">snapshot</a></span><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></code></pre></div></div>
<p>The <code><a href="https://rstudio.github.io/renv/reference/snapshot.html">renv::snapshot()</a></code> command updates your <code>renv.lock</code> file to reflect your current package versions. This lockfile is a plain text JSON file that precisely records every package version, where it was installed from, and its dependencies.</p>
</li>
</ol>
<p>When someone else (or future you) opens this project, they simply run:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renv</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rstudio.github.io/renv/reference/restore.html">restore</a></span><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></code></pre></div></div>
<p>This command reads the <code>renv.lock</code> file and installs the exact same package versions into the project-local library. The result is perfect package-level reproducibility across machines and over time. Again, it is not perfect but this is the best solution we’ve got in R currently.</p>
</section><section id="git-for-version-control-the-other-half-of-layer-1" class="level2" data-number="2.3"><h2 data-number="2.3" class="anchored" data-anchor-id="git-for-version-control-the-other-half-of-layer-1">
<span class="header-section-number">2.3</span> Git for Version Control: The Other Half of Layer 1</h2>
<p>While renv handles package versions, git handles code versions. I assume most readers are familiar with git basics, so I will focus on reproducibility-specific considerations.</p>
<section id="what-to-version-control" class="level3" data-number="2.3.1"><h3 data-number="2.3.1" class="anchored" data-anchor-id="what-to-version-control">
<span class="header-section-number">2.3.1</span> What to version control</h3>
<p>Simple: Moderate all R scripts and analysis code, the <code>renv.lock</code> file, the <code>renv/activate.R</code> file and <code>.Rprofile</code>, documentation and README files, and some small reference data files (preferably under a few MB, only if allowed).</p>
</section><section id="what-not-to-version-control" class="level3" data-number="2.3.2"><h3 data-number="2.3.2" class="anchored" data-anchor-id="what-not-to-version-control">
<span class="header-section-number">2.3.2</span> What NOT to version control</h3>
<p>List of things NOT recommendable to moderate:</p>
<ul>
<li>The <code>renv/library/</code> directory (large, binary, machine-specific)</li>
<li>Large data files (use git-lfs or store separately)</li>
<li>Temporary files and build artifacts</li>
<li>Personal configuration files</li>
</ul>
<p>Here is a sensible <code>.gitignore</code> for R projects:</p>
<div class="code-copy-outer-scaffold"><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;">.Rproj.user</span></span>
<span id="cb7-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.Rhistory</span></span>
<span id="cb7-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.RData</span></span>
<span id="cb7-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.Ruserdata</span></span>
<span id="cb7-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">renv/library/</span></span>
<span id="cb7-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">renv/local/</span></span>
<span id="cb7-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">renv/staging/</span></span>
<span id="cb7-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">*.csv</span></span>
<span id="cb7-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">*.rds</span></span>
<span id="cb7-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">data/raw/*</span></span>
<span id="cb7-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.DS_Store</span></span>
<span id="cb7-12"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">*.Rproj</span></span></code></pre></div></div>
<blockquote class="blockquote">
<p>Note: ignoring <code>*.Rproj</code> is totally optional.</p>
</blockquote>
</section></section><section id="drawback" class="level2" data-number="2.4"><h2 data-number="2.4" class="anchored" data-anchor-id="drawback">
<span class="header-section-number">2.4</span> Drawback</h2>
<p>This tooling only helps at moderating and freezing the environment you are working on. It is good at handling isolation and moderation, but not good enough at handling:</p>
<ol type="1">
<li>Organization of your code (discussed later)</li>
<li>System dependencies are not tracked (discussed later)</li>
<li>Cross-platform differences (discussed later)</li>
<li>Multi-language workflows — it works with Python, but went off once it crossed with multiple languages, such as Rust or C++ (discussed later)</li>
</ol></section></section><section id="option-2-targets" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> Option 2: {targets}</h1>
<p>Although I admit that I don’t use <a href="https://docs.ropensci.org/targets/">targets</a> that often, this package is part of my stack. Take note that in this part, I am only gonna explain the surface part of this tooling.</p>
<section id="rationale-1" class="level2" data-number="3.1"><h2 data-number="3.1" class="anchored" data-anchor-id="rationale-1">
<span class="header-section-number">3.1</span> Rationale</h2>
<p>This package is inspired by <a href="https://www.gnu.org/software/make/">GNU Make</a>, suitable for bulky pipeline in statistics and data science, which of course, uses R. R is historically bad at being a proper programming language when handling such huge pipelines you handle. This package fills the gap, and this is only a good tool we have, before <a href="https://docs.ropensci.org/drake">drake</a>, of course, as most “pipeline tools” are mostly Python-focused. Sad? I am sad.</p>
<p>I’d say that <a href="https://docs.ropensci.org/targets/">targets</a> is not actually good for reproducibility, but having this in your stack is unsurprisingly good. For instance, simple scripts waste time on long-running analyses. If step 5 of your 10-step pipeline fails, you don’t want to re-run steps 1-4 again. You want to fix step 5 and continue from there.</p>
</section><section id="how-targets-works" class="level2" data-number="3.2"><h2 data-number="3.2" class="anchored" data-anchor-id="how-targets-works">
<span class="header-section-number">3.2</span> How {targets} works</h2>
<p>At its core, targets is simple: you define a pipeline in a <code>_targets.R</code> file where each “target” is a named R object produced by running some code. Here’s a minimal example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># _targets.R</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/targets/">targets</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/tarchetypes/">tarchetypes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_option_set</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>packages <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"readr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span><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></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">raw_data_file</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/raw.csv"</span>, format <span 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>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">raw_data</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">raw_data_file</span><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>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">clean_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">raw_data</span> <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;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">outcome</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">predictor</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">clean_data</span><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>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_target</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">clean_data</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">predictor</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">outcome</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  <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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>Run the pipeline with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">targets</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tar_make</span><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></code></pre></div></div>
<p>The first time, targets runs everything. If you modify <code>raw_data_file</code>, targets automatically knows to re-run everything that depends on it. If you only modify the plot code, only that target re-runs.</p>
</section></section><section id="option-3-box" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> Option 3: {box}</h1>
<p>I’ve been using this package lately for my R projects and blogs, just to import the namespace of the package I needed.</p>
<p>Hot take: I freaking hate using <code><a href="https://rdrr.io/r/base/source.html">source()</a></code>, I feel like I bit my tongue off whenever I use it. I abandoned the <code><a href="https://rdrr.io/r/base/source.html">source()</a></code> habit for (quite not) a long time now after discovering this package.</p>
<section id="rationale-2" class="level2" data-number="4.1"><h2 data-number="4.1" class="anchored" data-anchor-id="rationale-2">
<span class="header-section-number">4.1</span> Rationale</h2>
<p>For modular code organization problem and Python-like import system, <a href="https://klmr.me/box/">box</a> is the most ergonomic tool, thanks for Konrad Rudolph for creating and engineering this package. As R projects grow, a common pattern emerges: you start with a single script, then split it into multiple files using <code><a href="https://rdrr.io/r/base/source.html">source()</a></code>. Eventually you have dozens of functions spread across multiple files, all living in the global namespace.</p>
<p>This creates several problems:</p>
<ol type="1">
<li><p><em>Namespace clash:</em> Functions from different files can overwrite each other if they share names. Tracking down which function is actually running becomes difficult.</p></li>
<li><p><em>Unclear dependencies:</em> When you see a function call, where is that function defined? Which file should you look in? What other functions does it depend on?</p></li>
<li><p><em>Testing becomes difficult:</em> Testing functions that rely on global state or that modify global variables is challenging and unreliable.</p></li>
<li><p><em>Reusability suffers:</em> You want to reuse code across directories or in a higher degree, large projects, but extracting functions from tangled dependencies is painful.</p></li>
</ol>
<p>R natively suffers this, and mainly because it lacks a tooling that <a href="https://klmr.me/box/">box</a> package eventually filled, so thank <a href="https://klmr.me/box/">box</a> for existing.</p>
</section><section id="how-box-functions" class="level2" data-number="4.2"><h2 data-number="4.2" class="anchored" data-anchor-id="how-box-functions">
<span class="header-section-number">4.2</span> How {box} functions</h2>
<p>I don’t wanna write out everything here to explain how it works, because it has a huge list of things to be detailed. Please, read one of my books: https://joshuamarie.github.io/modules-in-r/, for more details instead.</p>
<p>On the surface level: The <a href="https://klmr.me/box/">box</a> package brings a module system to R, similar to Python’s import system or JavaScript’s modules. Instead of sourcing entire files into the global namespace, you explicitly import specific functions from specific modules.</p>
<p>Here’s a quick example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I hate doing this:</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/source.html">source</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R/data_processing.r"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/source.html">source</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R/plotting.r"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Instead, do this:</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    dm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data_munging</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">clean_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">validate_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plotting</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">make_plot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plotting</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>This makes it crystal clear which functions you’re using and where they come from.</p>
</section><section id="benefits" class="level2" data-number="4.3"><h2 data-number="4.3" class="anchored" data-anchor-id="benefits">
<span class="header-section-number">4.3</span> Benefits</h2>
<p>How does it help on reproduciblity? By being explicit about what you import and gaining better code reusability, <a href="https://klmr.me/box/">box</a> becomes so much a valuable tool for long-term maintainability — it fills gaps that R natively lacks. This is not <a href="https://docs.ropensci.org/targets/">targets</a> package that helps on your computational activity (it is utilized but still orthogonal), or improves <em>package version reproducibility</em> like what <a href="https://rstudio.github.io/renv/">renv</a> does, but improves on what I dubbed as <em>structural reproducibility</em> (e.g.&nbsp;involves code modularity, reusability, …) instead.</p>
</section></section><section id="option-4-rix-and-rixpress" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Option 4: {rix} and {rixpress}</h1>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>You may want to add the previous options I inscribed above in your “reproducibility” stack.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Correct me if I am wrong here. I barely use Nix, let alone these toolings.</p>
</div>
</div>
<section id="when-renv-is-not-enough" class="level2" data-number="5.1"><h2 data-number="5.1" class="anchored" data-anchor-id="when-renv-is-not-enough">
<span class="header-section-number">5.1</span> When {renv} is not enough</h2>
<p>The renv package provides excellent package-level reproducibility, but it has limitations:</p>
<ol type="1">
<li><p><strong>System dependencies are not tracked:</strong> Many R packages depend on system libraries. For example, the sf package requires GDAL and PROJ. The exact versions of these system libraries can affect behavior and availability.</p></li>
<li><p><strong>Cross-platform differences:</strong> An analysis that works on macOS might fail on Linux or Windows due to different system configurations, compilers, or library versions.</p></li>
<li><p><strong>Multi-language workflows:</strong> Projects combining R with Python, then potentially with Julia, or other languages have dependencies beyond R’s package system.</p></li>
</ol>
<p>For most projects, these limitations are acceptable. But for long-lived research projects, production systems, or work requiring absolute reproducibility across platforms and years, you need stronger guarantees.</p>
</section><section id="rationale-3" class="level2" data-number="5.2"><h2 data-number="5.2" class="anchored" data-anchor-id="rationale-3">
<span class="header-section-number">5.2</span> Rationale:</h2>
<p>First, let’s understand Nix and why it matters. Nix is a package manager and build system that provides deterministic, reproducible builds. Unlike traditional package managers, Nix uses cryptographic hashes to uniquely identify every package and its dependencies. This means:</p>
<ul>
<li>Every package version is immutably stored and never changes</li>
<li>Dependencies are explicitly declared and tracked</li>
<li>Builds are isolated and reproducible</li>
<li>You can have multiple versions of the same software coexisting without conflict</li>
</ul>
<p>The only problem. Nix has a steep learning curve and requires learning a new configuration language. This is where rix and rixpress come in: they provide R-native interfaces to Nix’s power.</p>
</section><section id="the-rix-package-declarative-nix-environments-from-r" class="level2" data-number="5.3"><h2 data-number="5.3" class="anchored" data-anchor-id="the-rix-package-declarative-nix-environments-from-r">
<span class="header-section-number">5.3</span> The rix Package: Declarative Nix Environments from R</h2>
<p>The rix package lets you define reproducible computational environments using familiar R syntax. You specify the R version, packages, and system dependencies you need, and rix generates the appropriate Nix configuration.</p>
<p>Here’s a simple example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install rix</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("rix")</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pak</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pak.r-lib.org/reference/pkg_install.html">pkg_install</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rix"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rix</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rix</span><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></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rix</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    r_ver <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4.4.1"</span>,</span>
<span>    date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-11-30"</span>,</span>
<span>    packages <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidyr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"box"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    system_packages <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gdal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"proj"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    project_path <span 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>    overwrite <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span>    ide <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rstudio"</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>This generates a <code>default.nix</code> file in your project directory and modifies your <code>.Rprofile</code> to work with Nix. The <code>date</code> parameter pins packages to their state on that date, ensuring reproducibility even as CRAN changes.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Key parameters explained
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>
<code>r_ver</code>: Exact R version to use</li>
<li>
<code>date</code>: Pin package versions to a specific date</li>
<li>
<code>packages</code>: R packages to install</li>
<li>
<code>system_packages</code>: System-level dependencies (compilers, libraries, etc.)</li>
<li>
<code>ide</code>: Configure integration with RStudio or other IDEs</li>
</ul>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Activating the environment
</div>
</div>
<div class="callout-body-container callout-body">
<p>Once rix has generated your Nix configuration, you activate it using:</p>
<div class="code-copy-outer-scaffold"><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;">nix</span> develop</span></code></pre></div></div>
<p>This drops you into a shell with the exact R version, packages, and system dependencies specified. Everything is isolated from your main system, preventing conflicts.</p>
</div>
</div>
</section><section id="the-rixpress-package-nix-powered-workflows" class="level2" data-number="5.4"><h2 data-number="5.4" class="anchored" data-anchor-id="the-rixpress-package-nix-powered-workflows">
<span class="header-section-number">5.4</span> The rixpress Package: Nix-Powered Workflows</h2>
<p>While rix handles environment reproducibility, rixpress extends this to workflow orchestration, similar to targets but with Nix isolation. Each step in your pipeline can run in its own hermetic environment with precisely controlled dependencies.</p>
<p><strong>Setting up a rixpress pipeline:</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install rixpress</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remotes::install_github("b-rodrigues/rixpress")</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># devtools::install_github("b-rodrigues/rixpress")</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pak</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pak.r-lib.org/reference/pak.html">pak</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b-rodrigues/rixpress"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rixpress</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rxp_pipeline</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rxp_r</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rxp_py</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rxp_populate</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, keep_when <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">readr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">read_csv</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">write_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pipeline</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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># R step: load and clean data</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rxp_r</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clean_data"</span>,</span>
<span>        expr <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>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/raw.csv"</span><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;">|&gt;</span> </span>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">keep_when</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/clean.csv"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>,</span>
<span>        rix_env <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r_env_4.4.1"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Python step: run ML model</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rxp_py</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train_model"</span>,</span>
<span>        expr <span 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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">import pandas as pd</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">from sklearn.ensemble import RandomForestRegressor</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">import pickle</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"></span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">data = pd.read_csv('data/clean.csv')</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">X = data[['feature1', 'feature2']]</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">y = data['target']</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"></span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">model = RandomForestRegressor()</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">model.fit(X, y)</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"></span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">with open('model.pkl', 'wb') as f:</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    pickle.dump(model, f)</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    "</span>,</span>
<span>        deps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clean_data"</span>,</span>
<span>        python_version <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3.11"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rxp_r</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"report"</span>,</span>
<span>        expr <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>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmarkdown</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pkgs.rstudio.com/rmarkdown/reference/render.html">render</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"report.Rmd"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>,</span>
<span>        deps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"train_model"</span>,</span>
<span>        rix_env <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r_env_4.4.1"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rxp_populate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pipeline</span>, build <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>It looks like its behavior is similar to <a href="https://docs.ropensci.org/targets/">targets</a> but with different API and semantics. But, how rixpress differs from targets?</p>
<p>I’ll make it simple:</p>
<ul>
<li>Each step runs in an isolated Nix environment</li>
<li>System dependencies are explicitly tracked</li>
<li>Multi-language workflows are first-class citizens</li>
<li>Reproducibility guarantees extend to the OS level</li>
<li>Builds are truly deterministic and bit-for-bit reproducible</li>
</ul></section><section id="practical-considerations-for-using-rix-and-rixpress" class="level2" data-number="5.5"><h2 data-number="5.5" class="anchored" data-anchor-id="practical-considerations-for-using-rix-and-rixpress">
<span class="header-section-number">5.5</span> Practical Considerations for Using rix and rixpress</h2>
<p>Using this tooling is a bit tedious, especially for beginners. Here’s why:</p>
<ol type="1">
<li><p>Nix has a reputation for complexity, and it is deserved. Good thing, {rix} and {rixpress} abstract away much of the difficulty, but spoiler: It still has a bit steeper learning curve. You can get started with basic usage without deep Nix knowledge.</p></li>
<li><p>Before using <a href="https://docs.ropensci.org/rix/">rix</a> or <a href="https://docs.ropensci.org/rixpress">rixpress</a>, you have to install Nix itself. On Linux and macOS, this is straightforward. On Windows, you will need to use WSL (Windows Subsystem for Linux).</p></li>
<li><p>This one can make you bite your tongue a bit: Nix stores all packages in its store, which can consume significant disk space. However, packages are shared across projects, so the cost amortizes over multiple projects.</p></li>
<li><p><em>Build times</em>: The first time you build a Nix environment, it may take considerable time as packages are downloaded and built. Subsequent builds use cached results and are much faster.</p></li>
</ol>
<section id="when-to-use-rixrixpress" class="level3" data-number="5.5.1"><h3 data-number="5.5.1" class="anchored" data-anchor-id="when-to-use-rixrixpress">
<span class="header-section-number">5.5.1</span> When to use rix/rixpress</h3>
<ul>
<li>Long-term research projects that must remain runnable for years or decades</li>
<li>Production environment where stability is critical</li>
<li>Cross-platform work requiring identical behavior on different operating systems</li>
<li>Projects with complex system dependencies</li>
<li>Multi-language pipelines combining R, Python, Julia, etc.</li>
<li>Situations where you need to prove exact reproducibility (regulatory compliance, peer review)</li>
</ul></section><section id="when-not-to-use-rixrixpress" class="level3" data-number="5.5.2"><h3 data-number="5.5.2" class="anchored" data-anchor-id="when-not-to-use-rixrixpress">
<span class="header-section-number">5.5.2</span> When NOT to use rix/rixpress</h3>
<p>Of course, it has a bit steeper learning curve, so use it on:</p>
<ul>
<li>Quick analyses or exploratory work</li>
<li>Projects where <a href="https://rstudio.github.io/renv/">renv</a> provides sufficient reproducibility</li>
<li>Situations where the learning curve cost exceeds the benefit</li>
<li>Environments where you cannot install Nix (some corporate systems)</li>
</ul></section></section></section><section id="option-5-reporting-with-r-markdown-quarto" class="level1" data-number="6"><h1 data-number="6">
<span class="header-section-number">6</span> Option 5: Reporting with R Markdown / Quarto</h1>
<p>Reproducible reporting is where the rubber meets the road. You can have perfect package management, clean code organization, and efficient workflows. Those options above doesn’t solves actual reporting, and on how you presented the code you just run. This is where R Markdown / Quarto comes in.</p>
<section id="rationale-4" class="level2" data-number="6.1"><h2 data-number="6.1" class="anchored" data-anchor-id="rationale-4">
<span class="header-section-number">6.1</span> Rationale</h2>
<p>You can choose between R Markdown and Quarto, although the latter is currently got more updates than the former. Both tools follow the principle of <em>literate programming</em> — combining code, output, and narrative in a single document. When someone runs your <code>.Rmd</code> or <code>.qmd</code> file, they get the exact same results, figures, and tables you generated. No more “copy-paste this plot into Word” or “manually update this table with new numbers.”</p>
<p>Here’s two comparison:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">R Markdown</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Quarto</a></li>
</ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>For me, R Markdown is only better than Quarto if it is a quick reporting. R Markdown is nice, but it is superseded by Quarto — it also got most of R Markdown features, e.g.&nbsp;inline codes.</p>
<p>Except, for now, Quarto can’t do this (unless <code>engine: knitr</code> is set):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">title</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
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;">"Report for February 07, 2026"</span></span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">author</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
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;">"John Doe"</span></span>
<span id="cb14-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">output</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> html_document</span></span></code></pre></div></div>
<p>Hence, R Markdown is still better for quick reporting. Use RStudio to run the cells interactively, and you can have similar interface as Jupyter notebooks while being plain text.</p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>Quarto have most of R markdown features, except it is more <em>extendable</em> and provides multi-language support (so, not just R, this includes Python, Julia, Observable), most especially <code>revealjs</code> is so much better for HTML presentation.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> revealjs</span></span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filters</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb15-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> social-share</span></span>
<span id="cb15-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> collapse-output</span></span>
<span id="cb15-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">engine</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> knitr</span></span></code></pre></div></div>
<p>I recommend using <a href="https://yihui.org/knitr/">knitr</a> as the default engine when using multiple languages in 1 QMD — use <a href="https://rstudio.github.io/reticulate/">reticulate</a> for Python, <a href="https://github.com/JuliaInterop/JuliaCall">JuliaCall</a> for Julia, both are pre-configured.</p>
</div>
</div>
</div>
</section></section><section id="combining-tools-in-practice" class="level1" data-number="7"><h1 data-number="7">
<span class="header-section-number">7</span> Combining Tools in Practice</h1>
<p>Can you combine the options being placed here? Straightforwardly, yes.</p>
<ol type="a">
<li><p>In my work, I use options 1 and 3. Having them for reproducibility and maintainability, especially for my own work and make my work shareable to others, is more than enough.</p></li>
<li><p>Options 1 + 2 + 4 is good when you’re building production-grade data pipelines that need to run reliably over years.</p></li>
<li><p>You can combine either of the first 4 options then option 5 for reporting.</p></li>
</ol>
<p>Except for one thing: <a href="https://docs.ropensci.org/targets/">targets</a> + <a href="https://klmr.me/box/">box</a>, and it is being discussed <a href="https://github.com/ropensci/targets/discussions/936">here</a> why it is a case.</p>
<p>I’ll make it simple (this is at as far as I know):</p>
<ol type="1">
<li><p><a href="https://docs.ropensci.org/targets/">targets</a> relies on static analysis through <a href="https://gitlab.com/luke-tierney/codetools">codetools</a> to discover symbols in your target expressions and build a proper dependency graph (DAG) without running the code. It needs functions and objects to be statically visible.</p></li>
<li><p>In contrast, <a href="https://klmr.me/box/">box</a> resolves functions at runtime inside module environments (e.g., via <code>$</code>), which hides internal symbols from static analysis. As a result, <a href="https://docs.ropensci.org/targets/">targets</a> may not fully “see” the real function-level dependencies inside <a href="https://klmr.me/box/">box</a> modules.</p></li>
</ol></section><section id="special-mention" class="level1" data-number="8"><h1 data-number="8">
<span class="header-section-number">8</span> Special Mention</h1>
<p>Here are other tools used for reproducibility, but more niche, superseded, and/or known less (by me):</p>
<ol type="1">
<li>
<a href="https://reproducible.predictiveecology.org">reproducible</a> - it is now on its version 3.0.0, and expect to have some breaking changes. This is niche.</li>
<li>
<a href="https://github.com/rstudio/packrat">packrat</a> - this is now a legacy, mostly superseded by <a href="https://rstudio.github.io/renv/">renv</a>.</li>
<li>
<a href="https://groundhogr.com/">groundhog</a> - purely for packages, specialized in timing. I don’t know much about this one.</li>
<li>
<a href="https://docs.ropensci.org/drake">drake</a> - mostly superseded by <a href="https://docs.ropensci.org/targets/">targets</a>.</li>
<li>
<a href="https://reprex.tidyverse.org">reprex</a> - only good for providing reproducible example.</li>
</ol>


</section> ]]></description>
  <category>R</category>
  <category>programming</category>
  <category>box</category>
  <category>targets</category>
  <category>renv</category>
  <category>version-control</category>
  <category>rixpress</category>
  <guid>https://joshuamarie.com/posts/18-reproducibility-r/</guid>
  <pubDate>Mon, 26 Jan 2026 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/18-reproducibility-r/image.png" medium="image" type="image/png"/>
</item>
<item>
  <title>What do you need to know before starting Pandas 3.0?</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/17-pandas3/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/17-pandas3&amp;text=Pandas%203.0%20%E2%80%94%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/17-pandas3&amp;title=Pandas%203.0%20%E2%80%94%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Pandas%203.0%20%E2%80%94%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/17-pandas3" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/17-pandas3" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/17-pandas3&amp;title=Pandas%203.0%20%E2%80%94%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/17-pandas3&amp;title=Pandas%203.0%20%E2%80%94%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/17-pandas3&amp;name=Pandas%203.0%20%E2%80%94%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Pandas 3.0 — Joshua Marie https://joshuamarie.com/posts/17-pandas3'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/17-pandas3%20Pandas%203.0%20%E2%80%94%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="introduction" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Introduction</h1>
<p>Pandas 3.0 is the biggest update in the library’s history, bringing breaking changes that fix long-standing confusions and performance issues. If you’re new to pandas, you’re starting with a cleaner API. If you’re migrating, this guide covers what you need to know.</p>
</section><section id="the-three-big-changes" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> The Three Big Changes</h1>
<p>Here’s what it potentially breaks your pandas pipeline.</p>
<section id="string-dtype-by-default" class="level2" data-number="2.1"><h2 data-number="2.1" class="anchored" data-anchor-id="string-dtype-by-default">
<span class="header-section-number">2.1</span> 1. String Dtype by Default</h2>
<section id="what-changed" class="level3" data-number="2.1.1"><h3 data-number="2.1.1" class="anchored" data-anchor-id="what-changed">
<span class="header-section-number">2.1.1</span> What Changed</h3>
<p>String columns now use a dedicated <code>str</code> dtype instead of <code>object</code>, backed by PyArrow for better performance and memory efficiency.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"></span>
<span id="cb1-3">ser <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.Series([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"apple"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"banana"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cherry"</span>])</span>
<span id="cb1-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(ser.dtype)  </span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt;&gt; str </span></span></code></pre></div></div>
<p>The data type of <code>ser</code> is not ‘object’ anymore!</p>
</section><section id="why-it-matters" class="level3" data-number="2.1.2"><h3 data-number="2.1.2" class="anchored" data-anchor-id="why-it-matters">
<span class="header-section-number">2.1.2</span> Why It Matters</h3>
<p>String operations are 2-10x faster, use less memory, and provide better type safety. PyArrow is now a required dependency.</p>
</section><section id="breaking-change" class="level3" data-number="2.1.3"><h3 data-number="2.1.3" class="anchored" data-anchor-id="breaking-change">
<span class="header-section-number">2.1.3</span> Breaking Change</h3>
<p>Code checking for <code>dtype == 'object'</code> will break:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This breaks</span></span>
<span id="cb2-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'column'</span>].dtype <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'object'</span>:</span>
<span id="cb2-3">    process_strings(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'column'</span>])</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use this instead</span></span>
<span id="cb2-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> pd.api.types.is_string_dtype(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'column'</span>]):</span>
<span id="cb2-7">    process_strings(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'column'</span>])</span></code></pre></div></div>
</section></section><section id="copy-on-write-is-default" class="level2" data-number="2.2"><h2 data-number="2.2" class="anchored" data-anchor-id="copy-on-write-is-default">
<span class="header-section-number">2.2</span> 2. Copy-on-Write is Default</h2>
<section id="the-problem-it-solved" class="level3" data-number="2.2.1"><h3 data-number="2.2.1" class="anchored" data-anchor-id="the-problem-it-solved">
<span class="header-section-number">2.2.1</span> The Problem It Solved</h3>
<p>Remember the infamous <code>SettingWithCopyWarning</code>? It’s gone. Pandas 3.0 guarantees that all indexing operations behave like copies.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb3-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'name'</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Alice'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Bob'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Charlie'</span>],</span>
<span id="cb3-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'score'</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>]</span>
<span id="cb3-4">})</span>
<span id="cb3-5"></span>
<span id="cb3-6">high_scorers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'score'</span>] <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;">90</span>]</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Modify filtered data</span></span>
<span id="cb3-9">high_scorers[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'passed'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span></code></pre></div></div>
<p>In this update, <code>df</code> is NEVER modified, guaranteed!</p>
</section><section id="breaking-change-chained-assignment" class="level3" data-number="2.2.2"><h3 data-number="2.2.2" class="anchored" data-anchor-id="breaking-change-chained-assignment">
<span class="header-section-number">2.2.2</span> Breaking Change: Chained Assignment</h3>
<p>Chained assignment no longer works:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Doesn't work anymore</span></span>
<span id="cb4-2">df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'score'</span>][df[<span class="st" style="color: #20794D;
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;">'Alice'</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;">100</span></span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use loc instead</span></span>
<span id="cb4-5">df.loc[df[<span class="st" style="color: #20794D;
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;">'Alice'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'score'</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;">100</span></span></code></pre></div></div>
<p>The upside? No more confusing warnings, and you don’t need defensive <code>.copy()</code> calls anymore.</p>
</section></section><section id="the-pd.col-syntax" class="level2" data-number="2.3"><h2 data-number="2.3" class="anchored" data-anchor-id="the-pd.col-syntax">
<span class="header-section-number">2.3</span> 3. The <code>pd.col()</code> Syntax</h2>
<p>This is the feature that stands out among the additions within the update: <code>pandas.col()</code> (or just <code>pd.col()</code> when you import <code>pandas</code> as <code>pd</code>).</p>
<p>No more cluttered lambdas in <code>.assign()</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Old way</span></span>
<span id="cb5-2">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.assign(temp_f <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;">lambda</span> x: x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'temp_c'</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;">9</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;">5</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;">32</span>)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New way with pd.col()</span></span>
<span id="cb5-5">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.assign(temp_f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'temp_c'</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;">9</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;">5</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;">32</span>)</span></code></pre></div></div>
<section id="examples" class="level3" data-number="2.3.1"><h3 data-number="2.3.1" class="anchored" data-anchor-id="examples">
<span class="header-section-number">2.3.1</span> Examples</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Arithmetic</span></span>
<span id="cb6-2">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.assign(</span>
<span id="cb6-3">    total<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'price'</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'quantity'</span>),</span>
<span id="cb6-4">    discounted<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'total'</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span></span>
<span id="cb6-5">)</span>
<span id="cb6-6"></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># String operations</span></span>
<span id="cb6-8">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.assign(</span>
<span id="cb6-9">    name_upper<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'name'</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.upper(),</span>
<span id="cb6-10">    is_alice<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pd.col(<span class="st" style="color: #20794D;
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;">'Alice'</span></span>
<span id="cb6-11">)</span>
<span id="cb6-12"></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filtering</span></span>
<span id="cb6-14">expensive <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.loc[pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'price'</span>) <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;">100</span>]</span></code></pre></div></div>
</section><section id="current-limitation" class="level3" data-number="2.3.2"><h3 data-number="2.3.2" class="anchored" data-anchor-id="current-limitation">
<span class="header-section-number">2.3.2</span> Current Limitation</h3>
<p><code>pd.col()</code> doesn’t work in groupby aggregations yet:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">df.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'category'</span>).agg(pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'value'</span>).mean())</span></code></pre></div></div>
</section></section></section><section id="migration-strategy" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> Migration Strategy</h1>
<section id="step-1-upgrade-to-2.3-first" class="level2" data-number="3.1"><h2 data-number="3.1" class="anchored" data-anchor-id="step-1-upgrade-to-2.3-first">
<span class="header-section-number">3.1</span> Step 1: Upgrade to 2.3 First</h2>
<div class="code-copy-outer-scaffold"><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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Upgrade to 2.3, fix warnings</span></span>
<span id="cb8-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install pandas==2.3.0</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then upgrade to 3.0</span></span>
<span id="cb8-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install pandas==3.0.0</span></code></pre></div></div>
</section><section id="step-2-test-new-behavior-in-2.3" class="level2" data-number="3.2"><h2 data-number="3.2" class="anchored" data-anchor-id="step-2-test-new-behavior-in-2.3">
<span class="header-section-number">3.2</span> Step 2: Test New Behavior in 2.3</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Enable new behavior before upgrading</span></span>
<span id="cb9-2">pd.options.mode.copy_on_write <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb9-3">pd.options.future.infer_string <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span></code></pre></div></div>
</section><section id="step-3-fix-common-issues" class="level2" data-number="3.3"><h2 data-number="3.3" class="anchored" data-anchor-id="step-3-fix-common-issues">
<span class="header-section-number">3.3</span> Step 3: Fix Common Issues</h2>
<ol type="1">
<li>
<p>Remove defensive copies:**</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unnecessary now</span></span>
<span id="cb10-2">df2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'value'</span>] <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;">10</span>].copy()</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Copy happens automatically</span></span>
<span id="cb10-5">df2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'value'</span>] <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;">10</span>]</span></code></pre></div></div>
</li>
<li>
<p>Fix chained assignment:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Old</span></span>
<span id="cb11-2">df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'col'</span>][mask] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New</span></span>
<span id="cb11-5">df.loc[mask, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'col'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value</span></code></pre></div></div>
</li>
<li>
<p>Update dtype checks:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Old</span></span>
<span id="cb12-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'col'</span>].dtype <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'object'</span>:</span>
<span id="cb12-3"></span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New</span></span>
<span id="cb12-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> pd.api.types.is_string_dtype(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'col'</span>]):</span></code></pre></div></div>
</li>
</ol></section></section><section id="performance-improvements" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> Performance Improvements</h1>
<p>With PyArrow-backed strings, expect significant gains:</p>
<ul>
<li>String operations: up to 10x faster</li>
<li>Memory usage: ~50% reduction for string columns</li>
<li>Reduced unnecessary copies with Copy-on-Write</li>
</ul></section><section id="best-practices" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Best Practices</h1>
<ol type="1">
<li>Use <code>pd.col()</code> for clarity</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.assign(</span>
<span id="cb13-2">    full_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'first'</span>) <span 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'last'</span>),</span>
<span id="cb13-3">    is_senior <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'age'</span>) <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;">65</span></span>
<span id="cb13-4">)</span></code></pre></div></div>
<ol start="2" type="1">
<li>Always use <code>.loc</code> for assignment</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">df.loc[mask, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'column'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value</span></code></pre></div></div>
<ol start="3" type="1">
<li>Trust Copy-on-Write (no defensive <code>.copy()</code>)</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">subset <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'value'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> threshold]</span>
<span id="cb15-2">subset[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'processed'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># df unchanged!</span></span></code></pre></div></div>
<ol start="4" type="1">
<li>Use type-checking functions</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> pd.api.types.is_string_dtype(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'col'</span>]):</span>
<span id="cb16-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Handle strings</span></span></code></pre></div></div>
</section><section id="conclusion" class="level1" data-number="6"><h1 data-number="6">
<span class="header-section-number">6</span> Conclusion</h1>
<p>Don’t celebrate yet, although Pandas 3.0 fixes years of confusing behavior with three major changes: proper string types, predictable copy semantics, and cleaner column syntax, there are still some inconsistencies—this library is not the as other data libraries out there, e.g.&nbsp;<strong>dplyr</strong>, <strong>polars</strong>.</p>
<p>If you’re new, you’re learning the modern API. If you’re migrating, upgrade to 2.3 first, fix warnings, then jump to 3.0. The changes might feel disruptive, but they solve real problems and make pandas faster and more composable than before.</p>
<section id="resources" class="level2" data-number="6.1"><h2 data-number="6.1" class="anchored" data-anchor-id="resources">
<span class="header-section-number">6.1</span> Resources</h2>
<ul>
<li><a href="https://pandas.pydata.org/docs/dev/whatsnew/v3.0.0.html">Pandas 3.0 Release Notes</a></li>
<li><a href="https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html">Copy-on-Write Guide</a></li>
<li><a href="https://pandas.pydata.org/docs/dev/user_guide/text.html#migrating-to-string-dtype">String Dtype Migration</a></li>
</ul>


</section></section> ]]></description>
  <category>Python</category>
  <category>Pandas</category>
  <category>data-science</category>
  <guid>https://joshuamarie.com/posts/17-pandas3/</guid>
  <pubDate>Sat, 17 Jan 2026 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/17-pandas3/image.png" medium="image" type="image/png"/>
</item>
<item>
  <title>PyTorch is my go-to framework for deep learning</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/16-pytorch/</link>
  <description><![CDATA[ 
<div class="page-columns page-rows-contents page-layout-article"><div class="social-share"><a href="https://twitter.com/share?url=https://joshuamarie.com/posts/16-pytorch&amp;text=PyTorch – Joshua Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/16-pytorch&amp;title=PyTorch – Joshua Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=PyTorch – Joshua Marie&amp;body=Check out this link:https://joshuamarie.com/posts/16-pytorch" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/16-pytorch" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/16-pytorch&amp;title=PyTorch – Joshua Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/16-pytorch&amp;title=PyTorch – Joshua Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/16-pytorch&amp;name=PyTorch – Joshua Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=PyTorch – Joshua Marie https://joshuamarie.com/posts/16-pytorch'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/16-pytorch PyTorch – Joshua Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a></div></div>





<section id="introduction-to-pytorch" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction to PyTorch</h1>
<p>I’ve been writing Python to do deep learning stuffs for ~2 years now. I started from tensorflow then later PyTorch and it has become my go-to framework for deep learning, and for good reason. Developed by Meta’s AI Research lab, PyTorch is an ergonomic framework to buildi neural networks in Pythonic approach that feels natural to any Python developer. Unlike some other frameworks that can feel overly abstracted, PyTorch strikes the perfect balance between ease of use and fine control.</p>
<p>No worries, I am still using tensorflow / keras in this date.</p>
</section>
<section id="why-pytorch" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Why PyTorch?</h1>
<p>After working with TensorFlow, Keras, and other frameworks, I consistently find myself reaching for PyTorch. Here’s why:</p>
<p><strong>Dynamic Computation Graphs</strong>: PyTorch uses dynamic computational graphs (define-by-run), meaning the graph is built on the fly as operations are executed. This makes debugging significantly easier—you can use standard Python debugging tools and print statements to inspect tensors at any point in your code.</p>
<p><strong>Pythonic</strong>: Not gonna lie, using “Pythonic” word makes me feel cringe. Anyways, PyTorch feels like native Python. The API is less abstract but still clean, consistent, and follows Python conventions. If you know NumPy, you’ll feel right at home with PyTorch tensors.</p>
<p><strong>Research-Friendly</strong>: PyTorch has always been ideal for research and experimentation, well aside from JAX. You can easily modify architectures, implement custom layers, and try novel ideas without fighting against the framework. Perfect for ML researchers like me.</p>
<p><strong>Strong Community and Ecosystem</strong>: PyTorch has excellent documentation, a vibrant community, and a rich ecosystem of libraries like PyTorch Lightning, Hugging Face Transformers, and torchvision.</p>
</section>
<section id="getting-started" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Getting Started</h1>
<section id="installation" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="installation"><span class="header-section-number">3.1</span> Installation</h2>
<p>Installing PyTorch is straightforward. Visit <a href="https://pytorch.org">pytorch.org</a> to get the installation command for your system, or use:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CPU version</span></span>
<span id="cb1-2">pip install torch torchvision torchaudio</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># GPU version (CUDA 11.8)</span></span>
<span id="cb1-5">pip install torch torchvision torchaudio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>url https:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span>download.pytorch.org<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>whl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>cu118</span></code></pre></div></div>
</div>
<p>I recommend installing <code>{uv}</code> to quickly resolve the package and its dependencies. Here’s how you install the package with <code>{uv}</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CPU version</span></span>
<span id="cb2-2">uv pip install torch torchvision torchaudio</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># GPU version (CUDA 11.8)</span></span>
<span id="cb2-5">uv pip install torch torchvision torchaudio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>url https:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span>download.pytorch.org<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>whl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>cu118</span></code></pre></div></div>
</div>
</section>
<section id="basic-tensor-operations" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="basic-tensor-operations"><span class="header-section-number">3.2</span> Basic Tensor Operations</h2>
<p>Tensors are the fundamental building blocks in PyTorch, similar to NumPy arrays but with GPU acceleration support.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch</span></code></pre></div></div>
</div>
<p>Here’s how you use it:</p>
<ol type="1">
<li><p>Creating tensor objects</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.tensor([[<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]])</span>
<span id="cb4-2">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.ones(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-3">z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.randn(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)  </span></code></pre></div></div>
</div></li>
<li><p>Performing (basic) operations</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y</span>
<span id="cb5-2">matrix_mult <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> torch.mm(x, y)</span></code></pre></div></div>
</div></li>
<li><p>You can move to GPU if available</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> torch.cuda.is_available():</span>
<span id="cb6-2">    x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x.cuda()</span></code></pre></div></div>
</div></li>
</ol>
</section>
</section>
<section id="building-neural-networks" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Building Neural Networks</h1>
<p>PyTorch’s <code>nn.Module</code> class provides a clean way to define neural networks:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch.nn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> nn</span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch.nn.functional <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> F</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SimpleNN(nn.Module):</span>
<span id="cb7-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, input_size, hidden_size, output_size):</span>
<span id="cb7-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>(SimpleNN, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>).<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>()</span>
<span id="cb7-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.fc1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nn.Linear(input_size, hidden_size)</span>
<span id="cb7-8">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.fc2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nn.Linear(hidden_size, output_size)</span>
<span id="cb7-9">    </span>
<span id="cb7-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> forward(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, x):</span>
<span id="cb7-11">        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> F.relu(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.fc1(x))</span>
<span id="cb7-12">        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.fc2(x)</span>
<span id="cb7-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x</span></code></pre></div></div>
</div>
<p>After defining the module, then you can simply instantiate the model:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SimpleNN(input_size<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;">784</span>, hidden_size<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;">128</span>, output_size<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></code></pre></div></div>
</div>
</section>
<section id="training-loop" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Training Loop</h1>
<p>The training process in PyTorch is explicit and transparent:</p>
<ol type="1">
<li><p>Define loss and optimizer</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> torch.optim <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> optim</span>
<span id="cb9-2"></span>
<span id="cb9-3">criterion <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nn.CrossEntropyLoss()</span>
<span id="cb9-4">optimizer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> optim.Adam(model.parameters(), lr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>)</span></code></pre></div></div>
</div></li>
<li><p>Run the training loop:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="annotated-cell-10" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><span id="annotated-cell-10-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> epoch <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(num_epochs):</span>
<span id="annotated-cell-10-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> batch_idx, (data, target) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(train_loader):</span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-10" data-target-annotation="1">1</button><span id="annotated-cell-10-3" class="code-annotation-target">        optimizer.zero_grad()</span>
<span id="annotated-cell-10-4"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-10" data-target-annotation="2">2</button><span id="annotated-cell-10-5" class="code-annotation-target">        output <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model(data)</span>
<span id="annotated-cell-10-6">        loss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> criterion(output, target)</span>
<span id="annotated-cell-10-7"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-10" data-target-annotation="3">3</button><span id="annotated-cell-10-8" class="code-annotation-target">        loss.backward()</span>
<span id="annotated-cell-10-9"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-10" data-target-annotation="4">4</button><span id="annotated-cell-10-10" class="code-annotation-target">        optimizer.step()</span>
<span id="annotated-cell-10-11"></span>
<span id="annotated-cell-10-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> batch_idx <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;">100</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="annotated-cell-10-13">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'Epoch: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>epoch<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Loss: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>loss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>item()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-10" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-10" data-code-lines="3" data-code-annotation="1">Zero gradients</span>
</dd>
<dt data-target-cell="annotated-cell-10" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-10" data-code-lines="5,6" data-code-annotation="2">Forward pass</span>
</dd>
<dt data-target-cell="annotated-cell-10" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-10" data-code-lines="8" data-code-annotation="3">Backward pass</span>
</dd>
<dt data-target-cell="annotated-cell-10" data-target-annotation="4">4</dt>
<dd>
<span data-code-cell="annotated-cell-10" data-code-lines="10" data-code-annotation="4">Update weights</span>
</dd>
</dl>
</div>
</div></li>
</ol>
</section>
<section id="pytorch-vs.-other-frameworks" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> PyTorch vs.&nbsp;Other Frameworks</h1>
<ol type="1">
<li><p><strong>vs.&nbsp;TensorFlow/Keras</strong>: While TensorFlow 2.x has improved significantly with eager execution, PyTorch still seems much closer to the heart of DL to me. Sure, <code>{keras}</code> module is great for quick prototyping, but I find PyTorch’s explicit approach gives me better control and understanding of what’s happening under the hood.</p></li>
<li><p><strong>vs.&nbsp;JAX</strong>: JAX is actually excellent (been using this for a year now) for numerical computing and offers impressive performance, but PyTorch’s ecosystem, documentation, and community support are currently unmatched, which includes transformers, etc. For production applications and research, I would argue PyTorch provides better tooling and debugging capabilities.</p></li>
</ol>
</section>
<section id="key-features-i-love" class="level1" data-number="7">
<h1 data-number="7"><span class="header-section-number">7</span> Key Features I Love</h1>
<p><strong>Autograd</strong>: It has Automatic differentiation (autograd) and it is seamless. Just call <code>.backward()</code> on your loss, and PyTorch handles the gradient computation.</p>
<p><strong>DataLoader</strong>: Built-in utilities for efficient data loading, batching, and shuffling make preprocessing a breeze.</p>
<p><strong>TorchScript</strong>: When you need to deploy models to production, TorchScript allows you to serialize and optimize your models.</p>
<p><strong>Mixed Precision Training</strong>: Easy to implement with <code>torch.cuda.amp</code> for faster training and reduced memory usage.</p>
</section>
<section id="extra-torch-implementation-in-r" class="level1" data-number="8">
<h1 data-number="8"><span class="header-section-number">8</span> Extra: torch implementation in R</h1>
<p>PyTorch lives in LibTorch, its C++ distribution—the actual computational core where tensors, autograd, and neural network primitives are implemented. Python is “just” one of the frontends that talks to this core.</p>
<p>Thanks to <code>{Rcpp}</code>, the <code>{torch}</code> package in R is able to interface directly with LibTorch. Rather than wrapping Python or relying on <code>{reticulate}</code>, <code>{torch}</code> binds to PyTorch’s C++ backend itself, exposing the same tensor operations, automatic differentiation engine, and neural network building blocks to R users.</p>
<p>This design choice is crucial. It means that <code>{torch}</code> in R is not a secondary or reimplemented version of PyTorch, but another first-class frontend to the same underlying engine. Training a model in R with <code>{torch}</code> executes the very same C++ code paths as training in Python—only the surface language changes. As a result, performance characteristics, numerical behavior, and model semantics remain consistent across languages, while still allowing R users to work in an idiomatic R style.</p>
</section>
<section id="conclusion" class="level1" data-number="9">
<h1 data-number="9"><span class="header-section-number">9</span> Conclusion</h1>
<p>PyTorch has earned its place as the dominant framework in deep learning research and is increasingly popular in production environments. Its intuitive design, flexibility, and powerful features make it my framework of choice. Whether you’re just starting out or you’re an experienced practitioner, PyTorch provides the tools you need to build sophisticated models without unnecessary complexity.</p>
<p>The learning curve is gentle, the documentation is excellent, and the community is supportive. If you haven’t tried PyTorch yet, I highly recommend giving it a shot—you might find, like I did, that you never want to go back.</p>


</section>

 ]]></description>
  <category>Python</category>
  <category>machine-learning</category>
  <category>deep-learning</category>
  <category>torch</category>
  <category>tensorflow</category>
  <category>keras</category>
  <category>neural-networks</category>
  <guid>https://joshuamarie.com/posts/16-pytorch/</guid>
  <pubDate>Fri, 16 Jan 2026 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/16-pytorch/image.png" medium="image" type="image/png" height="87" width="144"/>
</item>
<item>
  <title>Neural Networks in R Remain Viable in 2026</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/15-nn-r/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/15-nn-r&amp;text=Neural%20Networks%20in%20R%20%E2%80%93%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/15-nn-r&amp;title=Neural%20Networks%20in%20R%20%E2%80%93%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Neural%20Networks%20in%20R%20%E2%80%93%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/15-nn-r" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/15-nn-r" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/15-nn-r&amp;title=Neural%20Networks%20in%20R%20%E2%80%93%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/15-nn-r&amp;title=Neural%20Networks%20in%20R%20%E2%80%93%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/15-nn-r&amp;name=Neural%20Networks%20in%20R%20%E2%80%93%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Neural Networks in R – Joshua Marie https://joshuamarie.com/posts/15-nn-r'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/15-nn-r%20Neural%20Networks%20in%20R%20%E2%80%93%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<p><img src="https://joshuamarie.com/posts/15-nn-r/image.png" class="img-fluid"></p>
<section id="introduction" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Introduction</h1>
<p>Python dominates the AI/DL space very well (Hugging Face, diffusion models, large-scale RL), and it’s such a pleasure to deploy those model into production at scale. However, many data scientists, statisticians, bioinformaticians, and analysts continue working in R because:</p>
<ul>
<li>Data naturally lives in tidy data frames, keeping preprocessing, exploration, and visualization in one language</li>
<li>
<a href="https://tidymodels.tidymodels.org">tidymodels</a> provides a consistent, production-ready modeling grammar</li>
<li>
<a href="https://torch.mlverse.org/docs">torch</a> now offers excellent native GPU support without requiring Python installation</li>
<li>Strong deployment options exist (Shiny, Posit Connect/Workbench, plumber APIs, vetiver)</li>
<li>Statistical modeling and deep learning can coexist seamlessly in the same session</li>
</ul></section><section id="why-neural-networks-in-r" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> Why Neural Networks in R?</h1>
<p>You see, R typically isn’t the best choice for training 100B-parameter LLMs or real-time video models, but it excels at:</p>
<ul>
<li>Tabular deep learning</li>
<li>Research prototyping where statistics and interpretability matter</li>
<li>Teaching deep learning in statistics-focused curricula</li>
</ul>
<p>Before diving into technical details, let’s address a key question: why choose R for deep learning when Python has such a robust ecosystem?</p>
<p><em>For many R users, the answer is straightforward: workflow integration.</em></p>
<p><img src="https://joshuamarie.com/posts/15-nn-r/grumpy-cat.gif" class="img-fluid"></p>
<section id="the-current-r-neural-networks-stack-in-2026" class="level2" data-number="2.1"><h2 data-number="2.1" class="anchored" data-anchor-id="the-current-r-neural-networks-stack-in-2026">
<span class="header-section-number">2.1</span> The current R Neural Networks Stack in 2026</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 16%">
<col style="width: 13%">
<col style="width: 13%">
<col style="width: 13%">
<col style="width: 41%">
</colgroup>
<thead><tr class="header">
<th>Package</th>
<th>Backend</th>
<th>Python dep?</th>
<th>tidymodels?</th>
<th>Best for</th>
</tr></thead>
<tbody>
<tr class="odd">
<td><strong>torch</strong></td>
<td>LibTorch</td>
<td>No</td>
<td>Partial</td>
<td>Full control, custom models</td>
</tr>
<tr class="even">
<td><strong>luz</strong></td>
<td>torch</td>
<td>No</td>
<td>No</td>
<td>High-level training loops</td>
</tr>
<tr class="odd">
<td><strong>brulee</strong></td>
<td>torch</td>
<td>No</td>
<td>Yes</td>
<td>Quick MLPs on tabular data</td>
</tr>
<tr class="even">
<td><strong>tabnet</strong></td>
<td>torch</td>
<td>No</td>
<td>Yes</td>
<td>Tabular data (TabNet architecture)</td>
</tr>
<tr class="odd">
<td>
<strong>keras</strong> / tensorflow</td>
<td>TF/Python</td>
<td>Yes</td>
<td>Partial</td>
<td>Familiar Keras API</td>
</tr>
<tr class="even">
<td><strong>kindling</strong></td>
<td>torch</td>
<td>No</td>
<td>Yes</td>
<td>Full architecture tuning</td>
</tr>
</tbody>
</table>
<p><a href="https://torch.mlverse.org/docs">torch</a> is clearly the most actively developed native option in 2026. <a href="https://github.com/tidymodels/brulee">brulee</a> remains the go-to for simple multilayer perceptrons (MLPs) within tidymodels. <a href="https://kindling.joshuamarie.com">kindling</a> aims to combine those—offering more flexible layer configuration than brulee while remaining fully tidymodels-native.</p>
</section></section><section id="the-r-neural-networks-ecosystem-in-2026" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> The R Neural Networks Ecosystem in 2026</h1>
<p>Let’s examine the available tools.</p>
<section id="core-frameworks" class="level2" data-number="3.1"><h2 data-number="3.1" class="anchored" data-anchor-id="core-frameworks">
<span class="header-section-number">3.1</span> Core Frameworks</h2>
<p>These are the primary frameworks for building neural network models.</p>
<section id="torch-for-r" class="level3" data-number="3.1.1"><h3 data-number="3.1.1" class="anchored" data-anchor-id="torch-for-r">
<span class="header-section-number">3.1.1</span> torch for R</h3>
<p>The <a href="https://torch.mlverse.org/docs">torch</a> package provides R bindings to LibTorch (PyTorch’s C++ backend), offering:</p>
<ol type="1">
<li>Full tensor operations with automatic differentiation</li>
<li>GPU acceleration via CUDA</li>
<li>Custom neural network architectures via <code>nn_module</code>
</li>
<li>Pre-trained models and transfer learning capabilities</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">torch</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>randn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">torch_randn</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">torch_matmul</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;">randn</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</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;">randn</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z</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;">torch_matmul</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</section><section id="kerastensorflow" class="level3" data-number="3.1.2"><h3 data-number="3.1.2" class="anchored" data-anchor-id="kerastensorflow">
<span class="header-section-number">3.1.2</span> Keras/TensorFlow</h3>
<p>While <a href="https://github.com/rstudio/tensorflow">tensorflow</a> and <a href="https://tensorflow.rstudio.com/">keras</a> remain available, they’ve become less prominent for R compared to <a href="https://torch.mlverse.org/docs">torch</a> in recent years, and still require Python installation. The torch ecosystem offers better R integration, more active development, and no Python dependency. This framework, however, beats <a href="https://torch.mlverse.org/docs">torch</a> in terms of composability.</p>
<p>Example usage:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">keras</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        model_sequential <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">keras_model_sequential</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">to_categorical</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_dense</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">optimizer_adam</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">compile</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">evaluate</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stats</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">predict</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/matrix.html">as.matrix</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>, <span class="fl" 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="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/integer.html">as.integer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span><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 class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_categorical</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>num_classes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model_sequential</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_dense</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        units <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span>        activation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relu"</span>,</span>
<span>        input_shape <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;"><a href="https://rdrr.io/r/base/nrow.html">ncol</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_dense</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        units <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span>        activation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relu"</span></span>
<span>    <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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_dense</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        units <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span>        activation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"softmax"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compile</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        optimizer <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;">optimizer_adam</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>learning_rate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        loss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categorical_crossentropy"</span>,</span>
<span>        metrics <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"accuracy"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">history</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>,</span>
<span>        epochs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span>        batch_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>,</span>
<span>        validation_split <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span>        verbose <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">evaluate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pred</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;"><a href="https://rdrr.io/r/stats/predict.html">predict</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">class_pred</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;"><a href="https://rdrr.io/r/base/maxCol.html">max.col</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pred</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span></code></pre></div></div>
</div>
</section></section></section><section id="high-level-interfaces" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> High-Level Interfaces</h1>
<p><img src="https://joshuamarie.com/posts/15-nn-r/next-level-steve.gif" class="img-fluid"></p>
<p>This is where things get interesting. Raw <a href="https://torch.mlverse.org/docs">torch</a> requires substantial boilerplate—defining modules, training loops, data loaders, etc. Several packages have emerged to simplify this workflow:</p>
<ul>
<li>
<a href="https://mlverse.github.io/luz/">luz</a>: A high-level interface for torch with built-in training loops</li>
<li>
<a href="https://mlverse.github.io/tabnet/">tabnet</a>: Implements TabNet architecture specifically for tabular data</li>
<li>
<a href="https://github.com/tidymodels/brulee">brulee</a>: High-level modeling functions with torch</li>
<li>
<a href="https://mlr3torch.mlr-org.com/">mlr3torch</a>: Bridges torch deep learning into the <a href="https://mlr3.mlr-org.com">mlr3</a> ecosystem</li>
<li>
<a href="https://kindling.joshuamarie.com">kindling</a>: (My package!) Simplified torch neural network modeling with reduced boilerplate</li>
</ul></section><section id="introducing-kindling-torch-with-less-typing" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Introducing {kindling}: Torch with Less Typing</h1>
<p>Here’s the problem I aimed to solve: <a href="https://torch.mlverse.org/docs">torch</a> is powerful for deep learning and scientific computing, but verbose and less expressive (no offense), while <a href="https://tidymodels.tidymodels.org">tidymodels</a> has an ergonomic API, thanks to <a href="https://tidyverse.tidyverse.org">tidyverse</a> for traditional ML, but has limited deep learning stacks and integrations.</p>
<!-- The `{tidymodels}` framework has `{brulee}` for this kind of thing, but limited to 1-2 hidden layers, while  -->
<p>The <a href="https://kindling.joshuamarie.com">kindling</a> package goes further beyond by enabling you to:</p>
<ol type="1">
<li>Customize the number of hidden layers</li>
<li>Specify activation functions for each layer, and their specific parameters (e.g.&nbsp;<code>lambd</code> from <code>nnf_softshrink()</code> function)</li>
<li>Tune neural network architecture while incorporating both capabilities above</li>
</ol>
<p>I built this package for convenience, consistency, and ease of modeling. In its current implementation, it’s limited to data frames only, and planning to extend this for image datasets.</p>
<p><img src="https://joshuamarie.com/posts/15-nn-r/typing-slow.gif" class="img-fluid"></p>
<div class="callout callout-style-default callout-note callout-titled" title="Core Goal">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Core Goal
</div>
</div>
<div class="callout-body-container callout-body">
<p>The primary goal of this package is to reduce boilerplate and reduce typing when modelling neural network with <a href="https://torch.mlverse.org/docs">torch</a>.</p>
</div>
</div>
<section id="philosophy" class="level2" data-number="5.1"><h2 data-number="5.1" class="anchored" data-anchor-id="philosophy">
<span class="header-section-number">5.1</span> Philosophy</h2>
<p>I powered <a href="https://kindling.joshuamarie.com">kindling</a> with R’s metaprogramming and non-standard evaluation (NSE) through code generation with an embedded Domain-Specific Language (DSL) for specifying activation functions. Under the hood, it generates <code><a href="https://torch.mlverse.org/docs/reference/nn_module.html">torch::nn_module()</a></code> expressions based on your specifications and then wraps training logic into reusable functions.</p>
<p>The <code>*_kindling()</code> functions expose everything through the familiar <a href="https://github.com/tidymodels/parsnip">parsnip</a> interface, integrating seamlessly with <a href="https://tidymodels.tidymodels.org">tidymodels</a>. This package makes neural network modeling with <a href="https://torch.mlverse.org/docs">torch</a> easier, more concise, and more readable (with <a href="https://tidymodels.tidymodels.org">tidymodels</a>, of course).</p>
<p>The package gives you flexibility to work at whatever abstraction level suits your task, from raw torch code to fully integrated tidymodels workflows.</p>
</section><section id="installation" class="level2" data-number="5.2"><h2 data-number="5.2" class="anchored" data-anchor-id="installation">
<span class="header-section-number">5.2</span> Installation</h2>
<p>You can install this package from:</p>
<ol type="1">
<li>
<p>CRAN</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kindling"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pak</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pak.r-lib.org/reference/pkg_install.html">pkg_install</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kindling"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</li>
<li>
<p>GitHub</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">remotes</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://remotes.r-lib.org/reference/install_github.html">install_github</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"joshuamarie/kindling"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pak</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pak.r-lib.org/reference/pak.html">pak</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"joshuamarie/kindling"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</li>
</ol></section><section id="future-plans" class="level2" data-number="5.3"><h2 data-number="5.3" class="anchored" data-anchor-id="future-plans">
<span class="header-section-number">5.3</span> Future Plans</h2>
<p><img src="https://joshuamarie.com/posts/15-nn-r/future-spongebob.gif" class="img-fluid"></p>
<p>As of January 13, 2026, there are some stuffs that I want to add for the future versions. Here are my current plans for the future to enhance this package:</p>
<ol type="1">
<li>More <a href="https://mlr3.mlr-org.com">mlr3</a> integration.</li>
<li>Suitability for time series modelling with customized algorithms.</li>
<li>Ease to add more neural network models, e.g.&nbsp;deep belief networks.</li>
</ol></section></section><section id="references" class="level1" data-number="6"><h1 data-number="6">
<span class="header-section-number">6</span> References</h1>
<p>Falbel D, Luraschi J (2023). <em>torch: Tensors and Neural Networks with ‘GPU’ Acceleration</em>. R package version 0.13.0.</p>
<p>Goodfellow I, Bengio Y, Courville A (2016). <em>Deep Learning</em>. MIT Press.</p>
<p>Kuhn M, Wickham H (2020). <em>Tidymodels: a collection of packages for modeling and machine learning using tidyverse principles</em>.</p>
<p>Wickham H (2019). <em>Advanced R</em>, 2nd edition. Chapman and Hall/CRC.</p>
<p>Keydana S (2023). <em>Deep Learning and Scientific Computing with R torch</em></p>


</section> ]]></description>
  <category>R</category>
  <category>machine-learning</category>
  <category>deep-learning</category>
  <category>torch</category>
  <category>tensorflow</category>
  <category>keras</category>
  <category>tidymodels</category>
  <category>kindling</category>
  <category>neural-networks</category>
  <guid>https://joshuamarie.com/posts/15-nn-r/</guid>
  <pubDate>Fri, 09 Jan 2026 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/15-nn-r/image.png" medium="image" type="image/png" height="121" width="144"/>
</item>
<item>
  <title>The genius invention about aesthetics in ggplot2</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/14-aes-ggplot2/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/14-aes-ggplot2&amp;text=Aesthetics%20in%20ggplot2%20%E2%80%93%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/14-aes-ggplot2&amp;title=Aesthetics%20in%20ggplot2%20%E2%80%93%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Aesthetics%20in%20ggplot2%20%E2%80%93%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/14-aes-ggplot2" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/14-aes-ggplot2" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/14-aes-ggplot2&amp;title=Aesthetics%20in%20ggplot2%20%E2%80%93%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/14-aes-ggplot2&amp;title=Aesthetics%20in%20ggplot2%20%E2%80%93%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/14-aes-ggplot2&amp;name=Aesthetics%20in%20ggplot2%20%E2%80%93%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Aesthetics in ggplot2 – Joshua Marie https://joshuamarie.com/posts/14-aes-ggplot2'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/14-aes-ggplot2%20Aesthetics%20in%20ggplot2%20%E2%80%93%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/image.png" class="img-fluid"></p>
<section id="what-do-i-love-about-aes" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> What do I love about <em>aes()</em>?</h1>
<p>The <code>aes()</code> function is a <strong>domain-specific language</strong> (DSL) embedded inside R — a Lisp-y function that deeply ties with lazy evaluation, quosures, and deep integration with the <a href="https://tidyverse.tidyverse.org">tidyverse</a>.</p>
<p>What keeps pulling me back is how elegantly it balances simplicity for beginners with incredible power for advanced use — all without ever feeling clunky.</p>
<p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/pacha-perfection.gif" class="img-fluid"></p>
</section><section id="mapped-vs-fixed-aesthetics" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> Mapped vs Fixed Aesthetics</h1>
<p>One of the first things to understand is the distinction between <strong>mapped</strong> and <strong>fixed</strong> aesthetics.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<div class="columns">
<div class="column" style="width:50%;">
<section id="the-colors-are-mapped-through-groups" class="level2" data-number="2.1"><h2 data-number="2.1" class="anchored" data-anchor-id="the-colors-are-mapped-through-groups">
<span class="header-section-number">2.1</span> The colors are mapped through groups</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>, color <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;"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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 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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</section>
</div><div class="column" style="width:50%;">
<section id="the-color-is-fixed-for-all-points" class="level2" data-number="2.2"><h2 data-number="2.2" class="anchored" data-anchor-id="the-color-is-fixed-for-all-points">
<span class="header-section-number">2.2</span> The color is fixed for all points</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</section>
</div>
</div>
<p>When you put an aesthetic inside <code>aes()</code>, it’s <strong>mapped</strong> to a variable. Outside <code>aes()</code>, it’s <strong>fixed</strong> to a constant value. This distinction is crucial but often trips up beginners.</p>
</section><section id="the-group-aesthetic" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> The <em>group</em> Aesthetic</h1>
<p>The <code>group</code> aesthetic controls how <a href="https://ggplot2.tidyverse.org">ggplot2</a> groups data for statistical transformations and geoms like lines.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<div class="columns">
<div class="column" style="width:50%;">
<section id="without-group-one-line-connects-all-points" class="level2" data-number="3.1"><h2 data-number="3.1" class="anchored" data-anchor-id="without-group-one-line-connects-all-points">
<span class="header-section-number">3.1</span> Without group, one line connects all points</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span><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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</section>
</div><div class="column" style="width:50%;">
<section id="with-group-separate-lines-for-each-cylinder-count" class="level2" data-number="3.2"><h2 data-number="3.2" class="anchored" data-anchor-id="with-group-separate-lines-for-each-cylinder-count">
<span class="header-section-number">3.2</span> With group, separate lines for each cylinder count</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>, group <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span><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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</section>
</div>
</div>
<p>Often, <code>group</code> is set automatically when you use <code>color</code>, <code>linetype</code>, or <code>fill</code>, but sometimes you need explicit control.</p>
</section><section id="stage-based-transformations-after_stat-and-after_scale" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> Stage-Based Transformations: <em>after_stat()</em> and <em>after_scale()</em>
</h1>
<p>I already made a blog talking about delaying <code>aes()</code> arguments, you can check it <a href="https://joshuamarie.com/posts/09-ggplot2/#the-art-of-delay">here</a>. These functions let you compute aesthetics based on statistical transformations or after scale transformations.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_histogram</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">after_stat</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">after_scale</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">alpha</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<ol type="1">
<li>
<p>Color bars based on computed count from <code>stat_bin</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</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;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <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;">after_stat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">count</span><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>, bins <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</li>
<li>
<p>Use <code>after_scale()</code> to modify colors after scale mapping</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>, color <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;"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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 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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</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;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <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;">after_scale</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;">alpha</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">color</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</li>
</ol>
<p><code>after_stat()</code> accesses variables computed by statistical transformations (like <code>count</code>, <code>density</code>, etc.). <code>after_scale()</code> works with the final scaled aesthetic values, allowing post-processing of colors, sizes, and more.</p>
</section><section id="tidy-evaluation-and-programming-with-aes" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Tidy Evaluation and Programming with <em>aes()</em>
</h1>
<p>When you’re writing functions that create <a href="https://ggplot2.tidyverse.org">ggplot2</a> plots, you need to programmatically pass variable names. This is where tidy evaluation with <code><a href="https://rdrr.io/r/base/Paren.html">{ }</a></code> (curly-curly) and <code>!!</code> (bang-bang) comes in.</p>
<ol type="1">
<li>
<p>The double curly brackets <code><a href="https://rdrr.io/r/base/Paren.html">{ }</a></code> is more recommended</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_scatter</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_var</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y_var</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">color_var</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_minimal</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_var</span> <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>, y <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y_var</span> <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>, color <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">color_var</span> <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 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_scatter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</li>
<li>
<p>With <code><a href="https://rlang.r-lib.org/reference/defusing-advanced.html">rlang::ensym()</a></code>, it accepts both string and a bare column</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_scatter_string</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_var</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y_var</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ensym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ensym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_var</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, y <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ensym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y_var</span><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 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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_scatter_string</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mpg"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hp"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot_scatter_string(mtcars, "mpg", hp)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot_scatter_string(mtcars, mpg, "hp")</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot_scatter_string(mtcars, mpg, hp)</span></span></code></pre></div></div>
</div>
</li>
</ol>
<p>This works because <code>aes()</code> uses quosures (quoted expressions with their environment) internally, which are part of <a href="https://rlang.r-lib.org">rlang</a>’s non-standard evaluation framework.</p>
</section><section id="dplyr-ggplot2-pipe" class="level1" data-number="6"><h1 data-number="6">
<span class="header-section-number">6</span> Integration with the Tidyverse</h1>
<p>The <code>aes()</code> function plays beautifully with <a href="https://tidyverse.tidyverse.org">tidyverse</a> workflows because they share the same evaluation model.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">labs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        efficiency <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>,</span>
<span>        cyl_category <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;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Small"</span>,</span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medium"</span>,</span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Large"</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">efficiency</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl_category</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Efficiency by Weight and Engine Size"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/dplyr-ggplot2-pipe-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>You can pipe data through <a href="https://dplyr.tidyverse.org">dplyr</a> transformations and directly into <a href="https://ggplot2.tidyverse.org">ggplot2</a>, and all the column names work seamlessly. This should be trivial to you since this is more taught by most <a href="https://ggplot2.tidyverse.org">ggplot2</a> tutorials.</p>
</section><section id="other-aesthetic-weaponries-you-want-to-use" class="level1" data-number="7"><h1 data-number="7">
<span class="header-section-number">7</span> Other aesthetic ‘weaponries’ you want to use</h1>
<p>Beyond the common patterns, <code>aes()</code> has several advanced capabilities that showcase its flexibility.</p>
<p>Here, I can name few:</p>
<section id="inheriting-and-overriding-aesthetics" class="level2" data-number="7.1"><h2 data-number="7.1" class="anchored" data-anchor-id="inheriting-and-overriding-aesthetics">
<span class="header-section-number">7.1</span> Inheriting and Overriding Aesthetics</h2>
<p>You can set aesthetics at the plot level and override them in specific layers. This is useful for creating complex plots with different mappings per layer.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_smooth</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">labs</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_line</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_minimal</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, color <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;"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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 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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Override color for this layer only</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, </span>
<span>        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#061E29"</span>, </span>
<span>        se <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span>        linetype <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span></span>
<span>    <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>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then the regression lines for each group</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>stat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"smooth"</span>, method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, se <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Individual trends vs overall trend"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</section><section id="using-aes-with-statistical-transformations" class="level2" data-number="7.2"><h2 data-number="7.2" class="anchored" data-anchor-id="using-aes-with-statistical-transformations">
<span class="header-section-number">7.2</span> Using <em>aes()</em> with Statistical Transformations</h2>
<p>Some geometries perform statistical transformations and create new variables. You can map aesthetics to these computed variables using <code>after_stat()</code> or by knowing the variable names.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_histogram</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_line</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">after_stat</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">labs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_minimal</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">margin</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">element_text</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            y <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;">after_stat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">density</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,  </span>
<span>            fill <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;">after_stat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">density</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>  </span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        bins <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span>        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span>        linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        stat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"density"</span>, </span>
<span>        mapping <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;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            y <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;">after_stat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">density</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,  </span>
<span>            color <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;">after_stat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">density</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>  </span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        show.legend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>option <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plasma"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>option <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plasma"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        title <span 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 with after_stat()"</span>,</span>
<span>        subtitle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y-axis shows density, fill color shows count"</span>,</span>
<span>        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Miles per gallon"</span>,</span>
<span>        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>,</span>
<span>        fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        legend.position <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>,</span>
<span>        plot.margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" 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>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Here we’re using <code>after_stat(scaled)</code> to access the scaled density values and conditionally coloring regions.</p>
</section><section id="constant-aesthetics-inside-aes" class="level2" data-number="7.3"><h2 data-number="7.3" class="anchored" data-anchor-id="constant-aesthetics-inside-aes">
<span class="header-section-number">7.3</span> Constant aesthetics inside <em>aes()</em>
</h2>
<p>In this section, we piped down the result from the data wrangling with <a href="https://dplyr.tidyverse.org">dplyr</a> into <a href="https://ggplot2.tidyverse.org">ggplot2</a> layers. How about the transformation happened within the layer itself? Reminder that <code>aes()</code> is not far from Lisp macros and handles homoiconicity, which means it will handle an expression</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">labs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">scale_color_identity</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">if_else</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, color <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;"><a href="https://rdrr.io/r/base/AsIs.html">I</a></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;">if_else</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgreen"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span><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 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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_identity</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        guide <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"legend"</span>, </span>
<span>        labels <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low MPG"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High MPG"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        breaks <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkgreen"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Efficiency"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p><code><a href="https://rdrr.io/r/base/AsIs.html">I()</a></code> tells <a href="https://ggplot2.tidyverse.org">ggplot2</a> to use the values as-is without scaling, while <code>scale_color_identity()</code> creates a legend.</p>
</section><section id="dynamic-aesthetics" class="level2" data-number="7.4"><h2 data-number="7.4" class="anchored" data-anchor-id="dynamic-aesthetics">
<span class="header-section-number">7.4</span> Dynamic Aesthetics</h2>
<p>You can create aesthetics on-the-fly using R functions directly in <code>aes()</code>. Here, I <em>cut</em> down <code>hp</code> into 3 groups, then use it as a basis for the shapes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">labs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span>,</span>
<span>            shape <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;"><a href="https://rdrr.io/r/base/cut.html">cut</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>, </span>
<span>                breaks <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                labels <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medium"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Horsepower"</span>, shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cylinders"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</section></section><section id="my-argument-regarding-ggplot2-implementation" class="level1" data-number="8"><h1 data-number="8">
<span class="header-section-number">8</span> My argument regarding ggplot2 implementation</h1>
<p>I’ve argued with Python users who say “there’s <code>{plotnine}</code> in Python and you have all the features in <a href="https://ggplot2.tidyverse.org">ggplot2</a> and you can’t look back.” — it’s not even close. I maintain that it’s just a pale imitation: you can’t really fully replicate this package. It’ll take a lot to FULLY replicate it, and it’s TOO complicated for Python to replicate.</p>
<p>The thing is, R in general is functional; the arguments are <em>lazily evaluated</em> by default and similar to Lisp macros, where the AST can be transformed/modified during compile-time—you can pretty much manipulate and inspect expressions before they’re evaluated, giving you metaprogramming capabilities that are first-class citizens in R.</p>
<section id="comparison" class="level2" data-number="8.1"><h2 data-number="8.1" class="anchored" data-anchor-id="comparison">
<span class="header-section-number">8.1</span> Comparison</h2>
<p>Here’s a side by side comparison:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">In R with ggplot2</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">In Python with plotnine</a></li>
</ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>This package uses first-class metaprogramming — you can use clean, bare variable names. You can also override and transform inline.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_smooth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, color <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;"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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 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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</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;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>Python in general lacks this environment as in R, we refer it as <a href="https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Computing-on-the-language-1">“computing on the language”</a>, which dubbed later by Hadley Wickham as “non-standard evaluation”, one of the techniques used in <a href="https://adv-r.hadley.nz/metaprogramming.html">R metaprogramming</a>.</p>
<p>Supplying arguments within <code>aes()</code> from <code>{plotnine}</code> package works with strings. Yes, it works, but notice the quotes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> plotnine <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ggplot, aes, geom_point, geom_smooth</span>
<span id="cb19-2"></span>
<span id="cb19-3">(ggplot(mtcars, aes(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mpg'</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hp * 2'</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'factor(cyl)'</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-4">    geom_point() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-5">    geom_smooth(aes(y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hp * 2'</span>), method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lm'</span>)) </span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/14-aes-ggplot2/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="614"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
</section><section id="why-stringly-typed-code-is-fundamentally-problematic" class="level2" data-number="8.2"><h2 data-number="8.2" class="anchored" data-anchor-id="why-stringly-typed-code-is-fundamentally-problematic">
<span class="header-section-number">8.2</span> Why stringly typed code is fundamentally problematic</h2>
<p>Let me clarify: The Python version suffers from being <a href="http://wiki.c2.com/?StringlyTyped">stringly typed</a>. The fact that <code>{plotnine}</code> forces to approximate things in <a href="https://ggplot2.tidyverse.org">ggplot2</a> R using strings where structured data types should be used. This creates concrete technical problems, so hear me out:</p>
<ol type="1">
<li>
<p>Loss of type safety and compile-time guarantees. All of the arguments supplied into <code>aes()</code> are valid strings to Python, but only one works</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1">aes(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mpg'</span>)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 </span></span>
<span id="cb20-2">aes(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mpgg'</span>)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 </span></span>
<span id="cb20-3">aes(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mpg + '</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 </span></span>
<span id="cb20-4">aes(x <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;">42</span>)         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4 </span></span></code></pre></div></div>
<ol type="1">
<li>This syntax is correct</li>
<li>You made a typo — no error until runtime</li>
<li>Syntax error in the mini-language - no error until runtime</li>
<li>Wrong type, maybe? Depends on plotnine’s implementation</li>
</ol>
<p>In R, <code>aes(x = mpg)</code> is a proper expression that participates in R’s type system and evaluation model.</p>
</li>
<li>
<p>You’re implementing a parser for a mini-language</p>
<p>Plotnine must parse strings like <code>'np.log(mpg) if mpg &gt; 0 else 0'</code> at runtime. This means:</p>
<ul>
<li>Writing a lexer/parser for Python expressions</li>
<li>Handling operator precedence, function calls, conditionals</li>
<li>Maintaining this parser as Python evolves</li>
<li>Debugging when the parser behaves differently than Python itself</li>
</ul>
<p><em>Note that supplying an argument with some kind of transformation, e.g.&nbsp;<code>aes(y = 'np.log(mpg)')</code> still works.</em></p>
<p>In R, it handles (lazy) evaluation:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;"><a href="https://rdrr.io/r/base/ifelse.html">ifelse</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span></code></pre></div></div>
<p>You can take this in different level with <code>stage()</code>, <code>after_stat()</code>, and <code>after_scale()</code>.</p>
</li>
</ol>
<!-- # Why This Implementation Matters --><!-- This design gives `{ggplot2}` its declarative, composable nature. You're building a specification of what you want, and the system figures out the rendering. Python's eager evaluation makes this pattern fundamentally harder to implement cleanly—you'd need to build an entire mini-language or use metaclasses and descriptors to approximate this behavior. --><!-- That's why `{plotnine}` always feels like it's playing catch-up: it's trying to replicate behavior that's deeply tied to R's evaluation model and metaprogramming capabilities. --></section></section><section id="resources" class="level1" data-number="9"><h1 data-number="9">
<span class="header-section-number">9</span> Resources</h1>
<ul>
<li><a href="https://ggplot2.tidyverse.org/">ggplot2 Official Documentation</a></li>
<li><a href="https://ggplot2.tidyverse.org/articles/ggplot2-specs.html">ggplot2 Aesthetic specification</a></li>
</ul>


</section> ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <category>visualization</category>
  <category>tidyverse</category>
  <guid>https://joshuamarie.com/posts/14-aes-ggplot2/</guid>
  <pubDate>Fri, 02 Jan 2026 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/14-aes-ggplot2/image.png" medium="image" type="image/png" height="70" width="144"/>
</item>
<item>
  <title>A ‘careful’ and ‘small’ guide to data science with ‘tidyverse’</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/13-careful-data/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/13-careful-data&amp;text=Careful%20data%20science%20with%20tidyverse%20%E2%80%94%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/13-careful-data&amp;title=Careful%20data%20science%20with%20tidyverse%20%E2%80%94%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Careful%20data%20science%20with%20tidyverse%20%E2%80%94%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/13-careful-data" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/13-careful-data" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/13-careful-data&amp;title=Careful%20data%20science%20with%20tidyverse%20%E2%80%94%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/13-careful-data&amp;title=Careful%20data%20science%20with%20tidyverse%20%E2%80%94%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/13-careful-data&amp;name=Careful%20data%20science%20with%20tidyverse%20%E2%80%94%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Careful data science with tidyverse — Joshua Marie https://joshuamarie.com/posts/13-careful-data'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/13-careful-data%20Careful%20data%20science%20with%20tidyverse%20%E2%80%94%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="why-careful" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Why “careful”?</h1>
<p>As you read in the title, why “careful”? Using <a href="https://tidyverse.tidyverse.org">tidyverse</a>, or any frameworks for data manipulation out there, is fine. Most introductory <a href="https://tidyverse.tidyverse.org">tidyverse</a> tutorials teach you how to write code with it. Very few teach you when the nice-looking code becomes dangerous or unnecessarily painful six months later.</p>
<p>This short guide focuses on the second part — the things that experienced people wish they had known earlier.</p>
<p>Before we start, here are the packages I’ll be using in this post:</p>
<ol type="1">
<li>
<p>Packages that are <a href="https://tidyverse.tidyverse.org">tidyverse</a>. This includes the following specifics:</p>
<ul>
<li><a href="https://dplyr.tidyverse.org">dplyr</a></li>
<li><a href="https://tidyr.tidyverse.org">tidyr</a></li>
<li><a href="https://readr.tidyverse.org">readr</a></li>
<li><a href="https://lubridate.tidyverse.org">lubridate</a></li>
<li><a href="https://purrr.tidyverse.org/">purrr</a></li>
</ul>
</li>
<li>
<p>Packages that are not <a href="https://tidyverse.tidyverse.org">tidyverse</a>. This includes the following specifics:</p>
<ul>
<li><a href="https://klmr.me/box/">box</a></li>
<li><a href="https://github.com/epiverse-trace/numberize">numberize</a></li>
<li><a href="https://broom.tidymodels.org/">broom</a></li>
<li><code>{e1071}</code></li>
</ul>
</li>
</ol>
<p>And they will be used much later on.</p>
</section><section id="tidyverse-is-not-just-unquoted-column-names" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> Tidyverse is not just unquoted column names</h1>
<p>You thought the reason why <a href="https://tidyverse.tidyverse.org">tidyverse</a> framework differ from other frameworks, e.g.&nbsp;Pandas, Polars, or <a href="https://r-datatable.com">data.table</a>, comes from accepting “columns” argument not being quoted? I mean, yes, and <a href="https://r-datatable.com">data.table</a> does this as well, although the syntax and APIs are not completely the same. That’s not what I am trying to convey here.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This fails R CMD check in a package</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <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;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span><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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt;                    mpg cyl disp  hp drat    wt  qsec vs am gear carb      new</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 8.015267</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 7.304348</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 9.827586</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 6.656299</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 5.436047</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># `R CMD CHECK` friendly</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <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;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span><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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt;                    mpg cyl disp  hp drat    wt  qsec vs am gear carb      new</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 8.015267</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 7.304348</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 9.827586</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 6.656299</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 5.436047</span></span></code></pre></div></div>
</div>
<p>Why did I tell you this? Let’s say you’re planning to use this package as one of your package dependencies. Unfortunately, <code>R CMD CHECK</code> will fail and will complain if you are using bare column names, so instead use <code>.data$colname</code>.</p>
<p>Update: Thanks to <a href="https://www.reddit.com/user/Sufficient_Meet6836/">u/Sufficient_Meet6836</a> from this <a href="https://www.reddit.com/r/rstats/comments/1q2vagq/comment/nxkp6ov/?utm_source=share&amp;utm_medium=web3x&amp;utm_name=web3xcss&amp;utm_term=1&amp;utm_content=share_button">thread</a> for pointing it out, and Hadley Wickham respectfully recommends <code>.data[[colname]]</code>, rather than <code>.data$colname</code>.</p>
<p>Here’s why:</p>
<ol type="1">
<li>
<p><code>.data$colname</code> will throws an error once used in a closure:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">new_col</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var2</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var2</span><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;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <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;">new_col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</li>
<li>
<p><code>.data[[colname]]</code> with <code>ensym()</code> is less painful and accepts both bare columns and stringed columns:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">new_col</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var2</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ensym</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">as_string</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var1</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;">as_string</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;">ensym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var1</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var2</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;">as_string</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;">ensym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var2</span><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></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var1</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var2</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <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;">new_col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <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;">new_col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mpg'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'wt'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</li>
</ol>
<p>Another method is the use of <code>{ name }</code> when used in a different context. The most optimal application is when you’re writing functions that takes column names as arguments.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">my_summarise1</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean_col</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span> <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>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <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;">my_summarise1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Sepal.Length</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  mean_col
1 5.843333</code></pre>
</div>
</div>
<p>Here’s more complex as it uses <code>!!</code> (pronounced as “bang-bang”) operator:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">my_summarise2</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summarise</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ensym</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">as_string</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">capt_col</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;">ensym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name_col</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;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean_{as_string(capt_col)}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name_col</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span> <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>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <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;">my_summarise2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Sepal.Length</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  mean_Sepal.Length
1          5.843333</code></pre>
</div>
</div>
<p>Click this <a href="https://dplyr.tidyverse.org/articles/programming.html#indirection">link</a> for more details.</p>
</section><section id="dont-just-straight-data-imports" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> Don’t just straight data imports</h1>
<p>Do not just import data (not limited to CSVs), most especially when the data wasn’t collected in one pattern. For example, a CSV data that has “date” column(s), where it doesn’t follow the right format, e.g.&nbsp;<code>%Y-%m-%d</code> (translated as “2005-01-29” for example). This is probably the #1 source of silent bugs in production data pipelines, especially if overlooked.</p>
<p>But first, let me tell you something:</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>When CSV file contains messy format in general
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>If the data contains messy format in general, set all the columns into string type first</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">readr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">read_csv</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cols</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col_character</span><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></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Dangerous (very common in real life)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.csv"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Much safer minimum</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.csv"</span>,</span>
<span>    col_types <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;">cols</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        .default <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;">col_character</span><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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This force everything as text first </span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>Auto-guessing fails when:</p>
<ol type="1">
<li>First 1000 rows look clean, row 1001 doesn’t</li>
<li>Excel exported dates as text in weird formats</li>
<li>IDs like “001234” get parsed as numbers (losing leading zeros)</li>
<li>Mix of <code>"N/A"</code>, <code>"null"</code>, <code>"NULL"</code>, <code>"-"</code>, <code>"nan"</code> all mean missing</li>
</ol>
</div>
</div>
<section id="synthetic-example-dealing-with-dates" class="level2" data-number="3.1"><h2 data-number="3.1" class="anchored" data-anchor-id="synthetic-example-dealing-with-dates">
<span class="header-section-number">3.1</span> Synthetic example: Dealing with dates</h2>
<p>Here, imagine you have this kind of data in a certain CSV, where it contains filedate/datetime columns that come:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sample_csv</span> <span 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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">date, readings, x, y, z\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"Mar.23,2005\",0.02,3.3,1.1,5.7\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"Feb.26,2005\",0.15,4.5,5.0,1.9\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"Apr.5,2005\",0.2,7.2,2.8,5.2\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"May.12,2005\", 0.5,4.9,1.3,6.8</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div></div>
</div>
<p>If you directly import this CSV data:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://readr.tidyverse.org/reference/read_delim.html">read_csv</a></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;"><a href="https://rdrr.io/r/base/AsIs.html">I</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sample_csv</span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 5
  date        readings     x     y     z
  &lt;chr&gt;          &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 Mar.23,2005     0.02   3.3   1.1   5.7
2 Feb.26,2005     0.15   4.5   5     1.9
3 Apr.5,2005      0.2    7.2   2.8   5.2
4 May.12,2005     0.5    4.9   1.3   6.8</code></pre>
</div>
</div>
<p>The problem arises when <code><a href="https://readr.tidyverse.org/reference/read_delim.html">readr::read_csv()</a></code> fails to parse the <code>"date"</code> column as an actual <code>date</code> type as you expect, instead it is parsed as a string type.</p>
<p>The <a href="https://readr.tidyverse.org">readr</a> package provides an API that can explicitly judge the column type.</p>
<p>Here’s how you do it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">readr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">read_csv</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cols</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col_date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/AsIs.html">I</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sample_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    col_types <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;">cols</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        date <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;">col_date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b.%d,%Y"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 5
  date       readings     x     y     z
  &lt;date&gt;        &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 2005-03-23     0.02   3.3   1.1   5.7
2 2005-02-26     0.15   4.5   5     1.9
3 2005-04-05     0.2    7.2   2.8   5.2
4 2005-05-12     0.5    4.9   1.3   6.8</code></pre>
</div>
</div>
</section><section id="synthetic-example-multiple-messy-date-formats-in-one-column" class="level2" data-number="3.2"><h2 data-number="3.2" class="anchored" data-anchor-id="synthetic-example-multiple-messy-date-formats-in-one-column">
<span class="header-section-number">3.2</span> Synthetic example: Multiple messy date formats in one column</h2>
<p>But, what if the <code>'date'</code> column doesn’t follow 1 pattern?</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">readr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">read_csv</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cols</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col_character</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lubridate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parse_date_time</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">as_date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sample_csv</span> <span 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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">date, readings, x, y, z\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"Mar.23,2005\",0.02,3.3,1.1,5.7\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"Feb 26, 2005\",0.15,4.5,5.0,1.9\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"04-5-2005\",0.2,7.2,2.8,5.2\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"May12,05\", 0.5,4.9,1.3,6.8\n</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"Aug.17,2005\", 0.17,9.5,0.8,4.2</span></span>
<span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/AsIs.html">I</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sample_csv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    col_types <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;">cols</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        date <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;">col_character</span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse_date_time</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">date</span>,</span>
<span>                orders <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b.d,Y"</span>,</span>
<span>                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b d, Y"</span>,</span>
<span>                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m-d-Y"</span>,</span>
<span>                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bd,y"</span></span>
<span>                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <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;">|&gt;</span> </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 5
  date       readings     x     y     z
  &lt;date&gt;        &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 2005-03-23     0.02   3.3   1.1   5.7
2 2005-02-26     0.15   4.5   5     1.9
3 2005-04-05     0.2    7.2   2.8   5.2
4 2005-05-12     0.5    4.9   1.3   6.8
5 2005-08-17     0.17   9.5   0.8   4.2</code></pre>
</div>
</div>
<p>Unfortunately, right now, <code><a href="https://readr.tidyverse.org/reference/read_delim.html">readr::read_csv()</a></code> doesn’t know how to parse CSV files with different data format, so I have to take some roundabouts to properly parse the <code>"date"</code> column.</p>
</section></section><section id="use-tidyverse-aggressively-for-serious-data-cleaning" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> Use tidyverse aggressively for serious data cleaning</h1>
<p>Now you know how easy it get when importing the data with messed up pattern structure. Use <a href="https://tidyverse.tidyverse.org">tidyverse</a> for data cleaning for f sake, trust me. Seriously, if you can do data cleaning from other tools, e.g.&nbsp;Excel or Python-Pandas (I know some companies you are working with will choose them), <a href="https://tidyverse.tidyverse.org">tidyverse</a> makes things much easier, conventional, readable, and maintainable (arguable). Some practitioners actually use <a href="https://tidyverse.tidyverse.org">tidyverse</a> for modeling and resort to base R loops or hacky solutions for cleaning. Don’t be that person.</p>
<p>Let us demonstrate with the <a href="https://raw.githubusercontent.com/eyowhite/Messy-dataset/main/messy_HR_data.csv%22">Messy HR Data</a> dataset from this <a href="https://github.com/eyowhite/Messy-dataset">repository</a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hr_data</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;">readr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://readr.tidyverse.org/reference/read_delim.html">read_csv</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/eyowhite/Messy-dataset/main/messy_HR_data.csv"</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>If you glimpse this data:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hr_data</span> <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;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://pillar.r-lib.org/reference/glimpse.html">glimpse</a></span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 1,000
Columns: 10
$ Name                &lt;chr&gt; "grace", "david", "hannah", "eve", "grace", "jack"…
$ Age                 &lt;chr&gt; "25", "nan", "35", "nan", "nan", "nan", "nan", "40…
$ Salary              &lt;chr&gt; "50000", "65000", "SIXTY THOUSAND", "50000", "NAN"…
$ Gender              &lt;chr&gt; "Male", "Female", "Female", "Female", "Female", "O…
$ Department          &lt;chr&gt; "HR", "Finance", "Sales", "IT", "Finance", "Market…
$ Position            &lt;chr&gt; "Manager", "Director", "Director", "Manager", "Man…
$ `Joining Date`      &lt;chr&gt; "April 5, 2018", "2020/02/20", "01/15/2020", "Apri…
$ `Performance Score` &lt;chr&gt; "D", "F", "C", "A", "F", "F", "B", "C", "C", "A", …
$ Email               &lt;chr&gt; "email@example.com", "user@domain.com", "email@exa…
$ `Phone Number`      &lt;chr&gt; "nan", "123-456-7890", "098-765-4321", NA, "098-76…</code></pre>
</div>
</div>
<p>You see how bad it is, even if you said it’s not “dirty enough” or similar. The reason why <a href="https://tidyverse.tidyverse.org">tidyverse</a> makes things “conventional” because in R, missing values has its own unique representation value, denoted by <code>NA</code>, not through <code>null</code>, <code>NULL</code>, or <code>NaN</code> / <code>nan</code>.</p>
<p>The data cleaning in R with <a href="https://tidyverse.tidyverse.org">tidyverse</a> is much easier than you thought</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">across</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">everything</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">if_else</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stringr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>detect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">str_detect</span>, capitalize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">str_to_sentence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lubridate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parse_date_time</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">as_date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hr_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span><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>            \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;">if_else</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;">detect</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^(?i)nan$"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        `Joining Date` <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;">parse_date_time</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`Joining Date`</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%B %d, %Y"</span>,</span>
<span>                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y/%m/%d"</span>, </span>
<span>                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%m-%d-%Y"</span>,</span>
<span>                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y.%m.%d"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span><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>        Salary <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;">numberize</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/numberize/man/numberize.html">numberize</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Salary</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        Name <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;">capitalize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        Age <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;">numberize</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/numberize/man/numberize.html">numberize</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Age</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1,000 × 10
   Name      Age Salary Gender Department Position  `Joining Date`
   &lt;chr&gt;   &lt;dbl&gt;  &lt;dbl&gt; &lt;chr&gt;  &lt;chr&gt;      &lt;chr&gt;     &lt;date&gt;        
 1 Grace      25  50000 Male   HR         Manager   2018-04-05    
 2 David      NA  65000 Female Finance    Director  2020-02-20    
 3 Hannah     35  60000 Female Sales      Director  2020-01-15    
 4 Eve        NA  50000 Female IT         Manager   2018-04-05    
 5 Grace      NA     NA Female Finance    Manager   2020-01-15    
 6 Jack       NA  65000 Other  Marketing  Director  2019-03-25    
 7 Charlie    NA  50000 Male   Marketing  Clerk     2019-12-01    
 8 Grace      40  50000 Other  HR         Director  2019-03-25    
 9 Hannah     40  60000 Female Marketing  Manager   2020-01-15    
10 Eve        30     NA Other  Finance    Assistant 2020-02-20    
# ℹ 990 more rows
# ℹ 3 more variables: `Performance Score` &lt;chr&gt;, Email &lt;chr&gt;,
#   `Phone Number` &lt;chr&gt;</code></pre>
</div>
</div>
<p>You see how easy is this? The fact that <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">dplyr::mutate()</a></code> accepts bare expressions together with its bare column, you can easily use other functions, like <code><a href="https://rdrr.io/pkg/numberize/man/numberize.html">numberize::numberize()</a></code> to translate bare word value, i.e.&nbsp;<code>"thirty"</code> in <code>Age</code> column and <code>"SIXTY THOUSAND"</code> in <code>Salary</code> column.</p>
</section><section id="use-across-for-safe-type-role-based-transformations" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Use across() + <tidy-select> for safe type / role-based transformations</tidy-select>
</h1>
<p>You see how I clean the data above, right? I use one of <a href="https://tidyverse.tidyverse.org">tidyverse</a>’s most underrated features: <code>across()</code> combined with <code>&lt;tidy-select&gt;</code> helpers, commonly used in “data masking” functions, where the operation is applied <em>across</em> multiple columns. This lets you apply transformations to multiple columns based on their type or name pattern, making your code both DRY and maintainable.</p>
<section id="clean-all-text-columns-at-once" class="level2" data-number="5.1"><h2 data-number="5.1" class="anchored" data-anchor-id="clean-all-text-columns-at-once">
<span class="header-section-number">5.1</span> Clean all text columns at once</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">across</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stringr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">str_trim</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">str_to_upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">survey</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;">tibble</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    resp_id <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" 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>
<span>    name <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  alice  "</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BOB"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  Charlie  "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    city <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York  "</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  Boston"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chicago  "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    rating <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">survey</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</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;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.character</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">str_trim</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</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;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.character</span><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;">&amp;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">resp_id</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">str_to_upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 4
  resp_id name    city     rating
    &lt;dbl&gt; &lt;chr&gt;   &lt;chr&gt;     &lt;dbl&gt;
1       1 ALICE   NEW YORK    4.2
2       2 BOB     BOSTON      3.8
3       3 CHARLIE CHICAGO     4.5</code></pre>
</div>
</div>
<p>The second <code>across()</code> shows combining conditions: all character columns <em>except</em> the ID field.</p>
</section><section id="scale-financial-columns" class="level2" data-number="5.2"><h2 data-number="5.2" class="anchored" data-anchor-id="scale-financial-columns">
<span class="header-section-number">5.2</span> Scale financial columns</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">across</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">starts_with</span><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></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">financials</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;">tibble</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    company <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TechCo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RetailCorp"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    revenue_q1 <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">850000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    revenue_q2 <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1350000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">920000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    costs_q1 <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">800000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">600000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    costs_q2 <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">850000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">640000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">financials</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"revenue"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>,</span>
<span>            .names <span 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}_M"</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 7
  company    revenue_q1 revenue_q2 costs_q1 costs_q2 revenue_q1_M revenue_q2_M
  &lt;chr&gt;           &lt;dbl&gt;      &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;        &lt;dbl&gt;        &lt;dbl&gt;
1 TechCo        1200000    1350000   800000   850000         1.2          1.35
2 RetailCorp     850000     920000   600000   640000         0.85         0.92</code></pre>
</div>
</div>
<p>The <code>.names</code> argument controls output column names. When you add <code>revenue_q3</code> later, it automatically gets converted too.</p>
</section><section id="conditional-rounding" class="level2" data-number="5.3"><h2 data-number="5.3" class="anchored" data-anchor-id="conditional-rounding">
<span class="header-section-number">5.3</span> Conditional rounding</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">across</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">where</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">any_of</span><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></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">measurements</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;">tibble</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    sample_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span>    temperature <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">98.6234</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">99.1456</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">97.8921</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">98.4567</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    pressure <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">120.456</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">118.234</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">122.789</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">119.123</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    ph_level <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.4123</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.3987</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.4256</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.4089</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">measurements</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><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;">&amp;</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;">any_of</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample_id"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">round</span>,</span>
<span>            digits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 4
  sample_id temperature pressure ph_level
      &lt;int&gt;       &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1         1        98.6     120.     7.41
2         2        99.2     118.     7.4 
3         3        97.9     123.     7.43
4         4        98.5     119.     7.41</code></pre>
</div>
</div>
<p>This pattern scales: add new numeric columns and they’re automatically rounded. No need to update the rounding code.</p>
</section><section id="multiple-functions-and-their-names" class="level2" data-number="5.4"><h2 data-number="5.4" class="anchored" data-anchor-id="multiple-functions-and-their-names">
<span class="header-section-number">5.4</span> Multiple functions and their “names”</h2>
<p>The <code>.fns</code> parameter from <code><a href="https://dplyr.tidyverse.org/reference/across.html">dplyr::across()</a></code> function allows multiple functions, stored in a list, and name them (I strongly recommend naming them). They could be a known function, a lambda / anonymous function (written through <code>function()</code> keyword or its short-hand <code>\()</code>), a purrr-style lambda function, or a mix of them — as long as they are “function” objects, nothing else.</p>
<p>My most used scenario is when you want to calculate the descriptive statistics for all numeric columns:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyselect</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">where</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">matches</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summarise</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">across</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>long <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_longer</span>, wide <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_wider</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purrr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">compose</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">partial</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stats</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sd</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">quantile</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">e1071</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">skewness</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">kurtosis</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                n_missing <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;">compose</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sum</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.na</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Since compose() returns a function</span></span>
<span>                mean <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                sd <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                min <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">min</span>, </span>
<span>                q1 <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/quantile.html">quantile</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, prob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                md <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/stats/median.html">median</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                q3 <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/quantile.html">quantile</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, prob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                max <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                skew <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">skewness</span>,</span>
<span>                kurt <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;">partial</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">kurtosis</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Since partial() returns a function</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            </span>
<span>            .names <span 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}..{.fn}"</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">long</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        cols <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;">matches</span><span 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        names_pattern <span 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>        names_to <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"variable"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"statistic"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        values_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"est"</span></span>
<span>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wide</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        names_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">statistic</span>,</span>
<span>        values_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">est</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 11
  variable     n_missing  mean    sd   min    q1    md    q3   max   skew   kurt
  &lt;chr&gt;            &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;
1 Sepal.Length         0  5.84 0.828   4.3   5.1  5.8    6.4   7.9  0.309 -0.606
2 Sepal.Width          0  3.06 0.436   2     2.8  3      3.3   4.4  0.313  0.139
3 Petal.Length         0  3.76 1.77    1     1.6  4.35   5.1   6.9 -0.269 -1.42 
4 Petal.Width          0  1.20 0.762   0.1   0.3  1.3    1.8   2.5 -0.101 -1.36 </code></pre>
</div>
</div>
</section></section><section id="joins-flavor-in-dplyr" class="level1" data-number="6"><h1 data-number="6">
<span class="header-section-number">6</span> Joins flavor in dplyr</h1>
<p>Since the stable version (1.1.x) release of <a href="https://dplyr.tidyverse.org">dplyr</a>, specification of joins is now more flexible, more explicit, and more “controlled” using <code><a href="https://dplyr.tidyverse.org/reference/join_by.html">dplyr::join_by()</a></code>. While the new API provides clearer complex “join” tasks, this also makes the operation less error-prone.</p>
<section id="inequality-joins-for-binning" class="level2" data-number="6.1"><h2 data-number="6.1" class="anchored" data-anchor-id="inequality-joins-for-binning">
<span class="header-section-number">6.1</span> Inequality joins for binning</h2>
<p>Assign customer segments based on purchase totals:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left_join</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">join_by</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">group_by</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">slice_max</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ungroup</span><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></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purchases</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;">tibble</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    customer_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span>    total_spent <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">280</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">520</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">650</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">segments</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;">tibble</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    segment <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bronze"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Silver"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gold"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Platinum"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    min_spend <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purchases</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">segments</span>, by <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_by</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">total_spent</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">min_spend</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">customer_id</span><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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_max</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">min_spend</span>, n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, with_ties <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span><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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 8 × 4
  customer_id total_spent segment  min_spend
        &lt;int&gt;       &lt;dbl&gt; &lt;chr&gt;        &lt;dbl&gt;
1           1          45 Bronze           0
2           2         280 Silver         100
3           3         520 Gold           500
4           4          95 Bronze           0
5           5        1200 Platinum      1000
6           6         180 Silver         100
7           7         650 Gold           500
8           8          35 Bronze           0</code></pre>
</div>
</div>
<p>The “greater than or equal to (<code>&gt;</code> + <code>=</code>)” operator finds all matching tiers, then we keep the highest one. Much clearer than manual conditional logic.</p>
</section><section id="range-joins-for-time-based-matching" class="level2" data-number="6.2"><h2 data-number="6.2" class="anchored" data-anchor-id="range-joins-for-time-based-matching">
<span class="header-section-number">6.2</span> Range joins for time-based matching</h2>
<p>Match transactions to active promotions:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb31" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>tbl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tibble</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inner_join</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">join_by</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">between</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">transmute</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lubridate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">as_date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">transactions</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;">tbl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    tx_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,</span>
<span>    tx_date <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;">as_date</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-01-20"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-02-15"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-03-25"</span>,</span>
<span>        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-04-10"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-05-15"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-06-05"</span></span>
<span>    <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>    amount <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">220</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">175</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">190</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">promos</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;">tbl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    promo <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spring Sale"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Summer Blast"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>    start <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;">as_date</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-01-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-03-01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-05-01"</span><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>    end <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;">as_date</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-01-31"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-04-30"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-06-30"</span><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>    discount <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">transactions</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inner_join</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">promos</span>,</span>
<span>        by <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_by</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;">between</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tx_date</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">start</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">end</span><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>    <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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tx_id</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tx_date</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">promo</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">amount</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">discount</span>, </span>
<span>        savings <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">amount</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">discount</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 6
  tx_id tx_date    promo        amount discount savings
  &lt;int&gt; &lt;date&gt;     &lt;chr&gt;         &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;
1     1 2024-01-20 New Year        150     0.15    22.5
2     3 2024-03-25 Spring Sale     180     0.1     18  
3     4 2024-04-10 Spring Sale     220     0.1     22  
4     5 2024-05-15 Summer Blast    175     0.2     35  
5     6 2024-06-05 Summer Blast    190     0.2     38  </code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled" title="SQL Code Equivalent">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>SQL Code Equivalent
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Here’s the equivalent SQL code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb33-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span></span>
<span id="cb33-2">    tx_id,</span>
<span id="cb33-3">    tx_date,</span>
<span id="cb33-4">    promo,</span>
<span id="cb33-5">    amount,</span>
<span id="cb33-6">    discount,</span>
<span id="cb33-7">    amount <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> discount <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> savings</span>
<span id="cb33-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> (</span>
<span id="cb33-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> t.<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>, p.<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb33-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> transactions <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> t</span>
<span id="cb33-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INNER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> promos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> p</span>
<span id="cb33-12">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> (</span>
<span id="cb33-13">            t.tx_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> p.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">start</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span></span>
<span id="cb33-14">            t.tx_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> p.<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb33-15">        )</span>
<span id="cb33-16">) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> new_table</span></code></pre></div></div>
</div>
</div>
</div>
<p>This beats manually checking date ranges with <code><a href="https://rdrr.io/r/stats/filter.html">filter()</a></code>. The join expresses intent directly.</p>
</section></section><section id="advanced-tidyverse-tricks-for-grouped-modeling" class="level1" data-number="7"><h1 data-number="7">
<span class="header-section-number">7</span> Advanced tidyverse Tricks for Grouped Modeling</h1>
<p>What’s so great about <a href="https://tidyverse.tidyverse.org">tidyverse</a> is while easy to use, there are a lot of things you can take advantage of, such as the code below — it’s far more flexible than you thought. In the example below, I take advantage of <code><a href="https://dplyr.tidyverse.org/reference/reframe.html">dplyr::reframe()</a></code> to generate a new data frame with a variable number of rows directly from grouped operations on the original data.</p>
<p>In this case, we’re exploring the linear relationship between vehicle weight (<code>wt</code>) and fuel efficiency (<code>mpg</code>) in the classic <code>{mtcars}</code> dataset, stratified by the number of cylinders (<code>cyl</code>). The code below computes key summary statistics — including regression coefficients, Pearson correlation, R-squared, and adjusted R-squared—for each cylinder group.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb34" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">group_by</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">reframe</span><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></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reframe</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>         </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stats</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>linear_reg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lm</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coef</span>, pearson_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cor</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purrr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">imap_dfc</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">set_names</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tibble</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>tbl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tibble</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            </span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</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;">linear_reg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coefs</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;"><a href="https://rdrr.io/r/stats/coef.html">coef</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coef_table</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;">imap_dfc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coefs</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bi</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nm</span><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>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</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;">tbl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bi</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_names</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nm</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <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>            </span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">corr</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;">pearson_r</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            </span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test</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;"><a href="https://rdrr.io/r/base/summary.html">summary</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coef_table</span>, </span>
<span>                corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">corr</span>, </span>
<span>                rsq <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r.squared</span>,</span>
<span>                adj_rsq <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">adj.r.squared</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>        </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># , .by = cyl</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 6
    cyl `(Intercept)`    wt   corr   rsq adj_rsq
  &lt;dbl&gt;         &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt;
1     4          39.6 -5.65 -0.713 0.509   0.454
2     6          28.4 -2.78 -0.682 0.465   0.357
3     8          23.9 -2.19 -0.650 0.423   0.375</code></pre>
</div>
</div>
<p>The code cleverly uses <code><a href="https://dplyr.tidyverse.org/reference/reframe.html">dplyr::reframe()</a></code> with an anonymous block <code><a href="https://rdrr.io/r/base/Paren.html">{}</a></code>to run a complete mini-analysis per cylinder group in <code>mtcars</code> dataset. Inside the block, it loads just the needed functions via <code><a href="http://klmr.me/box/reference/use.html">box::use()</a></code>, fits a linear model of <code>mpg ~ wt</code> on the current group, extracts coefficients into a tidy one-row table, computes Pearson correlation, and grabs <img src="https://latex.codecogs.com/png.latex?R%5E2"> and adjusted <img src="https://latex.codecogs.com/png.latex?R%5E2"> from the model summary. Everything is then combined into a single row per group. This compact, namespace-safe pattern lets you perform fairly complex grouped modeling without cluttering the environment with temporary objects.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Beyond p-Values: Why Effect Size and Power Matter
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>I chose this scenario to somehow demonstrate the flaw of NHST, at least part of this “careful” data science guide with <a href="https://tidyverse.tidyverse.org">tidyverse</a>. Here are some insights of the results above why I chose this scenario:</p>
<ul>
<li>
<p>When investigating relationships between variables, it’s tempting to rely solely on p-values from null hypothesis significance testing (NHST). For instance, a simple linear regression on the full <code>mtcars</code> dataset shows a highly significant relationship between <code>wt</code> and <code>mpg</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb36" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</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;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">broom</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://generics.r-lib.org/reference/tidy.html">tidy</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  &lt;chr&gt;          &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;
1 (Intercept)    37.3      1.88      19.9  8.24e-19
2 wt             -5.34     0.559     -9.56 1.29e-10</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb38" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">broom</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://generics.r-lib.org/reference/glance.html">glance</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 12
  r.squared adj.r.squared sigma statistic  p.value    df logLik   AIC   BIC
      &lt;dbl&gt;         &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     0.753         0.745  3.05      91.4 1.29e-10     1  -80.0  166.  170.
# ℹ 3 more variables: deviance &lt;dbl&gt;, df.residual &lt;int&gt;, nobs &lt;int&gt;</code></pre>
</div>
</div>
</li>
<li><p>After running both linear regression and correlation test at the same time, you can say it rejects the null hypothesis of the test, implying that there are strong linear relationship between <code>wt</code> and <code>mpg</code>. Keep in mind that directly using “Null Hypothesis Significant Testing” (NHST) is a direct flaw for reproducibility. The low p-value (&lt; 0.001) and strong correlation suggest a clear negative linear association—higher weight predicts lower mileage.</p></li>
<li>
<p>However, stratifying by cylinder count reveals a more nuanced picture. The 6-cylinder group with only 7 observations fails to reject the null hypothesis (p = 0.092), despite having a similar effect size to the other groups. This isn’t because the relationship doesn’t exist – it’s because the sample size is too small to detect the effect reliably.</p>
<p>This demonstrates why:</p>
<ul>
<li>
<strong>Sample size matters</strong>: The 6-cylinder group lacks statistical power</li>
<li>
<strong>Effect sizes differ</strong>: The slope varies substantially across groups (-2.19 to -5.65), suggesting the relationship isn’t uniform</li>
</ul>
</li>
</ul>
<p>That’s why when doing an analysis, it is better to run simulation before conducting the analysis and the sample size should be large enough.</p>
</div>
</div>
</div>
<!-- # When using parallel mapping from {purrr} package -->
<!-- Keep in mind that the `mutate()` function accepts bare expressions, and this is true if the expression you made allows vectorization — true for all the rows. What if it is not vectorized for all the rows?  -->
<!-- Well, operations in `dplyr::rowwise()` does this, but my solution is, although verbose, it is more explicit, logical, and maintainable. Since it allows bare expression, directly using `purrr::pmap_*()` variants with `dplyr::pick()` are also valid within `mutate()`.  -->
</section><section id="closing-remarks-and-resources" class="level1" data-number="8"><h1 data-number="8">
<span class="header-section-number">8</span> Closing Remarks and Resources</h1>
<p>Those are all my “simple annotation” on your fussy data work, potentially (hoping) will help in your future jobs.</p>
<ul>
<li><a href="https://r4ds.hadley.nz/">R for Data Science</a></li>
<li><a href="https://modules-in-r.joshuamarie.com/">Box: Placing module system into R</a></li>
<li><a href="https://klmr.me/box/">box: Write Reusable, Composable and Modular R Code</a></li>
<li><a href="https://xcelab.net/rm/statistical-rethinking/">Statistical Rethinking</a></li>
<li>
<a href="https://www.tidyverse.org/blog/2023/01/dplyr-1-1-0-joins/">dplyr 1.1.0 blog post</a> - Deep dive on <code>join_by()</code>
</li>
<li>
<a href="https://dplyr.tidyverse.org/articles/programming.html">Programming with dplyr</a> - When to use <code>.data$</code> and <code><a href="https://rdrr.io/r/base/Paren.html">{ }</a></code>
</li>
</ul>


</section> ]]></description>
  <category>R</category>
  <category>data-science</category>
  <category>analytics</category>
  <category>tidyverse</category>
  <guid>https://joshuamarie.com/posts/13-careful-data/</guid>
  <pubDate>Sat, 27 Dec 2025 16:00:00 GMT</pubDate>
</item>
<item>
  <title>Bother yourself writing reusable codes in R, not throwaway codes</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/12-reusable-r-code/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/12-reusable-r-code&amp;text=Reusable%20codes%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/12-reusable-r-code&amp;title=Reusable%20codes%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Reusable%20codes%20in%20R%20%E2%80%94%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/12-reusable-r-code" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/12-reusable-r-code" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/12-reusable-r-code&amp;title=Reusable%20codes%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/12-reusable-r-code&amp;title=Reusable%20codes%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/12-reusable-r-code&amp;name=Reusable%20codes%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Reusable codes in R — Joshua Marie https://joshuamarie.com/posts/12-reusable-r-code'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/12-reusable-r-code%20Reusable%20codes%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="the-problem-with-throwaway-code" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> The Problem with Throwaway Code</h1>
<p>Why bother yourself writing reusable codes? R is so behind in terms of reusability and maintainability of the codes written, and I don’t like the fact that there are piles of garbage codes in the wild — although other languages, even Python, is guilty at this one.</p>
<p>If you’ve been working with R for any length of time, you’ve probably encountered (or written) code that looks something like this:</p>
<div class="cell">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>analysis_final_v3_ACTUAL.R</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://tidyverse.tidyverse.org">tidyverse</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">records</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://readr.tidyverse.org/reference/read_delim.html">read_csv</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"records.csv"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Directly import CSV files into R</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Modify the records a bit</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">records</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">date</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/as.Date.html">as.Date</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">records</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">records</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">records</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><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></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate some statistics</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean_value</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">records</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sd_value</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">records</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Then the usual records viz</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">records</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">date</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_path.html">geom_line</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">theme_minimal</a></span><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></code></pre></div></div>
</div>
</div>
<p>This script works, yes. It does what you need it to do right now, yes. But what happens when you need to:</p>
<ul>
<li>Run the same analysis on a different dataset?</li>
<li>Share this code with a colleague?</li>
<li>Come back to this code in six months?</li>
<li>Use these calculations in another project?</li>
</ul>
<p>You end up copying and pasting, making small modifications, and before you know it, you have <code>analysis_v1.R</code>, <code>analysis_v2.R</code>, <code>analysis_final.R</code>, <code>analysis_final_ACTUAL.R</code>, and <code>analysis_final_ACTUAL_USE_THIS_ONE.R</code> scattered across your projects.</p>
<p>This is throwaway code. It solves an immediate problem but creates long-term technical debt.</p>
</section><section id="what-makes-code-reusable" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> What Makes Code Reusable?</h1>
<p>Reusability of the code is about writing code with intention, structure, and foresight. It shouldn’t be limited about writing functions (though that helps).</p>
<p>Here are the key characteristics:</p>
<ol type="1">
<li>
<strong><em>Clear separation of concerns</em></strong> - Each piece of code should do one thing well. Data loading, cleaning, analysis, and visualization should be separate operations that can be mixed and matched.</li>
<li>
<strong><em>Minimal dependencies</em></strong> - Your code should depend on what it actually needs, not load 20 packages “just in case”. This helps for better long-term maintainability, and easier to understand.</li>
<li>
<strong><em>Explicitness</em></strong> - Functions and codes in general should have clear inputs and outputs. No hidden dependencies on global variables or less mysterious side effects.</li>
<li>
<strong><em>Documentation</em></strong> - Do not just indicate the code with comments, I recommend writing an actual documentation that explains what the code does, what it expects, and what it returns.</li>
</ol></section><section id="the-cost-of-throwaway-code" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> The Cost of Throwaway Code</h1>
<p>Let me be blunt: throwaway code is expensive. Not in terms of money (though that too), but in terms of time, mental energy, and opportunity cost.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Time Wasted on Repetition
</div>
</div>
<div class="callout-body-container callout-body">
<p>Every time you copy-paste code and modify it slightly, you’re not just duplicating code—you’re duplicating bugs, duplicating maintenance burden, and duplicating the cognitive load of understanding what the code does.</p>
</div>
</div>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Broken Knowledge Transfer
</div>
</div>
<div class="callout-body-container callout-body">
<p>When your colleague needs to use your analysis, they shouldn’t need to reverse-engineer a 500-line script to figure out which parts are relevant to them. They shouldn’t need to schedule a meeting to ask you what <code>temp_var_2</code> means.</p>
</div>
</div>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Technical Debt Compounds
</div>
</div>
<div class="callout-body-container callout-body">
<p>That script you wrote six months ago? The one that “just works”? It’s now a black box. You’re afraid to touch it. You build around it instead of on top of it. This is how projects become less maintainable.</p>
</div>
</div>
</section><section id="reusability-in-native-r" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> Reusability in native R</h1>
<p>So how do we write reusable code in R? R offers few functionalities, but they are too fragile and suffered with numbers of limitations. I can’t recommend them enough, even for new R users.</p>
<section id="start-with-functions" class="level2" data-number="4.1"><h2 data-number="4.1" class="anchored" data-anchor-id="start-with-functions">
<span class="header-section-number">4.1</span> Start with Functions</h2>
<p>Even if you think you’ll only use code once, wrap it in a function. Future you will thank present you.</p>
<p>Imagine this code is repeatable:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" 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></div>
<p>Instead, minimize a bit by writing a function that does the similar, but for any data frame:</p>
<div class="cell">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>filter.R</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">retain_positive_value</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span> <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 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span> <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" 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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">retain_positive_value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</div>
<p><em>To know more what I did, please learn more about <a href="https://dplyr.tidyverse.org/articles/programming.html">tidy evaluation</a>.</em></p>
<p>You gotta have to store this function in some R script, R (and programming in general) cannot remember the codes you wrote and you execute, unless you saved the <code>.Rdata</code>, which is a big no-no. So, let’s go to another step.</p>
</section><section id="sourcing-a-script" class="level2" data-number="4.2"><h2 data-number="4.2" class="anchored" data-anchor-id="sourcing-a-script">
<span class="header-section-number">4.2</span> Sourcing a script</h2>
<p>As you know and if you read <a href="https://joshuamarie.com/posts/06-load-pkg/">my previous blog</a>, I have some beefs with package import system, but I have personal beefs with code reusability in R in general. This includes “sourcing a script” using <code><a href="https://rdrr.io/r/base/source.html">source()</a></code> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/source.html">source</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./filter.R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>What’s the big matter about sourcing a script with <code><a href="https://rdrr.io/r/base/source.html">source()</a></code>?</p>
<ol type="1">
<li>Everything from the sourced file goes into your global environment, resulting to a <em>namespace clash</em>.</li>
<li>
<strong>No explicit imports</strong>: You don’t know what functions you’re actually using.</li>
<li>You need to source files in the right order.</li>
<li>
<strong>No encapsulation</strong>: Functions can conflict with each other.</li>
</ol></section><section id="creating-an-r-package" class="level2" data-number="4.3"><h2 data-number="4.3" class="anchored" data-anchor-id="creating-an-r-package">
<span class="header-section-number">4.3</span> Creating an R Package</h2>
<p>If reusability is the problem, I mean, you could turn every project into an R package. But it is too heavy (even the implication of R package being “lightweight”), sometimes overkill, and unnecessary.</p>
<p>That’s because it:</p>
<ul>
<li>Requires understanding package structure</li>
<li>Needs <code>DESCRIPTION</code>, <code>NAMESPACE</code>, and other boilerplates</li>
<li>Must follow CRAN conventions even for internal code (sometimes this is not necessary, but it is when publishing an R package to CRAN)</li>
<li>Overhead of package development for simple projects</li>
</ul>
<p>And besides, the structure of <code>R/</code> in your R package is ALWAYS flat. You can’t organize modules into subdirectories naturally.</p>
</section></section><section id="enter-box-package" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Enter {box} package</h1>
<p>I already have <a href="https://modules-in-r.joshuamarie.com/">a book dedicated to code reusability and module systems using <code>{box}</code> package</a>, with discussions about it. In fact, in every blog post I write, I always use <a href="https://klmr.me/box/">box</a> (sometimes just <code>::</code>) to qualify the imports, rather than loading an entire package and attaching all exports of that particular package.</p>
<p>The <a href="https://klmr.me/box/">box</a> package provides a “lightweight”, modern module system for R that gives you the benefits of packages without the overhead.</p>
<p>Example usage:</p>
<div class="cell">
<details class="code-fold"><summary>Two ways to install this package (you can’t install this package from GitHub)</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"box"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'box'</span>, repos <span 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://klmr.r-universe.dev'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span>,                                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading the package without attaching the names</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data_cleaning</span>,                              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading an entire particular script for data cleaning from the root path</span></span>
<span>    etl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data_cleaning</span>,                        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same as above but the alias was provided</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data_cleaning</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">clean_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">validate_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading some names from a particular script for data cleaning from the root path</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>With <a href="https://klmr.me/box/">box</a>, you get:</p>
<ul>
<li>
<strong>Explicit imports</strong>: Only load what you need</li>
<li>
<strong>Namespace isolation</strong>: No pollution of global environment</li>
<li>
<strong>Module encapsulation</strong>: Clear boundaries between code</li>
<li>
<strong>Simple syntax</strong>: Easy to learn and use</li>
<li>
<strong>Hierarchical structure</strong>: Organize modules in nested directories</li>
</ul>
<section id="organize-your-code-into-modules" class="level2" data-number="5.1"><h2 data-number="5.1" class="anchored" data-anchor-id="organize-your-code-into-modules">
<span class="header-section-number">5.1</span> Organize Your Code into Modules</h2>
<p>Instead of one giant script, break your code into bunch of R scripts as logical modules:</p>
<ul>
<li>
<code>data_loading.R</code> - Functions for reading and importing data</li>
<li>
<code>data_cleaning.R</code> - Functions for cleaning and validation</li>
<li>
<code>analysis.R</code> - Core analytical functions</li>
<li>
<code>visualization.R</code> - Plotting functions</li>
</ul></section><section id="use-a-consistent-structure" class="level2" data-number="5.2"><h2 data-number="5.2" class="anchored" data-anchor-id="use-a-consistent-structure">
<span class="header-section-number">5.2</span> Use a Consistent Structure</h2>
<p>Every project should follow a similar structure so you (and others) know where to find things. Just imagine you have a particular project:</p>
<div class="code-copy-outer-scaffold"><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;">project/</span></span>
<span id="cb7-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> R/</span>
<span id="cb7-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── __init__.R       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;------ This will mark `{./R}` folder as a module</span></span>
<span id="cb7-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── data_loading.R</span>
<span id="cb7-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── data_cleaning.R</span>
<span id="cb7-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── analysis.R</span>
<span id="cb7-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── visualization.R</span>
<span id="cb7-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> data/</span>
<span id="cb7-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> output/</span>
<span id="cb7-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> main.R</span></code></pre></div></div>
</section><section id="writing-a-module" class="level2" data-number="5.3"><h2 data-number="5.3" class="anchored" data-anchor-id="writing-a-module">
<span class="header-section-number">5.3</span> Writing a module</h2>
<p>Under <code>R/analysis.R</code> file, place this practical example code for the module that provides summary statistics:</p>
<div class="cell">
<div class="code-with-filename">
<details class="code-fold"><summary>Code</summary><div class="code-with-filename-file">
<pre><strong>./R/analysis.R</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summarise</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">across</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">relocate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pick</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cur_group_id</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">matches</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_longer</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_wider</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summary_data</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">vars</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.by</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            grp_id <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;"><a href="https://dplyr.tidyverse.org/reference/context.html">cur_group_id</a></span><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>            n <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;"><a href="https://dplyr.tidyverse.org/reference/context.html">n</a></span><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>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/across.html">across</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">vars</span> <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>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                    mean <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    median <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/median.html">median</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                    q25 <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/quantile.html">quantile</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    q75 <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/quantile.html">quantile</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                    sd <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    cv <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    iqr <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/IQR.html">IQR</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    mad <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/mad.html">mad</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>  </span>
<span>                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                .names <span 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}..{.fn}"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            </span>
<span>            .by <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.by</span> <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>        <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;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            cols <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;"><a href="https://tidyselect.r-lib.org/reference/starts_with.html">matches</a></span><span 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            names_pattern <span 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>            names_to <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"variable"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"statistic"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            values_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"est"</span></span>
<span>        <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;">|&gt;</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            names_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">statistic</span>,</span>
<span>            values_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">est</span></span>
<span>        <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;">|&gt;</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/relocate.html">relocate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>, .after <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">variable</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</details>
</div>
</div>
<div class="cell" data-output-fold="true" data-output-summary="Yes, it (just) works">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pander</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>ptb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pandoc.table</span><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></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vars <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, .by <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ptb</span><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></code></pre></div></div>
<details><summary>Yes, it (just) works</summary><div class="cell-output cell-output-stdout">
<pre><code>
------------------------------------------------------------------------
 cyl   grp_id   variable   n    mean    median    q25     q75      sd   
----- -------- ---------- ---- ------- -------- ------- ------- --------
  6      1        mpg      7    19.74    19.7    18.65    21     1.454  

  6      1         hp      7    122.3    110      110     123    24.26  

  6      1         wt      7    3.117   3.215    2.822   3.44    0.3563 

  4      2        mpg      11   26.66     26     22.8    30.4     4.51  

  4      2         hp      11   82.64     91     65.5     96     20.93  

  4      2         wt      11   2.286    2.2     1.885   2.622   0.5696 

  8      3        mpg      14   15.1     15.2    14.4    16.25    2.56  

  8      3         hp      14   209.2   192.5    176.2   241.2   50.98  

  8      3         wt      14   3.999   3.755    3.533   4.014   0.7594 
------------------------------------------------------------------------

Table: Table continues below

 
---------------------------
   cv       iqr      mad   
--------- -------- --------
 0.07362    2.35    1.927  

 0.1984      13     7.413  

 0.1143    0.6175   0.3632 

 0.1691     7.6     6.523  

 0.2533     30.5    32.62  

 0.2492    0.7375   0.5411 

 0.1695     1.85    1.557  

 0.2437      65     44.48  

 0.1899    0.4812   0.4077 
---------------------------</code></pre>
</div>
</details>
</div>
<p>Notice a few key things here:</p>
<ol type="1">
<li>We only import the specific <a href="https://dplyr.tidyverse.org">dplyr</a> and <a href="https://tidyr.tidyverse.org">tidyr</a> functions we need</li>
<li>
<strong><code>#' @export</code> annotation</strong>: This marks the function as public (available when the module is imported)</li>
<li>This module has a function that does one thing: provide summary statistics</li>
</ol>
<p>Then reuse it by:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">analysis</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summary_data</span><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></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vars <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span><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></code></pre></div></div>
</div>
<p>You are also allowed to import multiple functions or even the entire module:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import the module itself without attaching the names (access functions with summary$function_name)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">analysis</span>, </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import specific names</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">analysis</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summary_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">another_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Attach all exported functions</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">analysis</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</section><section id="document-your-functions" class="level2" data-number="5.4"><h2 data-number="5.4" class="anchored" data-anchor-id="document-your-functions">
<span class="header-section-number">5.4</span> Document Your Functions</h2>
<p>Use <a href="https://roxygen2.r-lib.org/">roxygen2</a>-style comments even if you’re not building a package:</p>
<div class="cell">
<div class="code-with-filename">
<details class="code-fold"><summary>Code</summary><div class="code-with-filename-file">
<pre><strong>./R/analysis.R</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summarise</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">across</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">relocate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pick</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cur_group_id</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">matches</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_longer</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_wider</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Get summary data from numeric column</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Calculate comprehensive summary statistics for numeric variables,</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' including measures of central tendency, dispersion, and spread.</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param data A data frame</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param vars Vector of columns</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param .by Optional grouping variable(s)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return A data frame with summary statistics in long format</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @examples </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' mtcars |&gt; summary_data(vars = c(mpg, hp, wt))</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' mtcars |&gt; summary_data(vars = c(mpg, hp), .by = cyl)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summary_data</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">vars</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.by</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            grp_id <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;"><a href="https://dplyr.tidyverse.org/reference/context.html">cur_group_id</a></span><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>            n <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;"><a href="https://dplyr.tidyverse.org/reference/context.html">n</a></span><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>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/across.html">across</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">vars</span> <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>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                    mean <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    median <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/median.html">median</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                    q25 <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/quantile.html">quantile</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    q75 <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/quantile.html">quantile</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                    sd <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    cv <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    iqr <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/IQR.html">IQR</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                    mad <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/mad.html">mad</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>  </span>
<span>                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                .names <span 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}..{.fn}"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            </span>
<span>            .by <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.by</span> <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>        <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;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            cols <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;"><a href="https://tidyselect.r-lib.org/reference/starts_with.html">matches</a></span><span 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            names_pattern <span 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>            names_to <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"variable"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"statistic"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            values_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"est"</span></span>
<span>        <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;">|&gt;</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            names_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">statistic</span>,</span>
<span>            values_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">est</span></span>
<span>        <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;">|&gt;</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/relocate.html">relocate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>, .after <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">variable</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</details>
</div>
</div>
<p>And you can access the documentation through <code><a href="http://klmr.me/box/reference/help.html">box::help()</a></code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">analysis</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/help.html">help</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">analysis</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summary_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</section></section><section id="conclusion" class="level1" data-number="6"><h1 data-number="6">
<span class="header-section-number">6</span> Conclusion</h1>
<p>I don’t know about you (who read this blog post), but writing reusable code isn’t about being pedantic or following rules for the sake of rules. It’s about respecting your future self, your colleagues, and the craft of programming.</p>
<p>R suffers a lot of limitations in terms of reusability and maintainability. Many other programming languages’ users remarks R as being an odd one, and goes to say “it suffers for large projects”, and simply because R doesn’t have a right tool. Fortunately, I can’t thank <a href="https://klmr.me/box/">box</a> package enough by giving R users a modern, lightweight way to organize code into reusable modules without the overhead of creating full packages, similar to Python module system. It’s a middle ground that’s been missing from the R ecosystem.</p>
<p>Start small. Pick one project and try organizing it with <a href="https://klmr.me/box/">box</a>. You’ll quickly see the benefits:</p>
<ul>
<li>Clearer code structure</li>
<li>Easier maintenance</li>
<li>Better collaboration</li>
<li>Less time wasted on repetitive tasks</li>
</ul>
<p>Your future self will thank you. And maybe, just maybe, we can reduce the amount of garbage code in the wild.</p>
</section><section id="resources" class="level1" data-number="7"><h1 data-number="7">
<span class="header-section-number">7</span> Resources</h1>
<ul>
<li><a href="https://klmr.me/box/"><code>{box}</code> package documentation</a></li>
<li><a href="https://github.com/klmr/box"><code>{box}</code> GitHub repository</a></li>
<li><a href="https://modules-in-r.joshuamarie.com/">Box: Placing module system into R</a></li>
<li>
<a href="https://r-pkgs.org/">R Packages book by Hadley Wickham</a> - for when you’re ready to go beyond modules</li>
</ul>


</section> ]]></description>
  <category>R</category>
  <category>box</category>
  <category>programming</category>
  <guid>https://joshuamarie.com/posts/12-reusable-r-code/</guid>
  <pubDate>Fri, 19 Dec 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/12-reusable-r-code/image.png" medium="image" type="image/png"/>
</item>
<item>
  <title>Three levels to compose R functions</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/11-composing-r-function/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/11-composing-r-function&amp;text=Three%20levels%20to%20compose%20R%20functions%20%E2%80%94%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/11-composing-r-function&amp;title=Three%20levels%20to%20compose%20R%20functions%20%E2%80%94%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Three%20levels%20to%20compose%20R%20functions%20%E2%80%94%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/11-composing-r-function" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/11-composing-r-function" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/11-composing-r-function&amp;title=Three%20levels%20to%20compose%20R%20functions%20%E2%80%94%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/11-composing-r-function&amp;title=Three%20levels%20to%20compose%20R%20functions%20%E2%80%94%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/11-composing-r-function&amp;name=Three%20levels%20to%20compose%20R%20functions%20%E2%80%94%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Three levels to compose R functions — Joshua Marie https://joshuamarie.com/posts/11-composing-r-function'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/11-composing-r-function%20Three%20levels%20to%20compose%20R%20functions%20%E2%80%94%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<p>Maybe few people who use R have forgotten already that R is functional by heart. R has Python dogma OO system, thanks to <em>Reference Class</em> (RC) and R6. R functions can be treated as Lisp’s macros, where it can let you meddle the function and <em>abstract syntax tree</em> (AST) of the function call.</p>
<p>R has few ways to compose a function, divided by three (3) levels:</p>
<ol type="1">
<li>Manual Typing</li>
<li>Higher-order Functions</li>
<li>Programmatic</li>
</ol>
<section id="manual-compose" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Level 1: Manual Composition</h1>
<p>At the most basic level, we compose functions by manually writing them out. This is the approach most R users start with, and it’s perfectly valid for many use cases.</p>
<section id="creating-wrapper-functions" class="level2" data-number="1.1"><h2 data-number="1.1" class="anchored" data-anchor-id="creating-wrapper-functions">
<span class="header-section-number">1.1</span> Creating Wrapper Functions</h2>
<p>Sometimes we want to reuse a composition pattern. We can manually create a wrapper function with <code>function</code> keyword:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">normalize</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normalize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -1.2649111 -0.6324555  0.0000000  0.6324555  1.2649111</code></pre>
</div>
</div>
<p>While this level of composition is explicit, straightforward, and easy to understand, it requires writing out each function definition manually, which can become repetitive when you have many similar transformations.</p>
</section><section id="application-in-functionals" class="level2" data-number="1.2"><h2 data-number="1.2" class="anchored" data-anchor-id="application-in-functionals">
<span class="header-section-number">1.2</span> Application in <em>functionals</em>
</h2>
<p>Many base R and tidyverse functions are <em>functionals</em>: they take a function as input and apply it across data (e.g., <code><a href="https://rdrr.io/r/base/lapply.html">sapply()</a></code>, <code><a href="https://purrr.tidyverse.org/reference/map.html">purrr::map()</a></code>, <code><a href="https://rdrr.io/r/stats/integrate.html">integrate()</a></code>).</p>
<p>To compose functions for functionals, instead of manually writing function from the outside of the call:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt2</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/lapply.html">sapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.000000 1.414214 1.732051 2.000000 2.236068</code></pre>
</div>
</div>
<p>You can compose on the fly with anonymous functions (you can just refer it as an “unassigned function” if you want):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/lapply.html">sapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.000000 1.414214 1.732051 2.000000 2.236068</code></pre>
</div>
</div>
<p>Or use the modern shorthand <code>\()</code> (after 4.1):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/lapply.html">sapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1.000000 1.414214 1.732051 2.000000 2.236068</code></pre>
</div>
</div>
<p>Even better — show that <code>\()</code> is just syntactic sugar for <code>function()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/substitute.html">quote</a></span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>function(x) x^2</code></pre>
</div>
</div>
</section></section><section id="higher-order" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> Level 2: Using Higher-order Functions</h1>
<p>How about we comprises the existing functions and give birth to another function? We can easily make that with <code><a href="https://purrr.tidyverse.org/reference/compose.html">purrr::compose()</a></code>. How about we create a function out of the function? That’s <em>function operators</em> in action.</p>
<p>Higher-order functions can return new functions by combining or modifying existing ones, reducing manual code writing.</p>
<section id="function-composition-with-purrrcompose" class="level2" data-number="2.1"><h2 data-number="2.1" class="anchored" data-anchor-id="function-composition-with-purrrcompose">
<span class="header-section-number">2.1</span> Function Composition with purrr::compose()</h2>
<p>The nice thing about this is that it takes multiple functions and creates a single new function that applies them in sequence (the default direction is <em>backwards</em>).</p>
<p>So, instead of writing this manually:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">transform</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/MathFun.html">sqrt</a></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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></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;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/transform.html">transform</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9785184</code></pre>
</div>
</div>
<p>We can compose it through <code><a href="https://purrr.tidyverse.org/reference/compose.html">purrr::compose()</a></code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">purrr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/compose.html">compose</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log</span><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 class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9785184</code></pre>
</div>
</div>
<p>Furthermore, this is also (almost) as readable as using pipe:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <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;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><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 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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 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;"><a href="https://rdrr.io/r/base/MathFun.html">sqrt</a></span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9785184</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p>I have a <a href="https://joshuamarie.com/posts/05-pipes/#the-early-days-pre-pipe-workflows-2010">different blog</a> mentioning on how bad can the nested function call go.</p>
</blockquote>
<p>This function can be read from left to right (by default, it is read vice-versa):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">purrr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/compose.html">compose</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log</span>, .dir <span 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 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 class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5166883</code></pre>
</div>
</div>
<p>Which is an equivalent of:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <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;"><a href="https://rdrr.io/r/base/MathFun.html">sqrt</a></span><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 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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 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;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5166883</code></pre>
</div>
</div>
<p>This function can take the inverse: can be read from left to right (by default, it is read vice-versa):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">transform_forward</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;">purrr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/compose.html">compose</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log</span>, .dir <span 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform_forward</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5166883</code></pre>
</div>
</div>
<p>Which is an equivalent of:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <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;"><a href="https://rdrr.io/r/base/MathFun.html">sqrt</a></span><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 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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 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;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5166883</code></pre>
</div>
</div>
</section><section id="partial-application-with-purrrpartial" class="level2" data-number="2.2"><h2 data-number="2.2" class="anchored" data-anchor-id="partial-application-with-purrrpartial">
<span class="header-section-number">2.2</span> Partial Application with purrr::partial()</h2>
<p>Another function from <a href="https://purrr.tidyverse.org/">purrr</a> is the <code><a href="https://purrr.tidyverse.org/reference/partial.html">purrr::partial()</a></code>, where the application is where you “pre-fill” some arguments of a function, creating a new function with fewer parameters. This is incredibly useful for creating specialized versions of general functions.</p>
<p>Consider this function, where a number argument <code>x</code> is divided by 100:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">divide_by_100</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">divide_by_100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
</div>
<p>We can write as:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">divide_by_100</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;">purrr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/partial.html">partial</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`/`</span>, e2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">divide_by_100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
</div>
<p>If you ask “what’s the point of partial application”, this can be particularly useful in conjunction with functionals and other function operators, and also supports <a href="https://rlang.r-lib.org">rlang</a> and NSE APIs — which means you can write it without “typing too much”.</p>
</section><section id="function-operators-in-general" class="level2" data-number="2.3"><h2 data-number="2.3" class="anchored" data-anchor-id="function-operators-in-general">
<span class="header-section-number">2.3</span> Function Operators in general</h2>
<p>In case you don’t know, decorators in Python are just “function operators” in general, except it is pretty much tied into Python and coated with syntactic sugars.</p>
<p>Function operators take functions as input and return modified functions as output. They’re like functions that operate on the “function space” rather than the data space.</p>
<section id="example-1" class="level3" data-number="2.3.1"><h3 data-number="2.3.1" class="anchored" data-anchor-id="example-1">
<span class="header-section-number">2.3.1</span> Example 1:</h3>
<p>Function operators can add behavior without changing the core logic:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">with_logging</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><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>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/cat.html">cat</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Calling function with args:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/paste.html">paste</a></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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, collapse <span 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</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>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">res</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;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/cat.html">cat</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Result:"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">res</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>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">res</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">logged_sqrt</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;">with_logging</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logged_sqrt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Calling function with args: 16 
Result: 4 </code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
</div>
<p>If you want to do this with Python, here take a look:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> with_logging(f):</span>
<span id="cb32-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> call(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args):</span>
<span id="cb32-3">        args_str <span 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>.join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(a) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> args)</span>
<span id="cb32-4">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Calling function with args: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>args_str<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-5">        res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args)</span>
<span id="cb32-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Result: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> res</span>
<span id="cb32-8">    </span>
<span id="cb32-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> call</span>
<span id="cb32-10"></span>
<span id="cb32-11"></span>
<span id="cb32-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@with_logging</span></span>
<span id="cb32-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> logged_sqrt(x):</span>
<span id="cb32-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb32-15">    </span>
<span id="cb32-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(logged_sqrt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Calling function with args: 16
Result: 4.0
4.0</code></pre>
</div>
</div>
</section><section id="example-2-creating-a-memoization-operator" class="level3" data-number="2.3.2"><h3 data-number="2.3.2" class="anchored" data-anchor-id="example-2-creating-a-memoization-operator">
<span class="header-section-number">2.3.2</span> Example 2: Creating a Memoization Operator</h3>
<p>Memoization caches function results to speed up repeated calls:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb34" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">memoize</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cache</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;"><a href="https://rdrr.io/r/base/environment.html">new.env</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>parent <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;"><a href="https://rdrr.io/r/base/environment.html">emptyenv</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">key</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;"><a href="https://rdrr.io/r/base/paste.html">paste</a></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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, collapse <span 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/exists.html">exists</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">key</span>, envir <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cache</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cache</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">key</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cache</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">key</span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">slow_function</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Sys.sleep.html">Sys.sleep</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fast_function</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;">memoize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">slow_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/system.time.html">system.time</a></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;">fast_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   user  system elapsed 
   0.00    0.00    1.01 </code></pre>
</div>
</div>
</section></section></section><section id="program" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> Level 3: Programmatic Approach</h1>
<p>Swear, this is not an easy thing to do — you need at least better understanding on how to build / generate expressions in R, and this involves understanding metaprogramming in R — Yes, most of the part on manipulating ASTs.</p>
<p>The <code><a href="https://rlang.r-lib.org/reference/new_function.html">rlang::new_function()</a></code> provides an API that programmatically constructs a function expression (yes, it does creates formals, body, and an environment — 3 main components of R functions). Keep in mind that the construction of the function expression with <code><a href="https://rlang.r-lib.org/reference/new_function.html">rlang::new_function()</a></code> happens in runtime.</p>
<section id="start-with-the-basic-first" class="level2" data-number="3.1"><h2 data-number="3.1" class="anchored" data-anchor-id="start-with-the-basic-first">
<span class="header-section-number">3.1</span> Start with the basic first</h2>
<p>The <code>new_function()</code> function takes three arguments:</p>
<ul>
<li>
<code>args</code>: The formal arguments under that function</li>
<li>
<code>body</code>: An expression that takes the function body</li>
<li>
<code>env</code>: If supplied, this will be the parent environment of the function.</li>
</ul>
<p>Let’s start with a function that squares the number:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb36" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">new_function</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">expr</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caller_env</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">square</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;">new_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    args <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">expr</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>    body <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;">call2</span><span 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/substitute.html">quote</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>    env <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;">caller_env</span><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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 25</code></pre>
</div>
</div>
<p>Or instead of <code><a href="https://rdrr.io/r/base/list.html">list()</a></code> in <code>args</code> parameter, how about using <code>pairlist2()</code> instead?</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb38" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pairlist2</span><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></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">square2</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;">new_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    args <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;">pairlist2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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>    body <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;">expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" 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>
<span>    env <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;">caller_env</span><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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 25</code></pre>
</div>
</div>
<p>Alright, you might be asking: What’s the point of using <code>new_function()</code> if we can just use <code>function()</code> instead? The purpose of this approach is that you can <em>programmatically generate functions</em> based on data or configuration.</p>
<p>In fact, this is how I <em>automatically</em> generate <code><a href="https://torch.mlverse.org/docs/reference/nn_module.html">torch::nn_module()</a></code> expression used in my new R package called <a href="https://kindling.joshuamarie.com">kindling</a>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb40" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">kindling</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ffnn_generator</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">act_funs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">args</span><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></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ffnn_generator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    nn_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MyFFNN2"</span>,</span>
<span>    hd_neurons <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, <span class="fl" 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>
<span>    no_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,</span>
<span>    no_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span>    activations <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;">act_funs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">relu</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">selu</span>,</span>
<span>        softshrink <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;"><a href="https://rdrr.io/r/base/args.html">args</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>lambd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"celu(alpha = 0.5)"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>torch::nn_module("MyFFNN2", initialize = function () 
{
    self$fc1 = torch::nn_linear(20, 128, bias = TRUE)
    self$fc2 = torch::nn_linear(128, 64, bias = TRUE)
    self$fc3 = torch::nn_linear(64, 32, bias = TRUE)
    self$fc4 = torch::nn_linear(32, 15, bias = TRUE)
    self$out = torch::nn_linear(15, 5, bias = TRUE)
}, forward = function (x) 
{
    x = self$fc1(x)
    x = torch::nnf_relu(x)
    x = self$fc2(x)
    x = torch::nnf_selu(x)
    x = self$fc3(x)
    x = torch::nnf_softshrink(x, lambd = 0.5)
    x = self$fc4(x)
    x = torch::nnf_celu(x, alpha = 0.5)
    x = self$out(x)
    x
})</code></pre>
</div>
</div>
<p>In which, this function is a reference from this <a href="https://joshuamarie.com/posts/01-meta-nn/">blog post</a> of mine quite a while ago, based on what I learned in <a href="https://adv-r.hadley.nz">Advanced R</a> to automatically generate <code><a href="https://torch.mlverse.org/docs/reference/nn_module.html">torch::nn_module()</a></code>.</p>
</section></section><section id="when-to-use-each-approach-here-are-some-of-my-guides" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> When to Use Each Approach (here are some of my guides)</h1>
<ul>
<li>
<strong>Level 1 (Manual)</strong>: When you’re prototyping, the logic is simple, or you need maximum clarity</li>
<li>
<strong>Level 2 (Higher-order)</strong>: When you want to reuse composition patterns and keep code DRY (Don’t Repeat Yourself)</li>
<li>
<strong>Level 3 (Programmatic)</strong>: When you’re building frameworks, DSLs, or need to generate many similar functions from specifications (my package <a href="https://kindling.joshuamarie.com">kindling</a> is one of the many example packages)</li>
</ul></section><section id="resources" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Resources</h1>
<ul>
<li><a href="https://adv-r.hadley.nz">Advanced R by Hadley Wickham</a></li>
<li><a href="https://purrr.tidyverse.org/">Functional Programming in R with purrr</a></li>
<li><a href="https://rlang.r-lib.org/">rlang Documentation</a></li>
</ul>


</section> ]]></description>
  <category>R</category>
  <category>data-science</category>
  <category>tidyverse</category>
  <category>purrr</category>
  <category>functional-programming</category>
  <category>fp</category>
  <guid>https://joshuamarie.com/posts/11-composing-r-function/</guid>
  <pubDate>Sun, 14 Dec 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/11-composing-r-function/image.png" medium="image" type="image/png"/>
</item>
<item>
  <title>What’s so special about formulas in R</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/10-formulas-r/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/11-formulas-r&amp;text=Formulas%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/11-formulas-r&amp;title=Formulas%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Formulas%20in%20R%20%E2%80%94%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/11-formulas-r" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/11-formulas-r" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/11-formulas-r&amp;title=Formulas%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/11-formulas-r&amp;title=Formulas%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/11-formulas-r&amp;name=Formulas%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Formulas in R — Joshua Marie https://joshuamarie.com/posts/11-formulas-r'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/11-formulas-r%20Formulas%20in%20R%20%E2%80%94%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<p><img src="https://joshuamarie.com/posts/10-formulas-r/image.png" class="img-fluid"></p>
<p>If you’ve used R for any statistical modeling, I am certainly sure you’ve encountered formulas, like, by a lot.</p>
<p>However, what if I told you they’re generally everywhere? For example:</p>
<ol type="1">
<li><p>Linear model <code>lm(y ~ x)</code></p></li>
<li>
<p>ggplot2’s <code>facet_grid()</code> and <code>formula</code> from <code>geom_smooth()</code>:</p>
<pre><code>mtcars |&gt; 
    ggplot(aes(wt, mpg, color = factor(cyl))) + 
    geom_point(size = 3, alpha = 0.7) + 
    theme_minimal(base_size = 12) +  

    # You can modify the existing formula
    geom_smooth(
        method = "lm", 
        formula = y ~ log(x), 
        se = TRUE, 
        alpha = 0.2, 
        linewidth = 1
    ) + 

    # Facet grid accepts formula for rows and cols as an argument
    facet_grid(~ cyl) + 

    scale_color_manual(
        values = c("4" = "#2E86AB", "6" = "#A23B72", "8" = "#F18F01"),
        name = "Cylinders"
    )</code></pre>
</li>
<li>
<p>Machine learning with <a href="https://tidymodels.tidymodels.org">tidymodels</a>’s <a href="https://github.com/tidymodels/recipes">recipes</a> API, e.g.:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recipe</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x3</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_normalize</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;">all_numeric_predictors</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_corr</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;">all_predictors</span><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>, threshold <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</li>
</ol>
<p>The application is not limited to statistical modelling per se, where it describes the relationship between the variables, whereas the left hand side is the dependent variable, while the right hand side is the independent variable, for example. Also, have you ever wondered what makes them tick?</p>
<p>Formula <code>~</code> in R is just another tool, most particular in metaprogramming, where it captures expressions in runtime without evaluating them.</p>
<p>Can we dive deep into what makes formulas special and how you can leverage them in your own code?</p>
<section id="what-exactly-are-the-formulas" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> What exactly are the formulas?</h1>
<p>At their core, formulas are R objects created with the <code>~</code> operator. They look simple:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/class.html">class</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "formula"</code></pre>
</div>
</div>
<p>But there’s more than meets the eye. A formula is actually a call object that stores unevaluated expressions along with their environment.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/typeof.html">typeof</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "language"</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div>
<p>A formula typically has three parts:</p>
<ol type="1">
<li>The tilde operator <code>~</code>
</li>
<li>The left-hand side (LHS) - optional</li>
<li>The right-hand side (RHS) - required</li>
</ol>
<p>Example 1 contains both LHS and RHS:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>  </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>`~`</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>  </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>y</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>  </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>x + z</code></pre>
</div>
</div>
<p>Example 2 contains RHS but LHS:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f2</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>  </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>`~`</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>  </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>x + z</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>It is okay to not have LHS, only RHS. It is definitely not okay to have LHS but RHS.</p>
</div>
</div>
</section><section id="truth-it-doesnt-have-an-exact-meaning" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> Truth: It doesn’t have an exact meaning</h1>
<p>Let me clarify something upfront: formulas in R don’t have a rigid, predefined meaning.</p>
<p>What I mean is this:</p>
<ul>
<li>The formula interface is deliberately vague. The <code>~</code> operator doesn’t enforce any particular semantics; it just captures structure. What that structure <em>means</em> entirely depends on how you choose to interpret it.</li>
</ul>
<p>In short: The formulas in R actually lean on what you exactly do it.</p>
<p>You can really do anything. For instance:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">transform_data</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_lhs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_rhs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">enexprs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">eval_tidy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purrr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">reduce</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tibble</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>tbl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">as_tibble</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dots</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;">enexprs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">new_data</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;">reduce</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dots</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">expr</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col_name</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;"><a href="https://rdrr.io/r/base/character.html">as.character</a></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;">f_lhs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">expr</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">operation</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;">f_rhs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col_name</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval_tidy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">operation</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>, .init <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">new_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">petal_ratio</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Petal.Length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Petal.Width</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pr_2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">petal_ratio</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</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;"><a href="https://rdrr.io/r/base/integer.html">as.integer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 150 × 7
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species petal_ratio  pr_2
          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt;   &lt;int&gt;       &lt;dbl&gt; &lt;dbl&gt;
 1          5.1         3.5          1.4         0.2       1        7     49  
 2          4.9         3            1.4         0.2       1        7     49  
 3          4.7         3.2          1.3         0.2       1        6.5   42.2
 4          4.6         3.1          1.5         0.2       1        7.5   56.2
 5          5           3.6          1.4         0.2       1        7     49  
 6          5.4         3.9          1.7         0.4       1        4.25  18.1
 7          4.6         3.4          1.4         0.3       1        4.67  21.8
 8          5           3.4          1.5         0.2       1        7.5   56.2
 9          4.4         2.9          1.4         0.2       1        7     49  
10          4.9         3.1          1.5         0.1       1       15    225  
# ℹ 140 more rows</code></pre>
</div>
</div>
<p>In this example, I’m treating <code>~</code> like an assignment operator — the left side becomes the column name, and the right side defines the computation. This is completely different from the statistical modeling interpretation where formulas express variable relationships.</p>
<p>In <a href="https://tidyverse.tidyverse.org">tidyverse</a>takes yet another approach, using formulas as anonymous function shorthand:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">purrr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map.html">map_int</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1]   1   4   9  16  25  36  49  64  81 100</code></pre>
</div>
</div>
<p>Here’s another example — a function that uses formulas to define probability distributions, assigning them directly into your environment:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">define_rv</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_rhs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_lhs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">enexprs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">env</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caller_env</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">as_string</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purrr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">walk</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dots</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;">enexprs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">distributions</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;">env</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        normal <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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma</span><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>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                density <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Normal.html">dnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                cdf <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Normal.html">pnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                quantile <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Normal.html">qnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                generate <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span>, sd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>, </span>
<span>        uniform <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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">min</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max</span><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>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                density <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Uniform.html">dunif</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, min <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">min</span>, max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                cdf <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Uniform.html">punif</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, min <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">min</span>, max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                quantile <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Uniform.html">qunif</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, min <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">min</span>, max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                generate <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Uniform.html">runif</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, min <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">min</span>, max <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">target_env</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;">caller_env</span><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>    </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">walk</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dots</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</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;">as_string</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;">f_lhs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dist</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;">f_rhs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rv_object</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;"><a href="https://rdrr.io/r/base/eval.html">eval</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dist</span>, envir <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">distributions</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/assign.html">assign</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rv_object</span>, pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">target_env</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</details>
</div>
<p>Let’s give it a try:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_minimal</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">element_text</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">element_blank</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">element_line</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">labs</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">define_rv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;">normal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" 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>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</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;">uniform</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" 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>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;">generate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</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;">generate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F5AFAF"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Normally distributed"</span>, </span>
<span>        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Uniformly distributed"</span>, </span>
<span>        title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Try visualizing it"</span>, </span>
<span>        subtitle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Generate up to 100 samples"</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey40"</span>, face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>        plot.subtitle <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey40"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        panel.grid.minor <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;">element_blank</span><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>        panel.grid.major <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;">element_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>, linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        axis.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        axis.text <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>        axis.title.x <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        axis.title.y <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/10-formulas-r/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p><em>That’s how vague the true definition of the formula is. </em></p>
<p>Like I said in the intro, formula <code>~</code> in R is just another tool, without an exact meaning. In my application you see, it is most particular in metaprogramming, where it captures expressions without or before evaluating them.</p>
</section><section id="the-ancestor-of-quosures" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> The ancestor of quosures</h1>
<p>Back in 2017, the <a href="https://rlang.r-lib.org">rlang</a> package arrived with an ambitious goal: refine and modernize base R’s metaprogramming tools. One of its key innovations — quosures — draws direct inspiration from R’s formula system.</p>
<section id="it-captures-both-the-expression-and-the-environment" class="level2" data-number="3.1"><h2 data-number="3.1" class="anchored" data-anchor-id="it-captures-both-the-expression-and-the-environment">
<span class="header-section-number">3.1</span> It captures both the expression and the environment</h2>
<p>The formulas themselves are, in fact, objects, which means you can extract and manipulate the expressions they contain. But they also carry something “hidden”: their environment. This dual nature: code plus context, is exactly what inspired quosures in <a href="https://rlang.r-lib.org">rlang</a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/environment.html">environment</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; &lt;environment: R_GlobalEnv&gt;</span></span></code></pre></div></div>
</div>
<p>You can even set a custom environment:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_custom</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/environment.html">environment</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_custom</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlang</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rlang.r-lib.org/reference/env.html">env</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/environment.html">environment</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_custom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; &lt;environment: 0x000001db174bd0b0&gt;</span></span></code></pre></div></div>
</div>
<p><em>Unless needed, don’t try this in your applications</em></p>
<p>Environment capture enables delayed evaluation. When you create a formula inside a function, it retains a snapshot of where it was born:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">create_formula</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">multiplier</span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">multiplier</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_mult</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;">create_formula</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/environment.html">environment</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f_mult</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">multiplier</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The formula remembers!</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; [1] 5</span></span></code></pre></div></div>
</div>
<blockquote class="blockquote">
<p>The <code>environment(f_mult)$multiplier</code> actually remembers!</p>
</blockquote>
</section><section id="difference-between-and-quo" class="level2" data-number="3.2"><h2 data-number="3.2" class="anchored" data-anchor-id="difference-between-and-quo">
<span class="header-section-number">3.2</span> Difference between “~” and quo</h2>
<p>Both formulas and quosures are language objects at their core, but they differ in their class structure:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/class.html">class</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; [1] "formula"</span></span></code></pre></div></div>
</div>
<p>Meanwhile, <code>quo()</code> returns an object with dual inheritance — it’s both a quosure <em>and</em> a formula:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/class.html">class</a></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;">rlang</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rlang.r-lib.org/reference/defusing-advanced.html">quo</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><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><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; [1] "quosure" "formula"</span></span></code></pre></div></div>
</div>
<p>Quosures build on formulas by adding better introspection tools, cleaner printing, and support for quasiquotation (the ability to selectively evaluate parts of an expression).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb30" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">quo</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">quo_get_expr</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">quo_get_env</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</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;">quo</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><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></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quo_get_expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1               </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; mean(x, na.rm = TRUE)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quo_get_env</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2              </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; &lt;environment: R_GlobalEnv&gt;</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>                            </span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quo</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3         </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; &lt;quosure&gt;</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; expr: ^x + 10</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; env:  global</span></span></code></pre></div></div>
</div>
<p>I can explain each commented lines:</p>
<ol type="1">
<li>You can inspect the expression thanks to <code><a href="https://rlang.r-lib.org/reference/quosure-tools.html">rlang::quo_get_expr()</a></code>.</li>
<li>You can inspect the environment thanks to <code><a href="https://rlang.r-lib.org/reference/quosure-tools.html">rlang::quo_get_env()</a></code>.</li>
<li>The <a href="https://rlang.r-lib.org">rlang</a> package provides an good API for quasiquoation, by unquoting the <code>value</code>.</li>
</ol></section><section id="difference-between-and-enquo" class="level2" data-number="3.3"><h2 data-number="3.3" class="anchored" data-anchor-id="difference-between-and-enquo">
<span class="header-section-number">3.3</span> Difference between “~” and enquo</h2>
<p>There’s some noted difference between <code>quo()</code> and <code>enquo()</code> when capturing expressions.</p>
<ol type="1">
<li>
<code>quo()</code> takes the quosure of the expression you write.</li>
<li>
<code>enquo()</code> takes the quosure of the expression taken by the user input.</li>
</ol>
<p>Both are still quosures and inspired by formulas in R, as they return both the same classes.</p>
<p>Now, the distinction between using raw formulas (<code>~</code>) and <code>enquo()</code> is subtle but important when building functions that accept user expressions.</p>
<p>For instance:</p>
<ol type="1">
<li>
<p>Using <code>~</code> captures literally in the current scope</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb31" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">my_formula</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_formula</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">a</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; ~x</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; &lt;environment: 0x000001db15d5db20&gt;</span></span></code></pre></div></div>
</div>
</li>
<li>
<p><code><a href="https://rlang.r-lib.org/reference/enquo.html">rlang::enquo()</a></code> captures what the caller passed</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb32" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">my_enquo</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlang</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rlang.r-lib.org/reference/enquo.html">enquo</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_enquo</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">a</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; &lt;quosure&gt;</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; expr: ^a + b</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;&gt; env:  global</span></span></code></pre></div></div>
</div>
</li>
</ol>
<div class="callout callout-style-default callout-note callout-titled" title="General rule of thumb between `quo()` and `enquo()`">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>General rule of thumb between <code>quo()</code> and <code>enquo()</code>
</div>
</div>
<div class="callout-body-container callout-body">
<p>Use <code>enquo()</code> when building functions that accept user-supplied names or expressions. Use <code>quo()</code> when you’re programmatically constructing expressions yourself.</p>
</div>
</div>
</section></section><section id="why-other-languages-struggle-with-formulas" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> Why other languages struggle with formulas</h1>
<p>Python has an attempt on emulating formulas in R, done by some libraries like <code>{statsmodels}</code>, but failed miserably due to Python’s limitation. The core issue is because Python evaluates expressions eagerly, while R formulas require lazy evaluation</p>
<p>So to approximate (not really even close) R’s formula interface, they had to use strings, which itself is a bad solution.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> statsmodels.formula.api <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> smf</span>
<span id="cb33-2">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> smf.ols(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y ~ x + I(x**2)'</span>, data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df).fit()</span></code></pre></div></div>
<p>Julia has an actual R’s formula interface, but there are some slight differences</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb34-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">GLM</span></span>
<span id="cb34-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@formula</span>(y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df)</span></code></pre></div></div>
<p>The formulas API in R is one of the reasons why some languages cannot fully integrated into other programming languages. The tight integration of:</p>
<ol type="1">
<li>Lazy evaluation</li>
<li>Environment capture<br>
</li>
<li>Special operator semantics</li>
<li>Implicit data masking</li>
</ol>
<p>creates a unique programming model that’s hard to transplant. This is both R’s greatest strength in statistical computing.</p>
</section><section id="further-reading" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Further Reading</h1>
<ul>
<li><a href="https://cran.r-project.org/doc/manuals/r-release/R-lang.html">R Language Definition: Formula Objects</a></li>
<li><a href="https://adv-r.hadley.nz/metaprogramming.html">Advanced R: Metaprogramming</a></li>
<li><a href="https://rlang.r-lib.org/">rlang documentation</a></li>
</ul>


</section> ]]></description>
  <category>R</category>
  <category>formulas</category>
  <category>rlang</category>
  <category>machine-learning</category>
  <category>statistics</category>
  <guid>https://joshuamarie.com/posts/10-formulas-r/</guid>
  <pubDate>Sun, 07 Dec 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/10-formulas-r/image.png" medium="image" type="image/png" height="121" width="144"/>
</item>
<item>
  <title>Things you may or you may not know in ggplot2</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/09-ggplot2/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/09-ggplot2&amp;text=Things%20you%20may%20or%20you%20may%20not%20know%20in%20ggplot2%20%E2%80%94%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/09-ggplot2&amp;title=Things%20you%20may%20or%20you%20may%20not%20know%20in%20ggplot2%20%E2%80%94%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Things%20you%20may%20or%20you%20may%20not%20know%20in%20ggplot2%20%E2%80%94%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/09-ggplot2" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/09-ggplot2" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/09-ggplot2&amp;title=Things%20you%20may%20or%20you%20may%20not%20know%20in%20ggplot2%20%E2%80%94%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/09-ggplot2&amp;title=Things%20you%20may%20or%20you%20may%20not%20know%20in%20ggplot2%20%E2%80%94%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/09-ggplot2&amp;name=Things%20you%20may%20or%20you%20may%20not%20know%20in%20ggplot2%20%E2%80%94%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Things you may or you may not know in ggplot2 — Joshua Marie https://joshuamarie.com/posts/09-ggplot2'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/09-ggplot2%20Things%20you%20may%20or%20you%20may%20not%20know%20in%20ggplot2%20%E2%80%94%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="brief-introduction" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Brief Introduction</h1>
<p>The <a href="https://ggplot2.tidyverse.org">ggplot2</a> package is now over 18 years old (released way back in 2007) and has 2.2 million downloads per month, based on cranlogs. Thank you Hadley Wickham and co. for adapting “The Grammar of Graphics” by Leland Wilkinson into R programming language.</p>
<p>The things to be enumerated here are some brand new features and some hidden in plain sight. Here are my compilations of things you may or you may not know in <a href="https://ggplot2.tidyverse.org">ggplot2</a>. But first, let us load the package through <a href="https://klmr.me/box/">box</a>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</section><section id="starting-from-4.0.0-s7-is-the-new-imported-oo-system" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> 1. Starting from 4.0.0, S7 is the new imported OO system</h1>
<p>The 4.0.0 release of <a href="https://ggplot2.tidyverse.org">ggplot2</a> is now on CRAN few months ago (followed by 4.0.1 — published 18 days ago on November 14, 2025). This release introduces several significant updates, including a major internal shift: <a href="https://ggplot2.tidyverse.org">ggplot2</a> now imports the S7 object-oriented system, a new OO system, developed by the Object-Oriented Programming Working Group, R Consortium.</p>
<p>With this transition, several core internal objects, such as <code>ggplot()</code> and <code>layer()</code>, are now implemented using S7. However, the public-facing API remains largely S3-based, meaning that most users will experience the package exactly as before, with no changes required in existing code.</p>
<div class="cell" data-output-fold="true" data-output-summary="Check out its class">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/class.html">class</a></span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><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><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></code></pre></div></div>
<details><summary>Check out its class</summary><div class="cell-output cell-output-stdout">
<pre><code>[1] "ggplot2::ggplot" "ggplot"          "ggplot2::gg"     "S7_object"      
[5] "gg"             </code></pre>
</div>
</details>
</div>
<p>If you’re upgrading old projects, expect a few things to break (especially if you relied on internal structure). Lock your version with <a href="https://rstudio.github.io/renv/">renv</a> or <a href="https://groundhogr.com/">groundhog</a> until you’re ready.</p>
<p>This move to S7 represents the most substantial internal redesign of ggplot2 since the introduction of <code>ggproto</code> in 2015.</p>
</section><section id="it-has-its-own-oo-system-ggproto" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> 2. It has its own OO system: ggproto</h1>
<p>Even with the latest 4.0.0 release, the actual layers, scales, coords, etc. are still held by <code>ggproto</code> objects. This is what makes ggplot2 so extensible. This OO system allows reference semantics and inheritance method — a classic OO programming scheme.</p>
<div class="cell" data-output-fold="true" data-output-summary="Check out its class">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/class.html">class</a></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;">ggplot2</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/Geom.html">GeomPoint</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<details><summary>Check out its class</summary><div class="cell-output cell-output-stdout">
<pre><code>[1] "GeomPoint" "Geom"      "ggproto"   "gg"       </code></pre>
</div>
</details>
</div>
<p>Wanna know how <code>geom_smooth()</code> secretly uses loess or gam? Just look at <a href="https://github.com/tidyverse/ggplot2/blob/bc605b69de8f0e3e2fb2e510a9c398b5a948146f/R/stat-smooth.R"><code>ggplot2:::StatSmooth$compute_group</code></a>.</p>
</section><section id="it-has-5-ggplot2-based-interactive-viz" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> 3. It has 5 ggplot2-based interactive viz</h1>
<p>There are R packages that makes <a href="https://ggplot2.tidyverse.org">ggplot2</a> interactive, at least several:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 14%">
<col style="width: 44%">
<col style="width: 26%">
<col style="width: 15%">
</colgroup>
<thead><tr class="header">
<th>Package</th>
<th>Strength</th>
<th>Fidelity to static ggplot</th>
<th>Learning curve (verdict)</th>
</tr></thead>
<tbody>
<tr class="odd">
<td>plotly</td>
<td>Dynamic and one-liner using <code>ggplotly()</code>
</td>
<td>Medium (some features lost)</td>
<td>Low (1 star / easy to use)</td>
</tr>
<tr class="even">
<td>ggiraph</td>
<td>Giving full control over tooltip / hover / click</td>
<td>Perfect</td>
<td>Very High (4 stars / difficult for non-native ggplot2 users)</td>
</tr>
<tr class="odd">
<td>ggiraphExtra</td>
<td>Shiny-integrated version of ggiraph</td>
<td>High</td>
<td>Fairly Low (2 stars / quick interactive)</td>
</tr>
<tr class="even">
<td>gginteractive</td>
<td>Minimal code changes, HTMLwidget backend</td>
<td>High</td>
<td>Fairly Low (2 stars / not actively maintained)</td>
</tr>
<tr class="odd">
<td>ggvis (now a legacy)</td>
<td>Once the future, now archived</td>
<td>NULL (I don’t use it)</td>
<td>NULL (I don’t use it)</td>
</tr>
<tr class="even">
<td>vegafacet (2025)</td>
<td>Vega-Lite</td>
<td>Fairly Low (not even ggplot2, but close)</td>
<td>Seriously high (5 stars / still new)</td>
</tr>
</tbody>
</table>
<p>A quick showcase of <a href="https://davidgohel.github.io/ggiraph/">ggiraph</a>:</p>
<div class="cell">
<details class="code-fold"><summary>Click to see the code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggiraph</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point_interactive</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">girafe</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">opts_hover</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">opts_hover_inv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tibble</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rownames_to_column</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames_to_column</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>var <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"car"</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, color <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;"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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 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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point_interactive</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            tooltip <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;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{car} (wt: {wt}; mpg: {mpg})"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            data_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">car</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">girafe</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    ggobj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span>,</span>
<span>    width_svg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span>    height_svg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,</span>
<span>    options <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts_hover</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>css <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fill:yellow;stroke:black;stroke-width:2;cursor:pointer;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts_hover_inv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>css <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opacity:0.3;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div class="girafe html-widget html-fill-item" id="htmlwidget-1619843a415c15e28b68" style="width:100%;height:390px;"></div>
<script type="application/json" data-for="htmlwidget-1619843a415c15e28b68">{"x":{"html":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='ggiraph-svg' role='graphics-document' id='svg_45578868_d63b_42bd_8ad5_3155bfced548' viewBox='0 0 720 432'>\n <defs id='svg_45578868_d63b_42bd_8ad5_3155bfced548_defs'>\n  <clipPath id='svg_45578868_d63b_42bd_8ad5_3155bfced548_c1'>\n   <rect x='0' y='0' width='720' height='432'/>\n  <\/clipPath>\n  <clipPath id='svg_45578868_d63b_42bd_8ad5_3155bfced548_c2'>\n   <rect x='42.15' y='6.97' width='581.56' height='384.95'/>\n  <\/clipPath>\n <\/defs>\n <g id='svg_45578868_d63b_42bd_8ad5_3155bfced548_rootg' class='ggiraph-svg-rootg'>\n  <g clip-path='url(#svg_45578868_d63b_42bd_8ad5_3155bfced548_c1)'>\n   <rect x='0' y='0' width='720' height='432' fill='#FFFFFF' fill-opacity='1' stroke='#FFFFFF' stroke-opacity='1' stroke-width='0.75' stroke-linejoin='round' stroke-linecap='round' class='ggiraph-svg-bg'/>\n   <rect x='0' y='0' width='720' height='432' fill='#FFFFFF' fill-opacity='1' stroke='none'/>\n  <\/g>\n  <g clip-path='url(#svg_45578868_d63b_42bd_8ad5_3155bfced548_c2)'>\n   <polyline points='42.15,343.16 623.71,343.16' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,268.70 623.71,268.70' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,194.24 623.71,194.24' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,119.78 623.71,119.78' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,45.32 623.71,45.32' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='66.82,391.93 66.82,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='202.00,391.93 202.00,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='337.18,391.93 337.18,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='472.37,391.93 472.37,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='607.55,391.93 607.55,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.68' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,380.39 623.71,380.39' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,305.93 623.71,305.93' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,231.47 623.71,231.47' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,157.01 623.71,157.01' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,82.55 623.71,82.55' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='42.15,8.09 623.71,8.09' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='134.41,391.93 134.41,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='269.59,391.93 269.59,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='404.78,391.93 404.78,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='539.96,391.93 539.96,6.97' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.36' stroke-linejoin='round' stroke-linecap='butt'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e1' cx='218.23' cy='216.58' r='3.54pt' fill='#00BA38' fill-opacity='1' stroke='#00BA38' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Mazda RX4 (wt: 2.62; mpg: 21)' data-id='Mazda RX4'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e2' cx='252.7' cy='216.58' r='3.54pt' fill='#00BA38' fill-opacity='1' stroke='#00BA38' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Mazda RX4 Wag (wt: 2.875; mpg: 21)' data-id='Mazda RX4 Wag'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e3' cx='177.67' cy='189.77' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Datsun 710 (wt: 2.32; mpg: 22.8)' data-id='Datsun 710'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e4' cx='298.66' cy='210.62' r='3.54pt' fill='#00BA38' fill-opacity='1' stroke='#00BA38' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Hornet 4 Drive (wt: 3.215; mpg: 21.4)' data-id='Hornet 4 Drive'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e5' cx='329.07' cy='250.83' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Hornet Sportabout (wt: 3.44; mpg: 18.7)' data-id='Hornet Sportabout'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e6' cx='331.78' cy='259.76' r='3.54pt' fill='#00BA38' fill-opacity='1' stroke='#00BA38' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Valiant (wt: 3.46; mpg: 18.1)' data-id='Valiant'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e7' cx='346.65' cy='316.35' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Duster 360 (wt: 3.57; mpg: 14.3)' data-id='Duster 360'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e8' cx='295.28' cy='165.94' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Merc 240D (wt: 3.19; mpg: 24.4)' data-id='Merc 240D'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e9' cx='289.87' cy='189.77' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Merc 230 (wt: 3.15; mpg: 22.8)' data-id='Merc 230'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e10' cx='329.07' cy='243.38' r='3.54pt' fill='#00BA38' fill-opacity='1' stroke='#00BA38' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Merc 280 (wt: 3.44; mpg: 19.2)' data-id='Merc 280'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e11' cx='329.07' cy='264.23' r='3.54pt' fill='#00BA38' fill-opacity='1' stroke='#00BA38' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Merc 280C (wt: 3.44; mpg: 17.8)' data-id='Merc 280C'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e12' cx='414.24' cy='285.08' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Merc 450SE (wt: 4.07; mpg: 16.4)' data-id='Merc 450SE'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e13' cx='368.28' cy='271.68' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Merc 450SL (wt: 3.73; mpg: 17.3)' data-id='Merc 450SL'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e14' cx='375.04' cy='302.95' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Merc 450SLC (wt: 3.78; mpg: 15.2)' data-id='Merc 450SLC'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e15' cx='573.75' cy='374.43' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Cadillac Fleetwood (wt: 5.25; mpg: 10.4)' data-id='Cadillac Fleetwood'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e16' cx='597.27' cy='374.43' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Lincoln Continental (wt: 5.424; mpg: 10.4)' data-id='Lincoln Continental'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e17' cx='586.59' cy='310.39' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Chrysler Imperial (wt: 5.345; mpg: 14.7)' data-id='Chrysler Imperial'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e18' cx='161.45' cy='46.81' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Fiat 128 (wt: 2.2; mpg: 32.4)' data-id='Fiat 128'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e19' cx='82.37' cy='76.59' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Honda Civic (wt: 1.615; mpg: 30.4)' data-id='Honda Civic'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e20' cx='112.11' cy='24.47' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Toyota Corolla (wt: 1.835; mpg: 33.9)' data-id='Toyota Corolla'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e21' cx='197.27' cy='209.13' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Toyota Corona (wt: 2.465; mpg: 21.5)' data-id='Toyota Corona'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e22' cx='339.89' cy='298.48' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Dodge Challenger (wt: 3.52; mpg: 15.5)' data-id='Dodge Challenger'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e23' cx='328.4' cy='302.95' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='AMC Javelin (wt: 3.435; mpg: 15.2)' data-id='AMC Javelin'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e24' cx='383.15' cy='331.24' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Camaro Z28 (wt: 3.84; mpg: 13.3)' data-id='Camaro Z28'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e25' cx='383.82' cy='243.38' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Pontiac Firebird (wt: 3.845; mpg: 19.2)' data-id='Pontiac Firebird'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e26' cx='125.63' cy='122.76' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Fiat X1-9 (wt: 1.935; mpg: 27.3)' data-id='Fiat X1-9'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e27' cx='153.34' cy='142.12' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Porsche 914-2 (wt: 2.14; mpg: 26)' data-id='Porsche 914-2'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e28' cx='68.58' cy='76.59' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Lotus Europa (wt: 1.513; mpg: 30.4)' data-id='Lotus Europa'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e29' cx='292.58' cy='294.01' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Ford Pantera L (wt: 3.17; mpg: 15.8)' data-id='Ford Pantera L'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e30' cx='238.5' cy='235.94' r='3.54pt' fill='#00BA38' fill-opacity='1' stroke='#00BA38' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Ferrari Dino (wt: 2.77; mpg: 19.7)' data-id='Ferrari Dino'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e31' cx='346.65' cy='305.93' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Maserati Bora (wt: 3.57; mpg: 15)' data-id='Maserati Bora'/>\n   <circle id='svg_45578868_d63b_42bd_8ad5_3155bfced548_e32' cx='239.85' cy='210.62' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round' title='Volvo 142E (wt: 2.78; mpg: 21.4)' data-id='Volvo 142E'/>\n  <\/g>\n  <g clip-path='url(#svg_45578868_d63b_42bd_8ad5_3155bfced548_c1)'>\n   <text x='23.43' y='384.39' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>10<\/text>\n   <text x='23.43' y='309.93' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>15<\/text>\n   <text x='23.43' y='235.47' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>20<\/text>\n   <text x='23.43' y='161.02' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>25<\/text>\n   <text x='23.43' y='86.56' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>30<\/text>\n   <text x='23.43' y='12.1' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>35<\/text>\n   <text x='131.3' y='406.22' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>2<\/text>\n   <text x='266.48' y='406.22' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>3<\/text>\n   <text x='401.67' y='406.22' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>4<\/text>\n   <text x='536.85' y='406.22' font-size='8.4pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>5<\/text>\n   <text x='325.93' y='422.08' font-size='10.5pt' font-family='Arial'>wt<\/text>\n   <text transform='translate(17.00,213.06) rotate(-90.00)' font-size='10.5pt' font-family='Arial'>mpg<\/text>\n   <text x='644.63' y='175.05' font-size='10.5pt' font-family='Arial'>factor(cyl)<\/text>\n   <circle cx='653.27' cy='192.14' r='3.54pt' fill='#F8766D' fill-opacity='1' stroke='#F8766D' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round'/>\n   <circle cx='653.27' cy='209.42' r='3.54pt' fill='#00BA38' fill-opacity='1' stroke='#00BA38' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round'/>\n   <circle cx='653.27' cy='226.7' r='3.54pt' fill='#619CFF' fill-opacity='1' stroke='#619CFF' stroke-opacity='1' stroke-width='0.9' stroke-linejoin='round' stroke-linecap='round'/>\n   <text x='668.88' y='196.15' font-size='8.4pt' font-family='Arial'>4<\/text>\n   <text x='668.88' y='213.43' font-size='8.4pt' font-family='Arial'>6<\/text>\n   <text x='668.88' y='230.71' font-size='8.4pt' font-family='Arial'>8<\/text>\n  <\/g>\n <\/g>\n<\/svg>","js":null,"uid":"svg_45578868_d63b_42bd_8ad5_3155bfced548","ratio":1.666666666666667,"settings":{"tooltip":{"css":".tooltip_SVGID_ { padding:5px;background:black;color:white;border-radius:2px;text-align:left; ; position:absolute;pointer-events:none;z-index:999;}","placement":"doc","opacity":0.9,"offx":10,"offy":10,"use_cursor_pos":true,"use_fill":false,"use_stroke":false,"delay_over":200,"delay_out":500},"hover":{"css":".hover_data_SVGID_ { fill:yellow;stroke:black;stroke-width:2;cursor:pointer; }","reactive":false,"nearest_distance":null},"hover_inv":{"css":".hover_inv_SVGID_ { opacity:0.3; }"},"hover_key":{"css":".hover_key_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_key_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_key_SVGID_ { fill:orange;stroke:black; }\nline.hover_key_SVGID_, polyline.hover_key_SVGID_ { fill:none;stroke:orange; }\nrect.hover_key_SVGID_, polygon.hover_key_SVGID_, path.hover_key_SVGID_ { fill:orange;stroke:none; }\nimage.hover_key_SVGID_ { stroke:orange; }","reactive":true},"hover_theme":{"css":".hover_theme_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_theme_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_theme_SVGID_ { fill:orange;stroke:black; }\nline.hover_theme_SVGID_, polyline.hover_theme_SVGID_ { fill:none;stroke:orange; }\nrect.hover_theme_SVGID_, polygon.hover_theme_SVGID_, path.hover_theme_SVGID_ { fill:orange;stroke:none; }\nimage.hover_theme_SVGID_ { stroke:orange; }","reactive":true},"select":{"css":".select_data_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_data_SVGID_ { stroke:none;fill:red; }\ncircle.select_data_SVGID_ { fill:red;stroke:black; }\nline.select_data_SVGID_, polyline.select_data_SVGID_ { fill:none;stroke:red; }\nrect.select_data_SVGID_, polygon.select_data_SVGID_, path.select_data_SVGID_ { fill:red;stroke:none; }\nimage.select_data_SVGID_ { stroke:red; }","type":"multiple","only_shiny":true,"selected":[]},"select_inv":{"css":""},"select_key":{"css":".select_key_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_key_SVGID_ { stroke:none;fill:red; }\ncircle.select_key_SVGID_ { fill:red;stroke:black; }\nline.select_key_SVGID_, polyline.select_key_SVGID_ { fill:none;stroke:red; }\nrect.select_key_SVGID_, polygon.select_key_SVGID_, path.select_key_SVGID_ { fill:red;stroke:none; }\nimage.select_key_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"select_theme":{"css":".select_theme_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_theme_SVGID_ { stroke:none;fill:red; }\ncircle.select_theme_SVGID_ { fill:red;stroke:black; }\nline.select_theme_SVGID_, polyline.select_theme_SVGID_ { fill:none;stroke:red; }\nrect.select_theme_SVGID_, polygon.select_theme_SVGID_, path.select_theme_SVGID_ { fill:red;stroke:none; }\nimage.select_theme_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"zoom":{"min":1,"max":1,"duration":300},"toolbar":{"position":"topright","pngname":"diagram","tooltips":null,"fixed":false,"hidden":[],"delay_over":200,"delay_out":500},"sizing":{"rescale":true,"width":1}}},"evals":[],"jsHooks":[]}</script>
</div>
</div>
</section><section id="customized-fonts" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> 4. Customized fonts</h1>
<p>Font styles to be used in your plot is not limited to the fonts you had in your system — this includes the font found in the web, such as Google fonts.</p>
<p>Thanks to <a href="https://github.com/yixuan/sysfonts">sysfonts</a>, you can do:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sysfonts</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/sysfonts/man/font_add_google.html">font_add_google</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lobster"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lobster"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sysfonts</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/sysfonts/man/font_add_google.html">font_add_google</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Roboto Condensed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"roboto"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">showtext</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/showtext/man/showtext_auto.html">showtext_auto</a></span><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></code></pre></div></div>
</div>
<p>Then use it as the base font family of the whole plot:</p>
<div class="cell">
<details class="code-fold"><summary>Click to see the code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#C70039"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This title is in Lobster font"</span>,</span>
<span>        subtitle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"And the rest of the text too if you want"</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, base_family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lobster"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.subtitle <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey40"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        panel.grid.minor <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;">element_blank</span><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>        panel.grid.major <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;">element_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>, linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        axis.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        axis.text <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/09-ggplot2/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</section><section id="ggplot2-works-perfectly-without-a-data-frame" class="level1" data-number="6"><h1 data-number="6">
<span class="header-section-number">6</span> 5. ggplot2 works perfectly without a data frame</h1>
<p>R’s metaprogramming and its environment semantics is so handy but sometimes quirky: they’re so noble in data analysis and statistical modelling, but sometimes they’re so hard to predict. In many cases when it comes to working with data in R, NSE or the non-standard evaluation is used and one of the core features in <a href="https://tidyverse.tidyverse.org">tidyverse</a>.</p>
<p>Many tutorials and documentations show how to use <a href="https://ggplot2.tidyverse.org">ggplot2</a> with data frames, but do you know that it is still fine to use <a href="https://ggplot2.tidyverse.org">ggplot2</a> without providing a data frame?</p>
<p>The demo:</p>
<div class="cell">
<details class="code-fold"><summary>Click to see the code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</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;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is always required! No need to put a data frame</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#002455"</span>,</span>
<span>        size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>,</span>
<span>        alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>,</span>
<span>        shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, </span>
<span>        se <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,  </span>
<span>        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E7DEAF"</span>,</span>
<span>        fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E7DEAF"</span>,</span>
<span>        alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span>        linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X-axis"</span>,</span>
<span>        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y-axis"</span>,</span>
<span>        title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Scatter Plot with Linear Regression"</span>,</span>
<span>        subtitle <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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pearson's r = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></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;"><a href="https://rdrr.io/r/stats/cor.html">cor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>, </span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        breaks <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;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" 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>
<span>        limits <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" 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>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        breaks <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;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" 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>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, base_family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lobster"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        plot.subtitle <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey40"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        panel.grid.minor <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;">element_blank</span><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>        panel.grid.major <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;">element_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>, linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        axis.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        axis.text <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        plot.margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" 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>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/09-ggplot2/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Although I still recommend providing a data frame since that’s how <a href="https://ggplot2.tidyverse.org">ggplot2</a> is designed to be used — especially in many tutorials and documentations.</p>
</section><section id="the-art-of-delay" class="level1" data-number="7"><h1 data-number="7">
<span class="header-section-number">7</span> 6. The art of delay</h1>
<p>You may haven’t encounter this but there are 3 particular functions that manipulates aesthetic evaluation:</p>
<ol type="1">
<li>
<p><code>stage()</code> lets you delay the evaluation of aesthetics until the plot is drawn.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Simple demo">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Simple demo
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">displ</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            y <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;">stage</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hwy</span>,             </span>
<span>                after_scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>      </span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        stat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>,</span>
<span>        fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4A90E2"</span>,</span>
<span>        alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Using stage() to modify values after scaling"</span>,</span>
<span>        subtitle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y-axis values are doubled after ggplot2's internal scaling"</span>,</span>
<span>        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Engine Displacement (L)"</span>,</span>
<span>        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Highway MPG (scaled × 2)"</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"roboto"</span>, base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        panel.grid.minor <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;">element_blank</span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/09-ggplot2/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
</li>
<li>
<p><code>after_stat()</code> enables you to modify aesthetics based on computed statistics after they have been calculated.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Simple demo">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Simple demo
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hwy</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            y <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;">after_stat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">density</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,  </span>
<span>            fill <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;">after_stat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">count</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>  </span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        bins <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span>        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span>        linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_c</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>option <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plasma"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        title <span 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 with after_stat()"</span>,</span>
<span>        subtitle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y-axis shows density, fill color shows count"</span>,</span>
<span>        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Highway MPG"</span>,</span>
<span>        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>,</span>
<span>        fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, base_family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"roboto"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        legend.position <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/09-ggplot2/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
</li>
<li>
<p><code>after_scale()</code> allows you to modify aesthetics after they have been scaled but before they are rendered.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Simple demo">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Simple demo
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>,</span>
<span>            alpha <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;">after_scale</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;">scales</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://scales.r-lib.org/reference/rescale.html">rescale</a></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;"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></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;"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">color</span><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>, to <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" 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="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>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>option <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magma"</span>, end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Using after_scale() for dynamic transparency"</span>,</span>
<span>        subtitle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Point transparency adjusted based on scaled color values"</span>,</span>
<span>        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weight (1000 lbs)"</span>,</span>
<span>        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Miles per Gallon"</span>,</span>
<span>        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Horsepower"</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, base_family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"roboto"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        panel.grid.minor <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;">element_blank</span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/09-ggplot2/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
</li>
</ol>
<p>These are particularly useful when you want to create plots that depend on dynamic data or when you want to modify aesthetics based on user input in interactive applications.</p>
<p><em>This feature in <a href="https://ggplot2.tidyverse.org">ggplot2</a> is available in version 3.5.0 and later.</em></p>
</section><section id="markdown-within-ggplot2-text-elements" class="level1" data-number="8"><h1 data-number="8">
<span class="header-section-number">8</span> 7. Markdown within {ggplot2} text elements</h1>
<p>What I like about <a href="https://wilkelab.org/ggtext/">ggtext</a> is the ability to use markdown syntax within <a href="https://ggplot2.tidyverse.org">ggplot2</a> text elements, such as titles, subtitles, captions, and axis labels. This means you can easily format your text to include <strong>bold</strong>, <em>italic</em>, <code>code</code>, and even <a href="https://ggplot2.tidyverse.org/">links</a> directly within your plots.</p>
<div class="cell">
<details class="code-fold"><summary>Click to see the code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggtext</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">element_markdown</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>long <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_longer</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stringr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>replace_all <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">str_replace_all</span>, to_title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">str_to_title</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris_colors</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    setosa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D83F87"</span>,   </span>
<span>    versicolor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#36E2A3"</span>,   </span>
<span>    virginica <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7B68EE"</span>    </span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        Species <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;"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span>,</span>
<span>            levels <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"setosa"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"versicolor"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"virginica"</span><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>    <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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">long</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        cols <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;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        names_to  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"features"</span>,</span>
<span>        values_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sizes"</span></span>
<span>    <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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        features <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;">to_title</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;">replace_all</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">features</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="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>    <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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sizes</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span>, fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>, linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, </span>
<span>        outlier.alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span>        linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, </span>
<span>        fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggforce</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggforce.data-imaginist.com/reference/geom_sina.html">geom_sina</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, </span>
<span>        size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>, </span>
<span>        shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>,</span>
<span>        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray20"</span>, </span>
<span>        stroke <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris_colors</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris_colors</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>labels <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;">scales</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://scales.r-lib.org/reference/label_number.html">label_number</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>accuracy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">features</span>, scales <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>, ncol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        title    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;b&gt;Iris Dataset – Distribution of Measurements by Species&lt;/b&gt;"</span>,</span>
<span>        subtitle <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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species: &lt;span style='color:#D83F87;'&gt;**setosa**&lt;/span&gt; | "</span>,</span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;span style='color:#36E2A3;'&gt;**versicolor**&lt;/span&gt; | "</span>,</span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;span style='color:#7B68EE;'&gt;**virginica**&lt;/span&gt;"</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span>        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span>    <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lobster"</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.title <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;">element_markdown</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        plot.subtitle <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;">element_markdown</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>, margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        strip.text <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>, face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray30"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        strip.background <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;">element_rect</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray95"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        axis.text.x <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;">element_blank</span><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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># element_text(size = 12)</span></span>
<span>        axis.text.y <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        axis.ticks.x <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;">element_blank</span><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>        panel.grid.major.y <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;">element_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray88"</span>, linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,  </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># panel.grid.minor.y = element_line(color = "gray94", linewidth = 0.25), </span></span>
<span>        legend.position <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/09-ggplot2/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>The <code>element_markdown()</code> function replaces standard theme text elements and parses markdown/HTML. You can use:</p>
<ul>
<li>Markdown: <code>**bold**</code>, <code>*italic*</code>, <code>`code`</code>
</li>
<li>HTML tags: <code>&lt;span&gt;</code>, <code>&lt;br&gt;</code>, <code>&lt;sup&gt;</code>, <code>&lt;sub&gt;</code>, and more</li>
<li>Inline CSS: <code>&lt;span style='color:red;'&gt;colored text&lt;/span&gt;</code>
</li>
</ul></section><section id="resources" class="level1" data-number="9"><h1 data-number="9">
<span class="header-section-number">9</span> Resources</h1>
<ul>
<li><a href="https://ggplot2.tidyverse.org/">ggplot2 Official Documentation</a></li>
<li><a href="https://ggplot2-book.org/">ggplot2 Book by Hadley Wickham</a></li>
<li><a href="https://github.com/tidyverse/ggplot2">ggplot2 GitHub Repository</a></li>
<li><a href="https://r-graphics.org/">R Graphics Cookbook by Winston Chang</a></li>
<li><a href="https://github.com/yixuan/showtext">showtext Package Documentation</a></li>
<li><a href="https://coolbutuseless.github.io/package/ggpattern/">ggpattern Package</a></li>
<li><a href="https://davidgohel.github.io/ggiraph/">ggiraph Package</a></li>
<li><a href="https://github.com/RConsortium/S7">S7 OOP System</a></li>
<li><a href="https://exts.ggplot2.tidyverse.org/">ggplot2 Extensions Gallery</a></li>
<li><a href="https://www.cedricscherer.com/">Cedric Scherer’s ggplot2 Tutorials</a></li>
</ul>


</section> ]]></description>
  <category>R</category>
  <category>ggplot2</category>
  <category>data-science</category>
  <category>visualization</category>
  <category>tidyverse</category>
  <category>viz</category>
  <category>programming</category>
  <guid>https://joshuamarie.com/posts/09-ggplot2/</guid>
  <pubDate>Sun, 30 Nov 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/09-ggplot2/image.png" medium="image" type="image/png"/>
</item>
<item>
  <title>Why SQL + R is an affable combo when I start learning SQL?</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/08-sql-r/</link>
  <description><![CDATA[ 
<div class="page-columns page-rows-contents page-layout-article"><div class="social-share"><a href="https://twitter.com/share?url=https://joshuamarie.com/posts/08-sql-r&amp;text=Why SQL + R is an affable combo when I start learning SQL? — Joshua Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/08-sql-r&amp;title=Why SQL + R is an affable combo when I start learning SQL? — Joshua Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Why SQL + R is an affable combo when I start learning SQL? — Joshua Marie&amp;body=Check out this link:https://joshuamarie.com/posts/08-sql-r" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/08-sql-r" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/08-sql-r&amp;title=Why SQL + R is an affable combo when I start learning SQL? — Joshua Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/08-sql-r&amp;title=Why SQL + R is an affable combo when I start learning SQL? — Joshua Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/08-sql-r&amp;name=Why SQL + R is an affable combo when I start learning SQL? — Joshua Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Why SQL + R is an affable combo when I start learning SQL? — Joshua Marie https://joshuamarie.com/posts/08-sql-r'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/08-sql-r Why SQL + R is an affable combo when I start learning SQL? — Joshua Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a></div></div>





<p><img src="https://joshuamarie.com/posts/08-sql-r/image.png" class="img-fluid"></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<p>I am already using R since 2018, and uses SQL since around 2022-2023. Way back in 2023, I am learning one of the most valuable feature in R, and that’s the ability to integrate R into other software. That’s because I only use softwares independently, i.e.&nbsp;R only, Python only, etc. This is how I first learn SQL, and I learn few frameworks that integrates R and SQL databases.</p>
<p>If you’ve spent any time in data science, I am sure you encountered language wars and such debates — there’s like hundreds or maybe thousands of blogs spread in the community comparing which languages is better or worse. I am not talking about that in this blog post, however, here’s the thing — it’s not really a versus situation. SQL and R are like peanut butter and jelly. Each is good on its own, but why not both? <em>Flavorful</em>.</p>
<!-- ![](why-not-both.gif) -->
<p>Regardless, SQL excels at what databases do best: storing, organizing, and retrieving massive amounts of data with lightning speed. R, on the other hand, shines where creativity and complexity matter: statistical modeling, advanced visualizations, and transforming raw data into insights that actually mean something.</p>
<p>In this post, I’ll show you why combo-ing R and SQL isn’t just nice to have — it’s my stack. And more importantly, I’ll show you what I know how to make them work together so seamlessly you’ll wonder how you ever worked any other way.</p>
</section>
<section id="why-learn-sql-through-r" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="why-learn-sql-through-r"><span class="header-section-number">2</span> Why Learn SQL Through R?</h2>
<p>Before we dive into the technical details, let me explain why R is actually a fantastic environment for learning SQL:</p>
<ol type="1">
<li><p>Immediate Feedback Loop</p>
<p>When you’re learning SQL in a traditional database environment, you often need to set up servers, configure connections, and deal with authentication. With R, you can start writing queries in seconds and see results immediately in your familiar R environment.</p></li>
<li><p>Best of Both Worlds</p>
<p>You can write pure SQL when you want to practice, or use <code>{dplyr}</code> syntax and see the generated SQL. This dual approach accelerates learning because you can:</p>
<ul>
<li>Write <code>{dplyr}</code> code and inspect the SQL it produces</li>
<li>Compare your hand-written SQL with <code>{dbplyr}</code>’s output</li>
<li>Gradually transition from <code>{dplyr}</code> comfort to SQL mastery</li>
</ul></li>
<li><p>Visualization Integration</p>
<p>The moment you query data, you can pipe it directly into <code>{ggplot2}</code> or other R visualization tools. No export/import cycles, no switching between applications—just seamless analysis.</p></li>
<li><p>Reproducible Workflows</p>
<p>Everything lives in a script or R Markdown document. Your queries, analysis, and visualizations are all version-controlled and reproducible.</p></li>
</ol>
</section>
<section id="tools-and-packages" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="tools-and-packages"><span class="header-section-number">3</span> Tools and Packages</h2>
<p>I can name few tools and packages on working with SQL databases in R, most especially when you just started. I don’t have any database in my own device, but did you know you can simulate databases? These are the tools and packages to start:</p>
<ol type="1">
<li><p><code>{tidyverse}</code> — Why this? This is a package that holds the complete set of tools in data science, and that includes working with databases. Speaking of which, this is a <em>meta-package</em> that also contains what we need: <code>{dbplyr}</code>, which also contains <code>{DBI}</code> package dependency.</p></li>
<li><p><code>{box}</code> — I already talked about this package in my previous blog posts. Please, take a look at them if you have some time:</p>
<ul>
<li><a href="https://joshuamarie.com/posts/03-modules-in-r/">Box: Placing module system into R</a></li>
<li><a href="https://joshuamarie.com/posts/06-load-pkg/#box">In my “Ways to load / attach packages in R” blog post</a></li>
</ul></li>
<li><p><code>{dbplyr}</code> — This is the magic translator. It converts your familiar <code>{dplyr}</code> code into SQL queries behind the scenes. You write R, it speaks SQL to the database. The best part? You can inspect the SQL it generates, which makes it a fantastic learning tool.</p></li>
<li><p><code>{DBI}</code> — Think of this as the universal adapter for database connections. It provides a consistent interface whether you’re connecting to SQLite, PostgreSQL, MySQL, or other databases. It handles the connection, sending queries, and fetching results.</p></li>
<li><p><code>{RSQLite}</code> — This is the R interface to SQLite databases. SQLite is perfect for learning because it’s lightweight, requires no server setup, and the entire database is just a single file on your computer.</p></li>
</ol>
<p>Install them through this:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">Native R</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Using <strong>{pak}</strong></a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><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;">install.packages</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;">'tidyverse'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'box'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RSQLite'</span>))</span></code></pre></div></div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>Install them directly</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">pak<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;">pak</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb2-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tidyverse'</span>, </span>
<span id="cb2-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'box'</span>, </span>
<span id="cb2-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'RSQLite'</span></span>
<span id="cb2-5">))</span></code></pre></div></div>
<p>When you preferred the development version</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">pak<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;">pak</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidyverse/tidyverse"</span>, </span>
<span id="cb3-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"klmr/box"</span>, </span>
<span id="cb3-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r-dbi/RSQLite"</span></span>
<span id="cb3-5">))</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="with-existing-database" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="with-existing-database"><span class="header-section-number">4</span> With existing database</h2>
<p>I learn SQL thanks to SQLite. This is a language-agnostic library, written in C, that acts like a database while being lightweight. You can use it literally everywhere! It is also used to built into everywhere, it could be mobile phones and most computers.</p>
<p>Thanks to SQLite, I made a first move to learn SQL without installing heavy softwares, such as PostgreSQL and MySQL, just to learn SQL. Additionally, SQLite is an open-source, but not open for contribution (I believe this is designed for good purpose).</p>
<section id="why-sqlite-is-perfect-for-learning" class="level3" data-number="4.1">
<h3 data-number="4.1" class="anchored" data-anchor-id="why-sqlite-is-perfect-for-learning"><span class="header-section-number">4.1</span> Why SQLite is Perfect for Learning</h3>
<p>Here’s why SQLite is the ideal training ground:</p>
<ul>
<li><strong>No server required</strong> — It’s just a file on your computer</li>
<li><strong>Zero configuration</strong> — No ports, users, or permissions to set up</li>
<li><strong>Lightweight</strong> — Databases can be megabytes instead of gigabytes</li>
<li><strong>Production-ready</strong> — Despite being “lite,” it’s used in production by major applications</li>
<li><strong>SQL standard</strong> — You learn real SQL that transfers to other databases</li>
</ul>
<p>In a positive sense, R and SQL is a great combo. Maybe R and SQL is not a great combo for software development as Python and SQL combo, R and SQL can make a place in data analysis instead. As long as you have <code>{DBI}</code> and <code>{RSQLite}</code> installed in your R, you can now make a first move on integrating R and SQL, and you’re good to go.</p>
</section>
<section id="sqlite-in-r" class="level3" data-number="4.2">
<h3 data-number="4.2" class="anchored" data-anchor-id="sqlite-in-r"><span class="header-section-number">4.2</span> SQLite in R</h3>
<p>Oh, you can definitely learn SQL and R at the same time, considering that SQLite is portable and lightweight. The only primary requirements are <code>{DBI}</code> and <code>{RSQLite}</code>. If you know how to write a query, you don’t need a compatible set of packages in <code>{tidyverse}</code> and <code>{dbplyr}</code>, otherwise, as long as you know how to use <code>{tidyverse}</code> packages, namely <code>{dplyr}</code>, <code>{tidyr}</code>, etc., you can use it instead.</p>
<p>Let me show you how to connect to a SQLite database and work with it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="annotated-cell-1" style="background: #f1f3f5;"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-1-1">box<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</span>(</span>
<span id="annotated-cell-1-2">    DBI[dbConnect, dbWriteTable, dbDisconnect], </span>
<span id="annotated-cell-1-3">    RSQLite[SQLite]</span>
<span id="annotated-cell-1-4">)</span>
<span id="annotated-cell-1-5"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="1">1</button><span id="annotated-cell-1-6" class="code-annotation-target"></span>
<span id="annotated-cell-1-7">con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbConnect</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SQLite</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first_database.sqlite"</span>)</span>
<span id="annotated-cell-1-8"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="2">2</button><span id="annotated-cell-1-9" class="code-annotation-target"></span>
<span id="annotated-cell-1-10">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="annotated-cell-1-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="annotated-cell-1-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</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;">"Alice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bob"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Charlie"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diana"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Eve"</span>),</span>
<span id="annotated-cell-1-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</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;">25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>),</span>
<span id="annotated-cell-1-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">city =</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;">"New York"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"London"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tokyo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paris"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sydney"</span>)</span>
<span id="annotated-cell-1-15">)</span>
<span id="annotated-cell-1-16"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="3">3</button><span id="annotated-cell-1-17" class="code-annotation-target"></span>
<span id="annotated-cell-1-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbWriteTable</span>(</span>
<span id="annotated-cell-1-19">    con, </span>
<span id="annotated-cell-1-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customers"</span>,</span>
<span id="annotated-cell-1-21">    df, </span>
<span id="annotated-cell-1-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overwrite =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="annotated-cell-1-23">)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-1" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="6" data-code-annotation="1">Create a connection (this creates the database file if it doesn’t exist)</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="9" data-code-annotation="2">Create some sample data</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="17" data-code-annotation="3">Write the data as a <em>table</em> to the database</span>
</dd>
</dl>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled" title="Explanation">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Explanation
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<ol type="1">
<li>Create a connection (this creates the database file if it doesn’t exist)</li>
<li>Create some sample data</li>
<li>Write the data as a <em>table</em> to the database</li>
</ol>
</div>
</div>
</div>
<p><strong>What just happened?</strong><br>
We created a SQLite database file called <code>first_database.sqlite</code> in your working directory. Inside it, we created a table called <code>customers</code> with our sample data. If the file already exists, R simply connects to it.</p>
</section>
<section id="two-ways-to-query-pure-sql-vs-dplyr" class="level3" data-number="4.3">
<h3 data-number="4.3" class="anchored" data-anchor-id="two-ways-to-query-pure-sql-vs-dplyr"><span class="header-section-number">4.3</span> Two Ways to Query: Pure SQL vs {dplyr}</h3>
<p>Now comes the fun part—you can query this database in two different ways, and each has its benefits for learning.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">Just SQL</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href=""><strong>{dplyr}</strong> API</a></li></ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<p>Did you know that with <code>{knitr}</code>, you can write SQL code chunks directly in your R Markdown or Quarto documents? This is incredibly handy for mixing SQL queries with your data analysis in R.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb4-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{sql connection=con}</span></span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> name, age, city </span>
<span id="cb4-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> customers </span>
<span id="cb4-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> age <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;">30</span></span>
<span id="cb4-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
<p>Alright, I wrote the SQL code chunk above again here for your reference:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> name, age, city </span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> customers </span>
<span id="cb5-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> age <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;">30</span></span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>2 records</caption>
<thead>
<tr class="header">
<th style="text-align: left;">name</th>
<th style="text-align: right;">age</th>
<th style="text-align: left;">city</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Charlie</td>
<td style="text-align: right;">35</td>
<td style="text-align: left;">Tokyo</td>
</tr>
<tr class="even">
<td style="text-align: left;">Eve</td>
<td style="text-align: right;">42</td>
<td style="text-align: left;">Sydney</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Or you can store the query into a string, and send it via <code>DBI::dbGetQuery()</code>, placed in <code>statement</code> argument.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">DBI<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;">dbGetQuery</span>(</span>
<span id="cb6-2">    con, </span>
<span id="cb6-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT name, age, city </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;">FROM customers </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;">WHERE age &gt; 30"</span></span>
<span id="cb6-4">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     name age   city
1 Charlie  35  Tokyo
2     Eve  42 Sydney</code></pre>
</div>
</div>
<p><strong>Why this matters for learning:</strong><br>
You’re writing actual SQL. No training wheels. This builds muscle memory for SQL syntax and helps you think in terms of SQL operations: SELECT, FROM, WHERE, JOIN, GROUP BY, etc.</p>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<p>Nothing can make it so easy to work with databases using a familiar syntax with <code>{dplyr}</code>. Yes, with <code>{dbplyr}</code>, you can use <code>{dplyr}</code> functions to interact with your database tables as if they were regular data frames in R. Here’s how you can perform the same query using <code>{dplyr}</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">box<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</span>(</span>
<span id="cb8-2">    dplyr[filter, select, tbl, collect, show_query]</span>
<span id="cb8-3">)</span>
<span id="cb8-4"></span>
<span id="cb8-5">customers_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"customers"</span>)</span>
<span id="cb8-6"></span>
<span id="cb8-7">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> customers_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(age <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;">30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, age, city)</span>
<span id="cb8-10"></span>
<span id="cb8-11">out <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;">collect</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 3
  name      age city  
  &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt; 
1 Charlie    35 Tokyo 
2 Eve        42 Sydney</code></pre>
</div>
</div>
<p><strong>The magic behind this:</strong></p>
<p>When you use <code>{dplyr}</code> verbs on a database table, <code>{dbplyr}</code> doesn’t immediately execute anything. It’s lazy! It builds up the query and only executes it when you call <code>collect()</code>. This is efficient because:</p>
<ul>
<li>You can chain many operations without multiple round-trips to the database</li>
<li>Only the final result set gets pulled into R memory</li>
<li>The heavy computation happens in the database where it’s optimized</li>
</ul>
<p><strong>See the generated SQL:</strong></p>
<p>How about getting the SQL query generated by <code>{dbplyr}</code>? You can use the <code>show_query()</code> function to see the SQL that <code>{dbplyr}</code> generates for your <code>{dplyr}</code> code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">out <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">show_query</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>&lt;SQL&gt;
SELECT `name`, `age`, `city`
FROM `customers`
WHERE (`age` &gt; 30.0)</code></pre>
</div>
</div>
<p>Easy, right? If you know R already, treat it as your SQL teacher! Write familiar <code>{dplyr}</code> code, then check the SQL translation. Over time, you’ll start to intuitively understand how <code>filter()</code> becomes <code>WHERE</code>, how <code>select()</code> becomes <code>SELECT</code>, and how more complex operations translate to SQL.</p>
</div>
</div>
</div>
</section>
<section id="working-with-query-results" class="level3" data-number="4.4">
<h3 data-number="4.4" class="anchored" data-anchor-id="working-with-query-results"><span class="header-section-number">4.4</span> Working with Query Results</h3>
<p>Once you have query results, you can treat them like any R data frame:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="annotated-cell-2" style="background: #f1f3f5;"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-2-1">box<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</span>(</span>
<span id="annotated-cell-2-2">    ggplot2[ggplot, aes, geom_col, theme_minimal, labs]</span>
<span id="annotated-cell-2-3">)</span>
<span id="annotated-cell-2-4"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="1">1</button><span id="annotated-cell-2-5" class="code-annotation-target"></span>
<span id="annotated-cell-2-6">result_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(out)</span>
<span id="annotated-cell-2-7"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="2">2</button><span id="annotated-cell-2-8" class="code-annotation-target"></span>
<span id="annotated-cell-2-9">result_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>  </span>
<span id="annotated-cell-2-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> city)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="annotated-cell-2-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="annotated-cell-2-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="annotated-cell-2-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="annotated-cell-2-14">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Customers Over 30"</span>,</span>
<span id="annotated-cell-2-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name"</span>,</span>
<span id="annotated-cell-2-16">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age"</span></span>
<span id="annotated-cell-2-17">    )</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-2" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="5" data-code-annotation="1">Retrieve the data</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="8" data-code-annotation="2">Now you can analyze it with R</span>
</dd>
</dl>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://joshuamarie.com/posts/08-sql-r/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled" title="Explanation">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Explanation
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<ol type="1">
<li>Retrieve the data</li>
<li>Now you can analyze it with R</li>
</ol>
</div>
</div>
</div>
<p>This is where the R + SQL combination really shines. You use SQL’s efficiency to get exactly the data you need, then R’s rich ecosystem for analysis and visualization.</p>
</section>
<section id="database-hygiene" class="level3" data-number="4.5">
<h3 data-number="4.5" class="anchored" data-anchor-id="database-hygiene"><span class="header-section-number">4.5</span> Database Hygiene</h3>
<p>Always remember to close your database connections once you’re done:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbDisconnect</span>(con)</span></code></pre></div></div>
</div>
<p>This releases resources and ensures your database file isn’t locked. In practice, connections are also closed automatically when your R session ends, but it’s good practice to do it explicitly.</p>
</section>
</section>
<section id="learning-sql-in-r-without-a-server" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="learning-sql-in-r-without-a-server"><span class="header-section-number">5</span> Learning SQL in R without a server</h2>
<p>But I know some of you wants to know what it looks like to use the existing database and then call it in R.</p>
<p>I literally said in the introduction that you can <em>simulate</em> — I have another different meaning:</p>
<ul>
<li>Use <code>simulate_*</code> family functions in <code>{dbplyr}</code> package. These functions allow you to create in-memory database tables that mimic real database behavior without needing an actual database server. This is perfect for learning and testing SQL queries in R.</li>
</ul>
<section id="what-are-simulated-connections" class="level3" data-number="5.1">
<h3 data-number="5.1" class="anchored" data-anchor-id="what-are-simulated-connections"><span class="header-section-number">5.1</span> What Are Simulated Connections?</h3>
<p>Simulated connections create an in-memory representation of how different database systems handle SQL. This means you can:</p>
<ul>
<li>See how your <code>{dplyr}</code> code translates to different SQL dialects</li>
<li>Learn SQL without any database installation</li>
<li>Test queries before running them on production databases</li>
<li>Understand the quirks of different database systems</li>
</ul>
</section>
<section id="demonstration-simulating-microsoft-sql-server" class="level3" data-number="5.2">
<h3 data-number="5.2" class="anchored" data-anchor-id="demonstration-simulating-microsoft-sql-server"><span class="header-section-number">5.2</span> Demonstration: Simulating Microsoft SQL Server</h3>
<p>First, let me show you how it looks like to connect to a database, i.e.&nbsp;SQLite in this case, and work with it. Try imagine you have a SQL server, and you want to connect to it using R. Use <code>simulate_mssql()</code> function to simulate a Microsoft SQL Server database connection:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="annotated-cell-4" style="background: #f1f3f5;"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-4-1">box<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</span>(</span>
<span id="annotated-cell-4-2">    dbplyr[simulate_mssql, tbl_lazy]</span>
<span id="annotated-cell-4-3">)</span>
<span id="annotated-cell-4-4"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="1">1</button><span id="annotated-cell-4-5" class="code-annotation-target">con_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_mssql</span>()</span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="2">2</button><span id="annotated-cell-4-6" class="code-annotation-target">customers_sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_lazy</span>(df, con_sim)</span>
<span id="annotated-cell-4-7"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="3">3</button><span id="annotated-cell-4-8" class="code-annotation-target">customers_sim <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="annotated-cell-4-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(name, age, city) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="annotated-cell-4-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(age <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;">30</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-4" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-4" data-code-lines="5" data-code-annotation="1">Create a simulated MS SQL Server connection</span>
</dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-4" data-code-lines="6" data-code-annotation="2">Create a (local) lazy table from our data frame</span>
</dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-4" data-code-lines="8,9,10" data-code-annotation="3">Build a query</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>&lt;SQL&gt;
SELECT `name`, `age`, `city`
FROM `df`
WHERE (`age` &gt; 30.0)</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled" title="Explanation">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Explanation
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<ol type="1">
<li>Create a simulated MS SQL Server connection</li>
<li>Create a (local) lazy table from our data frame</li>
<li>Build a query</li>
</ol>
</div>
</div>
</div>
<p>We are performing a pure lazy evaluation in this step. Which means, we are not returning any information in the table and in the query process, we only generate the SQL code.</p>
</section>
<section id="comparing-sql-dialects" class="level3" data-number="5.3">
<h3 data-number="5.3" class="anchored" data-anchor-id="comparing-sql-dialects"><span class="header-section-number">5.3</span> Comparing SQL Dialects</h3>
<p>The only issue when learning SQL with R in the past is when I found out that different databases have different SQL dialects. Let’s compare how PostgreSQL and Microsoft SQL Server handle the same query:</p>
<div class="columns">
<p>Let’s import a bit first:</p><div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="annotated-cell-17" style="background: #f1f3f5;"><pre class="sourceCode r code-annotation-code code-with-copy code-annotated"><code class="sourceCode r"><span id="annotated-cell-17-1">box<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</span>(</span>
<span id="annotated-cell-17-2">    dplyr[</span>
<span id="annotated-cell-17-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">keep_when =</span> filter, arrange, desc, show_query, </span>
<span id="annotated-cell-17-4">        summarise, mutate, relocate</span>
<span id="annotated-cell-17-5">    ], </span>
<span id="annotated-cell-17-6">    tidyr[</span>
<span id="annotated-cell-17-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">long =</span> pivot_longer</span>
<span id="annotated-cell-17-8">    ], </span>
<span id="annotated-cell-17-9">    dbplyr[simulate_postgres, simulate_mssql], </span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-17" data-target-annotation="1">1</button><span id="annotated-cell-17-10" class="code-annotation-target">    magrittr[<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;">%&gt;%</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>]</span>
<span id="annotated-cell-17-11">)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-17" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-17" data-code-lines="10" data-code-annotation="1">This is totally optional, you still can use <code>|&gt;</code> base R pipes, after all.</span>
</dd>
</dl>
</div>
</div><div class="column" style="width:50%;">
<section id="postgresql-version" class="level4" data-number="5.3.1">
<h4 data-number="5.3.1" class="anchored" data-anchor-id="postgresql-version"><span class="header-section-number">5.3.1</span> PostgreSQL version</h4>
<div class="cell" data-output-fold="true" data-output-summary="Click to view generated SQL code">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">con_postgres <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_postgres</span>()</span>
<span id="cb14-2">mtcars_postgres <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_lazy</span>(mtcars, con_postgres)</span>
<span id="cb14-3"></span>
<span id="cb14-4">mtcars_postgres <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(cyl <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;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb14-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hp_per_cyl =</span> hp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> cyl,</span>
<span id="cb14-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">efficiency =</span> mpg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> disp</span>
<span id="cb14-9">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(mpg, disp, hp, hp_per_cyl, efficiency, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb14-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(</span>
<span id="cb14-13">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(mpg, hp_per_cyl, efficiency), </span>
<span id="cb14-14">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb14-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> \(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), </span>
<span id="cb14-16">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> \(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb14-17">            ), </span>
<span id="cb14-18">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.names =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{.col}..{.fn}"</span></span>
<span id="cb14-19">        ),</span>
<span id="cb14-20">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb14-21">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">long</span>(</span>
<span id="cb14-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</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;">"mu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>)), </span>
<span id="cb14-24">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_sep =</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;">.."</span>, </span>
<span id="cb14-25">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</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;">"Variable"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stat"</span>), </span>
<span id="cb14-26">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Est"</span></span>
<span id="cb14-27">    )</span></code></pre></div></div>
<details><summary>Click to view generated SQL code</summary>
<div class="cell-output cell-output-stdout">
<pre><code>&lt;SQL&gt;
SELECT `n`, 'mpg' AS `Variable`, 'mu' AS `Stat`, `mpg..mu` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDDEV_SAMP(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDDEV_SAMP(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDDEV_SAMP(`efficiency`) AS `efficiency..sigma`,
    COUNT(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT
  `n`,
  'hp_per_cyl' AS `Variable`,
  'mu' AS `Stat`,
  `hp_per_cyl..mu` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDDEV_SAMP(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDDEV_SAMP(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDDEV_SAMP(`efficiency`) AS `efficiency..sigma`,
    COUNT(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT
  `n`,
  'efficiency' AS `Variable`,
  'mu' AS `Stat`,
  `efficiency..mu` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDDEV_SAMP(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDDEV_SAMP(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDDEV_SAMP(`efficiency`) AS `efficiency..sigma`,
    COUNT(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT `n`, 'mpg' AS `Variable`, 'sigma' AS `Stat`, `mpg..sigma` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDDEV_SAMP(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDDEV_SAMP(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDDEV_SAMP(`efficiency`) AS `efficiency..sigma`,
    COUNT(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT
  `n`,
  'hp_per_cyl' AS `Variable`,
  'sigma' AS `Stat`,
  `hp_per_cyl..sigma` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDDEV_SAMP(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDDEV_SAMP(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDDEV_SAMP(`efficiency`) AS `efficiency..sigma`,
    COUNT(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT
  `n`,
  'efficiency' AS `Variable`,
  'sigma' AS `Stat`,
  `efficiency..sigma` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDDEV_SAMP(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDDEV_SAMP(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDDEV_SAMP(`efficiency`) AS `efficiency..sigma`,
    COUNT(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`</code></pre>
</div>
</details>
</div>
</section>
</div><div class="column" style="width:50%;">
<section id="microsoft-sql-server-version" class="level4" data-number="5.3.2">
<h4 data-number="5.3.2" class="anchored" data-anchor-id="microsoft-sql-server-version"><span class="header-section-number">5.3.2</span> Microsoft SQL Server version</h4>
<div class="cell" data-output-fold="true" data-output-summary="Click to view generated SQL code">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">con_mssql <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_mssql</span>()</span>
<span id="cb16-2">mtcars_mssql <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_lazy</span>(mtcars, con_mssql)</span>
<span id="cb16-3"></span>
<span id="cb16-4">mtcars_mssql <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(cyl <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;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb16-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hp_per_cyl =</span> hp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> cyl,</span>
<span id="cb16-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">efficiency =</span> mpg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> disp</span>
<span id="cb16-9">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(mpg, disp, hp, hp_per_cyl, efficiency, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb16-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(</span>
<span id="cb16-13">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(mpg, hp_per_cyl, efficiency), </span>
<span id="cb16-14">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb16-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> \(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), </span>
<span id="cb16-16">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> \(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb16-17">            ), </span>
<span id="cb16-18">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.names =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{.col}..{.fn}"</span></span>
<span id="cb16-19">        ),</span>
<span id="cb16-20">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb16-21">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb16-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">long</span>(</span>
<span id="cb16-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</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;">"mu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>)), </span>
<span id="cb16-24">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_sep =</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;">.."</span>, </span>
<span id="cb16-25">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</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;">"Variable"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stat"</span>), </span>
<span id="cb16-26">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Est"</span></span>
<span id="cb16-27">    )</span></code></pre></div></div>
<details><summary>Click to view generated SQL code</summary>
<div class="cell-output cell-output-stdout">
<pre><code>&lt;SQL&gt;
SELECT `n`, 'mpg' AS `Variable`, 'mu' AS `Stat`, `mpg..mu` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDEV(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDEV(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDEV(`efficiency`) AS `efficiency..sigma`,
    COUNT_BIG(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT
  `n`,
  'hp_per_cyl' AS `Variable`,
  'mu' AS `Stat`,
  `hp_per_cyl..mu` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDEV(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDEV(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDEV(`efficiency`) AS `efficiency..sigma`,
    COUNT_BIG(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT
  `n`,
  'efficiency' AS `Variable`,
  'mu' AS `Stat`,
  `efficiency..mu` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDEV(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDEV(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDEV(`efficiency`) AS `efficiency..sigma`,
    COUNT_BIG(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT `n`, 'mpg' AS `Variable`, 'sigma' AS `Stat`, `mpg..sigma` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDEV(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDEV(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDEV(`efficiency`) AS `efficiency..sigma`,
    COUNT_BIG(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT
  `n`,
  'hp_per_cyl' AS `Variable`,
  'sigma' AS `Stat`,
  `hp_per_cyl..sigma` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDEV(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDEV(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDEV(`efficiency`) AS `efficiency..sigma`,
    COUNT_BIG(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`

UNION ALL

SELECT
  `n`,
  'efficiency' AS `Variable`,
  'sigma' AS `Stat`,
  `efficiency..sigma` AS `Est`
FROM (
  SELECT
    AVG(`mpg`) AS `mpg..mu`,
    STDEV(`mpg`) AS `mpg..sigma`,
    AVG(`hp_per_cyl`) AS `hp_per_cyl..mu`,
    STDEV(`hp_per_cyl`) AS `hp_per_cyl..sigma`,
    AVG(`efficiency`) AS `efficiency..mu`,
    STDEV(`efficiency`) AS `efficiency..sigma`,
    COUNT_BIG(*) AS `n`
  FROM (
    SELECT
      `mpg`,
      `disp`,
      `hp`,
      `hp` / `cyl` AS `hp_per_cyl`,
      `mpg` / `disp` AS `efficiency`,
      `cyl`,
      `drat`,
      `wt`,
      `qsec`,
      `vs`,
      `am`,
      `gear`,
      `carb`
    FROM `df`
    WHERE (`cyl` = 6.0)
  ) AS `q01`
) AS `q01`</code></pre>
</div>
</details>
</div>
</section>
</div>
</div>
</section>
<section id="when-to-use-simulated-connections" class="level3" data-number="5.4">
<h3 data-number="5.4" class="anchored" data-anchor-id="when-to-use-simulated-connections"><span class="header-section-number">5.4</span> When to Use Simulated Connections</h3>
<p>Simulated connections are perfect for:</p>
<ul>
<li><strong>Learning</strong>: Practice SQL translation without database setup</li>
<li><strong>Development</strong>: Test query logic before connecting to real databases</li>
<li><strong>Documentation</strong>: Show how queries work across different systems</li>
<li><strong>Teaching</strong>: Demonstrate SQL concepts without infrastructure requirements</li>
</ul>
</section>
</section>
<section id="common-pitfalls-and-how-to-avoid-them" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="common-pitfalls-and-how-to-avoid-them"><span class="header-section-number">6</span> Common Pitfalls and How to Avoid Them</h2>
<p>Some are based on my experience, so some of the list can be opinionated.</p>
<section id="collecting-too-early" class="level3" data-number="6.1">
<h3 data-number="6.1" class="anchored" data-anchor-id="collecting-too-early"><span class="header-section-number">6.1</span> 1. Collecting Too Early</h3>
<p>The problem in R can be overdramatic with large amounts of data when read and processed into memory. Unnecessarily bringing entire tables into R memory creates performance bottlenecks and can even crash your R session when datasets exceed available RAM.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Chain operations and collect only when needed. Let the database do the heavy lifting with filtering, aggregating, and joining before bringing results into R.</p>
</div>
</div>
</div>
</section>
<section id="not-checking-query-plans" class="level3" data-number="6.2">
<h3 data-number="6.2" class="anchored" data-anchor-id="not-checking-query-plans"><span class="header-section-number">6.2</span> 2. Not Checking Query Plans</h3>
<p>Slow queries are often the result of poor optimization, but many users never investigate why their queries take so long. Without examining the query plan, you’re flying blind—unable to identify bottlenecks, missing indexes, or inefficient joins.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Use <code>explain()</code> to understand query execution. This reveals how the database processes your query and highlights optimization opportunities.</p>
</div>
</div>
</div>
</section>
<section id="forgetting-database-differences" class="level3" data-number="6.3">
<h3 data-number="6.3" class="anchored" data-anchor-id="forgetting-database-differences"><span class="header-section-number">6.3</span> 3. Forgetting Database Differences</h3>
<p>SQL dialects vary between database systems, and code that runs perfectly in SQLite might fail spectacularly in PostgreSQL or MySQL. These differences range from subtle syntax variations to completely different function names and behaviors.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Test with simulated connections and understand dialect differences. Use <code>{dbplyr}</code>’s <code>simulate_*()</code> functions to preview how queries translate across databases before deploying to production systems.</p>
</div>
</div>
</div>
</section>
<section id="ignoring-indexes" class="level3" data-number="6.4">
<h3 data-number="6.4" class="anchored" data-anchor-id="ignoring-indexes"><span class="header-section-number">6.4</span> 4. Ignoring Indexes</h3>
<p>Queries that scan entire tables are a common performance killer, especially as datasets grow. Without proper indexes, your database must examine every single row to find matches, turning what should be millisecond queries into multi-second ordeals.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-7-contents" aria-controls="callout-7" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-7" class="callout-7-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Learn about indexes and how to create them for frequently queried columns. Understand which columns benefit from indexing and how composite indexes can optimize complex queries.</p>
</div>
</div>
</div>
</section>
<section id="not-parameterizing-queries" class="level3" data-number="6.5">
<h3 data-number="6.5" class="anchored" data-anchor-id="not-parameterizing-queries"><span class="header-section-number">6.5</span> 5. Not Parameterizing Queries</h3>
<p>Building queries with string concatenation isn’t just inelegant—it’s dangerous. This practice opens your code to SQL injection attacks, where malicious input can execute arbitrary database commands, potentially exposing or destroying data.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Solution">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-8-contents" aria-controls="callout-8" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Solution
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-8" class="callout-8-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Use parameterized queries with <code>DBI::dbBind()</code> or stick with <code>{dplyr}</code> operations, which handle parameterization automatically. Never concatenate user input directly into SQL strings.</p>
</div>
</div>
</div>
</section>
</section>
<section id="conclusion" class="level2" data-number="7">
<h2 data-number="7" class="anchored" data-anchor-id="conclusion"><span class="header-section-number">7</span> Conclusion</h2>
<p>SQL and R aren’t competitors — they’re collaborators. SQL is your data retrieval expert, getting you exactly the data you need with incredible efficiency. R is your analysis specialist, turning that data into insights, models, and visualizations. I can guarantee you that learning SQL through R is that you’re never starting from zero. Your existing R knowledge accelerates SQL mastery. The familiar <code>{dplyr}</code> syntax becomes your bridge to SQL fluency. And the ability to seamlessly move from database queries to statistical analysis to stunning visualizations—all in one environment—is genuinely powerful.</p>
<p>The data-driven professionals who thrive are those who can speak both languages fluently. They use SQL to ask databases the right questions and R to find the answers that matter. They understand when to leverage database optimization and when to bring data into R for complex transformations.</p>
<p>The journey from R user to R + SQL expert isn’t just about learning syntax—it’s about becoming someone who can efficiently bridge data storage and data science, who understands both the “how” of data retrieval and the “why” of data analysis.</p>
<p>So don’t pick sides. Master both. Your future self (and would be your future employers) will thank you.</p>


</section>

 ]]></description>
  <category>R</category>
  <category>SQL</category>
  <category>data-science</category>
  <category>analytics</category>
  <category>tidyverse</category>
  <guid>https://joshuamarie.com/posts/08-sql-r/</guid>
  <pubDate>Thu, 27 Nov 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/08-sql-r/image.png" medium="image" type="image/png" height="121" width="144"/>
</item>
<item>
  <title>Tutorial on Generalized Linear Model from scratch</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/07-glm/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/07-glm&amp;text=Tutorial%20on%20Generalized%20Linear%20Model%20-%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/07-glm&amp;title=Tutorial%20on%20Generalized%20Linear%20Model%20-%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Tutorial%20on%20Generalized%20Linear%20Model%20-%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/07-glm" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/07-glm" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/07-glm&amp;title=Tutorial%20on%20Generalized%20Linear%20Model%20-%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/07-glm&amp;title=Tutorial%20on%20Generalized%20Linear%20Model%20-%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/07-glm&amp;name=Tutorial%20on%20Generalized%20Linear%20Model%20-%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Tutorial on Generalized Linear Model - Joshua Marie https://joshuamarie.com/posts/07-glm'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/07-glm%20Tutorial%20on%20Generalized%20Linear%20Model%20-%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<p>Stop asking:</p>
<ul>
<li>“What test do I need to use?”</li>
<li>“Do my data need to be normally distributed?”</li>
<li>“Can I just use linear regression on counts lol?”</li>
</ul>
<p><img src="https://joshuamarie.com/posts/07-glm/spongebob-brain-on-fire.gif" class="img-fluid"></p>
<p>Kidding~</p>
<p>I have no such problem of you guys asking these questions. This type of question, however, is a type of question that I heard many times already. I mean, I understand why there’s so many misconceptions, and it could be they are not properly taught or…I don’t know.</p>
<p>I have no problems with tutorials and blogs online about teaching statistics, like predicting what species of iris is gonna be, or how likely you’ll get ebola. There are ten thousand blog posts teaching you how to predict iris species with <code><a href="https://rdrr.io/r/stats/glm.html">glm()</a></code>, or Python’s <code>sklearn.linear_model.LogisticRegression()</code></p>
<p>Today we’re going <strong>full math gremlin mode</strong> and actually understanding why GLMs exist, why linear regression is a lie for most real data, and why the link function is the most genius hack in 20th-century statistics.</p>
<section id="lets-start-with-an-overview" class="level2" data-number="1"><h2 data-number="1" class="anchored" data-anchor-id="lets-start-with-an-overview">
<span class="header-section-number">1</span> Let’s start with an overview</h2>
<p><img src="https://joshuamarie.com/posts/07-glm/lr.jpg" class="img-fluid"></p>
<p>What did you observe on the mathematical model formula of linear regression and generalized linear model (GLM)?</p>
<p>Take a look:</p>
<p><strong><em>Linear Regression:</em></strong></p>
<p><img src="https://latex.codecogs.com/png.latex?Y%20=%20%5Cmathbf%7BX%7D%5Cbeta%5C%20+%5C%20%5Cepsilon"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D%5Cbeta"> expands to: <img src="https://latex.codecogs.com/png.latex?%5Cbeta_0%20+%20x_1%5Cbeta_1%20+%20x_2%5Cbeta_2%20+%20x_3%5Cbeta_3%20+%20%5Cdots%20+%20x_n%5Cbeta_n"></p>
<p>How about on GLM?</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ag(%5Cmu_i)%20=%20%5Cmathbf%7Bx%7D_i%5E%5Ctop%20%5Cboldsymbol%7B%5Cbeta%7D%20=%20%5Ceta_i%20%5Cquad%20%5Ctext%7B(linear%20predictor)%7D%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?g(%5Cmu)=%5Ceta=%5Cmathbf%7BX%7D%5Cbeta"></p>
<p>both are taking the account of the linear model, where the response have linear relationship with the predictors, taken account by <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D%5Cbeta">.</p>
<p>There are few exceptions:</p>
<ol type="1">
<li><p>GLMs can take handle of the range not only lies on the real number, but with some strict intervals, unlike Linear regression who has always a range lies on the real number.</p></li>
<li>
<p>GLMs doesn’t have an error term. Why is that? GLMs takes considerations on the <em>expected value</em> of the linear model, and when you take the expected value like this:</p>
<p><img src="https://latex.codecogs.com/png.latex?E(Y)=E(%5Cmathbf%7BX%7D%5Cbeta%5C%20+%20%5C%20%5Cepsilon)"></p>
<p>The expected values <img src="https://latex.codecogs.com/png.latex?E(%5Cmathbf%7BX%7D%5Cbeta)%20=%20%5Cmathbf%7BX%7D%5Cbeta"> (assuming no randomness in both terms here) and <img src="https://latex.codecogs.com/png.latex?E(%5Cepsilon)%20=%200">. Hence:</p>
<p><img src="https://latex.codecogs.com/png.latex?E(Y)=%5Cmathbf%7BX%7D%5Cbeta"></p>
<p>and GLMs only care about the expected value</p>
</li>
</ol>
<section id="but-wait-whats-this-gmu-doing-here" class="level3" data-number="1.1"><h3 data-number="1.1" class="anchored" data-anchor-id="but-wait-whats-this-gmu-doing-here">
<span class="header-section-number">1.1</span> But wait, what’s this <img src="https://latex.codecogs.com/png.latex?g(%5Cmu)"> doing here?</h3>
<p>Yes, it’s odd, but hey, it just works.</p>
<p>There’s this thing called <strong>link function</strong>, and it’s the reason GLMs exist. Let me break down why we need this weird little function:</p>
<p>In linear regression, the mean of the model would be: <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20E(Y)%20=%20%5Cmathbf%7BX%7D%5Cbeta"></p>
<p>Okay, this definitely feels like “Hey, it works, okay?”, but that’s my intuition about GLMs. I hope you understood a little in this part. Anyways, that’s the mean of the linear regression that we know of, and this works great when <img src="https://latex.codecogs.com/png.latex?y"> can be any real number. But, I am asking you a question:</p>
<p>What if your outcome isn’t allowed to be just any real number?</p>
<ul>
<li>What if <img src="https://latex.codecogs.com/png.latex?Y"> is a <em>count</em> <img src="https://latex.codecogs.com/png.latex?(0,%201,%202,%203,%20...)">? Let’s say <em>number of kids in a family</em> — <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D%5Cbeta"> could give you -3.7 customers</li>
<li>What if <img src="https://latex.codecogs.com/png.latex?Y"> is a <em>probability</em> (0 to 1)? Probability of rain? Between 0 and 1, inclusive, you monster</li>
<li>What if <img src="https://latex.codecogs.com/png.latex?Y"> is <em>binary</em> (0 or 1)? Let’s say “whether someone clicks your ad” — <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D%5Cbeta"> could give you 0.3, and it can’t be 1.337, you know.</li>
</ul>
<p>This strange thing-y in GLMs, link function <img src="https://latex.codecogs.com/png.latex?g(%5Ccdot)">, solves this by transforming the mean so that the linear predictor <img src="https://latex.codecogs.com/png.latex?%5Ceta%20=%20%5Cmathbf%7BX%7D%5Cbeta"> can be any real number, while <img src="https://latex.codecogs.com/png.latex?%5Cmu"> stays in the valid range.</p>
<p>Understand? No? Alright, let’s say, the output of <img src="https://latex.codecogs.com/png.latex?%5Ceta">, which is an equivalent of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D%5Cbeta">, should be at least close to the value in <img src="https://latex.codecogs.com/png.latex?g(%5Cmu)">, which is an equivalent of <img src="https://latex.codecogs.com/png.latex?g(E(y))">.</p>
<p>Mathematically: <img src="https://latex.codecogs.com/png.latex?g(%5Cmu)%20=%20%5Ceta%20=%20%5Cmathbf%7BX%7D%5Cbeta"></p>
<p>So the inverse is: <img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20g%5E%7B-1%7D(%5Ceta)%20=%20g%5E%7B-1%7D(%5Cmathbf%7BX%7D%5Cbeta)"></p>
<p>Again, to understand, we fit the model in “transformed space” where everything is linear, then transform back to get predictions in the correct range.</p>
<p><strong>List of common link functions:</strong></p>
<table class="caption-top table">
<colgroup>
<col style="width: 11%">
<col style="width: 19%">
<col style="width: 40%">
<col style="width: 19%">
<col style="width: 9%">
</colgroup>
<thead><tr class="header">
<th>Link</th>
<th><img src="https://latex.codecogs.com/png.latex?g(%5Cmu)"></th>
<th><img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20g%5E%7B-1%7D(%5Ceta)"></th>
<th>Used for</th>
<th>Why</th>
</tr></thead>
<tbody>
<tr class="odd">
<td>Identity</td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cmu"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Ceta"></td>
<td>Normal/Gaussian</td>
<td>No transformation needed</td>
</tr>
<tr class="even">
<td>Logit</td>
<td><img src="https://latex.codecogs.com/png.latex?%5Clog%5Cleft(%5Cfrac%7B%5Cmu%7D%7B1-%5Cmu%7D%5Cright)"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B1+e%5E%7B-%5Ceta%7D%7D"></td>
<td>Binomial/Binary</td>
<td>Maps <img src="https://latex.codecogs.com/png.latex?(0,1)%20%5Cto%20(-%5Cinfty,%20%5Cinfty)">
</td>
</tr>
<tr class="odd">
<td>Log</td>
<td><img src="https://latex.codecogs.com/png.latex?%5Clog(%5Cmu)"></td>
<td><img src="https://latex.codecogs.com/png.latex?e%5E%7B%5Ceta%7D"></td>
<td>Poisson/Counts</td>
<td>Maps <img src="https://latex.codecogs.com/png.latex?(0,%20%5Cinfty)%20%5Cto%20(-%5Cinfty,%20%5Cinfty)">
</td>
</tr>
<tr class="even">
<td>Probit</td>
<td><img src="https://latex.codecogs.com/png.latex?%5CPhi%5E%7B-1%7D(%5Cmu)"></td>
<td><img src="https://latex.codecogs.com/png.latex?%5CPhi(%5Ceta)"></td>
<td>Binomial</td>
<td>For masochists</td>
</tr>
</tbody>
</table></section><section id="why-cant-we-just-minimize-squared-errors" class="level3" data-number="1.2"><h3 data-number="1.2" class="anchored" data-anchor-id="why-cant-we-just-minimize-squared-errors">
<span class="header-section-number">1.2</span> Why Can’t We Just Minimize Squared Errors?</h3>
<p>In linear regression, we minimize the sum of squared residuals (SSR): <img src="https://latex.codecogs.com/png.latex?SSR%20=%20%5Csum_%7Bi%7D(Y_i%20-%20%5Chat%7BY%7D_i)%5E2%20=%20%5Csum_%7Bi%7D(Y_i%20-%20%5Cmathbf%7BX%7D%5Cbeta)%5E2"></p>
<p>This has a nice closed-form solution and works because:</p>
<ul>
<li>The errors are approximately normally distributed</li>
<li>The variance is constant (a.k.a. homoscedasticity)</li>
<li>Minimizing SSR is equivalent to maximum likelihood estimation</li>
</ul>
<p>But in GLMs, these assumptions break down:</p>
<ul>
<li>Binomial data has variance <img src="https://latex.codecogs.com/png.latex?%5Cmu(1-%5Cmu)">, which depends on the mean</li>
<li>Poisson data has variance equal to the mean <img src="https://latex.codecogs.com/png.latex?%5Cmu">
</li>
<li>The response isn’t always in real number or approximately normally distributed (even with the presence of CLT)</li>
</ul>
<p>So we need the alternative approach: <strong>Maximum Likelihood Estimation</strong> — this is the alternative estimation method for Linear regression.</p>
<p>In GLM, it will be proven difficult to estimate the optimal solution as the use case will become different. You see the table above? There are actually plenty of them.</p>
</section><section id="the-three-sacred-components-of-glm" class="level3" data-number="1.3"><h3 data-number="1.3" class="anchored" data-anchor-id="the-three-sacred-components-of-glm">
<span class="header-section-number">1.3</span> The Three Sacred Components of GLM</h3>
<p>Every GLM has exactly three components. Forget one at your dissertation defense, I dare you:</p>
<ol type="1">
<li>
<p>Random Component (Distribution Family)</p>
<p>Your response <img src="https://latex.codecogs.com/png.latex?Y"> has a likelihood function, expressed in an <strong>exponential family distribution</strong>. The fancy math looks like: <img src="https://latex.codecogs.com/png.latex?f(Y;%20%5Ctheta,%20%5Cphi)%20=%20%5Cexp%5Cleft%5C%7B%5Cfrac%7BY%5Ctheta%20-%20b(%5Ctheta)%7D%7Ba(%5Cphi)%7D%20+%20c(Y,%20%5Cphi)%5Cright%5C%7D"></p>
<p>Translation: Normal, Binomial, Poisson, Gamma — they’re all in this family because they can be written in this form.</p>
<p>Why do we care? Because exponential family distributions have nice properties:</p>
<ul>
<li>
<img src="https://latex.codecogs.com/png.latex?E%5BY%5D%20=%20b'(%5Ctheta)"> (the mean is the derivative of that <img src="https://latex.codecogs.com/png.latex?b"> function)</li>
<li>
<img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(Y)%20=%20b''(%5Ctheta)%20%5Ccdot%20a(%5Cphi)"> (variance is also derived from <img src="https://latex.codecogs.com/png.latex?b">)</li>
</ul>
</li>
<li>
<p>Systematic Component (Linear Predictor)</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ceta%20=%20%5Cmathbf%7BX%7D%5Cbeta%20=%20%5Cbeta_0%20+%20%5Cbeta_1%20x_1%20+%20%5Cbeta_2%20x_2%20+%20%5Cldots"></p>
<p>This is just your boring linear combination of predictors. Nothing fancy. We’re still doing regression.</p>
</li>
<li>
<p>Link Function</p>
<p><img src="https://latex.codecogs.com/png.latex?g(%5Cmu)%20=%20%5Ceta"></p>
<p>Connects your linear predictor <img src="https://latex.codecogs.com/png.latex?%5Ceta"> (which lives in <img src="https://latex.codecogs.com/png.latex?(-%5Cinfty,%20%5Cinfty)">) to your mean response <img src="https://latex.codecogs.com/png.latex?%5Cmu"> (which might live in <img src="https://latex.codecogs.com/png.latex?(0,%201)"> or <img src="https://latex.codecogs.com/png.latex?%5B0,%20%5Cinfty)"> or wherever your response is allowed to be).</p>
</li>
</ol></section><section id="why-linear-regression-is-just-a-special-case" class="level3" data-number="1.4"><h3 data-number="1.4" class="anchored" data-anchor-id="why-linear-regression-is-just-a-special-case">
<span class="header-section-number">1.4</span> Why Linear Regression is Just a Special Case</h3>
<p>Notice that if you:</p>
<ul>
<li>Use the <strong>Normal distribution</strong> (random component)</li>
<li>Its <em>identity link</em> <img src="https://latex.codecogs.com/png.latex?g(%5Cmu)%20=%20%5Cmu"> (link function)</li>
<li>Keep the linear predictor <img src="https://latex.codecogs.com/png.latex?%5Ceta%20=%20%5Cmathbf%7BX%7D%5Cbeta"> (systematic component)</li>
</ul>
<p>You get just it:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmu%20=%20%5Cmathbf%7BX%7D%5Cbeta"></p>
<p>which itself is equivalent to <img src="https://latex.codecogs.com/png.latex?E(y)"></p>
<p>Which is just…your good old ordinary regression:</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>In R
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="true" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>In Python
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse show">
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> statsmodels.formula.api <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> smf</span>
<span id="cb2-2"></span>
<span id="cb2-3">smf. \ </span>
<span id="cb2-4">    ols(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y ~ x"</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data). <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb2-5">    fit(). \ </span>
<span id="cb2-6">    summary()</span></code></pre></div></div>
</div>
</div>
</div>
<p>Linear regression is a GLM with no identity, literally and figuratively.</p>
<hr></section></section><section id="math" class="level2" data-number="2"><h2 data-number="2" class="anchored" data-anchor-id="math">
<span class="header-section-number">2</span> In-depth mathematics and computation</h2>
<p><img src="https://joshuamarie.com/posts/07-glm/math.gif" class="img-fluid"></p>
<p><em>What mathematics do I need to know more?</em></p>
<p>Now, you know it takes a lot to optimize the GLM. What are the things you need to know?</p>
<p>First, in Linear regression, we have two ways to estimate the coefficients: MLE and ordinary least squares (OLS), which also can be derived and closed form from the maximum likelihood of the normal distribution. In GLM, we cannot use OLS, thus no closed form solution and MLE is the only main solution.</p>
<p>Then for the extra part, which we talk about the deviance. Imagine this as the residual sum of squares in GLMs.</p>
<section id="mle" class="level3" data-number="2.1"><h3 data-number="2.1" class="anchored" data-anchor-id="mle">
<span class="header-section-number">2.1</span> Maximum Likelihood Estimation</h3>
<p>But how do we actually estimate <img src="https://latex.codecogs.com/png.latex?%5Cbeta">? We use <em>maximum likelihood estimation</em> (MLE), but considering there are many forms of likelihood functions, derived from several probability distributions came from exponential family, and so there’s several link functions to be derived.</p>
<p>The likelihood for a GLM is:</p>
<p><img src="https://latex.codecogs.com/png.latex?L(%5Cbeta)%20=%20%5Cprod_%7Bi=1%7D%5En%20f(y_i;%20%5Ctheta_i,%20%5Cphi)=%5Cprod_%7Bi=1%7D%5En%5Cexp%5Cleft%5C%7B%5Cfrac%7By_i%5Ctheta_i%20-%20b(%5Ctheta_i)%7D%7Ba(%5Cphi)%7D%20+%20c(y_i,%20%5Cphi)%5Cright%5C%7D"></p>
<p>Fancy, isn’t it?</p>
<p>SIKE!!</p>
<p><img src="https://joshuamarie.com/posts/07-glm/sike.gif" class="img-fluid"></p>
<p>You don’t want this, trust me. This thing is so hard to calculate, even the computer starts to act crazy if you really direcly executed a program for this. Direct maximization of this product is computationally difficult. Taking logarithms simplifies things enormously (thanks, John Napier!).</p>
<p>A sane person would only like what’s the easier to eat — The log-likelihood function for the GLM instead:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cell(%5Cbeta)%20=%20%5Csum_%7Bi=1%7D%5En%20%5Cleft%5C%7B%5Cfrac%7By_i%5Ctheta_i%20-%20b(%5Ctheta_i)%7D%7Ba(%5Cphi)%7D%20+%20c(y_i,%20%5Cphi)%5Cright%5C%7D"></p>
<p>Now, this is much easier to work with because:</p>
<ul>
<li>Products become sums</li>
<li>It’s numerically more stable</li>
<li>The maximum is the same (logarithm is monotonic, after all)</li>
</ul></section><section id="irls" class="level3" data-number="2.2"><h3 data-number="2.2" class="anchored" data-anchor-id="irls">
<span class="header-section-number">2.2</span> Fisher Scoring / IRLS Algorithm (The Actual Work)</h3>
<p>Since, there’s no actual closed form to calculate the coefficients, we can’t solve <img src="https://latex.codecogs.com/png.latex?U(%5Cbeta)%20=%200"> directly. There’s a method we can use called <strong>Iteratively Reweighted Least Squares (IRLS)</strong>, which is just Newton-Raphson but with the Fisher Information Matrix, denoted as <img src="https://latex.codecogs.com/png.latex?W">.</p>
<p>Before starting, we need to assign a variable that arbitrarily assigns design matrix <code>X</code>, the response vector <code>y</code>, and then determines the number of predictors <code>p</code>, the number of observations <code>n</code>, the maximum iteration <code>max_iter</code>, and the tolerance <code>eps</code>.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">In R</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">In Python</a></li>
</ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</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;"><a href="https://rdrr.io/r/base/nrow.html">nrow</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">X</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</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;"><a href="https://rdrr.io/r/base/nrow.html">ncol</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">X</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_iter</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">eps</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-7</span></span></code></pre></div></div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb4-2">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb4-3">max_iter <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;">100</span></span>
<span id="cb4-4">eps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-7</span>  </span></code></pre></div></div>
</div>
</div>
</div>
<p>Here are the steps:</p>
<section id="the-algorithm" class="level4" data-number="2.2.1"><h4 data-number="2.2.1" class="anchored" data-anchor-id="the-algorithm">
<span class="header-section-number">2.2.1</span> 1. <strong>The Algorithm:</strong>
</h4>
<p>Start with initial values <img src="https://latex.codecogs.com/png.latex?%5Cbeta%5E%7B(0)%7D"> (usually from an unweighted regression or just zeros).</p>
<p>This is how you program it in R and Python:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">R</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">Python</a></li>
</ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta</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;"><a href="https://rdrr.io/r/base/matrix.html">matrix</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, nrow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span>, ncol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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></code></pre></div></div>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb6-2"></span>
<span id="cb6-3">beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
</div>
</div>
</div>
</section><section id="repeat-until-convergence" class="level4" data-number="2.2.2"><h4 data-number="2.2.2" class="anchored" data-anchor-id="repeat-until-convergence">
<span class="header-section-number">2.2.2</span> 2. <strong>Repeat until convergence:</strong>
</h4>
<p>In this step, the extra steps, i.e.:</p>
<ul>
<li><p>Calculate linear predictor: <img src="https://latex.codecogs.com/png.latex?%5Ceta%5E%7B(t)%7D%20=%20%5Cmathbf%7BX%7D%5Cbeta%5E%7B(t)%7D"></p></li>
<li><p>Calculate fitted values: <img src="https://latex.codecogs.com/png.latex?%5Cmu%5E%7B(t)%7D%20=%20g%5E%7B-1%7D(%5Ceta%5E%7B(t)%7D)"></p></li>
<li>
<p>Calculate weights:</p>
<p><img src="https://latex.codecogs.com/png.latex?w_i%5E%7B(t)%7D%20=%20%5Cfrac%7B1%7D%7B%5Ctext%7BVar%7D(Y_i)%7D%20%5Cleft(%5Cfrac%7B%5Cpartial%20%5Cmu_i%7D%7B%5Cpartial%20%5Ceta_i%7D%5Cright)%5E2"></p>
</li>
<li>
<p>Calculate working response:</p>
<p><img src="https://latex.codecogs.com/png.latex?z_i%5E%7B(t)%7D%20=%20%5Ceta_i%5E%7B(t)%7D%20+%20(y_i%20-%20%5Cmu_i%5E%7B(t)%7D)%20%5Cfrac%7B%5Cpartial%20%5Ceta_i%7D%7B%5Cpartial%20%5Cmu_i%7D"></p>
</li>
<li>
<p>Update coefficients by solving weighted least squares:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbeta%5E%7B(t+1)%7D%20=%20(%5Cmathbf%7BX%7D%5ET%20W%5E%7B(t)%7D%20%5Cmathbf%7BX%7D)%5E%7B-1%7D%20%5Cmathbf%7BX%7D%5ET%20W%5E%7B(t)%7D%20z%5E%7B(t)%7D"></p>
</li>
<li><p>Check convergence: If <img src="https://latex.codecogs.com/png.latex?%7C%5Cbeta%5E%7B(t+1)%7D%20-%20%5Cbeta%5E%7B(t)%7D%7C%20%3C%20%5Cepsilon">, stop. Else, go back to step 1.</p></li>
</ul>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-3-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-1" aria-controls="tabset-3-1" aria-selected="true" href="">R</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-3-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-2" aria-controls="tabset-3-2" aria-selected="false" href="">Python</a></li>
</ul>
<div class="tab-content">
<div id="tabset-3-1" class="tab-pane active" aria-labelledby="tabset-3-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_iter</span><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>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Calculate linear predictor</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">eta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">X</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/matmult.html">%*%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta</span></span>
<span>    </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Calculate fitted values</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">family</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;">linkinv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">eta</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Calculate weights</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">V</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;"><a href="https://rdrr.io/r/base/vector.html">as.vector</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">family</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;">variance</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gradient</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;"><a href="https://rdrr.io/r/base/vector.html">as.vector</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">family</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;">mu.eta</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">eta</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w_vec</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gradient</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">V</span></span>
<span>    </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Working response</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z</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;"><a href="https://rdrr.io/r/base/vector.html">as.vector</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">eta</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/vector.html">as.vector</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gradient</span></span>
<span>    </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Update coefficients by solving weighted least squares</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">W</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;"><a href="https://rdrr.io/r/base/diag.html">diag</a></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;"><a href="https://rdrr.io/r/base/vector.html">as.vector</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w_vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta_new</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;"><a href="https://rdrr.io/r/base/solve.html">solve</a></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;"><a href="https://rdrr.io/r/base/t.html">t</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">X</span><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;"><a href="https://rdrr.io/r/base/matmult.html">%*%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">W</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/matmult.html">%*%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">X</span><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;"><a href="https://rdrr.io/r/base/matmult.html">%*%</a></span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/t.html">t</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">X</span><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;"><a href="https://rdrr.io/r/base/matmult.html">%*%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">W</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/matmult.html">%*%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z</span></span>
<span>    </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 6. Check convergence</span></span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/Extremes.html">max</a></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;"><a href="https://rdrr.io/r/base/MathFun.html">abs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta_new</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tol</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta_new</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">converged</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span>        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>    </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Final beta</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">beta_new</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<div id="tabset-3-2" class="tab-pane" aria-labelledby="tabset-3-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(max_iter):</span>
<span id="cb8-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Calculate linear predictor</span></span>
<span id="cb8-3">    eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> beta</span>
<span id="cb8-4">    </span>
<span id="cb8-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Calculate fitted values</span></span>
<span id="cb8-6">    mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> family.linkinv(eta)</span>
<span id="cb8-7">    </span>
<span id="cb8-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Calculate weights</span></span>
<span id="cb8-9">    V <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> family.variance(mu).flatten()</span>
<span id="cb8-10">    gradient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> family.mu_eta(eta).flatten()</span>
<span id="cb8-11">    w_vec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (gradient <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> V</span>
<span id="cb8-12">    </span>
<span id="cb8-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Working response</span></span>
<span id="cb8-14">    z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> gp</span>
<span id="cb8-15">    </span>
<span id="cb8-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Update coefficients by solving weighted least squares</span></span>
<span id="cb8-17">    W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.diag(w_vec)</span>
<span id="cb8-18">    beta_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linalg.inv(X.T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> X) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> (X.T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> z)</span>
<span id="cb8-19">    </span>
<span id="cb8-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 6. Check convergence</span></span>
<span id="cb8-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(beta_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> beta)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> tol:</span>
<span id="cb8-22">        beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> beta_new</span>
<span id="cb8-23">        converged <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb8-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb8-25">    </span>
<span id="cb8-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Final beta</span></span>
<span id="cb8-27">    beta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> beta_new</span></code></pre></div></div>
</div>
</div>
</div>
</section></section><section id="deviance" class="level3" data-number="2.3"><h3 data-number="2.3" class="anchored" data-anchor-id="deviance">
<span class="header-section-number">2.3</span> Be deviant: Deviance (GLM’s Version of Sum of Squares)</h3>
<p>To be frank, I am not planning to include this, but I was thinking if anyone is curious on how the fitness in GLMs is being measured. In Linear regression, we measure model fit by summarizing the residuals using Residual Sum of Squares (RSS). It is so odd for GLM to use other way to measure (well, the key term is “Generalized”, after all) — Instead, GLMs use <strong>deviance</strong>:</p>
<p><img src="https://latex.codecogs.com/png.latex?D%20=%202%5B%5Cell(%5Ctext%7Bsaturated%20model%7D)%20-%20%5Cell(%5Ctext%7Bfitted%20model%7D)%5D"></p>
<p>The saturated model has <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmu%7D_i%20=%20y_i"> (perfect fit). Deviance measures how much worse your model is than this.</p>
<p><strong>For specific families:</strong></p>
<ul>
<li>
<strong>Normal / Gaussian</strong>: <img src="https://latex.codecogs.com/png.latex?D%20=%20%5Csum(y_i%20-%20%5Chat%7B%5Cmu%7D_i)%5E2"> (literally just RSS)</li>
<li>
<strong>Poisson</strong>: <img src="https://latex.codecogs.com/png.latex?D%20=%202%5Csum%5Cleft%5By_i%5Clog%5Cleft(%5Cfrac%7By_i%7D%7B%5Chat%7B%5Cmu%7D_i%7D%5Cright)%20-%20(y_i%20-%20%5Chat%7B%5Cmu%7D_i)%5Cright%5D">
</li>
<li>
<strong>Binomial</strong>: <img src="https://latex.codecogs.com/png.latex?D%20=%202%5Csum%5Cleft%5By_i%5Clog%5Cleft(%5Cfrac%7By_i%7D%7B%5Chat%7B%5Cmu%7D_i%7D%5Cright)%20+%20(n_i-y_i)%5Clog%5Cleft(%5Cfrac%7Bn_i-y_i%7D%7Bn_i-%5Chat%7B%5Cmu%7D_i%7D%5Cright)%5Cright%5D">
</li>
</ul>
<p>Lower deviance = better fit.</p>
</section></section><section id="full-function-implementation" class="level2" data-number="3"><h2 data-number="3" class="anchored" data-anchor-id="full-function-implementation">
<span class="header-section-number">3</span> Full Function implementation</h2>
<p>The main programming languages are still R and Python. The functions I implemented expects to be brittle and not applicable for some cases.</p>
<p><em>The functions are implemented in this <a href="https://github.com/joshuamarie/joshuamarie/tree/main/posts/07-glm">source code</a></em>.</p>
<hr></section><section id="show-me-the-money-mtcars-logistic-regression-showdown" class="level2" data-number="4"><h2 data-number="4" class="anchored" data-anchor-id="show-me-the-money-mtcars-logistic-regression-showdown">
<span class="header-section-number">4</span> Show Me the Money: mtcars Logistic Regression Showdown</h2>
<p><em>Okay, enough theory — show me the money!</em></p>
<p>Now, we can implement a full function only from <em>In-depth mathematics and computation</em>. That is, if you only need the coefficients. Use any dataset you want, but on our example, we’ll use the <code>mtcars</code> dataset for the simpletons.</p>
<p>Why <code>mtcars</code>? Because every R tutorial on earth has used it at least once, and I’m not about to break tradition, am I? We’ll predict whether a car has an automatic (0) or manual (1) transmission (<code>am</code>) using horsepower (<code>hp</code>), and weight (<code>wt</code>). Classic binary outcome -&gt; logistic regression -&gt; GLM with binomial family and logit link.</p>
<p>We’ll do it three ways so you can flex at parties:</p>
<ol type="1">
<li>The lazy way (<code><a href="https://rdrr.io/r/stats/glm.html">stats::glm()</a></code> in R / <code>{statsmodels}</code> in Python)</li>
<li>The “I Implemented IRLS at 3 AM” way (calling our own IRLS function in this part)</li>
</ol>
<section id="tier-1-the-lazy-way-everyone-does-this" class="level3" data-number="4.1"><h3 data-number="4.1" class="anchored" data-anchor-id="tier-1-the-lazy-way-everyone-does-this">
<span class="header-section-number">4.1</span> Tier 1: The lazy way (everyone does this)</h3>
<p>This is the “why should I bother making myself one if there’s one existed already” part. R has built-in <code><a href="https://rdrr.io/r/stats/glm.html">glm()</a></code> from <code>{stats}</code> package, while Python has <code>{statsmodels}</code> package, ideally under <code>statsmodels.api</code>, not under <code>statsmodels.formula.api</code>, but for our example, we can use that instead.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-4-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-1" aria-controls="tabset-4-1" aria-selected="true" href="">R</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-4-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-2" aria-controls="tabset-4-2" aria-selected="false" href="">Python</a></li>
</ul>
<div class="tab-content">
<div id="tabset-4-1" class="tab-pane active" aria-labelledby="tabset-4-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_r</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;"><a href="https://rdrr.io/r/stats/glm.html">glm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">am</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, family <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;"><a href="https://rdrr.io/r/stats/family.html">binomial</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>link <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logit"</span><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><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/summary.html">summary</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fit_r</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = am ~ hp + wt, family = binomial(link = "logit"), 
    data = mtcars)

Coefficients:
            Estimate Std. Error z value Pr(&gt;|z|)   
(Intercept) 18.86630    7.44356   2.535  0.01126 * 
hp           0.03626    0.01773   2.044  0.04091 * 
wt          -8.08348    3.06868  -2.634  0.00843 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 43.230  on 31  degrees of freedom
Residual deviance: 10.059  on 29  degrees of freedom
AIC: 16.059

Number of Fisher Scoring iterations: 8</code></pre>
</div>
</div>
</div>
<div id="tabset-4-2" class="tab-pane" aria-labelledby="tabset-4-2-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> statsmodels.api <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sm</span>
<span id="cb11-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.genmod.families <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Binomial</span>
<span id="cb11-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> statsmodels.genmod.families.links <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Logit</span>
<span id="cb11-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> statsmodels.formula.api <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> smf</span>
<span id="cb11-5"></span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mtcars = sm.datasets.get_rdataset("mtcars").data</span></span>
<span id="cb11-7">mtcars <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r.mtcars</span>
<span id="cb11-8"></span>
<span id="cb11-9">fit_py <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> smf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb11-10">    .glm(</span>
<span id="cb11-11">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"am ~ hp + wt"</span>,</span>
<span id="cb11-12">        data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mtcars,</span>
<span id="cb11-13">        family<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Binomial(link<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Logit())</span>
<span id="cb11-14">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb11-15">    .fit()</span>
<span id="cb11-16"></span>
<span id="cb11-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(fit_py.summary())</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                 Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:                     am   No. Observations:                   32
Model:                            GLM   Df Residuals:                       29
Model Family:                Binomial   Df Model:                            2
Link Function:                  Logit   Scale:                          1.0000
Method:                          IRLS   Log-Likelihood:                -5.0296
Date:                Thu, 11 Dec 2025   Deviance:                       10.059
Time:                        14:35:33   Pearson chi2:                     15.0
No. Iterations:                     8   Pseudo R-squ. (CS):             0.6453
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          z      P&gt;|z|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     18.8663      7.444      2.535      0.011       4.277      33.455
hp             0.0363      0.018      2.044      0.041       0.001       0.071
wt            -8.0835      3.069     -2.634      0.008     -14.098      -2.069
==============================================================================</code></pre>
</div>
</div>
</div>
</div>
</div>
</section><section id="tier-2-the-i-implemented-irls-at-3-am-way" class="level3" data-number="4.2"><h3 data-number="4.2" class="anchored" data-anchor-id="tier-2-the-i-implemented-irls-at-3-am-way">
<span class="header-section-number">4.2</span> Tier 2: The “I Implemented IRLS at 3 AM” way</h3>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-5-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-1" aria-controls="tabset-5-1" aria-selected="true" href="">R</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-5-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-2" aria-controls="tabset-5-2" aria-selected="false" href="">Python</a></li>
</ul>
<div class="tab-content">
<div id="tabset-5-1" class="tab-pane active" aria-labelledby="tabset-5-1-tab">
<blockquote class="blockquote">
<p>View the <a href="https://github.com/joshuamarie/joshuamarie/tree/main/posts/07-glm/glm.r">source code</a></p>
</blockquote>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glm</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glm_custom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm_custom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">am</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, family <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;"><a href="https://rdrr.io/r/stats/family.html">binomial</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$coefficients
[1] 18.8662987  0.0362556 -8.0834752

$converged
[1] TRUE

$iterations
[1] 9</code></pre>
</div>
</div>
</div>
<div id="tabset-5-2" class="tab-pane" aria-labelledby="tabset-5-2-tab">
<blockquote class="blockquote">
<p>View the <a href="https://github.com/joshuamarie/joshuamarie/tree/main/posts/07-glm/glm.py">source code</a></p>
</blockquote>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> glm <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> glm_custom</span>
<span id="cb15-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> glm</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(glm_custom(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'am ~ hp + wt'</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mtcars, family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> glm.Binomial()))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'coefficients': {'Intercept': np.float64(18.866298717203943), 'hp': np.float64(0.03625559608221654), 'wt': np.float64(-8.083475182444579)}, 'converged': True, 'iterations': 9, 'beta_vector': array([[18.86629872],
       [ 0.0362556 ],
       [-8.08347518]])}</code></pre>
</div>
</div>
</div>
</div>
</div>
<hr></section></section><section id="when-things-go-wrong-trust-me-they-will" class="level2" data-number="5"><h2 data-number="5" class="anchored" data-anchor-id="when-things-go-wrong-trust-me-they-will">
<span class="header-section-number">5</span> When Things Go Wrong (Trust me, they Will)</h2>
<section id="my-model-wont-converge" class="level3" data-number="5.1"><h3 data-number="5.1" class="anchored" data-anchor-id="my-model-wont-converge">
<span class="header-section-number">5.1</span> “My model won’t converge!”</h3>
<ul>
<li>Check for complete separation in logistic regression</li>
<li>Try scaling your predictors</li>
<li>Check for perfect multicollinearity</li>
<li>Or just accept that your data is cursed</li>
</ul></section><section id="my-coefficients-are-huge" class="level3" data-number="5.2"><h3 data-number="5.2" class="anchored" data-anchor-id="my-coefficients-are-huge">
<span class="header-section-number">5.2</span> “My coefficients are huge!”</h3>
<ul>
<li>You forgot to scale your variables</li>
<li>Or you have a separation issue</li>
<li>Or your data really is that dramatic</li>
</ul></section><section id="the-deviance-is-gigantic" class="level3" data-number="5.3"><h3 data-number="5.3" class="anchored" data-anchor-id="the-deviance-is-gigantic">
<span class="header-section-number">5.3</span> “The deviance is gigantic!”</h3>
<ul>
<li>Maybe your model actually sucks?</li>
<li>Or you have outliers</li>
<li>Check if you’re using the right family</li>
<li>Consider adding interaction terms or polynomial terms</li>
</ul></section><section id="im-getting-warnings-about-fitted-probabilities-of-0-or-1" class="level3" data-number="5.4"><h3 data-number="5.4" class="anchored" data-anchor-id="im-getting-warnings-about-fitted-probabilities-of-0-or-1">
<span class="header-section-number">5.4</span> “I’m getting warnings about fitted probabilities of 0 or 1”</h3>
<ul>
<li>This is actually okay sometimes! It means you have very confident predictions</li>
<li>But if you’re getting tons of these, you might have separation issues</li>
<li>Check your data for extreme values</li>
</ul>
<hr></section></section><section id="final-boss-wisdom" class="level2" data-number="6"><h2 data-number="6" class="anchored" data-anchor-id="final-boss-wisdom">
<span class="header-section-number">6</span> Final Boss Wisdom</h2>
<ul>
<li>Linear regression is a GLM that forgot it was a GLM</li>
<li>The link function is the reason you’re not predicting negative counts</li>
<li>IRLS is just weighted least squares that updates itself until it stops crying</li>
<li>Deviance is RSS for adults</li>
</ul>
<p>Go forth and model responsibly.</p>
</section><section id="some-good-references" class="level2" data-number="7"><h2 data-number="7" class="anchored" data-anchor-id="some-good-references">
<span class="header-section-number">7</span> Some good references</h2>
<ul>
<li>McCullagh &amp; Nelder (1989) – The Bible. Dense. Bring coffee and a therapist.</li>
<li>Dobson &amp; Barnett – Actually readable introduction</li>
<li>Faraway – Practical R examples, minimal pain</li>
<li>Nelder &amp; Wedderburn (1972) – The original paper. Short, brilliant, life-changing</li>
<li>The UCLA stats page on GLMs – Still the best free resource in 2025</li>
</ul>
<p>And remember:</p>
<blockquote class="blockquote">
<p>“All models are wrong. But GLMs are wrong in the most elegant way possible.”</p>
</blockquote>
<p>Now go write some proper models, you beautiful statistician.</p>
</section><section id="appendix-cheat-sheet" class="level2" data-number="8"><h2 data-number="8" class="anchored" data-anchor-id="appendix-cheat-sheet">
<span class="header-section-number">8</span> Appendix — Cheat Sheet</h2>
<p><em>Which GLM Should I Use?</em></p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 22%">
<col style="width: 9%">
<col style="width: 14%">
<col style="width: 20%">
</colgroup>
<thead><tr class="header">
<th>Your Y looks like…</th>
<th>Distribution</th>
<th>Link</th>
<th>R Code</th>
<th>Python Code</th>
</tr></thead>
<tbody>
<tr class="odd">
<td>0, 1, 2, 3… (counts)</td>
<td>Poisson</td>
<td>log</td>
<td><code>family = poisson()</code></td>
<td><code>sm.families.Poisson()</code></td>
</tr>
<tr class="even">
<td>Yes/No (binary)</td>
<td>Binomial</td>
<td>logit</td>
<td><code>family = binomial()</code></td>
<td><code>sm.families.Binomial()</code></td>
</tr>
<tr class="odd">
<td>0.2, 0.7, 0.4… (proportions)</td>
<td>Binomial</td>
<td>logit</td>
<td><code>family = binomial()</code></td>
<td><code>sm.families.Binomial()</code></td>
</tr>
<tr class="even">
<td>Always positive, right-skewed</td>
<td>Gamma</td>
<td>log</td>
<td><code>family = Gamma()</code></td>
<td><code>sm.families.Gamma()</code></td>
</tr>
<tr class="odd">
<td>Any real number</td>
<td>Gaussian</td>
<td>identity</td>
<td>
<code>family = gaussian()</code> or <code><a href="https://rdrr.io/r/stats/lm.html">lm()</a></code>
</td>
<td><code>sm.families.Gaussian()</code></td>
</tr>
</tbody>
</table>
<p><em>Oh wait, the overdispersion in count data? R pre-installs <a href="http://www.stats.ox.ac.uk/pub/MASS4/">MASS</a> anyways, so bother use <code>glm.nb()</code> yourself. </em></p>
<p><strong>Pro tip</strong>: When in doubt, plot your residuals. If they look like a crime scene, you chose wrong.</p>


</section> ]]></description>
  <category>R</category>
  <category>Python</category>
  <category>statistics</category>
  <category>regression</category>
  <category>machine-learning</category>
  <guid>https://joshuamarie.com/posts/07-glm/</guid>
  <pubDate>Sat, 22 Nov 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/07-glm/image.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>Ways to load / attach packages in R</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/06-load-pkg/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/06-load-pkg/&amp;text=Ways%20to%20load%20R%20packages%20-%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/06-load-pkg/&amp;title=Ways%20to%20load%20R%20packages%20-%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Ways%20to%20load%20R%20packages%20-%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/06-load-pkg/" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/06-load-pkg/" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/06-load-pkg/&amp;title=Ways%20to%20load%20R%20packages%20-%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/06-load-pkg/&amp;title=Ways%20to%20load%20R%20packages%20-%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/06-load-pkg/&amp;name=Ways%20to%20load%20R%20packages%20-%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Ways to load R packages - Joshua Marie https://joshuamarie.com/posts/06-load-pkg/'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/06-load-pkg/%20Ways%20to%20load%20R%20packages%20-%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<p>Isn’t it great that R has more than 1 solution to load packages? Some of them are beautiful. Some of them should be illegal in at least three countries. Let’s rank them from “please never do this” to “finally, some good food.”</p>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/image.png" class="img-fluid"></p>
<p>In this post, I will try enumerate the different ways to load packages in R, and discuss their pros and cons. I will also rank them from worst to best solution in practices.</p>
<section id="different-ways-to-load-attach-packages-in-r" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Different ways to load / attach packages in R</h1>
<p>I sorted the different ways to load packages in R from worst to best solution in practices. This may be subjective, but I will try to justify my ranking based on the principles of good programming practices.</p>
<p>This is how I rank them from worst to best:</p>
<table class="caption-top table">
<thead><tr class="header">
<th>Rank</th>
<th>Method</th>
<th>Verdict</th>
</tr></thead>
<tbody>
<tr class="odd">
<td>9</td>
<td><code>base::use()</code></td>
<td>Experimental copium (now officially dead last)</td>
</tr>
<tr class="even">
<td>8</td>
<td><code>require()</code></td>
<td>War criminal</td>
</tr>
<tr class="odd">
<td>7</td>
<td><code>{pacman}</code></td>
<td>Convenience trap</td>
</tr>
<tr class="even">
<td>6</td>
<td><code>library()</code></td>
<td>Boomer energy</td>
</tr>
<tr class="odd">
<td>5</td>
<td><code>library() + conflicted</code></td>
<td>Manual gearbox in 2025</td>
</tr>
<tr class="even">
<td>4</td>
<td><code>::</code> everywhere</td>
<td>Professional wrist pain</td>
</tr>
<tr class="odd">
<td>3</td>
<td><code>{import}</code></td>
<td>Extremely polite gentleman</td>
</tr>
<tr class="even">
<td>2</td>
<td><code>{box}</code></td>
<td>The chosen one</td>
</tr>
<tr class="odd">
<td>1</td>
<td><code>{box}</code> + coffee</td>
<td>Ascended plane of existence</td>
</tr>
</tbody>
</table>
<section id="base-use" class="level2" data-number="1.1"><h2 data-number="1.1" class="anchored" data-anchor-id="base-use">
<span class="header-section-number">1.1</span> The new <strong>base::use()</strong> function (v4.4.0+)</h2>
<p>Update: When I discover the bug, thanks to <a href="https://www.reddit.com/user/guepier/">u/guepier</a> and his <a href="https://www.reddit.com/r/rstats/comments/1p1y89p/comment/nptplse/?utm_source=share&amp;utm_medium=web3x&amp;utm_name=web3xcss&amp;utm_term=1&amp;utm_content=share_button">comment</a>, this changes my mind. Now, I put this in the worst place amongst the solution I listed here.</p>
<p>I feel like R Core saw the chaos and said “fine, we’ll do something”. This function is available in R version 4.4.0 and above, by the way.</p>
<p>It allows you to load packages in a way that minimizes namespace conflicts by only attaching the functions you explicitly use. Take note that <code><a href="https://rdrr.io/r/base/use.html">base::use()</a></code> is a short case of <code><a href="https://rdrr.io/r/base/library.html">library()</a></code>, a simple wrapper, where it keeps <code>include.only</code> and set:</p>
<ol type="1">
<li>
<code>lib.loc</code> to <code>NULL</code>
</li>
<li>
<code>character.only</code> to <code>TRUE</code>
</li>
<li>
<code>logical.return</code> to <code>TRUE</code>
</li>
<li>
<code>attach.required</code> to <code>FALSE</code>
</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pkg'</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'obj1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'fun1'</span><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></code></pre></div></div>
</div>
<p>This is still <code><a href="https://rdrr.io/r/base/library.html">library()</a></code>, but granular imports are explicit. Except…</p>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/problem-uh-oh.gif" class="img-fluid"></p>
<p>Another problem occurs: Remember, it is just a simple wrapper of <code><a href="https://rdrr.io/r/base/library.html">library()</a></code>, therefore the import still goes to the search path.</p>
<p>It’s like putting a fancy new paint job on a 1987 Honda Civic and calling it a Ferrari. It LOOKS different, but under the hood, same old engine, baby.</p>
<p>For example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean_data</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dplyr'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'summarise'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tidyr'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pivot_longer'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            cols <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;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            names_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variable"</span>, </span>
<span>            values_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ave"</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 2
  Variable       Ave
  &lt;chr&gt;        &lt;dbl&gt;
1 Sepal.Length  5.84
2 Sepal.Width   3.06
3 Petal.Length  3.76
4 Petal.Width   1.20</code></pre>
</div>
</div>
<p>After I execute <code>mean_data(iris)</code>, the imports are accessible <em>everywhere</em>. EVERYWHERE!</p>
<p>And <code><a href="https://rdrr.io/r/base/use.html">base::use()</a></code> is still broken even in the latest R versions.</p>
<p>Like, it is so completely broken:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dplyr'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mutate'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <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;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Petal.Area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Petal.Length</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Petal.Width</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in mutate(iris, Petal.Area = Petal.Length * Petal.Width) : </span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   could not find function "mutate"</span></span></code></pre></div></div>
<blockquote class="blockquote">
<p>The issue is that subsequent <code><a href="https://rdrr.io/r/base/library.html">library()</a></code> calls for an identical package are ignored, and the same is true for <code><a href="https://rdrr.io/r/base/use.html">base::use()</a></code>. Bananas. Completely broken.</p>
</blockquote>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This is noted by R core team:</p>
<blockquote class="blockquote">
<p>This functionality is still experimental: interfaces may change in future versions.</p>
</blockquote>
</div>
</div>
</section><section id="require" class="level2" data-number="1.2"><h2 data-number="1.2" class="anchored" data-anchor-id="require">
<span class="header-section-number">1.2</span> Worst: Using <strong>require()</strong>
</h2>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/meme1.jpg" class="img-fluid"></p>
<p>~Just…no. I’ll be making a hot take here that sounds controversial, but this solution is the worst thing ever existed in R to attach the packages. ~</p>
<p>Update: This solution may be bad, but at least not worse than <code><a href="https://rdrr.io/r/base/use.html">base::use()</a></code>.</p>
<p>This function returns a Boolean value:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">require</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/message.html">suppressMessages</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/warning.html">suppressWarnings</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/print.html">print</a></span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div>
<p>It returns <code>TRUE</code> if the package is successfully loaded and <code>FALSE</code> otherwise.</p>
<p>And should only be applicable inside functions to check if a package is available.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">check_package</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;">function</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/library.html">require</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg</span>, quietly <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Package loaded successfully"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Package not available"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_package</span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Package not available"</code></pre>
</div>
</div>
<p>Using <code><a href="https://rdrr.io/r/base/library.html">require()</a></code> at the top of a script is how you get mysterious errors 50 lines later. Only acceptable inside functions when you actually check the return value.</p>
<p>Seriously, this is just <code><a href="https://rdrr.io/r/base/library.html">library()</a></code> where you can place it at the top level of your script, but add another extra steps.</p>
</section><section id="pacman" class="level2" data-number="1.3"><h2 data-number="1.3" class="anchored" data-anchor-id="pacman">
<span class="header-section-number">1.3</span> The pro boxer: <strong>{pacman}</strong>
</h2>
<p>This guy will punch you to death. Just kidding, Manny Pacquiao is a great boxer :). The <a href="https://github.com/trinker/pacman">pacman</a> package tries to streamline package management with functions like <code>p_load()</code>.</p>
<p>Do you know this?</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <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 class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">require</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pkg"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Well, they made a shortcut, with <code><a href="https://rdrr.io/pkg/pacman/man/p_load.html">pacman::p_load()</a></code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacman</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/pacman/man/p_load.html">p_load</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>You can do the same as above, except you can do this for multiple packages.</p>
<p>Here’s how:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacman</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/pacman/man/p_load.html">p_load</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg2</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>Sounds convenient, right?</p>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/trap.gif" class="img-fluid"></p>
<p>Actually mixes two completely different responsibilities:</p>
<ul>
<li>Installation (one-time setup)</li>
<li>Loading (analysis step)</li>
</ul>
<p>Great for interactive playtime. Disastrous in scripts, packages, CI/CD, or any environment without internet. Also:</p>
<ul>
<li><p>It violates the <a href="https://en.wikipedia.org/wiki/Single-responsibility_principle">single responsibility principle</a>, harder than a toddler with a drum kit.</p></li>
<li>
<p>This is like a pineapple pizza</p>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/lionfield-not-approved.gif" class="img-fluid"></p>
</li>
</ul></section><section id="library-classic" class="level2" data-number="1.4"><h2 data-number="1.4" class="anchored" data-anchor-id="library-classic">
<span class="header-section-number">1.4</span> The classic <strong>library()</strong>
</h2>
<p>Such a classic function, isn’t it? After all, this is the most used function to attach R package namespace. It is a standard practice that most R users use, and it is safe: It will throw an error if <code>pkg</code> is not installed. This function is traditional and simple:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>That’s it, right?</p>
<p>I hope it was that simple, but it has some serious downsides:</p>
<ol type="i">
<li>It attaches the entire package namespace to the <em>search path</em>,</li>
<li>It can lead to namespace clash, particularly if multiple packages have functions with the same name. This can make debugging difficult and lead to unexpected behaviors in your code.</li>
<li>It makes the imports unclear which functions come from which packages</li>
<li>All exported functions are available, even if you only need one or two</li>
</ol>
<p>To detach the attached package namespace in the search path, use <code><a href="https://rdrr.io/r/base/detach.html">detach()</a></code> function with <code>package :</code> keyword:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/detach.html">detach</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">package</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pkg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Be minded that <code><a href="https://rdrr.io/r/base/library.html">library()</a></code> function still potentially silently fails, even though it will throw an error, unlike <code><a href="https://rdrr.io/r/base/library.html">require()</a></code> where silent fails are always prominent.</p>
</div>
</div>
</section><section id="combo-pack" class="level2" data-number="1.5"><h2 data-number="1.5" class="anchored" data-anchor-id="combo-pack">
<span class="header-section-number">1.5</span> <strong>library()</strong> and <strong>{conflicted}</strong> package combo</h2>
<p>How about forcing the search path to select / deselect the imports? Introducing <a href="https://conflicted.r-lib.org/">conflicted</a> package.</p>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/meme2.jpg" class="img-fluid"></p>
<p>In this approach, I combine traditional <code><a href="https://rdrr.io/r/base/library.html">library()</a></code> with the <a href="https://conflicted.r-lib.org/">conflicted</a> package to explicitly handle namespace conflicts.</p>
<p>How good? For example, I prefer using <code><a href="https://dplyr.tidyverse.org/reference/filter.html">dplyr::filter()</a></code> over <code><a href="https://rdrr.io/r/stats/filter.html">stats::filter()</a></code>, but a bit later on, I want to use <code><a href="https://rdrr.io/r/stats/filter.html">stats::filter()</a></code> when I want to run time series. The <code><a href="https://conflicted.r-lib.org/reference/conflict_prefer.html">conflicted::conflict_prefer()</a></code> handles which you want to declare “winners” of conflicts.</p>
<p>I’ll make a scenario to make you understand:</p>
<ol type="1">
<li>
<p>I have no use with <code><a href="https://rdrr.io/r/stats/filter.html">stats::filter()</a></code> because I only want to keep the data frame based on the condition using <code><a href="https://dplyr.tidyverse.org/reference/filter.html">dplyr::filter()</a></code>, and I want to load the entire <a href="https://dplyr.tidyverse.org">dplyr</a> namespace. Here, I declare <code><a href="https://dplyr.tidyverse.org/reference/filter.html">dplyr::filter()</a></code> as “winner” of the conflict:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">conflicted</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://conflicted.r-lib.org/reference/conflict_prefer.html">conflict_prefer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'filter'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dplyr'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'stats'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" 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></code></pre></div></div>
</div>
</li>
<li>
<p>Then, I stopped using <code><a href="https://dplyr.tidyverse.org/reference/filter.html">dplyr::filter()</a></code> because I want to perform time series modelling with linear filtering using <code><a href="https://rdrr.io/r/stats/filter.html">stats::filter()</a></code>. Re-state <code><a href="https://rdrr.io/r/stats/filter.html">stats::filter()</a></code> as the “winner” of the conflict:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">conflicted</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://conflicted.r-lib.org/reference/conflict_prefer.html">conflict_prefer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'filter'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'stats'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dplyr'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</li>
</ol>
<p>Still loads everything. Still global. Still manual work. In my standard, this is actually good, but still not enough because it never allows granular imports and import aliasing, and besides, I’ve had better.</p>
</section><section id="double-colon" class="level2" data-number="1.6"><h2 data-number="1.6" class="anchored" data-anchor-id="double-colon">
<span class="header-section-number">1.6</span> Tedious but Explicit: The <strong>::</strong> Operator</h2>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/everywhere-meme.jpg" class="img-fluid"></p>
<p>Before packages like <a href="https://klmr.me/box/">box</a> and <a href="https://rticulate.github.io/import/">import</a> introduced alternative import systems to R, the <code>::</code> operator was (and still is) R’s built-in way to explicitly reference functions from specific namespaces without loading entire packages.</p>
<p>The <code>::</code> operator is the most explicit base R solution for calling package functions. It’s part of R’s namespace system and requires no external dependencies - just base R.</p>
<p>Here’s how:</p>
<ul>
<li>The syntax is <code>package::function()</code>, which tells R exactly which package to pull the function from without attaching that package to your search path.</li>
</ul>
<p>Most of us using R are definitely using this (I am reusing an example from base::use):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean_data</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.data</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/across.html">across</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyselect.r-lib.org/reference/where.html">where</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            cols <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;"><a href="https://tidyselect.r-lib.org/reference/where.html">where</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            names_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variable"</span>, </span>
<span>            values_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ave"</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 2
  Variable       Ave
  &lt;chr&gt;        &lt;dbl&gt;
1 Sepal.Length  5.84
2 Sepal.Width   3.06
3 Petal.Length  3.76
4 Petal.Width   1.20</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled" title="Notice">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Notice
</div>
</div>
<div class="callout-body-container callout-body">
<blockquote class="blockquote">
<p>Noticed that I don’t call <code>dplyr::</code> for <code><a href="https://dplyr.tidyverse.org/reference/across.html">across()</a></code> and <code><a href="https://tidyselect.r-lib.org/reference/where.html">where()</a></code>? I have a <a href="https://joshuamarie.com/posts/04-tidyselect-helpers/">blog</a> talking about this.</p>
</blockquote>
</div>
</div>
<p>This is great, compared to the previous solutions, no external packages needed and works mostly in any R version. The problem is this is way too verbose and repetitive, especially with many function calls:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot2</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot2</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">date</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot2</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot2</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_path.html">geom_line</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot2</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">theme_minimal</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot2</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date (by month)"</span>,</span>
<span>        y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value (in dollars)"</span>, </span>
<span>        title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monthly Value in Dollar"</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>Being typing-intensive is why I called this solution “tedious”.</p>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/everywhere-meme.jpg" class="img-fluid"></p>
<p>Respectable, but I bet nobody wants to type that many in 2025.</p>
</section><section id="import-pack" class="level2" data-number="1.7"><h2 data-number="1.7" class="anchored" data-anchor-id="import-pack">
<span class="header-section-number">1.7</span> Second to best: <strong>{import}</strong> package</h2>
<p><em>It is so close!</em></p>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/almost-there-almost.jpg" class="img-fluid"></p>
<p>This package is made before <a href="https://klmr.me/box/">box</a>. So, before <a href="https://klmr.me/box/">box</a>, the <a href="https://rticulate.github.io/import/">import</a> package is the best solution ever made, arrived to fix <code><a href="https://rdrr.io/r/base/library.html">library()</a></code>’s most egregious issues. Created by Stefan Milton Bache (of pipe fame), it brings selective imports to R without requiring a complete paradigm shift.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">First Example</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Symbol binding</a></li>
</ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>The first example is simple: Normal imports with aliases.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">import</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://import.rticulate.org/reference/importfunctions.html">from</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span>, </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">select</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rename</span>, keep_when <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summarise</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">import</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://import.rticulate.org/reference/importfunctions.html">from</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span>, long <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_longer</span>, wide <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_wider</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">drop_na</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">import</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://import.rticulate.org/reference/importfunctions.html">from</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">diamonds</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cut_width</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">diamonds</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">keep_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cut</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/match.html">%in%</a></span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ideal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Premium"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">carat</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        price_per_carat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">price</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">carat</span>,</span>
<span>        size_category <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;">cut_width</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">carat</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">carat</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cut</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">color</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">price</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">price_per_carat</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">size_category</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wide</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        names_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cut</span>,</span>
<span>        values_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">price_per_carat</span>,</span>
<span>        values_fn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">median</span></span>
<span>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/across.html">across</a></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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ideal</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Premium</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><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>        n <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;"><a href="https://dplyr.tidyverse.org/reference/context.html">n</a></span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 3
  Ideal Premium     n
  &lt;dbl&gt;   &lt;dbl&gt; &lt;int&gt;
1 6494.   5978.  9951</code></pre>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>Use backticks around <code>%&gt;%</code> since it is under <a href="https://adv-r.hadley.nz/names-values.html#non-syntactic">non-syntactic names</a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">import</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://import.rticulate.org/reference/importfunctions.html">from</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">select</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summarise</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">relocate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">import</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://import.rticulate.org/reference/importfunctions.html">from</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">magrittr</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%&gt;%`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">import</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://import.rticulate.org/reference/importfunctions.html">from</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span>, long <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_longer</span>, wide <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_wider</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">drop_na</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        hp_per_cyl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span>,</span>
<span>        efficiency <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">disp</span></span>
<span>    <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;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">disp</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp_per_cyl</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">efficiency</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyselect.r-lib.org/reference/everything.html">everything</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/across.html">across</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp_per_cyl</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">efficiency</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                mu <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                sigma <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            .names <span 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}..{.fn}"</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        n <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;"><a href="https://dplyr.tidyverse.org/reference/context.html">n</a></span><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>    <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;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">long</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        cols <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;"><a href="https://tidyselect.r-lib.org/reference/starts_with.html">contains</a></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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span><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>        names_sep <span 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>        names_to <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variable"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        values_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Est"</span></span>
<span>    <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;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wide</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        names_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Stat</span>, </span>
<span>        values_from <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Est</span></span>
<span>    <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;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/relocate.html">relocate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>, .after <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;"><a href="https://tidyselect.r-lib.org/reference/everything.html">last_col</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        se <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma</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;"><a href="https://rdrr.io/r/base/MathFun.html">sqrt</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        cv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 6
  Variable       mu  sigma     n      se     cv
  &lt;chr&gt;       &lt;dbl&gt;  &lt;dbl&gt; &lt;int&gt;   &lt;dbl&gt;  &lt;dbl&gt;
1 mpg        19.7   1.45       7 0.549   0.0736
2 hp_per_cyl 20.4   4.04       7 1.53    0.198 
3 efficiency  0.112 0.0231     7 0.00872 0.206 </code></pre>
</div>
</div>
</div>
</div>
</div>
<p>It’s so awesome. Why?</p>
<ol type="1">
<li>No masking</li>
<li>Explicit at the top</li>
<li>Works with roxygen2 (<span class="citation" data-cites="importFrom">@importFrom</span>)</li>
<li>Imports the pipe like any other function</li>
</ol>
<p>There’s still some limitations. Even though <a href="https://rticulate.github.io/import/">import</a> provide necessities that solves my problem in R’s import system -</p>
<ol type="1">
<li>
<p>It has no unifying solution to attach the imports in the current environment. In fact, <a href="https://rticulate.github.io/import/">import</a> functions still attach imported functions to the parent environment (usually global). What I mean is that they’re not truly scoped to a module or function. Thus, the use of <code><a href="https://import.rticulate.org/reference/importfunctions.html">import::here()</a></code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/with.html">with</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">import</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://import.rticulate.org/reference/importfunctions.html">here</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stats</span>, corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cor</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Sepal.Length</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Petal.Length</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.8717538</code></pre>
</div>
</div>
<p>The expression we made, <code>with(iris, { ... })</code> creates a temporary environment that disappears immediately. So <code>corr()</code> is placed exactly there, inside that temporary environment, and you cannot reuse <code>corr()</code> somewhere in the environment, even in the global environment</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Error in corr(1:10, 2:11) : could not find function "corr"</span></span></code></pre></div></div>
<p>This is better than loading entire packages, but not as clean as lexical scoping.</p>
</li>
<li><p>The package was designed primarily for <strong>CRAN packages</strong>. File-based modules feel like an afterthought rather than a first-class feature.</p></li>
<li><p>It lacks support for <em>nested module hierarchies</em>. You can import from files with this package, but you can’t organize modules into sophisticated directory structures with their own internal dependencies.</p></li>
<li><p>Unlike <a href="https://klmr.me/box/">box</a>, there’s no way to import a whole package as an object without attaching names</p></li>
</ol></section><section id="box" class="level2" data-number="1.8"><h2 data-number="1.8" class="anchored" data-anchor-id="box">
<span class="header-section-number">1.8</span> The ergonomically superior <strong>{box}</strong> package</h2>
<p><img src="https://joshuamarie.com/posts/06-load-pkg/gordon-ramsay-finally.gif" class="img-fluid"></p>
<p><em>Finally, some good food. </em></p>
<div class="callout callout-style-default callout-note callout-titled" title="My impression">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>My impression
</div>
</div>
<div class="callout-body-container callout-body">
<p>In 2021, Konrad Rudolph looked at R’s prehistoric import system, said:</p>
<blockquote class="blockquote">
<p>“This is rubbish”</p>
</blockquote>
<p><em>Disclaimer: I don’t know if he said it, I said this just for fun ;)</em></p>
<p>I strongly agree. And then dropped one of the magnum opus: <a href="https://klmr.me/box/">box</a> — he dropped it like Gordon Ramsay dropping a perfectly seared Wellington on the pass.</p>
</div>
</div>
<p>This isn’t “slightly nicer imports” — it’s a complete rethinking of how R package should be loaded, and R code should be organized and namespaced. It brings true module systems (like Python, JavaScript, or Ruby) to R.</p>
<p>There’s 4 of a kind to import like a sane person:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purrr</span>,                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1</span></span>
<span>    tbl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tibble</span>,                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2</span></span>
<span>    dplyr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">select</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stats</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>st_filter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<blockquote class="blockquote">
<p>Source: https://github.com/klmr/box</p>
</blockquote>
<ol type="1">
<li>
<p>Attached the names? Nah, even better:</p>
<ul>
<li>Imports the entire <a href="https://purrr.tidyverse.org/">purrr</a> package as an <em>object</em>.<br>
</li>
<li>Nothing goes into the search path.<br>
</li>
<li>You use it as <code>purrr$map()</code>, <code>purrr$keep()</code>, etc.<br>
</li>
<li>Zero risk of masking, zero pollution. Pure bliss.</li>
</ul>
</li>
<li>
<p>Whole package? But make it <em>short</em></p>
<ul>
<li>Same vibe as 1, but you give the package a cute little nickname.</li>
<li>Now you write <code>tbl$tbl_df()</code>, <code>tbl$as_tibble()</code>, etc.<br>
</li>
<li>Perfect when you hate typing <code>tibble::</code> but also hate global mess.</li>
</ul>
</li>
<li>
<p>I want the whole namespace… but only some names in my face.</p>
<ul>
<li><p>A killer move, actually: import the whole package as an object, and selectively attach only the functions you actually want to write naked.<br></p></li>
<li><p>So your pipelines stay clean: <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code>, <code><a href="https://dplyr.tidyverse.org/reference/select.html">select()</a></code>, <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code> — all smooth, drama-free.</p></li>
<li>
<p>But when you need the weird stuff, you still have the entire namespace sitting there like:</p>
<pre><code>dplyr$reconstruct_tibble_from_who_knows_what()</code></pre>
</li>
</ul>
</li>
<li>
<p>“I refuse to be gaslit by <code><a href="https://rdrr.io/r/stats/filter.html">stats::filter()</a></code> ever again.”</p>
<ul>
<li>“I want everything from <code>{stats}</code> (because base R is already everywhere), but <code><a href="https://rdrr.io/r/stats/filter.html">stats::filter()</a></code> is a war criminal that keeps fighting with <code><a href="https://dplyr.tidyverse.org/reference/filter.html">dplyr::filter()</a></code>.”</li>
<li>So basically, everything from <code>{stats}</code> is attached, but rename that one cursed function to <code>st_filter()</code> so it never bites me again.</li>
<li>The <code>...</code> means “everything else, with their original names”.</li>
</ul>
</li>
</ol>
<p>But wait, there’s more!</p>
<p>Here, watch the madness of how I apply <a href="https://klmr.me/box/">box</a> to load package deps:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">select</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rename</span>, </span>
<span>        keep_when <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename because we want to avoid needless fighting</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summarise</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">across</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">everything</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_longer</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pivot_wider</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">drop_na</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">magrittr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%&gt;%`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># yes, don't forget that the pipe is just another import</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_minimal</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">labs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggsave</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lubridate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ymd</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">year</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">month</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">floor_date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data.table</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fread</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># because sometimes you need speed, not dignity</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>Less <code>::</code> spam. No <code>package::function()</code> that makes your code look like it’s been hit by shrapnel. Zero <code><a href="https://rdrr.io/r/base/library.html">library()</a></code> / <code><a href="https://rdrr.io/r/base/library.html">require()</a></code>.</p>
<p>And then, the part that makes grown R programmers cry tears of joy — You are also allowed to reuse exported namespace from an R script or a folder as a module.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">app</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glm_fit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># brings everything exported</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">app</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plots</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_pub</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">theme_pub</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># only the theme</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">app</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">utils</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cleaning</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">clean_names</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fix_dates</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">secret_sauce</span>                     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># local folder / script = module</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>With <a href="https://klmr.me/box/">box</a>, you can create modules that encapsulate your code and its dependencies — another revolutionary and W move in R community. This package is making my life easier in managing and reuse code across different projects.</p>
<p>This approach aligns well with modern programming practices and helps to keep your codebase clean and maintainable.</p>
<section id="little-resources" class="level3" data-number="1.8.1"><h3 data-number="1.8.1" class="anchored" data-anchor-id="little-resources">
<span class="header-section-number">1.8.1</span> Little resources</h3>
<p>Other resources to learn more about this package:</p>
<ol type="1">
<li><a href="https://cran.r-project.org/web/packages/box/index.html">CRAN Index</a></li>
<li><a href="https://klmr.me/box/">Box README</a></li>
<li><a href="https://joshuamarie.github.io/modules-in-r/">My book</a></li>
</ol></section></section></section><section id="remarks" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> Remarks</h1>
<p>While there are multiple ways to load packages in R, not all methods are created equal — some were created more equal than others, and some were created as war crimes. The choice of method can significantly impact the readability, maintainability, and reliability of your code, as well as your blood pressure and willingness to live.</p>
<p>I strongly recommend using the <a href="https://klmr.me/box/">box</a> package in your projects, personal or not, for its modular approach and ability to avoid namespace clashes, making it a superior choice for loading packages in R. Like, come on, it’s 2025. We have smartphones that can detect if you’re about to have a heart attack. We have AI that can write poetry. We should NOT still be debugging namespace conflicts like it’s 2005.</p>


</section> ]]></description>
  <category>R</category>
  <category>packages</category>
  <guid>https://joshuamarie.com/posts/06-load-pkg/</guid>
  <pubDate>Sun, 16 Nov 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/06-load-pkg/image.png" medium="image" type="image/png" height="121" width="144"/>
</item>
<item>
  <title>How much do you know about pipes?</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/05-pipes/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/05-pipes/&amp;text=Pipes%20in%20R%20-%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/05-pipes/&amp;title=Pipes%20in%20R%20-%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Pipes%20in%20R%20-%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/05-pipes/" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/05-pipes/" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/05-pipes/&amp;title=Pipes%20in%20R%20-%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/05-pipes/&amp;title=Pipes%20in%20R%20-%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/05-pipes/&amp;name=Pipes%20in%20R%20-%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Pipes in R - Joshua Marie https://joshuamarie.com/posts/05-pipes/'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/05-pipes/%20Pipes%20in%20R%20-%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="pledge" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Pledge</h1>
<p>I’ll go dive about the history of pipes in R. Pipes have revolutionized the way we write R code, making it more readable and maintainable. But the story of pipes in R is richer than many realize. While most R users are already familiar with <a href="https://magrittr.tidyverse.org">magrittr</a>’s <code>%&gt;%</code> or the native R <code>|&gt;</code>, the journey of pipes in R spans multiple packages and years of implementations, each with unique features and use cases.</p>
<p>In this post, I’ll be chronological about what I explore in the history and variety of pipes available in R, from the pioneering days to modern implementations.</p>
<p><em>But the question still remains: How much did you really learn about pipes in R? </em></p>
</section><section id="brief-definition-before-starting" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> Brief Definition before starting</h1>
<p>A <strong>pipe</strong> operator is a binary operator, just like <code>+</code>, that passes the output of one function as the input to the another expression. In R, it mimics Python’s method chaining:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">R</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Python</a></li>
</ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<ol type="i">
<li>Without breaking into another line:</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <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;">f</span><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 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;">g</span><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 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;">h</span><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></code></pre></div></div>
</div>
<ol start="2" type="i">
<li>Breaking into next line:</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">g</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">h</span><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></code></pre></div></div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<ol type="i">
<li>Without breaking into another line:</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">x.f().g().h()</span></code></pre></div></div>
<ol start="2" type="i">
<li>Breaking into next line:</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-2">    .f() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-3">    .g() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-4">    .h()</span>
<span id="cb4-5">    </span>
<span id="cb4-6">(</span>
<span id="cb4-7">    x</span>
<span id="cb4-8">      .f() </span>
<span id="cb4-9">      .g() </span>
<span id="cb4-10">      .h()</span>
<span id="cb4-11">)</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>In Python, you can’t break the code into another line, unless <code>\</code> between the methods, or closing the expression with <code>()</code> is applied. As you can see, R is more forgiving and a lot better than Python, because it is only bounded in its class.</p>
</div>
</div>
</div>
</div>
</div>
<p>It lets you at least avoid creating intermediate variables and write code that reads from <strong>left to right</strong> — Usually, some documentation refers it as left-hand side (LHS) and right-hand side (RHS), respectively.</p>
</section><section id="the-early-days-pre-pipe-workflows-2010" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> The Early Days: Pre-Pipe Workflows (≤2010)</h1>
<p>Before pipes, the usual workflow in R relies on the following on nested function calls:</p>
<ol type="i">
<li>Nested function calls</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><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 class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div>
<p>This would be difficult to read and maintain once the function calls get deeper</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></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;"><a href="https://rdrr.io/r/base/Log.html">log</a></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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.96</code></pre>
</div>
</div>
<ol start="2" type="i">
<li>Intermediate variables</li>
</ol>
<p>Instead of a deep nested call</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log_x</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;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu_x</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log_x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu_x_rounded</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;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu_x</span>, <span class="fl" 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mu_x_rounded</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.96</code></pre>
</div>
</div>
<p>My problem with <em>intermediate variables</em> is that it is cluttered with temporary variables.</p>
</section><section id="timeline-of-r-pipes" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> Timeline of R Pipes</h1>
<p>Many of R developers in the past invented pipes, like many times. Let’s explore them chronologically.</p>
<p>See how pipes evolved from experimental packages to core R:</p>
<p><img src="https://joshuamarie.com/posts/05-pipes/pipes-timeline.png" class="img-fluid"></p>
<section id="the-piper-pioneer-2013" class="level2" data-number="4.1"><h2 data-number="4.1" class="anchored" data-anchor-id="the-piper-pioneer-2013">
<span class="header-section-number">4.1</span> 1. The <strong>{pipeR}</strong> Pioneer (2013)</h2>
<p>The <a href="https://renkun.me/pipeR">pipeR</a> package by Kun Ren was one of the earliest pipe implementations in R, introducing the <code>%&gt;&gt;%</code> operator.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pipeR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%&gt;&gt;%`</span><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></span>
<span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.5</code></pre>
</div>
</div>
<p>Here’s the cool part:</p>
<ol type="i">
<li>Lambda expressions with parentheses:</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;&gt;%</span></span>
<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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><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 class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 11</code></pre>
</div>
</div>
<ol start="2" type="i">
<li>Side effects with continued piping:</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><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;">%&gt;&gt;%</span></span>
<span>    <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, main <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Random Normal Values"</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;&gt;%</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Side effect</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/05-pipes/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.09</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled" title="Why it faded">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Why it faded
</div>
</div>
<div class="callout-body-container callout-body">
<p>It’s not like it vanished from the existence, more like it is superseded by <a href="https://magrittr.tidyverse.org">magrittr</a> and took over.</p>
</div>
</div>
</section><section id="the-game-changer-magrittr-pipe-2014" class="level2" data-number="4.2"><h2 data-number="4.2" class="anchored" data-anchor-id="the-game-changer-magrittr-pipe-2014">
<span class="header-section-number">4.2</span> 2. The Game Changer: <strong>{magrittr}</strong> Pipe (2014)</h2>
<p>The <a href="https://magrittr.tidyverse.org">magrittr</a> package, created by Stefan Milton Bache and later maintained by Lionel Henry at Posit (formerly RStudio), became the most popular pipe implementation. It was inspired by F#’s pipe-forward operator and Unix pipes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">magrittr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%&gt;%`</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%&lt;&gt;%`</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%T&gt;%`</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%$%`</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%!&gt;%`</span><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></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><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;">%&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div>
<p>Do you know? There are plenty pipe operators in <a href="https://magrittr.tidyverse.org">magrittr</a> package, consists of at least 5 operators. Here are the special features:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">The classic</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">Assignment pipe</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-2-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-3" aria-controls="tabset-2-3" aria-selected="false" href="">Tee pipe</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-2-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-4" aria-controls="tabset-2-4" aria-selected="false" href="">Exposition pipe</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-2-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-5" aria-controls="tabset-2-5" aria-selected="false" href="">Eager pipe</a></li>
</ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<p>The <code>%&gt;%</code> is <a href="https://magrittr.tidyverse.org">magrittr</a>’s standard and “lazy” pipe - it doesn’t evaluate arguments until needed, which can affect behavior with certain functions. Lazy evaluation means that the RHS is only computed when its value is required, which optimizes performance but can lead to surprises with side-effect-heavy code.</p>
<p>To understand better how <code>%&gt;%</code> works, let’s give a demonstration by applying dot placeholder for non-first arguments:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = mpg ~ cyl, data = .)

Coefficients:
(Intercept)          cyl  
     37.885       -2.876  </code></pre>
</div>
</div>
<p>The dot (<code>.</code>) acts as a placeholder for the piped value, allowing it to be inserted into any argument position—not just the first. You can also apply multiple placeholders:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><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;">%&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/split.html">split</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$`4`
            mpg cyl disp hp drat   wt  qsec vs am gear carb
Datsun 710 22.8   4  108 93 3.85 2.32 18.61  1  1    4    1

$`6`
                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4      21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag  21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1

$`8`
                   mpg cyl disp  hp drat   wt  qsec vs am gear carb
Hornet Sportabout 18.7   8  360 175 3.15 3.44 17.02  0  0    3    2</code></pre>
</div>
</div>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<p>The <code>%&lt;&gt;%</code> operator is invoking reference semantics, where it pipes and assigns the result back to the original variable:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&lt;&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><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 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;"><a href="https://rdrr.io/r/base/sum.html">sum</a></span><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><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4.787492</code></pre>
</div>
</div>
<p>This is equivalent to <code>x = x %&gt;% log() %&gt;% sum()</code> but more concise. What happened here is we created a side-effect of <code>x</code>. Some <a href="https://www.reddit.com/r/rstats/comments/f77mjv/why_are_the_and_pipes_magrittr_not_used_with_dplyr/">pointed</a> it out why it is a problem.</p>
</div>
<div id="tabset-2-3" class="tab-pane" aria-labelledby="tabset-2-3-tab">
<p>The <code>%T&gt;%</code> “tee” pipe passes the left-hand side value forward, not the output of the right-hand side. Useful for side effects like plotting or printing, where you want to perform an action but continue with the original data:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><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;">%T&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>main <span 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 before mean"</span><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;">%&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/05-pipes/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.09</code></pre>
</div>
</div>
<p>This should be the equivalent:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></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;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, main <span 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 before mean"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></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;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><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 class="fl" 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>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>So, if you try the following:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%T&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3 4 5</code></pre>
</div>
</div>
<p>The <code>%T&gt;%</code> operator discards the output of <code>mean(1:5)</code>, and that’s because <code><a href="https://rdrr.io/r/base/mean.html">mean()</a></code> doesn’t return a side-value effect.</p>
<p>By the way, the “tee” name comes from Unix’s <code>tee</code> command, which splits output streams.</p>
</div>
<div id="tabset-2-4" class="tab-pane" aria-labelledby="tabset-2-4-tab">
<p>The <code>%$%</code> “exposition” pipe exposes the names within the left-hand side object to the right-hand side expression:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb30" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%$%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/cor.html">cor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.852162</code></pre>
</div>
</div>
<p>This is equivalent to:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb32" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/cor.html">cor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>This is particularly useful with functions that don’t have a <code>data</code> argument.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Do not use <code>%$%</code> operator when LHS is not a a list or data frame with named elements.</p>
</div>
</div>
</div>
<div id="tabset-2-5" class="tab-pane" aria-labelledby="tabset-2-5-tab">
<p>The <code>%!&gt;%</code> operator is the “eager” version of <code>%&gt;%</code> that evaluates arguments <em>immediately</em>. This can matter for functions with non-standard evaluation:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb33" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Standard (lazy) pipe</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/subset.html">subset</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"setosa"</span><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;">%&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb35" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Eager pipe (forces immediate evaluation)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%!&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/subset.html">subset</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"setosa"</span><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;">%!&gt;%</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa</code></pre>
</div>
</div>
<p>In most cases, the difference is subtle, but it can matter for advanced programming.</p>
<p>To see the actual difference:</p>
<ol type="i">
<li>
<code>%!&gt;%</code>: <code>cat(1)</code> is immediately evaluated (it evaluates from left to right)</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb37" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%!&gt;%</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/cat.html">cat</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%!&gt;%</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/cat.html">cat</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>12</code></pre>
</div>
</div>
<ol start="2" type="i">
<li>
<code>%&gt;%</code>: Evaluates only <code>cat(2)</code> as the first result is never used</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb39" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/cat.html">cat</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/cat.html">cat</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>  </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>2</code></pre>
</div>
</div>
<p>Source: https://stackoverflow.com/questions/76326742/what-are-the-differences-and-use-cases-of-the-five-magrittr-pipes</p>
</div>
</div>
</div>
</section><section id="the-wrapr-dot-arrow-2017" class="level2" data-number="4.3"><h2 data-number="4.3" class="anchored" data-anchor-id="the-wrapr-dot-arrow-2017">
<span class="header-section-number">4.3</span> 3. The <strong>{wrapr}</strong> Dot Arrow (2017)</h2>
<p>John Mount’s <a href="https://github.com/WinVector/wrapr">wrapr</a> package provides the <code>%.&gt;%</code> “dot arrow” pipe, a deliberate and explicit alternative to <code>%&gt;%</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb41" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wrapr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%.&gt;%`</span><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></span>
<span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><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;">%.&gt;%</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, <span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.5</code></pre>
</div>
</div>
<p>I don’t know much about this pipe, to be honest. As what I can see, this pipe requires the dot to always be explicit, which, for me, it’s so good that it can prevent some subtle bugs and makes code intentions clearer.</p>
</section><section id="the-bizarro-pipe-base-r-2017" class="level2" data-number="4.4"><h2 data-number="4.4" class="anchored" data-anchor-id="the-bizarro-pipe-base-r-2017">
<span class="header-section-number">4.4</span> 4. The Bizarro Pipe (Base R, ~2017)</h2>
<p>I am not sure when this operator released, but there’s a pipe operator (not categorically) in base R: the “Bizarro pipe” (<code>-&gt;.;</code>), that works like <code>%&gt;%</code> and <code>%.&gt;%</code>. It’s not a formal operator but an emergent behavior from combining existing R syntax.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb43" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>; </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><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;">-&gt;</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>; </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, <span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.5</code></pre>
</div>
</div>
<p>The Bizarro pipe works by:</p>
<ol type="1">
<li>Using right assignment <code>-&gt;</code> to assign to <code>.</code> (this is done by typing <code>-</code> + <code>&gt;</code> + <code>.</code>)</li>
<li>Ending each statement with <code>;</code> to separate expressions</li>
<li>The next line uses <code>.</code> as input</li>
</ol>
<p>It’s called “Bizarro” because it uses right-to-left assignment syntax (<code>-&gt;</code>) to create a left-to-right workflow.</p>
<p>However, it has disadvantages (talked in this <a href="https://stackoverflow.com/a/67868877">Stackoverflow</a> discussion):</p>
<ul>
<li>Creates hidden side-effects (the persistent <code>.</code> variable)</li>
<li>Goes against R style guides (right assignment and semicolons are discouraged)</li>
<li>Can lead to subtle bugs if you forget to assign to <code>.</code> at some step</li>
<li>The <code>.</code> variable is hidden from <code><a href="https://rdrr.io/r/base/ls.html">ls()</a></code> and IDE inspectors</li>
<li>It’s so pesky, it won’t auto-indent</li>
</ul>
<p>Seriously, I won’t recommend Bizarro pipe at all. It is still a nice touch as a temporary replacement of <code>%&gt;%</code> for chained R codes, and will not use it for production code.</p>
</section><section id="the-native-pipe-r-v4.1-2021" class="level2" data-number="4.5"><h2 data-number="4.5" class="anchored" data-anchor-id="the-native-pipe-r-v4.1-2021">
<span class="header-section-number">4.5</span> 5. The Native Pipe (R v4.1+, 2021)</h2>
<p>In May 2021, R v4.1 introduced the native pipe operator <code>|&gt;</code> (type <code>|</code> and <code>&gt;</code>), bringing pipe functionality into base R without the need for external packages. This operator is the actual operator that was inspired by the pipe-forward operator in F# and the concept of Unix pipes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb45" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3</code></pre>
</div>
</div>
<p>This is too identical to <code>%&gt;%</code> from <a href="https://magrittr.tidyverse.org">magrittr</a> with some obvious differences.</p>
<section id="common-differences-from-magrittr-pipe" class="level3" data-number="4.5.1"><h3 data-number="4.5.1" class="anchored" data-anchor-id="common-differences-from-magrittr-pipe">
<span class="header-section-number">4.5.1</span> Common differences from <code>{magrittr}</code> pipe</h3>
<p>The placeholder for <code>|&gt;</code> is now applied in R v4.2 and above. For the syntax, it rather uses <code>_</code>, not <code>.</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb47" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = mpg ~ cyl, data = mtcars)

Coefficients:
(Intercept)          cyl  
     37.885       -2.876  </code></pre>
</div>
</div>
<p>The native pipe:</p>
<ul>
<li>Is slightly faster (negligible in often cases, this matters for some cases like running for-loop)</li>
<li>Does not support the tee (<code>%T&gt;%</code>), exposition (<code>%$%</code>), or assignment (<code>%&lt;&gt;%</code>) operators</li>
<li>Cannot be used with compound assignment</li>
<li>Is more strict about valid syntax</li>
</ul></section><section id="performance-comparison" class="level3" data-number="4.5.2"><h3 data-number="4.5.2" class="anchored" data-anchor-id="performance-comparison">
<span class="header-section-number">4.5.2</span> Performance comparison</h3>
<p>The native pipe is clearly faster than the <a href="https://magrittr.tidyverse.org">magrittr</a> pipe because native pipe does not add more function calls within its implementation compared to the <a href="https://magrittr.tidyverse.org">magrittr</a> pipe.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb49" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bench</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://bench.r-lib.org/reference/mark.html">mark</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magrittr pipe"</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;"><a href="https://rdrr.io/r/base/lapply.html">replicate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <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;"><a href="https://rdrr.io/r/base/sum.html">sum</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native R pipe"</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;"><a href="https://rdrr.io/r/base/lapply.html">replicate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <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;"><a href="https://rdrr.io/r/base/sum.html">sum</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 6
  expression         min   median `itr/sec` mem_alloc `gc/sec`
  &lt;bch:expr&gt;    &lt;bch:tm&gt; &lt;bch:tm&gt;     &lt;dbl&gt; &lt;bch:byt&gt;    &lt;dbl&gt;
1 magrittr pipe   46.7ms   50.5ms      19.8     375KB     79.3
2 native R pipe   11.8ms   13.9ms      71.2     369KB     30.9</code></pre>
</div>
</div>
</section><section id="pipe-bind-operator" class="level3" data-number="4.5.3"><h3 data-number="4.5.3" class="anchored" data-anchor-id="pipe-bind-operator">
<span class="header-section-number">4.5.3</span> Pipe-bind operator</h3>
<p>After R v4.2, the pipe-bind operator <code>=&gt;</code> (type <code>=</code> + <code>&gt;</code>), or a pipe-binding syntax, allows you to bind the result of the left-hand side (LHS) to a name within the right-hand side (RHS) expression.</p>
<p>This feature is, however, disabled by default. You may want to enable it by running the following:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb51" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Sys.setenv.html">Sys.setenv</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_R_USE_PIPEBIND_"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>Another options:</p>
<ol type="i">
<li>Place this command into <code>.Renviron</code> file (Hint: run <code><a href="https://usethis.r-lib.org/reference/edit.html">usethis::edit_r_environ()</a></code>):</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb52-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">_R_USE_PIPEBIND_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>true</span></code></pre></div></div>
<ol start="2" type="i">
<li>Run this in a command prompt or PowerShell</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode powershell code-with-copy"><code class="sourceCode powershell"><span id="cb53-1">setx _R_USE_PIPEBIND_ true</span></code></pre></div></div>
<p>If you are in Linux / macOS (bash / zsh):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb54-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">_R_USE_PIPEBIND_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>true</span></code></pre></div></div>
<p>Then restart R.</p>
<p>Here’s what it does:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb55" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <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;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = mpg ~ wt, data = df)

Coefficients:
(Intercept)           wt  
     37.285       -5.344  </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb57" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <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;"><a href="https://rdrr.io/r/base/split.html">split</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/lapply.html">lapply</a></span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</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;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/lapply.html">vapply</a></span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mod</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;"><a href="https://rdrr.io/r/base/summary.html">summary</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mod</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r.squared</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/numeric.html">numeric</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>        4         6         8 
0.5086326 0.4645102 0.4229655 </code></pre>
</div>
</div>
<p>The <code>df</code> name temporarily exists only inside that RHS expression — not in your global environment. I like this because this is more explicit than <code>.</code> in <code>%&gt;%</code> operator. You can name the LHS result and refer to it directly inside the RHS expression anything you like.</p>
</section></section><section id="non-pipe-alternatives" class="level2" data-number="4.6"><h2 data-number="4.6" class="anchored" data-anchor-id="non-pipe-alternatives">
<span class="header-section-number">4.6</span> Non-Pipe Alternatives</h2>
<p>While the above are true pipe operators, it’s worth mentioning that some packages achieve similar left-to-right workflows through different mechanisms.</p>
<section id="chaining-in-data.table-2010" class="level3" data-number="4.6.1"><h3 data-number="4.6.1" class="anchored" data-anchor-id="chaining-in-data.table-2010">
<span class="header-section-number">4.6.1</span> Chaining in <strong>{data.table}</strong> (2010)</h3>
<p><a href="https://r-datatable.com">data.table</a> uses method chaining with <code>[][]</code> notation. It is NOT a pipe operator in a sense, but achieves a similar left-to-right flow. It behaves differently from the pipe operator — it chains operations within the same <code>[.data.table</code> method, and doesn’t pass values between functions, i.e.&nbsp;the use of placeholders.</p>
<p>Let’s look at the basic <a href="https://r-datatable.com">data.table</a> example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb59" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">data.table</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">as.data.table</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`:=`</span><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></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dt</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;">as.data.table</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/order.html">order</a></span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hp</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     mpg   cyl    hp
   &lt;num&gt; &lt;num&gt; &lt;num&gt;
1:  19.2     8   175
2:  18.7     8   175
3:  17.3     8   180
4:  16.4     8   180
5:  15.8     8   264</code></pre>
</div>
</div>
<p>Deeper method chaining in <a href="https://r-datatable.com">data.table</a> with grouping and aggregation:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb61" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log_mpg</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;"><a href="https://rdrr.io/r/base/Log.html">log</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>,</span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        mpg_mean <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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        log_mpg_mean <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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log_mpg</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, by <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span></span>
<span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/order.html">order</a></span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg_mean</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log_mpg_mean</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     cyl mpg_mean log_mpg_mean
   &lt;num&gt;    &lt;num&gt;        &lt;num&gt;
1:     4 26.66364     3.270454
2:     6 19.74286     2.980439
3:     8 15.10000     2.700171</code></pre>
</div>
</div>
<p>This is method chaining, not piping—the key difference is that pipes pass values between different functions, while <a href="https://r-datatable.com">data.table</a> chains operations within the same <code>[</code> method.</p>
</section><section id="multiple-assignment-with-zeallot-2018" class="level3" data-number="4.6.2"><h3 data-number="4.6.2" class="anchored" data-anchor-id="multiple-assignment-with-zeallot-2018">
<span class="header-section-number">4.6.2</span> Multiple Assignment with <strong>{zeallot}</strong> (2018)</h3>
<p>This is not exactly an operator that behaves like a pipe, where it passes LHS as an input for RHS, but I would like to point this one out. R lacks destructuring (also called “unpacking”) method, just like what you see in other languages, such as Python:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb63" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb63-1">x, 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;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span></code></pre></div></div>
<p>The <a href="https://github.com/r-lib/zeallot">zeallot</a> allows destructuring assignment with <code>%&lt;-%</code>. While not exactly a pipe operator to chain the commands, works well in pipe-like workflows.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb64" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeallot</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%&lt;-%`</span><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></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Multiple assignment</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">a</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b</span><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;">%&lt;-%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" 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>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">a</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2</code></pre>
</div>
</div>
<p>Destructuring with computations:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb66" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean_val</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sd_val</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span><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;">%&lt;-%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/eval.html">local</a></span> <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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">125</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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><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></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/cat.html">cat</a></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;">glue</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://glue.tidyverse.org/reference/glue.html">glue</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mean: {mean_val}, SD: {sd_val} , N: {n}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Mean: 0.100208694251594, SD: 1.06105719788861 , N: 100 </code></pre>
</div>
</div>
<p>Works with pipe workflows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb68" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">m</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">s</span><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;">%&lt;-%</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;"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><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;">%&gt;%</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;"><a href="https://rdrr.io/r/base/c.html">c</a></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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
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;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><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 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><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">m</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.09040591 0.91281588</code></pre>
</div>
</div>
</section></section></section><section id="acknowledgment" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Acknowledgment</h1>
<p>I would like to thank the <a href="https://www.reddit.com/r/rstats/">r/rstats</a> community for engaging my article about the pipes in R. I would like to thank <a href="https://www.reddit.com/user/guepier/">u/guepier</a> for pointing out the actual pipe history, where there’s actually a pipe that predates both <a href="https://renkun.me/pipeR">pipeR</a> and <a href="https://magrittr.tidyverse.org">magrittr</a> in this <a href="https://www.reddit.com/r/rstats/comments/1ouvesz/comment/nof96n6/?utm_source=share&amp;utm_medium=web3x&amp;utm_name=web3xcss&amp;utm_term=1&amp;utm_content=share_button">comment</a>.</p>
<p>Special thanks to the developers of <a href="https://magrittr.tidyverse.org">magrittr</a>, <a href="https://renkun.me/pipeR">pipeR</a>, <a href="https://github.com/WinVector/wrapr">wrapr</a>, and the R core team for introducing the native pipe operator.</p>
</section><section id="remarks-and-resources" class="level1" data-number="6"><h1 data-number="6">
<span class="header-section-number">6</span> Remarks and Resources</h1>
<p>As you can see, it needs a lot to came up with lots of crufts and inventions, after years of invention. The native pipe came up later after the release of R v4.1 in May 2021, thanks to the popularization of <a href="https://magrittr.tidyverse.org">magrittr</a> and <a href="https://tidyverse.tidyverse.org">tidyverse</a>. And that’s all I know about pipes in R.</p>
<p>Here are the additional resources:</p>
<ul>
<li><a href="https://magrittr.tidyverse.org/">magrittr documentation</a></li>
<li><a href="https://cran.r-project.org/doc/manuals/r-release/NEWS.html">R - Release Notes</a></li>
<li><a href="https://renkun-ken.github.io/pipeR/">pipeR package</a></li>
<li><a href="https://winvector.github.io/wrapr/">wrapr package</a></li>
<li><a href="https://rdatatable.gitlab.io/data.table/">data.table documentation</a></li>
<li><a href="https://cran.r-project.org/package=zeallot">zeallot package</a></li>
</ul>


</section> ]]></description>
  <category>R</category>
  <category>pipes</category>
  <category>tidyverse</category>
  <category>programming</category>
  <guid>https://joshuamarie.com/posts/05-pipes/</guid>
  <pubDate>Sun, 09 Nov 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/05-pipes/image.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>The Hidden Magic of Tidy-Select: R’s Universal Column Selection Language</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/04-tidyselect-helpers/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/04-tidyselect-helpers/&amp;text=%20helpers%20in%20R%20-%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/04-tidyselect-helpers/&amp;title=%20helpers%20in%20R%20-%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=%20helpers%20in%20R%20-%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/04-tidyselect-helpers/" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/04-tidyselect-helpers/" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/04-tidyselect-helpers/&amp;title=%20helpers%20in%20R%20-%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/04-tidyselect-helpers/&amp;title=%20helpers%20in%20R%20-%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/04-tidyselect-helpers/&amp;name=%20helpers%20in%20R%20-%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text= helpers in R - Joshua Marie https://joshuamarie.com/posts/04-tidyselect-helpers/'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/04-tidyselect-helpers/%20%20helpers%20in%20R%20-%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="introduction" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Introduction</h1>
<p>Have you ever wondered how <code><a href="https://tidyselect.r-lib.org/reference/where.html">where()</a></code>, <code><a href="https://tidyselect.r-lib.org/reference/starts_with.html">starts_with()</a></code>, and other selection helpers work seamlessly across different <a href="https://tidyverse.tidyverse.org">tidyverse</a> packages? I recently discovered something surprising: you can actually use these functions in <a href="https://dplyr.tidyverse.org">dplyr</a>, <a href="https://tidyr.tidyverse.org">tidyr</a>, and other packages that invokes <code>&lt;tidy-select&gt;</code> API, without explicitly loading them.</p>
<p>Here’s how it works:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        cols <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;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># using `where()` w/out calling dplyr / tidyselect</span></span>
<span>        names_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Variable'</span>,</span>
<span>        values_to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Measure'</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["Species"],"name":[1],"type":["fct"],"align":["left"]},{"label":["Variable"],"name":[2],"type":["chr"],"align":["left"]},{"label":["Measure"],"name":[3],"type":["dbl"],"align":["right"]}],"data":[{"1":"setosa","2":"Sepal.Length","3":"5.1"},{"1":"setosa","2":"Sepal.Width","3":"3.5"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.9"},{"1":"setosa","2":"Sepal.Width","3":"3.0"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.7"},{"1":"setosa","2":"Sepal.Width","3":"3.2"},{"1":"setosa","2":"Petal.Length","3":"1.3"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.6"},{"1":"setosa","2":"Sepal.Width","3":"3.1"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.0"},{"1":"setosa","2":"Sepal.Width","3":"3.6"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.4"},{"1":"setosa","2":"Sepal.Width","3":"3.9"},{"1":"setosa","2":"Petal.Length","3":"1.7"},{"1":"setosa","2":"Petal.Width","3":"0.4"},{"1":"setosa","2":"Sepal.Length","3":"4.6"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.3"},{"1":"setosa","2":"Sepal.Length","3":"5.0"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.4"},{"1":"setosa","2":"Sepal.Width","3":"2.9"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.9"},{"1":"setosa","2":"Sepal.Width","3":"3.1"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.1"},{"1":"setosa","2":"Sepal.Length","3":"5.4"},{"1":"setosa","2":"Sepal.Width","3":"3.7"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.8"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.6"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.8"},{"1":"setosa","2":"Sepal.Width","3":"3.0"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.1"},{"1":"setosa","2":"Sepal.Length","3":"4.3"},{"1":"setosa","2":"Sepal.Width","3":"3.0"},{"1":"setosa","2":"Petal.Length","3":"1.1"},{"1":"setosa","2":"Petal.Width","3":"0.1"},{"1":"setosa","2":"Sepal.Length","3":"5.8"},{"1":"setosa","2":"Sepal.Width","3":"4.0"},{"1":"setosa","2":"Petal.Length","3":"1.2"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.7"},{"1":"setosa","2":"Sepal.Width","3":"4.4"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.4"},{"1":"setosa","2":"Sepal.Length","3":"5.4"},{"1":"setosa","2":"Sepal.Width","3":"3.9"},{"1":"setosa","2":"Petal.Length","3":"1.3"},{"1":"setosa","2":"Petal.Width","3":"0.4"},{"1":"setosa","2":"Sepal.Length","3":"5.1"},{"1":"setosa","2":"Sepal.Width","3":"3.5"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.3"},{"1":"setosa","2":"Sepal.Length","3":"5.7"},{"1":"setosa","2":"Sepal.Width","3":"3.8"},{"1":"setosa","2":"Petal.Length","3":"1.7"},{"1":"setosa","2":"Petal.Width","3":"0.3"},{"1":"setosa","2":"Sepal.Length","3":"5.1"},{"1":"setosa","2":"Sepal.Width","3":"3.8"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.3"},{"1":"setosa","2":"Sepal.Length","3":"5.4"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.7"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.1"},{"1":"setosa","2":"Sepal.Width","3":"3.7"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.4"},{"1":"setosa","2":"Sepal.Length","3":"4.6"},{"1":"setosa","2":"Sepal.Width","3":"3.6"},{"1":"setosa","2":"Petal.Length","3":"1.0"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.1"},{"1":"setosa","2":"Sepal.Width","3":"3.3"},{"1":"setosa","2":"Petal.Length","3":"1.7"},{"1":"setosa","2":"Petal.Width","3":"0.5"},{"1":"setosa","2":"Sepal.Length","3":"4.8"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.9"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.0"},{"1":"setosa","2":"Sepal.Width","3":"3.0"},{"1":"setosa","2":"Petal.Length","3":"1.6"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.0"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.6"},{"1":"setosa","2":"Petal.Width","3":"0.4"},{"1":"setosa","2":"Sepal.Length","3":"5.2"},{"1":"setosa","2":"Sepal.Width","3":"3.5"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.2"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.7"},{"1":"setosa","2":"Sepal.Width","3":"3.2"},{"1":"setosa","2":"Petal.Length","3":"1.6"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.8"},{"1":"setosa","2":"Sepal.Width","3":"3.1"},{"1":"setosa","2":"Petal.Length","3":"1.6"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.4"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.4"},{"1":"setosa","2":"Sepal.Length","3":"5.2"},{"1":"setosa","2":"Sepal.Width","3":"4.1"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.1"},{"1":"setosa","2":"Sepal.Length","3":"5.5"},{"1":"setosa","2":"Sepal.Width","3":"4.2"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.9"},{"1":"setosa","2":"Sepal.Width","3":"3.1"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.0"},{"1":"setosa","2":"Sepal.Width","3":"3.2"},{"1":"setosa","2":"Petal.Length","3":"1.2"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.5"},{"1":"setosa","2":"Sepal.Width","3":"3.5"},{"1":"setosa","2":"Petal.Length","3":"1.3"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.9"},{"1":"setosa","2":"Sepal.Width","3":"3.6"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.1"},{"1":"setosa","2":"Sepal.Length","3":"4.4"},{"1":"setosa","2":"Sepal.Width","3":"3.0"},{"1":"setosa","2":"Petal.Length","3":"1.3"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.1"},{"1":"setosa","2":"Sepal.Width","3":"3.4"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.0"},{"1":"setosa","2":"Sepal.Width","3":"3.5"},{"1":"setosa","2":"Petal.Length","3":"1.3"},{"1":"setosa","2":"Petal.Width","3":"0.3"},{"1":"setosa","2":"Sepal.Length","3":"4.5"},{"1":"setosa","2":"Sepal.Width","3":"2.3"},{"1":"setosa","2":"Petal.Length","3":"1.3"},{"1":"setosa","2":"Petal.Width","3":"0.3"},{"1":"setosa","2":"Sepal.Length","3":"4.4"},{"1":"setosa","2":"Sepal.Width","3":"3.2"},{"1":"setosa","2":"Petal.Length","3":"1.3"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.0"},{"1":"setosa","2":"Sepal.Width","3":"3.5"},{"1":"setosa","2":"Petal.Length","3":"1.6"},{"1":"setosa","2":"Petal.Width","3":"0.6"},{"1":"setosa","2":"Sepal.Length","3":"5.1"},{"1":"setosa","2":"Sepal.Width","3":"3.8"},{"1":"setosa","2":"Petal.Length","3":"1.9"},{"1":"setosa","2":"Petal.Width","3":"0.4"},{"1":"setosa","2":"Sepal.Length","3":"4.8"},{"1":"setosa","2":"Sepal.Width","3":"3.0"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.3"},{"1":"setosa","2":"Sepal.Length","3":"5.1"},{"1":"setosa","2":"Sepal.Width","3":"3.8"},{"1":"setosa","2":"Petal.Length","3":"1.6"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"4.6"},{"1":"setosa","2":"Sepal.Width","3":"3.2"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.3"},{"1":"setosa","2":"Sepal.Width","3":"3.7"},{"1":"setosa","2":"Petal.Length","3":"1.5"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"setosa","2":"Sepal.Length","3":"5.0"},{"1":"setosa","2":"Sepal.Width","3":"3.3"},{"1":"setosa","2":"Petal.Length","3":"1.4"},{"1":"setosa","2":"Petal.Width","3":"0.2"},{"1":"versicolor","2":"Sepal.Length","3":"7.0"},{"1":"versicolor","2":"Sepal.Width","3":"3.2"},{"1":"versicolor","2":"Petal.Length","3":"4.7"},{"1":"versicolor","2":"Petal.Width","3":"1.4"},{"1":"versicolor","2":"Sepal.Length","3":"6.4"},{"1":"versicolor","2":"Sepal.Width","3":"3.2"},{"1":"versicolor","2":"Petal.Length","3":"4.5"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"6.9"},{"1":"versicolor","2":"Sepal.Width","3":"3.1"},{"1":"versicolor","2":"Petal.Length","3":"4.9"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"5.5"},{"1":"versicolor","2":"Sepal.Width","3":"2.3"},{"1":"versicolor","2":"Petal.Length","3":"4.0"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"6.5"},{"1":"versicolor","2":"Sepal.Width","3":"2.8"},{"1":"versicolor","2":"Petal.Length","3":"4.6"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"5.7"},{"1":"versicolor","2":"Sepal.Width","3":"2.8"},{"1":"versicolor","2":"Petal.Length","3":"4.5"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"6.3"},{"1":"versicolor","2":"Sepal.Width","3":"3.3"},{"1":"versicolor","2":"Petal.Length","3":"4.7"},{"1":"versicolor","2":"Petal.Width","3":"1.6"},{"1":"versicolor","2":"Sepal.Length","3":"4.9"},{"1":"versicolor","2":"Sepal.Width","3":"2.4"},{"1":"versicolor","2":"Petal.Length","3":"3.3"},{"1":"versicolor","2":"Petal.Width","3":"1.0"},{"1":"versicolor","2":"Sepal.Length","3":"6.6"},{"1":"versicolor","2":"Sepal.Width","3":"2.9"},{"1":"versicolor","2":"Petal.Length","3":"4.6"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"5.2"},{"1":"versicolor","2":"Sepal.Width","3":"2.7"},{"1":"versicolor","2":"Petal.Length","3":"3.9"},{"1":"versicolor","2":"Petal.Width","3":"1.4"},{"1":"versicolor","2":"Sepal.Length","3":"5.0"},{"1":"versicolor","2":"Sepal.Width","3":"2.0"},{"1":"versicolor","2":"Petal.Length","3":"3.5"},{"1":"versicolor","2":"Petal.Width","3":"1.0"},{"1":"versicolor","2":"Sepal.Length","3":"5.9"},{"1":"versicolor","2":"Sepal.Width","3":"3.0"},{"1":"versicolor","2":"Petal.Length","3":"4.2"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"6.0"},{"1":"versicolor","2":"Sepal.Width","3":"2.2"},{"1":"versicolor","2":"Petal.Length","3":"4.0"},{"1":"versicolor","2":"Petal.Width","3":"1.0"},{"1":"versicolor","2":"Sepal.Length","3":"6.1"},{"1":"versicolor","2":"Sepal.Width","3":"2.9"},{"1":"versicolor","2":"Petal.Length","3":"4.7"},{"1":"versicolor","2":"Petal.Width","3":"1.4"},{"1":"versicolor","2":"Sepal.Length","3":"5.6"},{"1":"versicolor","2":"Sepal.Width","3":"2.9"},{"1":"versicolor","2":"Petal.Length","3":"3.6"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"6.7"},{"1":"versicolor","2":"Sepal.Width","3":"3.1"},{"1":"versicolor","2":"Petal.Length","3":"4.4"},{"1":"versicolor","2":"Petal.Width","3":"1.4"},{"1":"versicolor","2":"Sepal.Length","3":"5.6"},{"1":"versicolor","2":"Sepal.Width","3":"3.0"},{"1":"versicolor","2":"Petal.Length","3":"4.5"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"5.8"},{"1":"versicolor","2":"Sepal.Width","3":"2.7"},{"1":"versicolor","2":"Petal.Length","3":"4.1"},{"1":"versicolor","2":"Petal.Width","3":"1.0"},{"1":"versicolor","2":"Sepal.Length","3":"6.2"},{"1":"versicolor","2":"Sepal.Width","3":"2.2"},{"1":"versicolor","2":"Petal.Length","3":"4.5"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"5.6"},{"1":"versicolor","2":"Sepal.Width","3":"2.5"},{"1":"versicolor","2":"Petal.Length","3":"3.9"},{"1":"versicolor","2":"Petal.Width","3":"1.1"},{"1":"versicolor","2":"Sepal.Length","3":"5.9"},{"1":"versicolor","2":"Sepal.Width","3":"3.2"},{"1":"versicolor","2":"Petal.Length","3":"4.8"},{"1":"versicolor","2":"Petal.Width","3":"1.8"},{"1":"versicolor","2":"Sepal.Length","3":"6.1"},{"1":"versicolor","2":"Sepal.Width","3":"2.8"},{"1":"versicolor","2":"Petal.Length","3":"4.0"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"6.3"},{"1":"versicolor","2":"Sepal.Width","3":"2.5"},{"1":"versicolor","2":"Petal.Length","3":"4.9"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"6.1"},{"1":"versicolor","2":"Sepal.Width","3":"2.8"},{"1":"versicolor","2":"Petal.Length","3":"4.7"},{"1":"versicolor","2":"Petal.Width","3":"1.2"},{"1":"versicolor","2":"Sepal.Length","3":"6.4"},{"1":"versicolor","2":"Sepal.Width","3":"2.9"},{"1":"versicolor","2":"Petal.Length","3":"4.3"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"6.6"},{"1":"versicolor","2":"Sepal.Width","3":"3.0"},{"1":"versicolor","2":"Petal.Length","3":"4.4"},{"1":"versicolor","2":"Petal.Width","3":"1.4"},{"1":"versicolor","2":"Sepal.Length","3":"6.8"},{"1":"versicolor","2":"Sepal.Width","3":"2.8"},{"1":"versicolor","2":"Petal.Length","3":"4.8"},{"1":"versicolor","2":"Petal.Width","3":"1.4"},{"1":"versicolor","2":"Sepal.Length","3":"6.7"},{"1":"versicolor","2":"Sepal.Width","3":"3.0"},{"1":"versicolor","2":"Petal.Length","3":"5.0"},{"1":"versicolor","2":"Petal.Width","3":"1.7"},{"1":"versicolor","2":"Sepal.Length","3":"6.0"},{"1":"versicolor","2":"Sepal.Width","3":"2.9"},{"1":"versicolor","2":"Petal.Length","3":"4.5"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"5.7"},{"1":"versicolor","2":"Sepal.Width","3":"2.6"},{"1":"versicolor","2":"Petal.Length","3":"3.5"},{"1":"versicolor","2":"Petal.Width","3":"1.0"},{"1":"versicolor","2":"Sepal.Length","3":"5.5"},{"1":"versicolor","2":"Sepal.Width","3":"2.4"},{"1":"versicolor","2":"Petal.Length","3":"3.8"},{"1":"versicolor","2":"Petal.Width","3":"1.1"},{"1":"versicolor","2":"Sepal.Length","3":"5.5"},{"1":"versicolor","2":"Sepal.Width","3":"2.4"},{"1":"versicolor","2":"Petal.Length","3":"3.7"},{"1":"versicolor","2":"Petal.Width","3":"1.0"},{"1":"versicolor","2":"Sepal.Length","3":"5.8"},{"1":"versicolor","2":"Sepal.Width","3":"2.7"},{"1":"versicolor","2":"Petal.Length","3":"3.9"},{"1":"versicolor","2":"Petal.Width","3":"1.2"},{"1":"versicolor","2":"Sepal.Length","3":"6.0"},{"1":"versicolor","2":"Sepal.Width","3":"2.7"},{"1":"versicolor","2":"Petal.Length","3":"5.1"},{"1":"versicolor","2":"Petal.Width","3":"1.6"},{"1":"versicolor","2":"Sepal.Length","3":"5.4"},{"1":"versicolor","2":"Sepal.Width","3":"3.0"},{"1":"versicolor","2":"Petal.Length","3":"4.5"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"6.0"},{"1":"versicolor","2":"Sepal.Width","3":"3.4"},{"1":"versicolor","2":"Petal.Length","3":"4.5"},{"1":"versicolor","2":"Petal.Width","3":"1.6"},{"1":"versicolor","2":"Sepal.Length","3":"6.7"},{"1":"versicolor","2":"Sepal.Width","3":"3.1"},{"1":"versicolor","2":"Petal.Length","3":"4.7"},{"1":"versicolor","2":"Petal.Width","3":"1.5"},{"1":"versicolor","2":"Sepal.Length","3":"6.3"},{"1":"versicolor","2":"Sepal.Width","3":"2.3"},{"1":"versicolor","2":"Petal.Length","3":"4.4"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"5.6"},{"1":"versicolor","2":"Sepal.Width","3":"3.0"},{"1":"versicolor","2":"Petal.Length","3":"4.1"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"5.5"},{"1":"versicolor","2":"Sepal.Width","3":"2.5"},{"1":"versicolor","2":"Petal.Length","3":"4.0"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"5.5"},{"1":"versicolor","2":"Sepal.Width","3":"2.6"},{"1":"versicolor","2":"Petal.Length","3":"4.4"},{"1":"versicolor","2":"Petal.Width","3":"1.2"},{"1":"versicolor","2":"Sepal.Length","3":"6.1"},{"1":"versicolor","2":"Sepal.Width","3":"3.0"},{"1":"versicolor","2":"Petal.Length","3":"4.6"},{"1":"versicolor","2":"Petal.Width","3":"1.4"},{"1":"versicolor","2":"Sepal.Length","3":"5.8"},{"1":"versicolor","2":"Sepal.Width","3":"2.6"},{"1":"versicolor","2":"Petal.Length","3":"4.0"},{"1":"versicolor","2":"Petal.Width","3":"1.2"},{"1":"versicolor","2":"Sepal.Length","3":"5.0"},{"1":"versicolor","2":"Sepal.Width","3":"2.3"},{"1":"versicolor","2":"Petal.Length","3":"3.3"},{"1":"versicolor","2":"Petal.Width","3":"1.0"},{"1":"versicolor","2":"Sepal.Length","3":"5.6"},{"1":"versicolor","2":"Sepal.Width","3":"2.7"},{"1":"versicolor","2":"Petal.Length","3":"4.2"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"5.7"},{"1":"versicolor","2":"Sepal.Width","3":"3.0"},{"1":"versicolor","2":"Petal.Length","3":"4.2"},{"1":"versicolor","2":"Petal.Width","3":"1.2"},{"1":"versicolor","2":"Sepal.Length","3":"5.7"},{"1":"versicolor","2":"Sepal.Width","3":"2.9"},{"1":"versicolor","2":"Petal.Length","3":"4.2"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"6.2"},{"1":"versicolor","2":"Sepal.Width","3":"2.9"},{"1":"versicolor","2":"Petal.Length","3":"4.3"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"versicolor","2":"Sepal.Length","3":"5.1"},{"1":"versicolor","2":"Sepal.Width","3":"2.5"},{"1":"versicolor","2":"Petal.Length","3":"3.0"},{"1":"versicolor","2":"Petal.Width","3":"1.1"},{"1":"versicolor","2":"Sepal.Length","3":"5.7"},{"1":"versicolor","2":"Sepal.Width","3":"2.8"},{"1":"versicolor","2":"Petal.Length","3":"4.1"},{"1":"versicolor","2":"Petal.Width","3":"1.3"},{"1":"virginica","2":"Sepal.Length","3":"6.3"},{"1":"virginica","2":"Sepal.Width","3":"3.3"},{"1":"virginica","2":"Petal.Length","3":"6.0"},{"1":"virginica","2":"Petal.Width","3":"2.5"},{"1":"virginica","2":"Sepal.Length","3":"5.8"},{"1":"virginica","2":"Sepal.Width","3":"2.7"},{"1":"virginica","2":"Petal.Length","3":"5.1"},{"1":"virginica","2":"Petal.Width","3":"1.9"},{"1":"virginica","2":"Sepal.Length","3":"7.1"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"5.9"},{"1":"virginica","2":"Petal.Width","3":"2.1"},{"1":"virginica","2":"Sepal.Length","3":"6.3"},{"1":"virginica","2":"Sepal.Width","3":"2.9"},{"1":"virginica","2":"Petal.Length","3":"5.6"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"6.5"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"5.8"},{"1":"virginica","2":"Petal.Width","3":"2.2"},{"1":"virginica","2":"Sepal.Length","3":"7.6"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"6.6"},{"1":"virginica","2":"Petal.Width","3":"2.1"},{"1":"virginica","2":"Sepal.Length","3":"4.9"},{"1":"virginica","2":"Sepal.Width","3":"2.5"},{"1":"virginica","2":"Petal.Length","3":"4.5"},{"1":"virginica","2":"Petal.Width","3":"1.7"},{"1":"virginica","2":"Sepal.Length","3":"7.3"},{"1":"virginica","2":"Sepal.Width","3":"2.9"},{"1":"virginica","2":"Petal.Length","3":"6.3"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"6.7"},{"1":"virginica","2":"Sepal.Width","3":"2.5"},{"1":"virginica","2":"Petal.Length","3":"5.8"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"7.2"},{"1":"virginica","2":"Sepal.Width","3":"3.6"},{"1":"virginica","2":"Petal.Length","3":"6.1"},{"1":"virginica","2":"Petal.Width","3":"2.5"},{"1":"virginica","2":"Sepal.Length","3":"6.5"},{"1":"virginica","2":"Sepal.Width","3":"3.2"},{"1":"virginica","2":"Petal.Length","3":"5.1"},{"1":"virginica","2":"Petal.Width","3":"2.0"},{"1":"virginica","2":"Sepal.Length","3":"6.4"},{"1":"virginica","2":"Sepal.Width","3":"2.7"},{"1":"virginica","2":"Petal.Length","3":"5.3"},{"1":"virginica","2":"Petal.Width","3":"1.9"},{"1":"virginica","2":"Sepal.Length","3":"6.8"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"5.5"},{"1":"virginica","2":"Petal.Width","3":"2.1"},{"1":"virginica","2":"Sepal.Length","3":"5.7"},{"1":"virginica","2":"Sepal.Width","3":"2.5"},{"1":"virginica","2":"Petal.Length","3":"5.0"},{"1":"virginica","2":"Petal.Width","3":"2.0"},{"1":"virginica","2":"Sepal.Length","3":"5.8"},{"1":"virginica","2":"Sepal.Width","3":"2.8"},{"1":"virginica","2":"Petal.Length","3":"5.1"},{"1":"virginica","2":"Petal.Width","3":"2.4"},{"1":"virginica","2":"Sepal.Length","3":"6.4"},{"1":"virginica","2":"Sepal.Width","3":"3.2"},{"1":"virginica","2":"Petal.Length","3":"5.3"},{"1":"virginica","2":"Petal.Width","3":"2.3"},{"1":"virginica","2":"Sepal.Length","3":"6.5"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"5.5"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"7.7"},{"1":"virginica","2":"Sepal.Width","3":"3.8"},{"1":"virginica","2":"Petal.Length","3":"6.7"},{"1":"virginica","2":"Petal.Width","3":"2.2"},{"1":"virginica","2":"Sepal.Length","3":"7.7"},{"1":"virginica","2":"Sepal.Width","3":"2.6"},{"1":"virginica","2":"Petal.Length","3":"6.9"},{"1":"virginica","2":"Petal.Width","3":"2.3"},{"1":"virginica","2":"Sepal.Length","3":"6.0"},{"1":"virginica","2":"Sepal.Width","3":"2.2"},{"1":"virginica","2":"Petal.Length","3":"5.0"},{"1":"virginica","2":"Petal.Width","3":"1.5"},{"1":"virginica","2":"Sepal.Length","3":"6.9"},{"1":"virginica","2":"Sepal.Width","3":"3.2"},{"1":"virginica","2":"Petal.Length","3":"5.7"},{"1":"virginica","2":"Petal.Width","3":"2.3"},{"1":"virginica","2":"Sepal.Length","3":"5.6"},{"1":"virginica","2":"Sepal.Width","3":"2.8"},{"1":"virginica","2":"Petal.Length","3":"4.9"},{"1":"virginica","2":"Petal.Width","3":"2.0"},{"1":"virginica","2":"Sepal.Length","3":"7.7"},{"1":"virginica","2":"Sepal.Width","3":"2.8"},{"1":"virginica","2":"Petal.Length","3":"6.7"},{"1":"virginica","2":"Petal.Width","3":"2.0"},{"1":"virginica","2":"Sepal.Length","3":"6.3"},{"1":"virginica","2":"Sepal.Width","3":"2.7"},{"1":"virginica","2":"Petal.Length","3":"4.9"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"6.7"},{"1":"virginica","2":"Sepal.Width","3":"3.3"},{"1":"virginica","2":"Petal.Length","3":"5.7"},{"1":"virginica","2":"Petal.Width","3":"2.1"},{"1":"virginica","2":"Sepal.Length","3":"7.2"},{"1":"virginica","2":"Sepal.Width","3":"3.2"},{"1":"virginica","2":"Petal.Length","3":"6.0"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"6.2"},{"1":"virginica","2":"Sepal.Width","3":"2.8"},{"1":"virginica","2":"Petal.Length","3":"4.8"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"6.1"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"4.9"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"6.4"},{"1":"virginica","2":"Sepal.Width","3":"2.8"},{"1":"virginica","2":"Petal.Length","3":"5.6"},{"1":"virginica","2":"Petal.Width","3":"2.1"},{"1":"virginica","2":"Sepal.Length","3":"7.2"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"5.8"},{"1":"virginica","2":"Petal.Width","3":"1.6"},{"1":"virginica","2":"Sepal.Length","3":"7.4"},{"1":"virginica","2":"Sepal.Width","3":"2.8"},{"1":"virginica","2":"Petal.Length","3":"6.1"},{"1":"virginica","2":"Petal.Width","3":"1.9"},{"1":"virginica","2":"Sepal.Length","3":"7.9"},{"1":"virginica","2":"Sepal.Width","3":"3.8"},{"1":"virginica","2":"Petal.Length","3":"6.4"},{"1":"virginica","2":"Petal.Width","3":"2.0"},{"1":"virginica","2":"Sepal.Length","3":"6.4"},{"1":"virginica","2":"Sepal.Width","3":"2.8"},{"1":"virginica","2":"Petal.Length","3":"5.6"},{"1":"virginica","2":"Petal.Width","3":"2.2"},{"1":"virginica","2":"Sepal.Length","3":"6.3"},{"1":"virginica","2":"Sepal.Width","3":"2.8"},{"1":"virginica","2":"Petal.Length","3":"5.1"},{"1":"virginica","2":"Petal.Width","3":"1.5"},{"1":"virginica","2":"Sepal.Length","3":"6.1"},{"1":"virginica","2":"Sepal.Width","3":"2.6"},{"1":"virginica","2":"Petal.Length","3":"5.6"},{"1":"virginica","2":"Petal.Width","3":"1.4"},{"1":"virginica","2":"Sepal.Length","3":"7.7"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"6.1"},{"1":"virginica","2":"Petal.Width","3":"2.3"},{"1":"virginica","2":"Sepal.Length","3":"6.3"},{"1":"virginica","2":"Sepal.Width","3":"3.4"},{"1":"virginica","2":"Petal.Length","3":"5.6"},{"1":"virginica","2":"Petal.Width","3":"2.4"},{"1":"virginica","2":"Sepal.Length","3":"6.4"},{"1":"virginica","2":"Sepal.Width","3":"3.1"},{"1":"virginica","2":"Petal.Length","3":"5.5"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"6.0"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"4.8"},{"1":"virginica","2":"Petal.Width","3":"1.8"},{"1":"virginica","2":"Sepal.Length","3":"6.9"},{"1":"virginica","2":"Sepal.Width","3":"3.1"},{"1":"virginica","2":"Petal.Length","3":"5.4"},{"1":"virginica","2":"Petal.Width","3":"2.1"},{"1":"virginica","2":"Sepal.Length","3":"6.7"},{"1":"virginica","2":"Sepal.Width","3":"3.1"},{"1":"virginica","2":"Petal.Length","3":"5.6"},{"1":"virginica","2":"Petal.Width","3":"2.4"},{"1":"virginica","2":"Sepal.Length","3":"6.9"},{"1":"virginica","2":"Sepal.Width","3":"3.1"},{"1":"virginica","2":"Petal.Length","3":"5.1"},{"1":"virginica","2":"Petal.Width","3":"2.3"},{"1":"virginica","2":"Sepal.Length","3":"5.8"},{"1":"virginica","2":"Sepal.Width","3":"2.7"},{"1":"virginica","2":"Petal.Length","3":"5.1"},{"1":"virginica","2":"Petal.Width","3":"1.9"},{"1":"virginica","2":"Sepal.Length","3":"6.8"},{"1":"virginica","2":"Sepal.Width","3":"3.2"},{"1":"virginica","2":"Petal.Length","3":"5.9"},{"1":"virginica","2":"Petal.Width","3":"2.3"},{"1":"virginica","2":"Sepal.Length","3":"6.7"},{"1":"virginica","2":"Sepal.Width","3":"3.3"},{"1":"virginica","2":"Petal.Length","3":"5.7"},{"1":"virginica","2":"Petal.Width","3":"2.5"},{"1":"virginica","2":"Sepal.Length","3":"6.7"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"5.2"},{"1":"virginica","2":"Petal.Width","3":"2.3"},{"1":"virginica","2":"Sepal.Length","3":"6.3"},{"1":"virginica","2":"Sepal.Width","3":"2.5"},{"1":"virginica","2":"Petal.Length","3":"5.0"},{"1":"virginica","2":"Petal.Width","3":"1.9"},{"1":"virginica","2":"Sepal.Length","3":"6.5"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"5.2"},{"1":"virginica","2":"Petal.Width","3":"2.0"},{"1":"virginica","2":"Sepal.Length","3":"6.2"},{"1":"virginica","2":"Sepal.Width","3":"3.4"},{"1":"virginica","2":"Petal.Length","3":"5.4"},{"1":"virginica","2":"Petal.Width","3":"2.3"},{"1":"virginica","2":"Sepal.Length","3":"5.9"},{"1":"virginica","2":"Sepal.Width","3":"3.0"},{"1":"virginica","2":"Petal.Length","3":"5.1"},{"1":"virginica","2":"Petal.Width","3":"1.8"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Take note that I never load <a href="https://tidyselect.r-lib.org">tidyselect</a> and <a href="https://dplyr.tidyverse.org">dplyr</a> (the <code><a href="https://tidyselect.r-lib.org/reference/where.html">where()</a></code> function in <a href="https://dplyr.tidyverse.org">dplyr</a> is just one of many re-exports). Yet, <code><a href="https://tidyselect.r-lib.org/reference/where.html">where()</a></code> works perfectly. It doesn’t belong to / re-exported by <a href="https://tidyr.tidyverse.org">tidyr</a>, but you can use <code><a href="https://tidyselect.r-lib.org/reference/where.html">where()</a></code>, if and only if the functions is invoking <code>&lt;tidy-select&gt;</code> API.</p>
</section><section id="what-are-these-functions-called" class="level1" data-number="2"><h1 data-number="2">
<span class="header-section-number">2</span> What Are These Functions Called?</h1>
<p>These are officially called <em>tidyselect helpers</em> (or “selection language”). They’re part of the <a href="https://tidyselect.r-lib.org">tidyselect</a> package, which provides a domain-specific language (DSL) for selecting columns in data frames.</p>
<p>You might also hear them referred to as:</p>
<ul>
<li>Selection helper functions</li>
<li>
<code>&lt;Tidy-select&gt;</code> helpers</li>
<li>Column selection helpers</li>
</ul></section><section id="the-complete-family-of-selection-helpers" class="level1" data-number="3"><h1 data-number="3">
<span class="header-section-number">3</span> The Complete Family of Selection Helpers</h1>
<p>The <a href="https://tidyselect.r-lib.org">tidyselect</a> package can be divided into 3 categories of helpers.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">Pattern Matching Helpers</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Predicate-Based Helpers</a></li>
<li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">“Positional” Helpers</a></li>
</ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<ol type="1">
<li>
<p>Columns starting with a prefix</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">starts_with</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sepal"</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Sepal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Sepal.Width"],"name":[2],"type":["dbl"],"align":["right"]}],"data":[{"1":"5.1","2":"3.5","_rn_":"1"},{"1":"4.9","2":"3.0","_rn_":"2"},{"1":"4.7","2":"3.2","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li>
<p>Columns ending with a suffix</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">ends_with</span><span 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"</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Sepal.Width"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Petal.Width"],"name":[2],"type":["dbl"],"align":["right"]}],"data":[{"1":"3.5","2":"0.2","_rn_":"1"},{"1":"3.0","2":"0.2","_rn_":"2"},{"1":"3.2","2":"0.2","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li>
<p>Columns containing a literal string</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">contains</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"al"</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Sepal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Sepal.Width"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["Petal.Length"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["Petal.Width"],"name":[4],"type":["dbl"],"align":["right"]}],"data":[{"1":"5.1","2":"3.5","3":"1.4","4":"0.2","_rn_":"1"},{"1":"4.9","2":"3.0","3":"1.4","4":"0.2","_rn_":"2"},{"1":"4.7","2":"3.2","3":"1.3","4":"0.2","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li>
<p>Columns matching a regular expression</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">matches</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^Sepal"</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Sepal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Sepal.Width"],"name":[2],"type":["dbl"],"align":["right"]}],"data":[{"1":"5.1","2":"3.5","_rn_":"1"},{"1":"4.9","2":"3.0","_rn_":"2"},{"1":"4.7","2":"3.2","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li>
<p>Columns following the number pattern</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">num_range</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x'</span>, <span class="fl" 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="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]}],"data":[{"_rn_":"1"},{"_rn_":"2"},{"_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
</ol>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>The <code><a href="https://tidyselect.r-lib.org/reference/where.html">where()</a></code> function is similar to SQL <code>WHERE</code>, except it is functional that (should) returns a Boolean value that satisfies the condition.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Sepal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Sepal.Width"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["Petal.Length"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["Petal.Width"],"name":[4],"type":["dbl"],"align":["right"]}],"data":[{"1":"5.1","2":"3.5","3":"1.4","4":"0.2","_rn_":"1"},{"1":"4.9","2":"3.0","3":"1.4","4":"0.2","_rn_":"2"},{"1":"4.7","2":"3.2","3":"1.3","4":"0.2","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.factor</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Species"],"name":[1],"type":["fct"],"align":["left"]}],"data":[{"1":"setosa","_rn_":"1"},{"1":"setosa","_rn_":"2"},{"1":"setosa","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">where</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/base/numeric.html">is.numeric</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span><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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["Sepal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Petal.Length"],"name":[2],"type":["dbl"],"align":["right"]}],"data":[{"1":"5.1","2":"1.4"},{"1":"4.9","2":"1.4"},{"1":"4.7","2":"1.3"},{"1":"4.6","2":"1.5"},{"1":"5.0","2":"1.4"},{"1":"5.4","2":"1.7"},{"1":"4.6","2":"1.4"},{"1":"5.0","2":"1.5"},{"1":"4.4","2":"1.4"},{"1":"4.9","2":"1.5"},{"1":"5.4","2":"1.5"},{"1":"4.8","2":"1.6"},{"1":"4.8","2":"1.4"},{"1":"4.3","2":"1.1"},{"1":"5.8","2":"1.2"},{"1":"5.7","2":"1.5"},{"1":"5.4","2":"1.3"},{"1":"5.1","2":"1.4"},{"1":"5.7","2":"1.7"},{"1":"5.1","2":"1.5"},{"1":"5.4","2":"1.7"},{"1":"5.1","2":"1.5"},{"1":"4.6","2":"1.0"},{"1":"5.1","2":"1.7"},{"1":"4.8","2":"1.9"},{"1":"5.0","2":"1.6"},{"1":"5.0","2":"1.6"},{"1":"5.2","2":"1.5"},{"1":"5.2","2":"1.4"},{"1":"4.7","2":"1.6"},{"1":"4.8","2":"1.6"},{"1":"5.4","2":"1.5"},{"1":"5.2","2":"1.5"},{"1":"5.5","2":"1.4"},{"1":"4.9","2":"1.5"},{"1":"5.0","2":"1.2"},{"1":"5.5","2":"1.3"},{"1":"4.9","2":"1.4"},{"1":"4.4","2":"1.3"},{"1":"5.1","2":"1.5"},{"1":"5.0","2":"1.3"},{"1":"4.5","2":"1.3"},{"1":"4.4","2":"1.3"},{"1":"5.0","2":"1.6"},{"1":"5.1","2":"1.9"},{"1":"4.8","2":"1.4"},{"1":"5.1","2":"1.6"},{"1":"4.6","2":"1.4"},{"1":"5.3","2":"1.5"},{"1":"5.0","2":"1.4"},{"1":"7.0","2":"4.7"},{"1":"6.4","2":"4.5"},{"1":"6.9","2":"4.9"},{"1":"5.5","2":"4.0"},{"1":"6.5","2":"4.6"},{"1":"5.7","2":"4.5"},{"1":"6.3","2":"4.7"},{"1":"4.9","2":"3.3"},{"1":"6.6","2":"4.6"},{"1":"5.2","2":"3.9"},{"1":"5.0","2":"3.5"},{"1":"5.9","2":"4.2"},{"1":"6.0","2":"4.0"},{"1":"6.1","2":"4.7"},{"1":"5.6","2":"3.6"},{"1":"6.7","2":"4.4"},{"1":"5.6","2":"4.5"},{"1":"5.8","2":"4.1"},{"1":"6.2","2":"4.5"},{"1":"5.6","2":"3.9"},{"1":"5.9","2":"4.8"},{"1":"6.1","2":"4.0"},{"1":"6.3","2":"4.9"},{"1":"6.1","2":"4.7"},{"1":"6.4","2":"4.3"},{"1":"6.6","2":"4.4"},{"1":"6.8","2":"4.8"},{"1":"6.7","2":"5.0"},{"1":"6.0","2":"4.5"},{"1":"5.7","2":"3.5"},{"1":"5.5","2":"3.8"},{"1":"5.5","2":"3.7"},{"1":"5.8","2":"3.9"},{"1":"6.0","2":"5.1"},{"1":"5.4","2":"4.5"},{"1":"6.0","2":"4.5"},{"1":"6.7","2":"4.7"},{"1":"6.3","2":"4.4"},{"1":"5.6","2":"4.1"},{"1":"5.5","2":"4.0"},{"1":"5.5","2":"4.4"},{"1":"6.1","2":"4.6"},{"1":"5.8","2":"4.0"},{"1":"5.0","2":"3.3"},{"1":"5.6","2":"4.2"},{"1":"5.7","2":"4.2"},{"1":"5.7","2":"4.2"},{"1":"6.2","2":"4.3"},{"1":"5.1","2":"3.0"},{"1":"5.7","2":"4.1"},{"1":"6.3","2":"6.0"},{"1":"5.8","2":"5.1"},{"1":"7.1","2":"5.9"},{"1":"6.3","2":"5.6"},{"1":"6.5","2":"5.8"},{"1":"7.6","2":"6.6"},{"1":"4.9","2":"4.5"},{"1":"7.3","2":"6.3"},{"1":"6.7","2":"5.8"},{"1":"7.2","2":"6.1"},{"1":"6.5","2":"5.1"},{"1":"6.4","2":"5.3"},{"1":"6.8","2":"5.5"},{"1":"5.7","2":"5.0"},{"1":"5.8","2":"5.1"},{"1":"6.4","2":"5.3"},{"1":"6.5","2":"5.5"},{"1":"7.7","2":"6.7"},{"1":"7.7","2":"6.9"},{"1":"6.0","2":"5.0"},{"1":"6.9","2":"5.7"},{"1":"5.6","2":"4.9"},{"1":"7.7","2":"6.7"},{"1":"6.3","2":"4.9"},{"1":"6.7","2":"5.7"},{"1":"7.2","2":"6.0"},{"1":"6.2","2":"4.8"},{"1":"6.1","2":"4.9"},{"1":"6.4","2":"5.6"},{"1":"7.2","2":"5.8"},{"1":"7.4","2":"6.1"},{"1":"7.9","2":"6.4"},{"1":"6.4","2":"5.6"},{"1":"6.3","2":"5.1"},{"1":"6.1","2":"5.6"},{"1":"7.7","2":"6.1"},{"1":"6.3","2":"5.6"},{"1":"6.4","2":"5.5"},{"1":"6.0","2":"4.8"},{"1":"6.9","2":"5.4"},{"1":"6.7","2":"5.6"},{"1":"6.9","2":"5.1"},{"1":"5.8","2":"5.1"},{"1":"6.8","2":"5.9"},{"1":"6.7","2":"5.7"},{"1":"6.7","2":"5.2"},{"1":"6.3","2":"5.0"},{"1":"6.5","2":"5.2"},{"1":"6.2","2":"5.4"},{"1":"5.9","2":"5.1"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>These are functions that select columns based on their position in the data frame</p>
<ol type="i">
<li>
<p><code><a href="https://tidyselect.r-lib.org/reference/everything.html">everything()</a></code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">everything</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Sepal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Sepal.Width"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["Petal.Length"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["Petal.Width"],"name":[4],"type":["dbl"],"align":["right"]},{"label":["Species"],"name":[5],"type":["fct"],"align":["left"]}],"data":[{"1":"5.1","2":"3.5","3":"1.4","4":"0.2","5":"setosa","_rn_":"1"},{"1":"4.9","2":"3.0","3":"1.4","4":"0.2","5":"setosa","_rn_":"2"},{"1":"4.7","2":"3.2","3":"1.3","4":"0.2","5":"setosa","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>This is equivalent to <code>relocate(iris, Species)</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Species"],"name":[1],"type":["fct"],"align":["left"]},{"label":["Sepal.Length"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["Sepal.Width"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["Petal.Length"],"name":[4],"type":["dbl"],"align":["right"]},{"label":["Petal.Width"],"name":[5],"type":["dbl"],"align":["right"]}],"data":[{"1":"setosa","2":"5.1","3":"3.5","4":"1.4","5":"0.2","_rn_":"1"},{"1":"setosa","2":"4.9","3":"3.0","4":"1.4","5":"0.2","_rn_":"2"},{"1":"setosa","2":"4.7","3":"3.2","4":"1.3","5":"0.2","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li><p><code><a href="https://tidyselect.r-lib.org/reference/everything.html">last_col()</a></code></p></li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">last_col</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Species"],"name":[1],"type":["fct"],"align":["left"]}],"data":[{"1":"setosa","_rn_":"1"},{"1":"setosa","_rn_":"2"},{"1":"setosa","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Offset: 2nd to the last</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">last_col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Petal.Width"],"name":[1],"type":["dbl"],"align":["right"]}],"data":[{"1":"0.2","_rn_":"1"},{"1":"0.2","_rn_":"2"},{"1":"0.2","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Offset: Multiple columns from the end</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></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;">last_col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">last_col</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Petal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Petal.Width"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["Species"],"name":[3],"type":["fct"],"align":["left"]}],"data":[{"1":"1.4","2":"0.2","3":"setosa","_rn_":"1"},{"1":"1.4","2":"0.2","3":"setosa","_rn_":"2"},{"1":"1.3","2":"0.2","3":"setosa","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</div>
</div>
</div>
<p>Noticed that I invoked most of <code>&lt;tidy-select&gt;</code> helpers, but never loaded <a href="https://dplyr.tidyverse.org">dplyr</a> or <a href="https://tidyselect.r-lib.org">tidyselect</a>, not once, just to use them.</p>
</section><section id="data-masking-subset" class="level1" data-number="4"><h1 data-number="4">
<span class="header-section-number">4</span> “Data-Masking” Subset</h1>
<p>Just like those <code>&lt;tidy-select&gt;</code> helpers, some functions found in <a href="https://dplyr.tidyverse.org">dplyr</a>, but doesn’t in <a href="https://tidyselect.r-lib.org">tidyselect</a>. These are all the functions that can be used within “data-masking” functions, such as <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">dplyr::mutate()</a></code> and <code><a href="https://dplyr.tidyverse.org/reference/summarise.html">dplyr::summarise()</a></code>. Take a note of the term “within”, which means, you can’t use them outside from “data-masking” functions.</p>
<p>I call <code><a href="https://dplyr.tidyverse.org/reference/across.html">across()</a></code>, <code><a href="https://dplyr.tidyverse.org/reference/across.html">if_any()</a></code>, and <code><a href="https://dplyr.tidyverse.org/reference/across.html">if_all()</a></code> as <em>projection helpers</em> because they correspond to the <code>SELECT</code> clause in SQL, except they both <code><a href="https://dplyr.tidyverse.org/reference/across.html">if_any()</a></code>, and <code><a href="https://dplyr.tidyverse.org/reference/across.html">if_all()</a></code> map over the selected columns and returns the Boolean vector, while the <code><a href="https://dplyr.tidyverse.org/reference/across.html">across()</a></code> function modifies the selected columns. The <code><a href="https://dplyr.tidyverse.org/reference/pick.html">pick()</a></code> function, on the other hand serves as a complement of <code><a href="https://dplyr.tidyverse.org/reference/across.html">across()</a></code> by extracting them as a data frame, however, this only applies to subset a data frame to be invoked within the operations in “data-masking” functions. All of them can make use of the <code>&lt;tidy-select&gt;</code> API, meaning you can apply selectors like <code><a href="https://tidyselect.r-lib.org/reference/starts_with.html">starts_with()</a></code> or <code><a href="https://tidyselect.r-lib.org/reference/everything.html">everything()</a></code> to specify which columns to project.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs">
<li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">Using <code>pick()</code></a></li>
<li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">Using <code>across()</code></a></li>
<li class="nav-item"><a class="nav-link" id="tabset-2-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-3" aria-controls="tabset-2-3" aria-selected="false" href="">Using <code>if_all()</code> / <code>if_any()</code></a></li>
<li class="nav-item"><a class="nav-link" id="tabset-2-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-4" aria-controls="tabset-2-4" aria-selected="false" href="">Functions you actually need to attach</a></li>
</ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<p>Here’s an example: Calculating mean and standard deviation</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/group_by.html">group_by</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        summary <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><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>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">num</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;">pick</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;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><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>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                vars <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;"><a href="https://rdrr.io/r/base/colnames.html">colnames</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">num</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                mean <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;"><a href="https://rdrr.io/r/base/colSums.html">colMeans</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">num</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                sd <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;"><a href="https://rdrr.io/r/base/apply.html">apply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">num</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sd</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <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>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/unnest.html">unnest</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">summary</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["Species"],"name":[1],"type":["fct"],"align":["left"]},{"label":["vars"],"name":[2],"type":["chr"],"align":["left"]},{"label":["mean"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["sd"],"name":[4],"type":["dbl"],"align":["right"]}],"data":[{"1":"setosa","2":"Sepal.Length","3":"5.006","4":"0.3524897"},{"1":"setosa","2":"Sepal.Width","3":"3.428","4":"0.3790644"},{"1":"setosa","2":"Petal.Length","3":"1.462","4":"0.1736640"},{"1":"setosa","2":"Petal.Width","3":"0.246","4":"0.1053856"},{"1":"versicolor","2":"Sepal.Length","3":"5.936","4":"0.5161711"},{"1":"versicolor","2":"Sepal.Width","3":"2.770","4":"0.3137983"},{"1":"versicolor","2":"Petal.Length","3":"4.260","4":"0.4699110"},{"1":"versicolor","2":"Petal.Width","3":"1.326","4":"0.1977527"},{"1":"virginica","2":"Sepal.Length","3":"6.588","4":"0.6358796"},{"1":"virginica","2":"Sepal.Width","3":"2.974","4":"0.3224966"},{"1":"virginica","2":"Petal.Length","3":"5.552","4":"0.5518947"},{"1":"virginica","2":"Petal.Width","3":"2.026","4":"0.2746501"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>I am aware there’s a better approach to calculate the mean and standard deviation of each column by group.</p>
</blockquote>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<p>Here’s an example: Apply min-max normalization among numeric columns in <code>iris</code> dataset</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/as_tibble.html">as_tibble</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["Sepal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Sepal.Width"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["Petal.Length"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["Petal.Width"],"name":[4],"type":["dbl"],"align":["right"]},{"label":["Species"],"name":[5],"type":["fct"],"align":["left"]}],"data":[{"1":"0.22222222","2":"0.62500000","3":"0.06779661","4":"0.04166667","5":"setosa"},{"1":"0.16666667","2":"0.41666667","3":"0.06779661","4":"0.04166667","5":"setosa"},{"1":"0.11111111","2":"0.50000000","3":"0.05084746","4":"0.04166667","5":"setosa"},{"1":"0.08333333","2":"0.45833333","3":"0.08474576","4":"0.04166667","5":"setosa"},{"1":"0.19444444","2":"0.66666667","3":"0.06779661","4":"0.04166667","5":"setosa"},{"1":"0.30555556","2":"0.79166667","3":"0.11864407","4":"0.12500000","5":"setosa"},{"1":"0.08333333","2":"0.58333333","3":"0.06779661","4":"0.08333333","5":"setosa"},{"1":"0.19444444","2":"0.58333333","3":"0.08474576","4":"0.04166667","5":"setosa"},{"1":"0.02777778","2":"0.37500000","3":"0.06779661","4":"0.04166667","5":"setosa"},{"1":"0.16666667","2":"0.45833333","3":"0.08474576","4":"0.00000000","5":"setosa"},{"1":"0.30555556","2":"0.70833333","3":"0.08474576","4":"0.04166667","5":"setosa"},{"1":"0.13888889","2":"0.58333333","3":"0.10169492","4":"0.04166667","5":"setosa"},{"1":"0.13888889","2":"0.41666667","3":"0.06779661","4":"0.00000000","5":"setosa"},{"1":"0.00000000","2":"0.41666667","3":"0.01694915","4":"0.00000000","5":"setosa"},{"1":"0.41666667","2":"0.83333333","3":"0.03389831","4":"0.04166667","5":"setosa"},{"1":"0.38888889","2":"1.00000000","3":"0.08474576","4":"0.12500000","5":"setosa"},{"1":"0.30555556","2":"0.79166667","3":"0.05084746","4":"0.12500000","5":"setosa"},{"1":"0.22222222","2":"0.62500000","3":"0.06779661","4":"0.08333333","5":"setosa"},{"1":"0.38888889","2":"0.75000000","3":"0.11864407","4":"0.08333333","5":"setosa"},{"1":"0.22222222","2":"0.75000000","3":"0.08474576","4":"0.08333333","5":"setosa"},{"1":"0.30555556","2":"0.58333333","3":"0.11864407","4":"0.04166667","5":"setosa"},{"1":"0.22222222","2":"0.70833333","3":"0.08474576","4":"0.12500000","5":"setosa"},{"1":"0.08333333","2":"0.66666667","3":"0.00000000","4":"0.04166667","5":"setosa"},{"1":"0.22222222","2":"0.54166667","3":"0.11864407","4":"0.16666667","5":"setosa"},{"1":"0.13888889","2":"0.58333333","3":"0.15254237","4":"0.04166667","5":"setosa"},{"1":"0.19444444","2":"0.41666667","3":"0.10169492","4":"0.04166667","5":"setosa"},{"1":"0.19444444","2":"0.58333333","3":"0.10169492","4":"0.12500000","5":"setosa"},{"1":"0.25000000","2":"0.62500000","3":"0.08474576","4":"0.04166667","5":"setosa"},{"1":"0.25000000","2":"0.58333333","3":"0.06779661","4":"0.04166667","5":"setosa"},{"1":"0.11111111","2":"0.50000000","3":"0.10169492","4":"0.04166667","5":"setosa"},{"1":"0.13888889","2":"0.45833333","3":"0.10169492","4":"0.04166667","5":"setosa"},{"1":"0.30555556","2":"0.58333333","3":"0.08474576","4":"0.12500000","5":"setosa"},{"1":"0.25000000","2":"0.87500000","3":"0.08474576","4":"0.00000000","5":"setosa"},{"1":"0.33333333","2":"0.91666667","3":"0.06779661","4":"0.04166667","5":"setosa"},{"1":"0.16666667","2":"0.45833333","3":"0.08474576","4":"0.04166667","5":"setosa"},{"1":"0.19444444","2":"0.50000000","3":"0.03389831","4":"0.04166667","5":"setosa"},{"1":"0.33333333","2":"0.62500000","3":"0.05084746","4":"0.04166667","5":"setosa"},{"1":"0.16666667","2":"0.66666667","3":"0.06779661","4":"0.00000000","5":"setosa"},{"1":"0.02777778","2":"0.41666667","3":"0.05084746","4":"0.04166667","5":"setosa"},{"1":"0.22222222","2":"0.58333333","3":"0.08474576","4":"0.04166667","5":"setosa"},{"1":"0.19444444","2":"0.62500000","3":"0.05084746","4":"0.08333333","5":"setosa"},{"1":"0.05555556","2":"0.12500000","3":"0.05084746","4":"0.08333333","5":"setosa"},{"1":"0.02777778","2":"0.50000000","3":"0.05084746","4":"0.04166667","5":"setosa"},{"1":"0.19444444","2":"0.62500000","3":"0.10169492","4":"0.20833333","5":"setosa"},{"1":"0.22222222","2":"0.75000000","3":"0.15254237","4":"0.12500000","5":"setosa"},{"1":"0.13888889","2":"0.41666667","3":"0.06779661","4":"0.08333333","5":"setosa"},{"1":"0.22222222","2":"0.75000000","3":"0.10169492","4":"0.04166667","5":"setosa"},{"1":"0.08333333","2":"0.50000000","3":"0.06779661","4":"0.04166667","5":"setosa"},{"1":"0.27777778","2":"0.70833333","3":"0.08474576","4":"0.04166667","5":"setosa"},{"1":"0.19444444","2":"0.54166667","3":"0.06779661","4":"0.04166667","5":"setosa"},{"1":"0.75000000","2":"0.50000000","3":"0.62711864","4":"0.54166667","5":"versicolor"},{"1":"0.58333333","2":"0.50000000","3":"0.59322034","4":"0.58333333","5":"versicolor"},{"1":"0.72222222","2":"0.45833333","3":"0.66101695","4":"0.58333333","5":"versicolor"},{"1":"0.33333333","2":"0.12500000","3":"0.50847458","4":"0.50000000","5":"versicolor"},{"1":"0.61111111","2":"0.33333333","3":"0.61016949","4":"0.58333333","5":"versicolor"},{"1":"0.38888889","2":"0.33333333","3":"0.59322034","4":"0.50000000","5":"versicolor"},{"1":"0.55555556","2":"0.54166667","3":"0.62711864","4":"0.62500000","5":"versicolor"},{"1":"0.16666667","2":"0.16666667","3":"0.38983051","4":"0.37500000","5":"versicolor"},{"1":"0.63888889","2":"0.37500000","3":"0.61016949","4":"0.50000000","5":"versicolor"},{"1":"0.25000000","2":"0.29166667","3":"0.49152542","4":"0.54166667","5":"versicolor"},{"1":"0.19444444","2":"0.00000000","3":"0.42372881","4":"0.37500000","5":"versicolor"},{"1":"0.44444444","2":"0.41666667","3":"0.54237288","4":"0.58333333","5":"versicolor"},{"1":"0.47222222","2":"0.08333333","3":"0.50847458","4":"0.37500000","5":"versicolor"},{"1":"0.50000000","2":"0.37500000","3":"0.62711864","4":"0.54166667","5":"versicolor"},{"1":"0.36111111","2":"0.37500000","3":"0.44067797","4":"0.50000000","5":"versicolor"},{"1":"0.66666667","2":"0.45833333","3":"0.57627119","4":"0.54166667","5":"versicolor"},{"1":"0.36111111","2":"0.41666667","3":"0.59322034","4":"0.58333333","5":"versicolor"},{"1":"0.41666667","2":"0.29166667","3":"0.52542373","4":"0.37500000","5":"versicolor"},{"1":"0.52777778","2":"0.08333333","3":"0.59322034","4":"0.58333333","5":"versicolor"},{"1":"0.36111111","2":"0.20833333","3":"0.49152542","4":"0.41666667","5":"versicolor"},{"1":"0.44444444","2":"0.50000000","3":"0.64406780","4":"0.70833333","5":"versicolor"},{"1":"0.50000000","2":"0.33333333","3":"0.50847458","4":"0.50000000","5":"versicolor"},{"1":"0.55555556","2":"0.20833333","3":"0.66101695","4":"0.58333333","5":"versicolor"},{"1":"0.50000000","2":"0.33333333","3":"0.62711864","4":"0.45833333","5":"versicolor"},{"1":"0.58333333","2":"0.37500000","3":"0.55932203","4":"0.50000000","5":"versicolor"},{"1":"0.63888889","2":"0.41666667","3":"0.57627119","4":"0.54166667","5":"versicolor"},{"1":"0.69444444","2":"0.33333333","3":"0.64406780","4":"0.54166667","5":"versicolor"},{"1":"0.66666667","2":"0.41666667","3":"0.67796610","4":"0.66666667","5":"versicolor"},{"1":"0.47222222","2":"0.37500000","3":"0.59322034","4":"0.58333333","5":"versicolor"},{"1":"0.38888889","2":"0.25000000","3":"0.42372881","4":"0.37500000","5":"versicolor"},{"1":"0.33333333","2":"0.16666667","3":"0.47457627","4":"0.41666667","5":"versicolor"},{"1":"0.33333333","2":"0.16666667","3":"0.45762712","4":"0.37500000","5":"versicolor"},{"1":"0.41666667","2":"0.29166667","3":"0.49152542","4":"0.45833333","5":"versicolor"},{"1":"0.47222222","2":"0.29166667","3":"0.69491525","4":"0.62500000","5":"versicolor"},{"1":"0.30555556","2":"0.41666667","3":"0.59322034","4":"0.58333333","5":"versicolor"},{"1":"0.47222222","2":"0.58333333","3":"0.59322034","4":"0.62500000","5":"versicolor"},{"1":"0.66666667","2":"0.45833333","3":"0.62711864","4":"0.58333333","5":"versicolor"},{"1":"0.55555556","2":"0.12500000","3":"0.57627119","4":"0.50000000","5":"versicolor"},{"1":"0.36111111","2":"0.41666667","3":"0.52542373","4":"0.50000000","5":"versicolor"},{"1":"0.33333333","2":"0.20833333","3":"0.50847458","4":"0.50000000","5":"versicolor"},{"1":"0.33333333","2":"0.25000000","3":"0.57627119","4":"0.45833333","5":"versicolor"},{"1":"0.50000000","2":"0.41666667","3":"0.61016949","4":"0.54166667","5":"versicolor"},{"1":"0.41666667","2":"0.25000000","3":"0.50847458","4":"0.45833333","5":"versicolor"},{"1":"0.19444444","2":"0.12500000","3":"0.38983051","4":"0.37500000","5":"versicolor"},{"1":"0.36111111","2":"0.29166667","3":"0.54237288","4":"0.50000000","5":"versicolor"},{"1":"0.38888889","2":"0.41666667","3":"0.54237288","4":"0.45833333","5":"versicolor"},{"1":"0.38888889","2":"0.37500000","3":"0.54237288","4":"0.50000000","5":"versicolor"},{"1":"0.52777778","2":"0.37500000","3":"0.55932203","4":"0.50000000","5":"versicolor"},{"1":"0.22222222","2":"0.20833333","3":"0.33898305","4":"0.41666667","5":"versicolor"},{"1":"0.38888889","2":"0.33333333","3":"0.52542373","4":"0.50000000","5":"versicolor"},{"1":"0.55555556","2":"0.54166667","3":"0.84745763","4":"1.00000000","5":"virginica"},{"1":"0.41666667","2":"0.29166667","3":"0.69491525","4":"0.75000000","5":"virginica"},{"1":"0.77777778","2":"0.41666667","3":"0.83050847","4":"0.83333333","5":"virginica"},{"1":"0.55555556","2":"0.37500000","3":"0.77966102","4":"0.70833333","5":"virginica"},{"1":"0.61111111","2":"0.41666667","3":"0.81355932","4":"0.87500000","5":"virginica"},{"1":"0.91666667","2":"0.41666667","3":"0.94915254","4":"0.83333333","5":"virginica"},{"1":"0.16666667","2":"0.20833333","3":"0.59322034","4":"0.66666667","5":"virginica"},{"1":"0.83333333","2":"0.37500000","3":"0.89830508","4":"0.70833333","5":"virginica"},{"1":"0.66666667","2":"0.20833333","3":"0.81355932","4":"0.70833333","5":"virginica"},{"1":"0.80555556","2":"0.66666667","3":"0.86440678","4":"1.00000000","5":"virginica"},{"1":"0.61111111","2":"0.50000000","3":"0.69491525","4":"0.79166667","5":"virginica"},{"1":"0.58333333","2":"0.29166667","3":"0.72881356","4":"0.75000000","5":"virginica"},{"1":"0.69444444","2":"0.41666667","3":"0.76271186","4":"0.83333333","5":"virginica"},{"1":"0.38888889","2":"0.20833333","3":"0.67796610","4":"0.79166667","5":"virginica"},{"1":"0.41666667","2":"0.33333333","3":"0.69491525","4":"0.95833333","5":"virginica"},{"1":"0.58333333","2":"0.50000000","3":"0.72881356","4":"0.91666667","5":"virginica"},{"1":"0.61111111","2":"0.41666667","3":"0.76271186","4":"0.70833333","5":"virginica"},{"1":"0.94444444","2":"0.75000000","3":"0.96610169","4":"0.87500000","5":"virginica"},{"1":"0.94444444","2":"0.25000000","3":"1.00000000","4":"0.91666667","5":"virginica"},{"1":"0.47222222","2":"0.08333333","3":"0.67796610","4":"0.58333333","5":"virginica"},{"1":"0.72222222","2":"0.50000000","3":"0.79661017","4":"0.91666667","5":"virginica"},{"1":"0.36111111","2":"0.33333333","3":"0.66101695","4":"0.79166667","5":"virginica"},{"1":"0.94444444","2":"0.33333333","3":"0.96610169","4":"0.79166667","5":"virginica"},{"1":"0.55555556","2":"0.29166667","3":"0.66101695","4":"0.70833333","5":"virginica"},{"1":"0.66666667","2":"0.54166667","3":"0.79661017","4":"0.83333333","5":"virginica"},{"1":"0.80555556","2":"0.50000000","3":"0.84745763","4":"0.70833333","5":"virginica"},{"1":"0.52777778","2":"0.33333333","3":"0.64406780","4":"0.70833333","5":"virginica"},{"1":"0.50000000","2":"0.41666667","3":"0.66101695","4":"0.70833333","5":"virginica"},{"1":"0.58333333","2":"0.33333333","3":"0.77966102","4":"0.83333333","5":"virginica"},{"1":"0.80555556","2":"0.41666667","3":"0.81355932","4":"0.62500000","5":"virginica"},{"1":"0.86111111","2":"0.33333333","3":"0.86440678","4":"0.75000000","5":"virginica"},{"1":"1.00000000","2":"0.75000000","3":"0.91525424","4":"0.79166667","5":"virginica"},{"1":"0.58333333","2":"0.33333333","3":"0.77966102","4":"0.87500000","5":"virginica"},{"1":"0.55555556","2":"0.33333333","3":"0.69491525","4":"0.58333333","5":"virginica"},{"1":"0.50000000","2":"0.25000000","3":"0.77966102","4":"0.54166667","5":"virginica"},{"1":"0.94444444","2":"0.41666667","3":"0.86440678","4":"0.91666667","5":"virginica"},{"1":"0.55555556","2":"0.58333333","3":"0.77966102","4":"0.95833333","5":"virginica"},{"1":"0.58333333","2":"0.45833333","3":"0.76271186","4":"0.70833333","5":"virginica"},{"1":"0.47222222","2":"0.41666667","3":"0.64406780","4":"0.70833333","5":"virginica"},{"1":"0.72222222","2":"0.45833333","3":"0.74576271","4":"0.83333333","5":"virginica"},{"1":"0.66666667","2":"0.45833333","3":"0.77966102","4":"0.95833333","5":"virginica"},{"1":"0.72222222","2":"0.45833333","3":"0.69491525","4":"0.91666667","5":"virginica"},{"1":"0.41666667","2":"0.29166667","3":"0.69491525","4":"0.75000000","5":"virginica"},{"1":"0.69444444","2":"0.50000000","3":"0.83050847","4":"0.91666667","5":"virginica"},{"1":"0.66666667","2":"0.54166667","3":"0.79661017","4":"1.00000000","5":"virginica"},{"1":"0.66666667","2":"0.41666667","3":"0.71186441","4":"0.91666667","5":"virginica"},{"1":"0.55555556","2":"0.20833333","3":"0.67796610","4":"0.75000000","5":"virginica"},{"1":"0.61111111","2":"0.41666667","3":"0.71186441","4":"0.79166667","5":"virginica"},{"1":"0.52777778","2":"0.58333333","3":"0.74576271","4":"0.91666667","5":"virginica"},{"1":"0.44444444","2":"0.41666667","3":"0.69491525","4":"0.70833333","5":"virginica"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>And once again, I never attach <a href="https://dplyr.tidyverse.org">dplyr</a> into the search path just to use <code><a href="https://dplyr.tidyverse.org/reference/across.html">across()</a></code> and <code><a href="https://dplyr.tidyverse.org/reference/pick.html">pick()</a></code>.</p>
<p>You can use <code><a href="https://dplyr.tidyverse.org/reference/across.html">across()</a></code> in some <a href="https://dplyr.tidyverse.org">dplyr</a> “data-masking” function like <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code>, but this is a deprecated behavior and attaching <a href="https://dplyr.tidyverse.org">dplyr</a> package is required.</p>
</div>
<div id="tabset-2-3" class="tab-pane" aria-labelledby="tabset-2-3-tab">
<p>Example: Removing all missing values across all columns in <code>airquality</code> data frame</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">airquality</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></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;">if_all</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;">everything</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["Ozone"],"name":[1],"type":["int"],"align":["right"]},{"label":["Solar.R"],"name":[2],"type":["int"],"align":["right"]},{"label":["Wind"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["Temp"],"name":[4],"type":["int"],"align":["right"]},{"label":["Month"],"name":[5],"type":["int"],"align":["right"]},{"label":["Day"],"name":[6],"type":["int"],"align":["right"]}],"data":[{"1":"41","2":"190","3":"7.4","4":"67","5":"5","6":"1","_rn_":"1"},{"1":"36","2":"118","3":"8.0","4":"72","5":"5","6":"2","_rn_":"2"},{"1":"12","2":"149","3":"12.6","4":"74","5":"5","6":"3","_rn_":"3"},{"1":"18","2":"313","3":"11.5","4":"62","5":"5","6":"4","_rn_":"4"},{"1":"23","2":"299","3":"8.6","4":"65","5":"5","6":"7","_rn_":"5"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>If <code><a href="https://dplyr.tidyverse.org/reference/across.html">if_all()</a></code> / <code><a href="https://dplyr.tidyverse.org/reference/across.html">if_any()</a></code> is used outside <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code>, those functions need <a href="https://dplyr.tidyverse.org">dplyr</a> package to be attached to use them.</p>
</div>
<div id="tabset-2-4" class="tab-pane" aria-labelledby="tabset-2-4-tab">
<p>Though, there are some exceptions: there are helper functions you actually need <a href="https://dplyr.tidyverse.org">dplyr</a> to be attached to use them, otherwise they don’t work and R will throw an error.</p>
<p>Here they are:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<ol type="1">
<li>
<p><code><a href="https://dplyr.tidyverse.org/reference/context.html">n()</a></code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/group_by.html">group_by</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/slice.html">slice_max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, order_by <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Sepal.Length</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        count <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;"><a href="https://dplyr.tidyverse.org/reference/context.html">n</a></span><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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 👈 </span></span>
<span>        m_sl <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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Sepal.Length</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["Species"],"name":[1],"type":["fct"],"align":["left"]},{"label":["count"],"name":[2],"type":["int"],"align":["right"]},{"label":["m_sl"],"name":[3],"type":["dbl"],"align":["right"]}],"data":[{"1":"setosa","2":"22","3":"5.313636"},{"1":"versicolor","2":"20","3":"6.450000"},{"1":"virginica","2":"22","3":"7.159091"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li>
<p><code><a href="https://dplyr.tidyverse.org/reference/context.html">cur_group()</a></code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mtcars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/group_by.html">group_by</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cyl</span><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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/reframe.html">reframe</a></span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</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;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wt</span>, 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;"><a href="https://dplyr.tidyverse.org/reference/context.html">cur_group</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 👈 </span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coefs</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;"><a href="https://rdrr.io/r/stats/coef.html">coef</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            terms <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;"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coefs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            estimate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">coefs</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["cyl"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["terms"],"name":[2],"type":["chr"],"align":["left"]},{"label":["estimate"],"name":[3],"type":["dbl"],"align":["right"]}],"data":[{"1":"4","2":"(Intercept)","3":"39.571196"},{"1":"4","2":"wt","3":"-5.647025"},{"1":"6","2":"(Intercept)","3":"28.408845"},{"1":"6","2":"wt","3":"-2.780106"},{"1":"8","2":"(Intercept)","3":"23.868029"},{"1":"8","2":"wt","3":"-2.192438"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li>
<p><code><a href="https://dplyr.tidyverse.org/reference/context.html">cur_group_id()</a></code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">starwars</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/group_by.html">group_by</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">species</span><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;">|&gt;</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/reframe.html">reframe</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">species</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name</span>, </span>
<span>        hierarchical_id <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;"><a href="https://rdrr.io/r/base/sprintf.html">sprintf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%02d-%03d"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/context.html">cur_group_id</a></span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/row_number.html">row_number</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 👈 </span></span>
<span>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/slice.html">slice_min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hierarchical_id</span>, n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["species"],"name":[1],"type":["chr"],"align":["left"]},{"label":["name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["hierarchical_id"],"name":[3],"type":["chr"],"align":["left"]}],"data":[{"1":"Aleena","2":"Ratts Tyerel","3":"01-001"},{"1":"Besalisk","2":"Dexter Jettster","3":"02-001"},{"1":"Cerean","2":"Ki-Adi-Mundi","3":"03-001"},{"1":"Chagrian","2":"Mas Amedda","3":"04-001"},{"1":"Clawdite","2":"Zam Wesell","3":"05-001"},{"1":"Droid","2":"C-3PO","3":"06-001"},{"1":"Droid","2":"R2-D2","3":"06-002"},{"1":"Droid","2":"R5-D4","3":"06-003"},{"1":"Droid","2":"IG-88","3":"06-004"},{"1":"Droid","2":"R4-P17","3":"06-005"},{"1":"Droid","2":"BB8","3":"06-006"},{"1":"Dug","2":"Sebulba","3":"07-001"},{"1":"Ewok","2":"Wicket Systri Warrick","3":"08-001"},{"1":"Geonosian","2":"Poggle the Lesser","3":"09-001"},{"1":"Gungan","2":"Jar Jar Binks","3":"10-001"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li>
<p><code><a href="https://dplyr.tidyverse.org/reference/context.html">cur_group_rows()</a></code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/group_by.html">group_by</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Species</span><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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/slice.html">slice_sample</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, replace <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span>    <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;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        m_sl <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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Sepal.Length</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        n <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></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;"><a href="https://dplyr.tidyverse.org/reference/context.html">cur_group_rows</a></span><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 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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 👈 </span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["Species"],"name":[1],"type":["fct"],"align":["left"]},{"label":["m_sl"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["n"],"name":[3],"type":["dbl"],"align":["right"]}],"data":[{"1":"setosa","2":"4.998667","3":"105"},{"1":"versicolor","2":"5.946667","3":"105"},{"1":"virginica","2":"6.614667","3":"105"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
<li>
<p><code><a href="https://dplyr.tidyverse.org/reference/context.html">cur_column()</a></code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/as_tibble.html">as_tibble</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/transmute.html">transmute</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/across.html">across</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyselect.r-lib.org/reference/where.html">where</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is.numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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>                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">stringr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://stringr.tidyverse.org/reference/str_detect.html">str_detect</a></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;"><a href="https://dplyr.tidyverse.org/reference/context.html">cur_column</a></span><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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sepal"</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 👈 </span></span>
<span>                    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">stringr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://stringr.tidyverse.org/reference/str_detect.html">str_detect</a></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;"><a href="https://dplyr.tidyverse.org/reference/context.html">cur_column</a></span><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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Petal"</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 👈 </span></span>
<span>                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</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;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col</span></span>
<span>                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["Sepal.Length"],"name":[1],"type":["dbl"],"align":["right"]},{"label":["Sepal.Width"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["Petal.Length"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["Petal.Width"],"name":[4],"type":["dbl"],"align":["right"]}],"data":[{"1":"-0.74333333","2":"0.44266667","3":"-1.33575163","4":"-1.3110521482"},{"1":"-0.94333333","2":"-0.05733333","3":"-1.33575163","4":"-1.3110521482"},{"1":"-1.14333333","2":"0.14266667","3":"-1.39239929","4":"-1.3110521482"},{"1":"-1.24333333","2":"0.04266667","3":"-1.27910398","4":"-1.3110521482"},{"1":"-0.84333333","2":"0.54266667","3":"-1.33575163","4":"-1.3110521482"},{"1":"-0.44333333","2":"0.84266667","3":"-1.16580868","4":"-1.0486667950"},{"1":"-1.24333333","2":"0.34266667","3":"-1.33575163","4":"-1.1798594716"},{"1":"-0.84333333","2":"0.34266667","3":"-1.27910398","4":"-1.3110521482"},{"1":"-1.44333333","2":"-0.15733333","3":"-1.33575163","4":"-1.3110521482"},{"1":"-0.94333333","2":"0.04266667","3":"-1.27910398","4":"-1.4422448248"},{"1":"-0.44333333","2":"0.64266667","3":"-1.27910398","4":"-1.3110521482"},{"1":"-1.04333333","2":"0.34266667","3":"-1.22245633","4":"-1.3110521482"},{"1":"-1.04333333","2":"-0.05733333","3":"-1.33575163","4":"-1.4422448248"},{"1":"-1.54333333","2":"-0.05733333","3":"-1.50569459","4":"-1.4422448248"},{"1":"-0.04333333","2":"0.94266667","3":"-1.44904694","4":"-1.3110521482"},{"1":"-0.14333333","2":"1.34266667","3":"-1.27910398","4":"-1.0486667950"},{"1":"-0.44333333","2":"0.84266667","3":"-1.39239929","4":"-1.0486667950"},{"1":"-0.74333333","2":"0.44266667","3":"-1.33575163","4":"-1.1798594716"},{"1":"-0.14333333","2":"0.74266667","3":"-1.16580868","4":"-1.1798594716"},{"1":"-0.74333333","2":"0.74266667","3":"-1.27910398","4":"-1.1798594716"},{"1":"-0.44333333","2":"0.34266667","3":"-1.16580868","4":"-1.3110521482"},{"1":"-0.74333333","2":"0.64266667","3":"-1.27910398","4":"-1.0486667950"},{"1":"-1.24333333","2":"0.54266667","3":"-1.56234224","4":"-1.3110521482"},{"1":"-0.74333333","2":"0.24266667","3":"-1.16580868","4":"-0.9174741184"},{"1":"-1.04333333","2":"0.34266667","3":"-1.05251337","4":"-1.3110521482"},{"1":"-0.84333333","2":"-0.05733333","3":"-1.22245633","4":"-1.3110521482"},{"1":"-0.84333333","2":"0.34266667","3":"-1.22245633","4":"-1.0486667950"},{"1":"-0.64333333","2":"0.44266667","3":"-1.27910398","4":"-1.3110521482"},{"1":"-0.64333333","2":"0.34266667","3":"-1.33575163","4":"-1.3110521482"},{"1":"-1.14333333","2":"0.14266667","3":"-1.22245633","4":"-1.3110521482"},{"1":"-1.04333333","2":"0.04266667","3":"-1.22245633","4":"-1.3110521482"},{"1":"-0.44333333","2":"0.34266667","3":"-1.27910398","4":"-1.0486667950"},{"1":"-0.64333333","2":"1.04266667","3":"-1.27910398","4":"-1.4422448248"},{"1":"-0.34333333","2":"1.14266667","3":"-1.33575163","4":"-1.3110521482"},{"1":"-0.94333333","2":"0.04266667","3":"-1.27910398","4":"-1.3110521482"},{"1":"-0.84333333","2":"0.14266667","3":"-1.44904694","4":"-1.3110521482"},{"1":"-0.34333333","2":"0.44266667","3":"-1.39239929","4":"-1.3110521482"},{"1":"-0.94333333","2":"0.54266667","3":"-1.33575163","4":"-1.4422448248"},{"1":"-1.44333333","2":"-0.05733333","3":"-1.39239929","4":"-1.3110521482"},{"1":"-0.74333333","2":"0.34266667","3":"-1.27910398","4":"-1.3110521482"},{"1":"-0.84333333","2":"0.44266667","3":"-1.39239929","4":"-1.1798594716"},{"1":"-1.34333333","2":"-0.75733333","3":"-1.39239929","4":"-1.1798594716"},{"1":"-1.44333333","2":"0.14266667","3":"-1.39239929","4":"-1.3110521482"},{"1":"-0.84333333","2":"0.44266667","3":"-1.22245633","4":"-0.7862814418"},{"1":"-0.74333333","2":"0.74266667","3":"-1.05251337","4":"-1.0486667950"},{"1":"-1.04333333","2":"-0.05733333","3":"-1.33575163","4":"-1.1798594716"},{"1":"-0.74333333","2":"0.74266667","3":"-1.22245633","4":"-1.3110521482"},{"1":"-1.24333333","2":"0.14266667","3":"-1.33575163","4":"-1.3110521482"},{"1":"-0.54333333","2":"0.64266667","3":"-1.27910398","4":"-1.3110521482"},{"1":"-0.84333333","2":"0.24266667","3":"-1.33575163","4":"-1.3110521482"},{"1":"1.15666667","2":"0.14266667","3":"0.53362088","4":"0.2632599711"},{"1":"0.55666667","2":"0.14266667","3":"0.42032558","4":"0.3944526477"},{"1":"1.05666667","2":"0.04266667","3":"0.64691619","4":"0.3944526477"},{"1":"-0.34333333","2":"-0.75733333","3":"0.13708732","4":"0.1320672944"},{"1":"0.65666667","2":"-0.25733333","3":"0.47697323","4":"0.3944526477"},{"1":"-0.14333333","2":"-0.25733333","3":"0.42032558","4":"0.1320672944"},{"1":"0.45666667","2":"0.24266667","3":"0.53362088","4":"0.5256453243"},{"1":"-0.94333333","2":"-0.65733333","3":"-0.25944625","4":"-0.2615107354"},{"1":"0.75666667","2":"-0.15733333","3":"0.47697323","4":"0.1320672944"},{"1":"-0.64333333","2":"-0.35733333","3":"0.08043967","4":"0.2632599711"},{"1":"-0.84333333","2":"-1.05733333","3":"-0.14615094","4":"-0.2615107354"},{"1":"0.05666667","2":"-0.05733333","3":"0.25038262","4":"0.3944526477"},{"1":"0.15666667","2":"-0.85733333","3":"0.13708732","4":"-0.2615107354"},{"1":"0.25666667","2":"-0.15733333","3":"0.53362088","4":"0.2632599711"},{"1":"-0.24333333","2":"-0.15733333","3":"-0.08950329","4":"0.1320672944"},{"1":"0.85666667","2":"0.04266667","3":"0.36367793","4":"0.2632599711"},{"1":"-0.24333333","2":"-0.05733333","3":"0.42032558","4":"0.3944526477"},{"1":"-0.04333333","2":"-0.35733333","3":"0.19373497","4":"-0.2615107354"},{"1":"0.35666667","2":"-0.85733333","3":"0.42032558","4":"0.3944526477"},{"1":"-0.24333333","2":"-0.55733333","3":"0.08043967","4":"-0.1303180588"},{"1":"0.05666667","2":"0.14266667","3":"0.59026853","4":"0.7880306775"},{"1":"0.25666667","2":"-0.25733333","3":"0.13708732","4":"0.1320672944"},{"1":"0.45666667","2":"-0.55733333","3":"0.64691619","4":"0.3944526477"},{"1":"0.25666667","2":"-0.25733333","3":"0.53362088","4":"0.0008746178"},{"1":"0.55666667","2":"-0.15733333","3":"0.30703027","4":"0.1320672944"},{"1":"0.75666667","2":"-0.05733333","3":"0.36367793","4":"0.2632599711"},{"1":"0.95666667","2":"-0.25733333","3":"0.59026853","4":"0.2632599711"},{"1":"0.85666667","2":"-0.05733333","3":"0.70356384","4":"0.6568380009"},{"1":"0.15666667","2":"-0.15733333","3":"0.42032558","4":"0.3944526477"},{"1":"-0.14333333","2":"-0.45733333","3":"-0.14615094","4":"-0.2615107354"},{"1":"-0.34333333","2":"-0.65733333","3":"0.02379201","4":"-0.1303180588"},{"1":"-0.34333333","2":"-0.65733333","3":"-0.03285564","4":"-0.2615107354"},{"1":"-0.04333333","2":"-0.35733333","3":"0.08043967","4":"0.0008746178"},{"1":"0.15666667","2":"-0.35733333","3":"0.76021149","4":"0.5256453243"},{"1":"-0.44333333","2":"-0.05733333","3":"0.42032558","4":"0.3944526477"},{"1":"0.15666667","2":"0.34266667","3":"0.42032558","4":"0.5256453243"},{"1":"0.85666667","2":"0.04266667","3":"0.53362088","4":"0.3944526477"},{"1":"0.45666667","2":"-0.75733333","3":"0.36367793","4":"0.1320672944"},{"1":"-0.24333333","2":"-0.05733333","3":"0.19373497","4":"0.1320672944"},{"1":"-0.34333333","2":"-0.55733333","3":"0.13708732","4":"0.1320672944"},{"1":"-0.34333333","2":"-0.45733333","3":"0.36367793","4":"0.0008746178"},{"1":"0.25666667","2":"-0.05733333","3":"0.47697323","4":"0.2632599711"},{"1":"-0.04333333","2":"-0.45733333","3":"0.13708732","4":"0.0008746178"},{"1":"-0.84333333","2":"-0.75733333","3":"-0.25944625","4":"-0.2615107354"},{"1":"-0.24333333","2":"-0.35733333","3":"0.25038262","4":"0.1320672944"},{"1":"-0.14333333","2":"-0.05733333","3":"0.25038262","4":"0.0008746178"},{"1":"-0.14333333","2":"-0.15733333","3":"0.25038262","4":"0.1320672944"},{"1":"0.35666667","2":"-0.15733333","3":"0.30703027","4":"0.1320672944"},{"1":"-0.74333333","2":"-0.55733333","3":"-0.42938920","4":"-0.1303180588"},{"1":"-0.14333333","2":"-0.25733333","3":"0.19373497","4":"0.1320672944"},{"1":"0.45666667","2":"0.24266667","3":"1.27004036","4":"1.7063794137"},{"1":"-0.04333333","2":"-0.35733333","3":"0.76021149","4":"0.9192233541"},{"1":"1.25666667","2":"-0.05733333","3":"1.21339271","4":"1.1816087073"},{"1":"0.45666667","2":"-0.15733333","3":"1.04344975","4":"0.7880306775"},{"1":"0.65666667","2":"-0.05733333","3":"1.15674505","4":"1.3128013839"},{"1":"1.75666667","2":"-0.05733333","3":"1.60992627","4":"1.1816087073"},{"1":"-0.94333333","2":"-0.55733333","3":"0.42032558","4":"0.6568380009"},{"1":"1.45666667","2":"-0.15733333","3":"1.43998331","4":"0.7880306775"},{"1":"0.85666667","2":"-0.55733333","3":"1.15674505","4":"0.7880306775"},{"1":"1.35666667","2":"0.54266667","3":"1.32668801","4":"1.7063794137"},{"1":"0.65666667","2":"0.14266667","3":"0.76021149","4":"1.0504160307"},{"1":"0.55666667","2":"-0.35733333","3":"0.87350679","4":"0.9192233541"},{"1":"0.95666667","2":"-0.05733333","3":"0.98680210","4":"1.1816087073"},{"1":"-0.14333333","2":"-0.55733333","3":"0.70356384","4":"1.0504160307"},{"1":"-0.04333333","2":"-0.25733333","3":"0.76021149","4":"1.5751867371"},{"1":"0.55666667","2":"0.14266667","3":"0.87350679","4":"1.4439940605"},{"1":"0.65666667","2":"-0.05733333","3":"0.98680210","4":"0.7880306775"},{"1":"1.85666667","2":"0.74266667","3":"1.66657392","4":"1.3128013839"},{"1":"1.85666667","2":"-0.45733333","3":"1.77986923","4":"1.4439940605"},{"1":"0.15666667","2":"-0.85733333","3":"0.70356384","4":"0.3944526477"},{"1":"1.05666667","2":"0.14266667","3":"1.10009740","4":"1.4439940605"},{"1":"-0.24333333","2":"-0.25733333","3":"0.64691619","4":"1.0504160307"},{"1":"1.85666667","2":"-0.25733333","3":"1.66657392","4":"1.0504160307"},{"1":"0.45666667","2":"-0.35733333","3":"0.64691619","4":"0.7880306775"},{"1":"0.85666667","2":"0.24266667","3":"1.10009740","4":"1.1816087073"},{"1":"1.35666667","2":"0.14266667","3":"1.27004036","4":"0.7880306775"},{"1":"0.35666667","2":"-0.25733333","3":"0.59026853","4":"0.7880306775"},{"1":"0.25666667","2":"-0.05733333","3":"0.64691619","4":"0.7880306775"},{"1":"0.55666667","2":"-0.25733333","3":"1.04344975","4":"1.1816087073"},{"1":"1.35666667","2":"-0.05733333","3":"1.15674505","4":"0.5256453243"},{"1":"1.55666667","2":"-0.25733333","3":"1.32668801","4":"0.9192233541"},{"1":"2.05666667","2":"0.74266667","3":"1.49663097","4":"1.0504160307"},{"1":"0.55666667","2":"-0.25733333","3":"1.04344975","4":"1.3128013839"},{"1":"0.45666667","2":"-0.25733333","3":"0.76021149","4":"0.3944526477"},{"1":"0.25666667","2":"-0.45733333","3":"1.04344975","4":"0.2632599711"},{"1":"1.85666667","2":"-0.05733333","3":"1.32668801","4":"1.4439940605"},{"1":"0.45666667","2":"0.34266667","3":"1.04344975","4":"1.5751867371"},{"1":"0.55666667","2":"0.04266667","3":"0.98680210","4":"0.7880306775"},{"1":"0.15666667","2":"-0.05733333","3":"0.59026853","4":"0.7880306775"},{"1":"1.05666667","2":"0.04266667","3":"0.93015445","4":"1.1816087073"},{"1":"0.85666667","2":"0.04266667","3":"1.04344975","4":"1.5751867371"},{"1":"1.05666667","2":"0.04266667","3":"0.76021149","4":"1.4439940605"},{"1":"-0.04333333","2":"-0.35733333","3":"0.76021149","4":"0.9192233541"},{"1":"0.95666667","2":"0.14266667","3":"1.21339271","4":"1.4439940605"},{"1":"0.85666667","2":"0.24266667","3":"1.10009740","4":"1.7063794137"},{"1":"0.85666667","2":"-0.05733333","3":"0.81685914","4":"1.4439940605"},{"1":"0.45666667","2":"-0.55733333","3":"0.70356384","4":"0.9192233541"},{"1":"0.65666667","2":"-0.05733333","3":"0.81685914","4":"1.0504160307"},{"1":"0.35666667","2":"0.34266667","3":"0.93015445","4":"1.4439940605"},{"1":"0.05666667","2":"-0.05733333","3":"0.76021149","4":"0.7880306775"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
</section><section id="conclusion" class="level1" data-number="5"><h1 data-number="5">
<span class="header-section-number">5</span> Conclusion</h1>
<p>I hope they don’t change this soon, it is quite a nice feature (definitely not a bug 😋), assembling the DSL strengths across <a href="https://tidyverse.tidyverse.org">tidyverse</a> APIs. Even if it is subtle. I still suggest you to attach these functions (through e.g.&nbsp;<code><a href="https://rdrr.io/r/base/library.html">library()</a></code> and <code><a href="http://klmr.me/box/reference/use.html">box::use()</a></code>) for better maintainability.</p>


</section> ]]></description>
  <category>R</category>
  <category>tidyselect</category>
  <category>tidyverse</category>
  <guid>https://joshuamarie.com/posts/04-tidyselect-helpers/</guid>
  <pubDate>Sun, 02 Nov 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/04-tidyselect-helpers/image.png" medium="image" type="image/png" height="121" width="144"/>
</item>
<item>
  <title>Box: Placing module system into R</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/03-modules-in-r/</link>
  <description><![CDATA[ 
<div class="page-columns page-rows-contents page-layout-article"><div class="social-share"><a href="https://twitter.com/share?url=https://joshuamarie.com/posts/03-modules-in-r/&amp;text=Module System in R - Joshua Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/03-modules-in-r/&amp;title=Module System in R - Joshua Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Module System in R - Joshua Marie&amp;body=Check out this link:https://joshuamarie.com/posts/03-modules-in-r/" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/03-modules-in-r/" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/03-modules-in-r/&amp;title=Module System in R - Joshua Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/03-modules-in-r/&amp;title=Module System in R - Joshua Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/03-modules-in-r/&amp;name=Module System in R - Joshua Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Module System in R - Joshua Marie https://joshuamarie.com/posts/03-modules-in-r/'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/03-modules-in-r/ Module System in R - Joshua Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a></div></div>





<section id="welcome" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Welcome!</h1>
<p><em>Click <a href="https://joshuamarie.github.io/modules-in-r/">here</a> to read the actual book. </em></p>
<p>Welcome to the definitive guide to module system in R using box!</p>
<p>Whether you want another way to import R packages, or simply seeking better ways to organize your code, this guide helps you to reimagine something did not exist in R for quite a long time: module system. This book introduces you the package that I discovered few years ago, it is called <a href="https://github.com/klmr/box">box</a>, initially <strong>modules</strong> package, by Konrad Rudolph. This package provides a clean, straightforward API to define and manage modules. R, especially its users, suffers from managing huge R pipelines, because there’s no clean way to manage such complex pipelines…this problem will potentially cease to exists, as <strong>box</strong> package affords closely equivalent module system to other programming languages like Python, C#, and more.</p>
<section id="what-youll-find-here" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="what-youll-find-here"><span class="header-section-number">1.1</span> What You’ll Find Here</h2>
<p>A definitive guide that walks you through:</p>
<ul>
<li>Alternative approach to import R codes (i.e.&nbsp;R packages, modules)</li>
<li>Modern approaches to compose and organize R codes</li>
<li>Step-by-step tutorials for using the <strong>box</strong> package</li>
<li>Best practices for maintainable R code</li>
</ul>
</section>
<section id="why-this-book" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="why-this-book"><span class="header-section-number">1.2</span> Why this book</h2>
<p>Only little to no books teach you to correctly write reusable, composable, and modular R codes :). Most of the books maybe teaches you about R, particularly application of R in different fields, but little to none explains one of the best practices. Most of them uses <code>library()</code> anyways, so you won’t certainly find similar book like this.</p>
<p>Consequently, while R offers various ways to organize code, the <strong>box</strong> package manages to be superior among them (I am bias) by introducing a fresh, modern approach to module system that may significantly improve your R development workflow, similar to the workflow made by other developers, particularly Python devs. This book bridges the gap between basic R programming and professional-grade code organization.</p>
</section>
<section id="how-to-use-this-guide" class="level2" data-number="1.3">
<h2 data-number="1.3" class="anchored" data-anchor-id="how-to-use-this-guide"><span class="header-section-number">1.3</span> How to Use This Guide</h2>
<p><em>Click <a href="https://joshuamarie.github.io/modules-in-r/">here</a> and it will send you to the actual book. </em></p>
<p>The content is structured progressively, building from foundational concepts to deep applications. For the best learning experience:</p>
<ol type="1">
<li>Start with the introduction to understand the core concepts</li>
<li>Learning the fundamentals of import system with <strong>{box}</strong> package</li>
<li>Learning the structures and constructions of reusability of modules, treating them like R packages, and supplied with documentation</li>
<li>Learning unit testing the modules</li>
</ol>
</section>
<section id="contributing" class="level2" data-number="1.4">
<h2 data-number="1.4" class="anchored" data-anchor-id="contributing"><span class="header-section-number">1.4</span> Contributing</h2>
<p>Contributions are welcome! If you have suggestions or improvements, please open an <a href="https://github.com/joshuamarie/modules-in-r/issues">issue</a> or submit a pull request on the <a href="https://github.com/joshuamarie/modules-in-r">GitHub repository</a>.</p>
</section>
<section id="license" class="level2" data-number="1.5">
<h2 data-number="1.5" class="anchored" data-anchor-id="license"><span class="header-section-number">1.5</span> License</h2>
<p>This book is licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0). See the <a href="https://github.com/joshuamarie/modules-in-r/blob/main/LICENSE.md">LICENSE</a> file for details.</p>
</section>
<section id="credits" class="level2" data-number="1.6">
<h2 data-number="1.6" class="anchored" data-anchor-id="credits"><span class="header-section-number">1.6</span> Credits</h2>
<p>This book was created by <a href="https://github.com/joshuamarie">Joshua Marie</a>.</p>
<p>Special thanks to:</p>
<ul>
<li><a href="https://github.com/klmr">Konrad Rudolph</a> for creating and maintaining the <a href="https://github.com/klmr/box">box</a> package</li>
<li>The R community for their continued support and feedback</li>
</ul>
<p>The book is built with <a href="https://quarto.org/">Quarto</a>, hosted on GitHub Pages.</p>


</section>
</section>

 ]]></description>
  <category>R</category>
  <category>book</category>
  <guid>https://joshuamarie.com/posts/03-modules-in-r/</guid>
  <pubDate>Tue, 07 Oct 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/03-modules-in-r/tmbnl.png" medium="image" type="image/png" height="166" width="144"/>
</item>
<item>
  <title>First level of time series modelling: Basic ARIMA model hyperparameter tuning and grid search in R</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/02-arima-grid-search/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/posts/02-arima-grid-search/&amp;text=Arima%20Grid%20Search%20in%20R%20-%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/posts/02-arima-grid-search/&amp;title=Arima%20Grid%20Search%20in%20R%20-%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Arima%20Grid%20Search%20in%20R%20-%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/posts/02-arima-grid-search/" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/posts/02-arima-grid-search/" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/posts/02-arima-grid-search/&amp;title=Arima%20Grid%20Search%20in%20R%20-%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/posts/02-arima-grid-search/&amp;title=Arima%20Grid%20Search%20in%20R%20-%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/posts/02-arima-grid-search/&amp;name=Arima%20Grid%20Search%20in%20R%20-%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Arima Grid Search in R - Joshua Marie https://joshuamarie.com/posts/02-arima-grid-search/'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/posts/02-arima-grid-search/%20Arima%20Grid%20Search%20in%20R%20-%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="exploring-and-visualizing-arima-models-in-r" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Exploring and Visualizing ARIMA Models in R</h1>
<p>How do you train ARIMA model in your time series models? Grid search, or Hyndman and Khandaka (2008) algorithm? I created this document to demonstrate you how to fit every possible ARIMA models using grid search with visualization. This is a basic problem of hyperparameter tuning. I prepare a simulated time series, and visualize the fitted values of every possible ARIMA models with ‘ggplot2’ and make it interactive with ggiraph.</p>
<hr>
<section id="introduction" class="level2" data-number="1.1"><h2 data-number="1.1" class="anchored" data-anchor-id="introduction">
<span class="header-section-number">1.1</span> Introduction</h2>
<p>The <strong>ARIMA (AutoRegressive Integrated Moving Average)</strong> model is defined by three parameters:</p>
<ul>
<li>
<strong>p</strong>: Autoregressive order that counts the past lagged terms (This is AR in ARIMA context)</li>
<li>
<strong>d</strong>: Differencing order that counts the number of differencing to achieve stationarity (This is ‘I’ or “integrate” in ARIMA context)</li>
<li>
<strong>q</strong>: Moving average order that counts the past error lagged terms (MA)</li>
</ul>
<p>Choosing the right combination of <code>(p, d, q)</code> is…not that easy, right when you want to achieve the best fit, even with Hyndman and Khandaka (2008) methodology with their <code><a href="https://pkg.robjhyndman.com/forecast/reference/auto.arima.html">forecast::auto.arima()</a></code>.</p>
<p>This is how it’s done:</p>
<ol type="1">
<li>Prepare a time series data. I generate a time series data from <a href="https://r-graph-gallery.com/279-plotting-time-series-with-ggplot2.html">this</a> in order for you to replicate this.<br>
</li>
<li>Then fit every possible ARIMA models across a <em>grid of (p, d, q) values</em>.<br>
</li>
<li>Then evaluate the models performance by calculating the maximum log-likelihood then weight them with <a href="https://en.wikipedia.org/wiki/Akaike_information_criterion">AIC (Akaike Information Criterion)</a> and <a href="https://en.wikipedia.org/wiki/Bayesian_information_criterion">BIC (Bayesian Information Criterion)</a>.<br>
</li>
<li>Then visualize the fitted values from every possible models, alongside the actual data.<br>
</li>
<li>From the visual, highlight the <em>best model</em> in red.<br>
</li>
<li>Optionally, you can make it <em>interactive</em>, using <a href="https://davidgohel.github.io/ggiraph/">‘ggiraph’</a>, and I prepare it so that you can hover and explore model fits.</li>
</ol>
<p>The packages used:</p>
<ol type="1">
<li><a href="https://github.com/klmr/box">box (v1.2.0)</a></li>
<li><a href="https://ggplot2.tidyverse.org/">ggplot2 (v4.0.0)</a></li>
<li><a href="https://davidgohel.github.io/ggiraph/">ggiraph (v0.9.1)</a></li>
<li><a href="https://purrr.tidyverse.org/">purrr (v1.0.2)</a></li>
<li><a href="https://dplyr.tidyverse.org/">dplyr (v1.1.4)</a></li>
<li><a href="https://otexts.com/fpp2/the-forecast-package-in-r.html">forecast (v8.23.0)</a></li>
<li><a href="https://glue.tidyverse.org/">glue (v1.7.0)</a></li>
<li><a href="https://tidyr.tidyverse.org/">tidyr (v1.3.1)</a></li>
<li><a href="https://rlang.r-lib.org/">rlang (v1.1.4)</a></li>
<li><a href="https://scales.r-lib.org/">scales (v1.4.0)</a></li>
</ol>
<hr></section><section id="simulating-data" class="level2" data-number="1.2"><h2 data-number="1.2" class="anchored" data-anchor-id="simulating-data">
<span class="header-section-number">1.2</span> Simulating Data</h2>
<p>We generate a synthetic dataset with some <em>trend</em> and <em>randomness</em>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</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;"><a href="https://rdrr.io/r/stats/Uniform.html">runif</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">140</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">224</span><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 class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</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;"><a href="https://rdrr.io/r/base/as.Date.html">as.Date</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2025-06-14"</span><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 class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">364</span></span></code></pre></div></div>
</div>
<p>This produces <strong>365 daily observations</strong> with both trend and noise, which is a good test case for ARIMA.</p>
<hr></section><section id="fitting-multiple-arima-models" class="level2" data-number="1.3"><h2 data-number="1.3" class="anchored" data-anchor-id="fitting-multiple-arima-models">
<span class="header-section-number">1.3</span> Fitting Multiple ARIMA Models</h2>
<p>We test a grid of ARIMA parameters:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Ap%20&amp;%5Cin%20%5C%7B0,1,2%5C%7D%20%5C%5C%0Ad%20&amp;%5Cin%20%5C%7B0,1%5C%7D%20%5C%5C%0Aq%20&amp;%5Cin%20%5C%7B0,1,2%5C%7D%0A%5Cend%7Baligned%7D%0A"></p>
<p>We exclude overly complex models where <code>p + q &gt; 3</code>.</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</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;"><a href="https://rdrr.io/r/base/eval.html">local</a></span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purrr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pmap</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pmap_chr</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">possibly</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_dbl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">transmute</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">slice_min</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">slice</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forecast</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Arima</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">expand_grid</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand_grid</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            models <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;">pmap_chr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pick</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" 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>
<span>                \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</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;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ARIMA({p},{d},{q})"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            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;">pmap</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pick</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="fl" 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>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">possibly</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</span><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>                        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" 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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/function.html">return</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Arima</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/ts.html">as.ts</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, order <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</span><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>                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>,</span>
<span>                    otherwise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span>                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            fits <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">res</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;">if</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;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><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 class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/fitted.values.html">fitted</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><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>            aic <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_dbl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">res</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;">if</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;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><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 class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/AIC.html">AIC</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><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>            bic <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_dbl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">res</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;">if</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;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><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 class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/AIC.html">BIC</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><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>        <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;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aic</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove failed models</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            day <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            is_lowest_aic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aic</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;"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aic</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            is_lowest_bic <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bic</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;"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bic</span>, na.rm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><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><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span></span></code></pre></div></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["models"],"name":[1],"type":["chr"],"align":["left"]},{"label":["res"],"name":[2],"type":["list"],"align":["right"]},{"label":["fits"],"name":[3],"type":["list"],"align":["right"]},{"label":["aic"],"name":[4],"type":["dbl"],"align":["right"]},{"label":["bic"],"name":[5],"type":["dbl"],"align":["right"]},{"label":["day"],"name":[6],"type":["list"],"align":["right"]},{"label":["is_lowest_aic"],"name":[7],"type":["lgl"],"align":["right"]},{"label":["is_lowest_bic"],"name":[8],"type":["lgl"],"align":["right"]}],"data":[{"1":"ARIMA(0,0,0)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1520.020","5":"1527.820","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(0,0,1)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1453.306","5":"1465.005","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(0,0,2)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1439.097","5":"1454.696","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(0,1,0)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1514.623","5":"1518.520","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(0,1,1)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1316.332","5":"1324.126","6":"<date [365]>","7":"TRUE","8":"TRUE"},{"1":"ARIMA(0,1,2)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1317.616","5":"1329.308","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(1,0,0)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1417.986","5":"1429.686","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(1,1,0)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1437.632","5":"1445.427","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(1,1,1)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1317.724","5":"1329.415","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(1,1,2)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1318.313","5":"1333.901","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(2,0,0)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1395.601","5":"1411.201","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(2,0,1)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1324.834","5":"1344.333","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(2,1,0)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1380.801","5":"1392.492","6":"<date [365]>","7":"FALSE","8":"FALSE"},{"1":"ARIMA(2,1,1)","2":"<S3: forecast_ARIMA>","3":"<ts>","4":"1318.031","5":"1333.619","6":"<date [365]>","7":"FALSE","8":"FALSE"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>This gives us a nested data frame of fitted models with their AIC, BIC, and fitted values.</p>
</section><section id="visualizing-models-with-ggplot2" class="level2" data-number="1.4"><h2 data-number="1.4" class="anchored" data-anchor-id="visualizing-models-with-ggplot2">
<span class="header-section-number">1.4</span> Visualizing Models with ggplot2</h2>
<p>Do you want to visualize everything better, including the actual, fitted values, and highlight the fitted values made by the best fit?</p>
<p>From <code>models</code> data, we just need the <code>is_lowest_aic</code> and <code>is_lowest_bic</code>. We just need to tweak the data a little bit here by expanding the fitted values with its corresponding data value. Then, set the <code>model_type</code> to condition the plotting data with <code><a href="https://dplyr.tidyverse.org/reference/case_when.html">dplyr::case_when()</a></code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_data</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;"><a href="https://rdrr.io/r/base/eval.html">local</a></span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">unnest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cols <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fits</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            model_type <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;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_lowest_aic</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Best AIC"</span>,</span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_lowest_bic</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Best BIC"</span>, </span>
<span>                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other Models"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><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></code></pre></div></div>
</div>
<p>Optionally:</p>
<ol type="1">
<li>
<p>You can put the information about the model which had the lowest AIC and BIC in an annotated box text in the plot.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">best_model_1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span> <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;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_lowest_aic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">best_model_2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span> <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;">dplyr</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_lowest_bic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
</li>
<li>
<p>Pointing out the maximum value of the time series data</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_val</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;"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_idx</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;"><a href="https://rdrr.io/r/base/which.min.html">which.max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_day</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_idx</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span></code></pre></div></div>
</div>
</li>
</ol>
<p>Then, visualize:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/eval.html">local</a></span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">scales</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">comma</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</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;">ggplot</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Original data</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <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;"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><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>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>,</span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span></span>
<span>        <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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <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;"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><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>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>, </span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span></span>
<span>        <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>        </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Other fitted models (light gray)</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            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;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_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;">"Other Models"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fits</span>, group <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#BDC3C7'</span>,</span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>,</span>
<span>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span>        <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>        </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Best BIC model (blue)</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            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;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_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;">"Best BIC"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fits</span>, group <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#3498DB'</span>,</span>
<span>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>,</span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span></span>
<span>        <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>        </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Best AIC model (red, on top)</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            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;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_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;">"Best AIC"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fits</span>, group <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#E74C3C'</span>,</span>
<span>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,</span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span></span>
<span>        <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>        </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Peak annotation</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"point"</span>,</span>
<span>            x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_val</span>,</span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E74C3C"</span>, shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, stroke <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span></span>
<span>        <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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, </span>
<span>            x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_day</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>,</span>
<span>            y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_val</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span>            label <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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Peak: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_val</span>, <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" sec"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E74C3C"</span>,</span>
<span>            fontface <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span></span>
<span>        <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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"curve"</span>, </span>
<span>            x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_day</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, xend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_day</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, </span>
<span>            y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_val</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, yend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_val</span>, </span>
<span>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E74C3C"</span>, </span>
<span>            curvature <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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span>            arrow <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;">arrow</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>length <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;">unit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <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>        </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model performance text box</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rect"</span>,</span>
<span>            xmin <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;"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, xmax <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;"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span>            ymin <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;"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, ymax <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;"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span>            fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#34495E"</span>, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span></span>
<span>        <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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span>            x <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;"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, y <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;"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span>            label <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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Best AIC: "</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">best_model_1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, </span>
<span>                           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\nAIC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">best_model_1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aic</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E74C3C"</span>, fontface <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span></span>
<span>        <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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>,</span>
<span>            x <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;"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, y <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;"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span>            label <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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Best BIC: "</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">best_model_2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>,</span>
<span>                           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\nBIC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">best_model_2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bic</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#3498DB"</span>, fontface <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span></span>
<span>        <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>        </span>
<span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Styling</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>date_labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b %Y"</span>, date_breaks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2 months"</span><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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">comma</span><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>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time Index"</span>, </span>
<span>            y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated Response Values"</span>, </span>
<span>            title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ARIMA Model Grid Search: Simulated Time Series Analysis"</span>,</span>
<span>            subtitle <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;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comparing {nrow(models)} successful ARIMA models • Best performing models highlighted"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            caption <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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Models tested: p∈[0,2], d∈[0,1], q∈[0,2] • "</span>,</span>
<span>                             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original data shown in dark gray • "</span>,</span>
<span>                             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total observations: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><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>        <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>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, base_family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span><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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            plot.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span>, </span>
<span>                colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>,</span>
<span>                face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            plot.subtitle <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span>,</span>
<span>                colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7F8C8D"</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            plot.caption <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span>,</span>
<span>                colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#95A5A6"</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            axis.text.x <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                angle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, </span>
<span>                hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>                color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#34495E"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            axis.text.y <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>                color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#34495E"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            axis.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>,</span>
<span>                face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            panel.grid.minor <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;">element_blank</span><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>            panel.grid.major <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;">element_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ECF0F1"</span>, </span>
<span>                linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            panel.background <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;">element_rect</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FEFEFE"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            plot.background <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;">element_rect</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            plot.margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span></span>
<span><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></code></pre></div></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://joshuamarie.com/posts/02-arima-grid-search/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>We visualize:</p>
<ol type="1">
<li>Original data (dark gray)</li>
<li>All fitted values from every possible ARIMA model (light gray), except, the fitted values from the best fit is highlighted (red, blue, based on AIC, BIC, respectively)</li>
<li>Annotated <em>peak point</em> in the data</li>
<li>Annotated best AIC/BIC values</li>
</ol>
<hr></section><section id="optional-interactive-visualization-with-ggiraph" class="level2" data-number="1.5"><h2 data-number="1.5" class="anchored" data-anchor-id="optional-interactive-visualization-with-ggiraph">
<span class="header-section-number">1.5</span> Optional: Interactive Visualization with ggiraph</h2>
<p>If you prefer your plot to be interactive like some figures in the website, use ‘ggiraph’ interactive interface version of ggplot2, then <code>girafe()</code>. The output produced by <code>girafe()</code> is wrapped with HTML, so it can be run in web.</p>
<p><em>I recommend ‘ggiraph’ to build web applications in R</em>.</p>
<p>This is the interactive version of the plot above:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/eval.html">local</a></span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggplot2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">...</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">scales</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">comma</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dplyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">filter</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mutate</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ggiraph</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_line_interactive</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geom_point_interactive</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">girafe</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">opts_hover</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">opts_hover_inv</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">opts_selection</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tidyr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">unnest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use this for preparation</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">original_data</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;"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        day <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>,</span>
<span>        ts_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span>,</span>
<span>        tooltip_line <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"actual data"</span>,</span>
<span>        tooltip_point <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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></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;"><a href="https://rdrr.io/r/base/format.html">format</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; readings: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span>, <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" secs"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cols <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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fits</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            model_type <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;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_lowest_aic</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Best AIC"</span>,</span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_lowest_bic</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Best BIC"</span>, </span>
<span>                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other Models"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create tooltip text for model lines</span></span>
<span>            tooltip_text <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;">case_when</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_lowest_aic</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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"best model: "</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; AIC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aic</span>, <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; BIC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bic</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_lowest_bic</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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"best model: "</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; AIC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aic</span>, <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; BIC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bic</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"model: "</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; AIC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">aic</span>, <span class="fl" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; BIC = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">round</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bic</span>, <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>  </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</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;">ggplot</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line_interactive</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">original_data</span>,</span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span>, tooltip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tooltip_line</span>, data_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"original_line"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>,</span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span></span>
<span>        <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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point_interactive</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">original_data</span>,</span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span>, tooltip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tooltip_point</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>, </span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span></span>
<span>        <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>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line_interactive</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            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;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_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;">"Other Models"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fits</span>, group <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, tooltip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tooltip_text</span>, data_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#BDC3C7'</span>,</span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>,</span>
<span>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span>        <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>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line_interactive</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            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;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_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;">"Best BIC"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fits</span>, group <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, tooltip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tooltip_text</span>, data_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#3498DB'</span>,</span>
<span>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>,</span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span></span>
<span>        <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>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line_interactive</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            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;"><a href="https://rdrr.io/r/stats/filter.html">filter</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot_data</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">model_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;">"Best AIC"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">day</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fits</span>, group <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span>, tooltip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tooltip_text</span>, data_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">models</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#E74C3C'</span>,</span>
<span>            linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>,</span>
<span>            alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span></span>
<span>        <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>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>date_labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b %Y"</span>, date_breaks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2 months"</span><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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">comma</span><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>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time Index"</span>, </span>
<span>            y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated Response Values"</span>, </span>
<span>            title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ARIMA Model Grid Search: Simulated Time Series Analysis"</span>,</span>
<span>            subtitle <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;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comparing {nrow(models)} successful ARIMA models • Best performing models highlighted"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            caption <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;"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Models tested: p∈[0,2], d∈[0,1], q∈[0,2] • "</span>,</span>
<span>                             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original data shown in dark gray • "</span>,</span>
<span>                             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total observations: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ts_sim</span><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>        <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>        </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>base_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, base_family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span><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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            plot.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span>, </span>
<span>                colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>,</span>
<span>                face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            plot.subtitle <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span>,</span>
<span>                colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7F8C8D"</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            plot.caption <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"serif"</span>,</span>
<span>                colour <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#95A5A6"</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            axis.text.x <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                angle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, </span>
<span>                hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>                color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#34495E"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            axis.text.y <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" 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>
<span>                color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#34495E"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            axis.title <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;">element_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2C3E50"</span>,</span>
<span>                face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            panel.grid.minor <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;">element_blank</span><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>            panel.grid.major <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;">element_line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>                color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ECF0F1"</span>, </span>
<span>                linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            panel.background <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;">element_rect</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FEFEFE"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            plot.background <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;">element_rect</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            plot.margin <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;">margin</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">interactive_plot</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;">girafe</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        ggobj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span>,</span>
<span>        options <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts_hover</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>css <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cursor:pointer;stroke-width:4;stroke-opacity:1;fill-opacity:1;r:4px;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts_hover_inv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>css <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opacity:0.1;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts_selection</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">interactive_plot</span></span>
<span><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></code></pre></div></div>
</details><div class="cell-output-display">
<div class="girafe html-widget html-fill-item" id="htmlwidget-b0818c17b0fec2d181a4" style="width:100%;height:390px;"></div>
<script type="application/json" data-for="htmlwidget-b0818c17b0fec2d181a4">{"x":{"html":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='ggiraph-svg' role='graphics-document' id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6' viewBox='0 0 720 432'>\n <defs id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_defs'>\n  <clipPath id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_c1'>\n   <rect x='0' y='0' width='720' height='432'/>\n  <\/clipPath>\n  <clipPath id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_c2'>\n   <rect x='52.66' y='60.92' width='647.42' height='285.97'/>\n  <\/clipPath>\n <\/defs>\n <g id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_rootg' class='ggiraph-svg-rootg'>\n  <g clip-path='url(#svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_c1)'>\n   <rect x='0' y='0' width='720' height='432' fill='#FFFFFF' fill-opacity='1' stroke='#FFFFFF' stroke-opacity='1' stroke-width='0.75' stroke-linejoin='round' stroke-linecap='round' class='ggiraph-svg-bg'/>\n   <rect x='0' y='0' width='720' height='432' fill='#FFFFFF' fill-opacity='1' stroke='none'/>\n  <\/g>\n  <g clip-path='url(#svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_c2)'>\n   <rect x='52.66' y='60.92' width='647.42' height='285.97' fill='#FEFEFE' fill-opacity='1' stroke='none'/>\n   <polyline points='52.66,335.33 700.07,335.33' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='52.66,266.91 700.07,266.91' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='52.66,198.50 700.07,198.50' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='52.66,130.08 700.07,130.08' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='52.66,61.66 700.07,61.66' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='107.96,346.88 107.96,60.92' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='208.21,346.88 208.21,60.92' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='306.84,346.88 306.84,60.92' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='405.47,346.88 405.47,60.92' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='500.87,346.88 500.87,60.92' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='599.50,346.88 599.50,60.92' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='698.13,346.88 698.13,60.92' fill='none' stroke='#ECF0F1' stroke-opacity='1' stroke-width='0.64' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e1' points='82.09,148.30 83.70,157.08 85.32,77.75 86.94,175.28 88.56,183.20 90.17,89.79 91.79,161.03 93.41,149.14 95.02,116.14 96.64,73.92 98.26,124.90 99.87,188.86 101.49,141.85 103.11,81.11 104.72,195.96 106.34,130.50 107.96,185.56 109.58,164.93 111.19,84.64 112.81,169.66 114.43,154.88 116.04,191.76 117.66,151.68 119.28,208.43 120.89,169.11 122.51,93.15 124.13,139.93 125.74,141.35 127.36,134.77 128.98,163.87 130.60,189.33 132.21,119.27 133.83,133.49 135.45,196.32 137.06,152.08 138.68,112.36 140.30,192.30 141.91,234.86 143.53,103.90 145.15,237.05 146.76,228.47 148.38,147.35 150.00,224.56 151.62,205.18 153.23,129.84 154.85,130.39 156.47,200.18 158.08,217.06 159.70,136.21 161.32,141.67 162.93,203.93 164.55,245.86 166.17,156.77 167.78,191.72 169.40,202.93 171.02,189.81 172.64,196.18 174.25,257.88 175.87,238.48 177.49,195.02 179.10,175.50 180.72,162.81 182.34,156.91 183.95,263.10 185.57,157.91 187.19,156.84 188.80,253.46 190.42,253.70 192.04,134.34 193.66,157.84 195.27,167.22 196.89,150.79 198.51,262.26 200.12,259.15 201.74,233.01 203.36,182.86 204.97,267.91 206.59,245.28 208.21,268.11 209.82,223.62 211.44,270.65 213.06,248.58 214.68,225.88 216.29,262.16 217.91,177.89 219.53,193.67 221.14,273.03 222.76,220.14 224.38,151.73 225.99,184.86 227.61,180.78 229.23,264.45 230.84,277.79 232.46,287.50 234.08,169.89 235.70,220.99 237.31,287.75 238.93,243.66 240.55,245.86 242.16,213.68 243.78,222.58 245.40,163.91 247.01,202.35 248.63,174.58 250.25,169.25 251.86,177.79 253.48,232.70 255.10,242.53 256.72,275.51 258.33,191.68 259.95,227.10 261.57,263.78 263.18,219.85 264.80,271.26 266.42,267.02 268.03,203.38 269.65,171.04 271.27,171.36 272.88,249.99 274.50,206.01 276.12,263.05 277.74,219.67 279.35,283.36 280.97,216.79 282.59,233.14 284.20,198.57 285.82,288.13 287.44,195.74 289.05,283.05 290.67,256.29 292.29,275.45 293.90,303.66 295.52,237.99 297.14,254.04 298.76,228.55 300.37,188.36 301.99,218.59 303.61,237.95 305.22,249.70 306.84,261.48 308.46,308.79 310.07,222.56 311.69,217.26 313.31,185.86 314.92,279.15 316.54,192.52 318.16,206.35 319.77,263.15 321.39,271.37 323.01,224.04 324.63,313.82 326.24,228.47 327.86,291.55 329.48,286.92 331.09,255.96 332.71,298.74 334.33,299.32 335.94,283.60 337.56,273.59 339.18,203.32 340.79,269.03 342.41,253.99 344.03,242.52 345.65,193.46 347.26,292.81 348.88,247.42 350.50,305.28 352.11,236.23 353.73,271.08 355.35,263.21 356.96,199.09 358.58,242.81 360.20,193.06 361.81,284.22 363.43,290.97 365.05,205.35 366.67,204.07 368.28,252.03 369.90,293.27 371.52,263.93 373.13,248.70 374.75,294.01 376.37,233.58 377.98,287.97 379.60,216.04 381.22,251.56 382.83,211.69 384.45,259.08 386.07,280.85 387.69,247.41 389.30,240.72 390.92,230.19 392.54,214.35 394.15,307.54 395.77,246.66 397.39,296.67 399.00,276.71 400.62,303.08 402.24,232.18 403.85,260.59 405.47,285.12 407.09,282.61 408.71,245.06 410.32,285.70 411.94,265.50 413.56,304.51 415.17,273.58 416.79,304.86 418.41,256.37 420.02,281.35 421.64,319.59 423.26,301.14 424.87,281.86 426.49,266.92 428.11,219.10 429.73,234.93 431.34,301.53 432.96,324.70 434.58,302.51 436.19,303.63 437.81,219.97 439.43,310.15 441.04,333.89 442.66,279.30 444.28,292.68 445.89,275.20 447.51,201.25 449.13,227.71 450.75,222.83 452.36,244.97 453.98,263.92 455.60,234.34 457.21,243.07 458.83,213.13 460.45,250.32 462.06,240.58 463.68,315.52 465.30,322.41 466.91,313.69 468.53,200.04 470.15,284.07 471.77,304.47 473.38,292.34 475.00,290.58 476.62,245.58 478.23,278.77 479.85,253.89 481.47,203.33 483.08,258.59 484.70,314.15 486.32,234.88 487.93,203.65 489.55,324.87 491.17,291.81 492.79,204.89 494.40,312.57 496.02,276.33 497.64,249.05 499.25,207.04 500.87,210.15 502.49,265.70 504.10,200.98 505.72,264.52 507.34,285.62 508.95,248.85 510.57,260.74 512.19,266.63 513.81,317.46 515.42,222.99 517.04,304.10 518.66,285.70 520.27,239.42 521.89,282.02 523.51,239.39 525.12,310.60 526.74,304.26 528.36,206.63 529.97,205.44 531.59,192.57 533.21,267.54 534.83,312.67 536.44,218.59 538.06,268.97 539.68,234.39 541.29,292.14 542.91,309.94 544.53,276.67 546.14,240.62 547.76,272.15 549.38,293.65 550.99,258.37 552.61,322.96 554.23,225.50 555.85,236.20 557.46,218.68 559.08,261.35 560.70,212.45 562.31,209.58 563.93,209.50 565.55,258.56 567.16,208.05 568.78,281.56 570.40,266.14 572.01,305.27 573.63,226.80 575.25,266.13 576.87,194.46 578.48,213.40 580.10,298.57 581.72,287.30 583.33,238.33 584.95,297.93 586.57,204.82 588.18,253.14 589.80,306.89 591.42,195.29 593.03,275.77 594.65,247.90 596.27,279.26 597.89,291.64 599.50,289.25 601.12,259.11 602.74,252.43 604.35,251.78 605.97,288.42 607.59,275.72 609.20,263.32 610.82,276.68 612.44,201.95 614.05,239.78 615.67,301.21 617.29,195.15 618.91,208.90 620.52,179.35 622.14,170.44 623.76,281.48 625.37,261.43 626.99,219.09 628.61,225.32 630.22,202.19 631.84,208.78 633.46,161.82 635.07,209.58 636.69,201.78 638.31,174.20 639.93,164.65 641.54,249.73 643.16,288.17 644.78,259.58 646.39,169.44 648.01,277.80 649.63,212.83 651.24,197.78 652.86,227.76 654.48,158.15 656.09,225.88 657.71,212.19 659.33,164.81 660.95,213.93 662.56,279.22 664.18,156.02 665.80,163.14 667.41,227.25 669.03,174.59 670.65,242.34' fill='none' stroke='#2C3E50' stroke-opacity='0.8' stroke-width='2.56' stroke-linejoin='round' stroke-linecap='butt' title='actual data' data-id='original_line'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e2' cx='670.65' cy='242.34' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-14; readings: 8.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e3' cx='669.03' cy='174.59' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-13; readings: 10.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e4' cx='667.41' cy='227.25' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-12; readings: 8.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e5' cx='665.8' cy='163.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-11; readings: 11.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e6' cx='664.18' cy='156.02' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-10; readings: 11.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e7' cx='662.56' cy='279.22' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-09; readings: 7.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e8' cx='660.95' cy='213.93' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-08; readings: 9.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e9' cx='659.33' cy='164.81' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-07; readings: 11.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e10' cx='657.71' cy='212.19' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-06; readings: 9.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e11' cx='656.09' cy='225.88' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-05; readings: 9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e12' cx='654.48' cy='158.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-04; readings: 11.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e13' cx='652.86' cy='227.76' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-03; readings: 8.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e14' cx='651.24' cy='197.78' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-02; readings: 10 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e15' cx='649.63' cy='212.83' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-06-01; readings: 9.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e16' cx='648.01' cy='277.8' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-31; readings: 7.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e17' cx='646.39' cy='169.44' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-30; readings: 11.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e18' cx='644.78' cy='259.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-29; readings: 7.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e19' cx='643.16' cy='288.17' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-28; readings: 6.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e20' cx='641.54' cy='249.73' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-27; readings: 8.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e21' cx='639.93' cy='164.65' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-26; readings: 11.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e22' cx='638.31' cy='174.2' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-25; readings: 10.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e23' cx='636.69' cy='201.78' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-24; readings: 9.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e24' cx='635.07' cy='209.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-23; readings: 9.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e25' cx='633.46' cy='161.82' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-22; readings: 11.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e26' cx='631.84' cy='208.78' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-21; readings: 9.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e27' cx='630.22' cy='202.19' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-20; readings: 9.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e28' cx='628.61' cy='225.32' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-19; readings: 9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e29' cx='626.99' cy='219.09' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-18; readings: 9.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e30' cx='625.37' cy='261.43' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-17; readings: 7.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e31' cx='623.76' cy='281.48' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-16; readings: 7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e32' cx='622.14' cy='170.44' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-15; readings: 11 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e33' cx='620.52' cy='179.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-14; readings: 10.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e34' cx='618.91' cy='208.9' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-13; readings: 9.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e35' cx='617.29' cy='195.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-12; readings: 10.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e36' cx='615.67' cy='301.21' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-11; readings: 6.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e37' cx='614.05' cy='239.78' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-10; readings: 8.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e38' cx='612.44' cy='201.95' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-09; readings: 9.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e39' cx='610.82' cy='276.68' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-08; readings: 7.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e40' cx='609.2' cy='263.32' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-07; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e41' cx='607.59' cy='275.72' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-06; readings: 7.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e42' cx='605.97' cy='288.42' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-05; readings: 6.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e43' cx='604.35' cy='251.78' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-04; readings: 8.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e44' cx='602.74' cy='252.43' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-03; readings: 8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e45' cx='601.12' cy='259.11' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-02; readings: 7.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e46' cx='599.5' cy='289.25' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-05-01; readings: 6.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e47' cx='597.89' cy='291.64' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-30; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e48' cx='596.27' cy='279.26' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-29; readings: 7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e49' cx='594.65' cy='247.9' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-28; readings: 8.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e50' cx='593.03' cy='275.77' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-27; readings: 7.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e51' cx='591.42' cy='195.29' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-26; readings: 10.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e52' cx='589.8' cy='306.89' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-25; readings: 6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e53' cx='588.18' cy='253.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-24; readings: 8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e54' cx='586.57' cy='204.82' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-23; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e55' cx='584.95' cy='297.93' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-22; readings: 6.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e56' cx='583.33' cy='238.33' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-21; readings: 8.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e57' cx='581.72' cy='287.3' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-20; readings: 6.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e58' cx='580.1' cy='298.57' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-19; readings: 6.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e59' cx='578.48' cy='213.4' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-18; readings: 9.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e60' cx='576.87' cy='194.46' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-17; readings: 10.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e61' cx='575.25' cy='266.13' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-16; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e62' cx='573.63' cy='226.8' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-15; readings: 9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e63' cx='572.01' cy='305.27' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-14; readings: 6.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e64' cx='570.4' cy='266.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-13; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e65' cx='568.78' cy='281.56' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-12; readings: 7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e66' cx='567.16' cy='208.05' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-11; readings: 9.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e67' cx='565.55' cy='258.56' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-10; readings: 7.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e68' cx='563.93' cy='209.5' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-09; readings: 9.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e69' cx='562.31' cy='209.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-08; readings: 9.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e70' cx='560.7' cy='212.45' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-07; readings: 9.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e71' cx='559.08' cy='261.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-06; readings: 7.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e72' cx='557.46' cy='218.68' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-05; readings: 9.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e73' cx='555.85' cy='236.2' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-04; readings: 8.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e74' cx='554.23' cy='225.5' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-03; readings: 9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e75' cx='552.61' cy='322.96' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-02; readings: 5.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e76' cx='550.99' cy='258.37' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-04-01; readings: 7.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e77' cx='549.38' cy='293.65' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-31; readings: 6.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e78' cx='547.76' cy='272.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-30; readings: 7.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e79' cx='546.14' cy='240.62' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-29; readings: 8.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e80' cx='544.53' cy='276.67' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-28; readings: 7.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e81' cx='542.91' cy='309.94' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-27; readings: 5.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e82' cx='541.29' cy='292.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-26; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e83' cx='539.68' cy='234.39' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-25; readings: 8.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e84' cx='538.06' cy='268.97' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-24; readings: 7.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e85' cx='536.44' cy='218.59' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-23; readings: 9.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e86' cx='534.83' cy='312.67' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-22; readings: 5.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e87' cx='533.21' cy='267.54' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-21; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e88' cx='531.59' cy='192.57' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-20; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e89' cx='529.97' cy='205.44' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-19; readings: 9.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e90' cx='528.36' cy='206.63' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-18; readings: 9.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e91' cx='526.74' cy='304.26' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-17; readings: 6.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e92' cx='525.12' cy='310.6' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-16; readings: 5.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e93' cx='523.51' cy='239.39' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-15; readings: 8.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e94' cx='521.89' cy='282.02' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-14; readings: 6.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e95' cx='520.27' cy='239.42' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-13; readings: 8.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e96' cx='518.66' cy='285.7' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-12; readings: 6.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e97' cx='517.04' cy='304.1' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-11; readings: 6.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e98' cx='515.42' cy='222.99' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-10; readings: 9.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e99' cx='513.81' cy='317.46' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-09; readings: 5.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e100' cx='512.19' cy='266.63' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-08; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e101' cx='510.57' cy='260.74' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-07; readings: 7.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e102' cx='508.95' cy='248.85' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-06; readings: 8.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e103' cx='507.34' cy='285.62' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-05; readings: 6.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e104' cx='505.72' cy='264.52' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-04; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e105' cx='504.1' cy='200.98' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-03; readings: 9.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e106' cx='502.49' cy='265.7' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-02; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e107' cx='500.87' cy='210.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-03-01; readings: 9.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e108' cx='499.25' cy='207.04' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-28; readings: 9.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e109' cx='497.64' cy='249.05' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-27; readings: 8.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e110' cx='496.02' cy='276.33' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-26; readings: 7.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e111' cx='494.4' cy='312.57' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-25; readings: 5.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e112' cx='492.79' cy='204.89' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-24; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e113' cx='491.17' cy='291.81' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-23; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e114' cx='489.55' cy='324.87' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-22; readings: 5.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e115' cx='487.93' cy='203.65' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-21; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e116' cx='486.32' cy='234.88' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-20; readings: 8.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e117' cx='484.7' cy='314.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-19; readings: 5.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e118' cx='483.08' cy='258.59' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-18; readings: 7.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e119' cx='481.47' cy='203.33' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-17; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e120' cx='479.85' cy='253.89' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-16; readings: 8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e121' cx='478.23' cy='278.77' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-15; readings: 7.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e122' cx='476.62' cy='245.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-14; readings: 8.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e123' cx='475' cy='290.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-13; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e124' cx='473.38' cy='292.34' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-12; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e125' cx='471.77' cy='304.47' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-11; readings: 6.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e126' cx='470.15' cy='284.07' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-10; readings: 6.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e127' cx='468.53' cy='200.04' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-09; readings: 9.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e128' cx='466.91' cy='313.69' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-08; readings: 5.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e129' cx='465.3' cy='322.41' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-07; readings: 5.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e130' cx='463.68' cy='315.52' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-06; readings: 5.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e131' cx='462.06' cy='240.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-05; readings: 8.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e132' cx='460.45' cy='250.32' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-04; readings: 8.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e133' cx='458.83' cy='213.13' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-03; readings: 9.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e134' cx='457.21' cy='243.07' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-02; readings: 8.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e135' cx='455.6' cy='234.34' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-02-01; readings: 8.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e136' cx='453.98' cy='263.92' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-31; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e137' cx='452.36' cy='244.97' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-30; readings: 8.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e138' cx='450.75' cy='222.83' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-29; readings: 9.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e139' cx='449.13' cy='227.71' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-28; readings: 8.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e140' cx='447.51' cy='201.25' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-27; readings: 9.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e141' cx='445.89' cy='275.2' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-26; readings: 7.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e142' cx='444.28' cy='292.68' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-25; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e143' cx='442.66' cy='279.3' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-24; readings: 7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e144' cx='441.04' cy='333.89' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-23; readings: 5.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e145' cx='439.43' cy='310.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-22; readings: 5.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e146' cx='437.81' cy='219.97' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-21; readings: 9.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e147' cx='436.19' cy='303.63' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-20; readings: 6.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e148' cx='434.58' cy='302.51' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-19; readings: 6.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e149' cx='432.96' cy='324.7' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-18; readings: 5.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e150' cx='431.34' cy='301.53' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-17; readings: 6.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e151' cx='429.73' cy='234.93' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-16; readings: 8.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e152' cx='428.11' cy='219.1' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-15; readings: 9.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e153' cx='426.49' cy='266.92' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-14; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e154' cx='424.87' cy='281.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-13; readings: 7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e155' cx='423.26' cy='301.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-12; readings: 6.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e156' cx='421.64' cy='319.59' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-11; readings: 5.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e157' cx='420.02' cy='281.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-10; readings: 7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e158' cx='418.41' cy='256.37' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-09; readings: 7.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e159' cx='416.79' cy='304.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-08; readings: 6.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e160' cx='415.17' cy='273.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-07; readings: 7.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e161' cx='413.56' cy='304.51' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-06; readings: 6.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e162' cx='411.94' cy='265.5' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-05; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e163' cx='410.32' cy='285.7' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-04; readings: 6.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e164' cx='408.71' cy='245.06' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-03; readings: 8.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e165' cx='407.09' cy='282.61' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-02; readings: 6.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e166' cx='405.47' cy='285.12' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2025-01-01; readings: 6.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e167' cx='403.85' cy='260.59' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-31; readings: 7.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e168' cx='402.24' cy='232.18' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-30; readings: 8.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e169' cx='400.62' cy='303.08' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-29; readings: 6.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e170' cx='399' cy='276.71' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-28; readings: 7.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e171' cx='397.39' cy='296.67' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-27; readings: 6.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e172' cx='395.77' cy='246.66' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-26; readings: 8.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e173' cx='394.15' cy='307.54' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-25; readings: 6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e174' cx='392.54' cy='214.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-24; readings: 9.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e175' cx='390.92' cy='230.19' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-23; readings: 8.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e176' cx='389.3' cy='240.72' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-22; readings: 8.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e177' cx='387.69' cy='247.41' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-21; readings: 8.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e178' cx='386.07' cy='280.85' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-20; readings: 7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e179' cx='384.45' cy='259.08' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-19; readings: 7.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e180' cx='382.83' cy='211.69' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-18; readings: 9.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e181' cx='381.22' cy='251.56' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-17; readings: 8.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e182' cx='379.6' cy='216.04' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-16; readings: 9.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e183' cx='377.98' cy='287.97' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-15; readings: 6.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e184' cx='376.37' cy='233.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-14; readings: 8.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e185' cx='374.75' cy='294.01' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-13; readings: 6.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e186' cx='373.13' cy='248.7' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-12; readings: 8.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e187' cx='371.52' cy='263.93' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-11; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e188' cx='369.9' cy='293.27' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-10; readings: 6.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e189' cx='368.28' cy='252.03' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-09; readings: 8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e190' cx='366.67' cy='204.07' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-08; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e191' cx='365.05' cy='205.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-07; readings: 9.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e192' cx='363.43' cy='290.97' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-06; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e193' cx='361.81' cy='284.22' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-05; readings: 6.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e194' cx='360.2' cy='193.06' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-04; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e195' cx='358.58' cy='242.81' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-03; readings: 8.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e196' cx='356.96' cy='199.09' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-02; readings: 10 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e197' cx='355.35' cy='263.21' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-12-01; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e198' cx='353.73' cy='271.08' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-30; readings: 7.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e199' cx='352.11' cy='236.23' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-29; readings: 8.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e200' cx='350.5' cy='305.28' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-28; readings: 6.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e201' cx='348.88' cy='247.42' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-27; readings: 8.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e202' cx='347.26' cy='292.81' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-26; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e203' cx='345.65' cy='193.46' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-25; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e204' cx='344.03' cy='242.52' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-24; readings: 8.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e205' cx='342.41' cy='253.99' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-23; readings: 8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e206' cx='340.79' cy='269.03' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-22; readings: 7.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e207' cx='339.18' cy='203.32' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-21; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e208' cx='337.56' cy='273.59' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-20; readings: 7.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e209' cx='335.94' cy='283.6' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-19; readings: 6.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e210' cx='334.33' cy='299.32' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-18; readings: 6.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e211' cx='332.71' cy='298.74' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-17; readings: 6.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e212' cx='331.09' cy='255.96' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-16; readings: 7.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e213' cx='329.48' cy='286.92' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-15; readings: 6.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e214' cx='327.86' cy='291.55' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-14; readings: 6.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e215' cx='326.24' cy='228.47' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-13; readings: 8.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e216' cx='324.63' cy='313.82' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-12; readings: 5.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e217' cx='323.01' cy='224.04' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-11; readings: 9.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e218' cx='321.39' cy='271.37' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-10; readings: 7.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e219' cx='319.77' cy='263.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-09; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e220' cx='318.16' cy='206.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-08; readings: 9.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e221' cx='316.54' cy='192.52' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-07; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e222' cx='314.92' cy='279.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-06; readings: 7.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e223' cx='313.31' cy='185.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-05; readings: 10.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e224' cx='311.69' cy='217.26' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-04; readings: 9.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e225' cx='310.07' cy='222.56' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-03; readings: 9.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e226' cx='308.46' cy='308.79' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-02; readings: 6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e227' cx='306.84' cy='261.48' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-11-01; readings: 7.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e228' cx='305.22' cy='249.7' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-31; readings: 8.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e229' cx='303.61' cy='237.95' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-30; readings: 8.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e230' cx='301.99' cy='218.59' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-29; readings: 9.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e231' cx='300.37' cy='188.36' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-28; readings: 10.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e232' cx='298.76' cy='228.55' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-27; readings: 8.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e233' cx='297.14' cy='254.04' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-26; readings: 8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e234' cx='295.52' cy='237.99' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-25; readings: 8.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e235' cx='293.9' cy='303.66' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-24; readings: 6.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e236' cx='292.29' cy='275.45' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-23; readings: 7.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e237' cx='290.67' cy='256.29' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-22; readings: 7.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e238' cx='289.05' cy='283.05' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-21; readings: 6.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e239' cx='287.44' cy='195.74' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-20; readings: 10.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e240' cx='285.82' cy='288.13' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-19; readings: 6.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e241' cx='284.2' cy='198.57' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-18; readings: 10 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e242' cx='282.59' cy='233.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-17; readings: 8.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e243' cx='280.97' cy='216.79' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-16; readings: 9.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e244' cx='279.35' cy='283.36' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-15; readings: 6.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e245' cx='277.74' cy='219.67' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-14; readings: 9.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e246' cx='276.12' cy='263.05' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-13; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e247' cx='274.5' cy='206.01' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-12; readings: 9.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e248' cx='272.88' cy='249.99' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-11; readings: 8.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e249' cx='271.27' cy='171.36' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-10; readings: 11 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e250' cx='269.65' cy='171.04' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-09; readings: 11 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e251' cx='268.03' cy='203.38' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-08; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e252' cx='266.42' cy='267.02' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-07; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e253' cx='264.8' cy='271.26' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-06; readings: 7.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e254' cx='263.18' cy='219.85' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-05; readings: 9.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e255' cx='261.57' cy='263.78' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-04; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e256' cx='259.95' cy='227.1' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-03; readings: 9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e257' cx='258.33' cy='191.68' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-02; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e258' cx='256.72' cy='275.51' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-10-01; readings: 7.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e259' cx='255.1' cy='242.53' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-30; readings: 8.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e260' cx='253.48' cy='232.7' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-29; readings: 8.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e261' cx='251.86' cy='177.79' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-28; readings: 10.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e262' cx='250.25' cy='169.25' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-27; readings: 11.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e263' cx='248.63' cy='174.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-26; readings: 10.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e264' cx='247.01' cy='202.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-25; readings: 9.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e265' cx='245.4' cy='163.91' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-24; readings: 11.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e266' cx='243.78' cy='222.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-23; readings: 9.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e267' cx='242.16' cy='213.68' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-22; readings: 9.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e268' cx='240.55' cy='245.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-21; readings: 8.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e269' cx='238.93' cy='243.66' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-20; readings: 8.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e270' cx='237.31' cy='287.75' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-19; readings: 6.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e271' cx='235.7' cy='220.99' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-18; readings: 9.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e272' cx='234.08' cy='169.89' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-17; readings: 11 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e273' cx='232.46' cy='287.5' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-16; readings: 6.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e274' cx='230.84' cy='277.79' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-15; readings: 7.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e275' cx='229.23' cy='264.45' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-14; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e276' cx='227.61' cy='180.78' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-13; readings: 10.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e277' cx='225.99' cy='184.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-12; readings: 10.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e278' cx='224.38' cy='151.73' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-11; readings: 11.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e279' cx='222.76' cy='220.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-10; readings: 9.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e280' cx='221.14' cy='273.03' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-09; readings: 7.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e281' cx='219.53' cy='193.67' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-08; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e282' cx='217.91' cy='177.89' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-07; readings: 10.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e283' cx='216.29' cy='262.16' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-06; readings: 7.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e284' cx='214.68' cy='225.88' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-05; readings: 9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e285' cx='213.06' cy='248.58' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-04; readings: 8.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e286' cx='211.44' cy='270.65' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-03; readings: 7.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e287' cx='209.82' cy='223.62' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-02; readings: 9.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e288' cx='208.21' cy='268.11' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-09-01; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e289' cx='206.59' cy='245.28' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-31; readings: 8.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e290' cx='204.97' cy='267.91' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-30; readings: 7.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e291' cx='203.36' cy='182.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-29; readings: 10.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e292' cx='201.74' cy='233.01' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-28; readings: 8.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e293' cx='200.12' cy='259.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-27; readings: 7.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e294' cx='198.51' cy='262.26' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-26; readings: 7.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e295' cx='196.89' cy='150.79' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-25; readings: 11.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e296' cx='195.27' cy='167.22' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-24; readings: 11.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e297' cx='193.66' cy='157.84' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-23; readings: 11.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e298' cx='192.04' cy='134.34' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-22; readings: 12.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e299' cx='190.42' cy='253.7' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-21; readings: 8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e300' cx='188.8' cy='253.46' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-20; readings: 8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e301' cx='187.19' cy='156.84' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-19; readings: 11.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e302' cx='185.57' cy='157.91' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-18; readings: 11.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e303' cx='183.95' cy='263.1' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-17; readings: 7.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e304' cx='182.34' cy='156.91' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-16; readings: 11.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e305' cx='180.72' cy='162.81' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-15; readings: 11.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e306' cx='179.1' cy='175.5' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-14; readings: 10.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e307' cx='177.49' cy='195.02' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-13; readings: 10.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e308' cx='175.87' cy='238.48' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-12; readings: 8.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e309' cx='174.25' cy='257.88' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-11; readings: 7.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e310' cx='172.64' cy='196.18' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-10; readings: 10.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e311' cx='171.02' cy='189.81' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-09; readings: 10.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e312' cx='169.4' cy='202.93' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-08; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e313' cx='167.78' cy='191.72' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-07; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e314' cx='166.17' cy='156.77' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-06; readings: 11.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e315' cx='164.55' cy='245.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-05; readings: 8.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e316' cx='162.93' cy='203.93' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-04; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e317' cx='161.32' cy='141.67' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-03; readings: 12.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e318' cx='159.7' cy='136.21' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-02; readings: 12.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e319' cx='158.08' cy='217.06' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-08-01; readings: 9.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e320' cx='156.47' cy='200.18' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-31; readings: 9.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e321' cx='154.85' cy='130.39' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-30; readings: 12.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e322' cx='153.23' cy='129.84' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-29; readings: 12.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e323' cx='151.62' cy='205.18' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-28; readings: 9.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e324' cx='150' cy='224.56' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-27; readings: 9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e325' cx='148.38' cy='147.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-26; readings: 11.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e326' cx='146.76' cy='228.47' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-25; readings: 8.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e327' cx='145.15' cy='237.05' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-24; readings: 8.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e328' cx='143.53' cy='103.9' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-23; readings: 13.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e329' cx='141.91' cy='234.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-22; readings: 8.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e330' cx='140.3' cy='192.3' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-21; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e331' cx='138.68' cy='112.36' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-20; readings: 13.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e332' cx='137.06' cy='152.08' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-19; readings: 11.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e333' cx='135.45' cy='196.32' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-18; readings: 10.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e334' cx='133.83' cy='133.49' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-17; readings: 12.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e335' cx='132.21' cy='119.27' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-16; readings: 12.9 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e336' cx='130.6' cy='189.33' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-15; readings: 10.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e337' cx='128.98' cy='163.87' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-14; readings: 11.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e338' cx='127.36' cy='134.77' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-13; readings: 12.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e339' cx='125.74' cy='141.35' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-12; readings: 12.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e340' cx='124.13' cy='139.93' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-11; readings: 12.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e341' cx='122.51' cy='93.15' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-10; readings: 13.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e342' cx='120.89' cy='169.11' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-09; readings: 11.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e343' cx='119.28' cy='208.43' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-08; readings: 9.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e344' cx='117.66' cy='151.68' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-07; readings: 11.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e345' cx='116.04' cy='191.76' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-06; readings: 10.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e346' cx='114.43' cy='154.88' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-05; readings: 11.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e347' cx='112.81' cy='169.66' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-04; readings: 11.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e348' cx='111.19' cy='84.64' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-03; readings: 14.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e349' cx='109.58' cy='164.93' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-02; readings: 11.2 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e350' cx='107.96' cy='185.56' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-07-01; readings: 10.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e351' cx='106.34' cy='130.5' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-30; readings: 12.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e352' cx='104.72' cy='195.96' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-29; readings: 10.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e353' cx='103.11' cy='81.11' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-28; readings: 14.3 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e354' cx='101.49' cy='141.85' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-27; readings: 12.1 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e355' cx='99.87' cy='188.86' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-26; readings: 10.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e356' cx='98.26' cy='124.9' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-25; readings: 12.7 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e357' cx='96.64' cy='73.92' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-24; readings: 14.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e358' cx='95.02' cy='116.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-23; readings: 13 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e359' cx='93.41' cy='149.14' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-22; readings: 11.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e360' cx='91.79' cy='161.03' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-21; readings: 11.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e361' cx='90.17' cy='89.79' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-20; readings: 14 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e362' cx='88.56' cy='183.2' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-19; readings: 10.6 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e363' cx='86.94' cy='175.28' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-18; readings: 10.8 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e364' cx='85.32' cy='77.75' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-17; readings: 14.4 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e365' cx='83.7' cy='157.08' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-16; readings: 11.5 secs'/>\n   <circle id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e366' cx='82.09' cy='148.3' r='0.91pt' fill='#2C3E50' fill-opacity='0.6' stroke='#2C3E50' stroke-opacity='0.6' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' title='2024-06-15; readings: 11.8 secs'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e367' points='82.09,231.87 83.70,231.87 85.32,231.87 86.94,231.87 88.56,231.87 90.17,231.87 91.79,231.87 93.41,231.87 95.02,231.87 96.64,231.87 98.26,231.87 99.87,231.87 101.49,231.87 103.11,231.87 104.72,231.87 106.34,231.87 107.96,231.87 109.58,231.87 111.19,231.87 112.81,231.87 114.43,231.87 116.04,231.87 117.66,231.87 119.28,231.87 120.89,231.87 122.51,231.87 124.13,231.87 125.74,231.87 127.36,231.87 128.98,231.87 130.60,231.87 132.21,231.87 133.83,231.87 135.45,231.87 137.06,231.87 138.68,231.87 140.30,231.87 141.91,231.87 143.53,231.87 145.15,231.87 146.76,231.87 148.38,231.87 150.00,231.87 151.62,231.87 153.23,231.87 154.85,231.87 156.47,231.87 158.08,231.87 159.70,231.87 161.32,231.87 162.93,231.87 164.55,231.87 166.17,231.87 167.78,231.87 169.40,231.87 171.02,231.87 172.64,231.87 174.25,231.87 175.87,231.87 177.49,231.87 179.10,231.87 180.72,231.87 182.34,231.87 183.95,231.87 185.57,231.87 187.19,231.87 188.80,231.87 190.42,231.87 192.04,231.87 193.66,231.87 195.27,231.87 196.89,231.87 198.51,231.87 200.12,231.87 201.74,231.87 203.36,231.87 204.97,231.87 206.59,231.87 208.21,231.87 209.82,231.87 211.44,231.87 213.06,231.87 214.68,231.87 216.29,231.87 217.91,231.87 219.53,231.87 221.14,231.87 222.76,231.87 224.38,231.87 225.99,231.87 227.61,231.87 229.23,231.87 230.84,231.87 232.46,231.87 234.08,231.87 235.70,231.87 237.31,231.87 238.93,231.87 240.55,231.87 242.16,231.87 243.78,231.87 245.40,231.87 247.01,231.87 248.63,231.87 250.25,231.87 251.86,231.87 253.48,231.87 255.10,231.87 256.72,231.87 258.33,231.87 259.95,231.87 261.57,231.87 263.18,231.87 264.80,231.87 266.42,231.87 268.03,231.87 269.65,231.87 271.27,231.87 272.88,231.87 274.50,231.87 276.12,231.87 277.74,231.87 279.35,231.87 280.97,231.87 282.59,231.87 284.20,231.87 285.82,231.87 287.44,231.87 289.05,231.87 290.67,231.87 292.29,231.87 293.90,231.87 295.52,231.87 297.14,231.87 298.76,231.87 300.37,231.87 301.99,231.87 303.61,231.87 305.22,231.87 306.84,231.87 308.46,231.87 310.07,231.87 311.69,231.87 313.31,231.87 314.92,231.87 316.54,231.87 318.16,231.87 319.77,231.87 321.39,231.87 323.01,231.87 324.63,231.87 326.24,231.87 327.86,231.87 329.48,231.87 331.09,231.87 332.71,231.87 334.33,231.87 335.94,231.87 337.56,231.87 339.18,231.87 340.79,231.87 342.41,231.87 344.03,231.87 345.65,231.87 347.26,231.87 348.88,231.87 350.50,231.87 352.11,231.87 353.73,231.87 355.35,231.87 356.96,231.87 358.58,231.87 360.20,231.87 361.81,231.87 363.43,231.87 365.05,231.87 366.67,231.87 368.28,231.87 369.90,231.87 371.52,231.87 373.13,231.87 374.75,231.87 376.37,231.87 377.98,231.87 379.60,231.87 381.22,231.87 382.83,231.87 384.45,231.87 386.07,231.87 387.69,231.87 389.30,231.87 390.92,231.87 392.54,231.87 394.15,231.87 395.77,231.87 397.39,231.87 399.00,231.87 400.62,231.87 402.24,231.87 403.85,231.87 405.47,231.87 407.09,231.87 408.71,231.87 410.32,231.87 411.94,231.87 413.56,231.87 415.17,231.87 416.79,231.87 418.41,231.87 420.02,231.87 421.64,231.87 423.26,231.87 424.87,231.87 426.49,231.87 428.11,231.87 429.73,231.87 431.34,231.87 432.96,231.87 434.58,231.87 436.19,231.87 437.81,231.87 439.43,231.87 441.04,231.87 442.66,231.87 444.28,231.87 445.89,231.87 447.51,231.87 449.13,231.87 450.75,231.87 452.36,231.87 453.98,231.87 455.60,231.87 457.21,231.87 458.83,231.87 460.45,231.87 462.06,231.87 463.68,231.87 465.30,231.87 466.91,231.87 468.53,231.87 470.15,231.87 471.77,231.87 473.38,231.87 475.00,231.87 476.62,231.87 478.23,231.87 479.85,231.87 481.47,231.87 483.08,231.87 484.70,231.87 486.32,231.87 487.93,231.87 489.55,231.87 491.17,231.87 492.79,231.87 494.40,231.87 496.02,231.87 497.64,231.87 499.25,231.87 500.87,231.87 502.49,231.87 504.10,231.87 505.72,231.87 507.34,231.87 508.95,231.87 510.57,231.87 512.19,231.87 513.81,231.87 515.42,231.87 517.04,231.87 518.66,231.87 520.27,231.87 521.89,231.87 523.51,231.87 525.12,231.87 526.74,231.87 528.36,231.87 529.97,231.87 531.59,231.87 533.21,231.87 534.83,231.87 536.44,231.87 538.06,231.87 539.68,231.87 541.29,231.87 542.91,231.87 544.53,231.87 546.14,231.87 547.76,231.87 549.38,231.87 550.99,231.87 552.61,231.87 554.23,231.87 555.85,231.87 557.46,231.87 559.08,231.87 560.70,231.87 562.31,231.87 563.93,231.87 565.55,231.87 567.16,231.87 568.78,231.87 570.40,231.87 572.01,231.87 573.63,231.87 575.25,231.87 576.87,231.87 578.48,231.87 580.10,231.87 581.72,231.87 583.33,231.87 584.95,231.87 586.57,231.87 588.18,231.87 589.80,231.87 591.42,231.87 593.03,231.87 594.65,231.87 596.27,231.87 597.89,231.87 599.50,231.87 601.12,231.87 602.74,231.87 604.35,231.87 605.97,231.87 607.59,231.87 609.20,231.87 610.82,231.87 612.44,231.87 614.05,231.87 615.67,231.87 617.29,231.87 618.91,231.87 620.52,231.87 622.14,231.87 623.76,231.87 625.37,231.87 626.99,231.87 628.61,231.87 630.22,231.87 631.84,231.87 633.46,231.87 635.07,231.87 636.69,231.87 638.31,231.87 639.93,231.87 641.54,231.87 643.16,231.87 644.78,231.87 646.39,231.87 648.01,231.87 649.63,231.87 651.24,231.87 652.86,231.87 654.48,231.87 656.09,231.87 657.71,231.87 659.33,231.87 660.95,231.87 662.56,231.87 664.18,231.87 665.80,231.87 667.41,231.87 669.03,231.87 670.65,231.87' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(0,0,0); AIC = 1520.02; BIC = 1527.82' data-id='ARIMA(0,0,0)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e368' points='82.09,222.49 83.70,182.16 85.32,211.14 86.94,230.85 88.56,185.83 90.17,213.31 91.79,210.77 93.41,205.70 95.02,186.29 96.64,196.20 98.26,220.59 99.87,219.03 101.49,176.21 103.11,230.46 104.72,199.64 106.34,216.95 107.96,225.51 109.58,181.91 111.19,218.71 112.81,204.90 114.43,227.21 116.04,204.17 117.66,225.97 119.28,224.17 120.89,189.67 122.51,206.36 124.13,208.34 125.74,204.43 127.36,208.36 128.98,226.91 130.60,202.55 132.21,197.90 133.83,224.61 135.45,215.70 137.06,195.40 138.68,210.18 140.30,250.45 141.91,184.87 143.53,230.01 145.15,241.94 146.76,201.33 148.38,229.25 150.00,231.50 151.62,206.08 153.23,198.99 154.85,218.59 156.47,235.74 158.08,206.56 159.70,204.06 161.32,216.25 162.93,245.76 164.55,208.44 166.17,219.57 167.78,224.66 169.40,222.19 171.02,215.68 172.64,239.54 174.25,237.17 175.87,224.16 177.49,215.63 179.10,219.01 180.72,197.24 182.34,249.79 183.95,214.87 185.57,203.46 187.19,233.02 188.80,250.27 190.42,204.18 192.04,208.60 193.66,220.23 195.27,198.37 196.89,240.64 198.51,238.60 200.12,240.98 201.74,208.45 203.36,245.64 204.97,230.82 206.59,248.00 208.21,224.68 209.82,242.82 211.44,241.13 213.06,223.61 214.68,247.95 216.29,218.88 217.91,212.67 219.53,245.13 221.14,237.32 222.76,205.38 224.38,222.75 225.99,209.24 227.61,241.45 229.23,238.62 230.84,259.54 232.46,213.09 234.08,220.21 235.70,252.19 237.31,233.08 238.93,240.30 240.55,223.12 242.16,237.08 243.78,208.47 245.40,226.63 247.01,216.31 248.63,216.26 250.25,211.06 251.86,233.56 253.48,228.06 255.10,252.65 256.72,219.59 258.33,224.55 259.95,246.63 261.57,224.03 263.18,240.81 264.80,247.15 266.42,225.89 268.03,219.33 269.65,204.61 271.27,244.45 272.88,216.10 274.50,248.27 276.12,218.91 277.74,254.36 279.35,222.87 280.97,240.85 282.59,208.92 284.20,260.09 285.82,212.23 287.44,248.38 289.05,238.61 290.67,238.08 292.29,258.66 293.90,231.62 295.52,238.56 297.14,235.97 298.76,217.44 300.37,226.99 301.99,231.59 303.61,238.60 305.22,231.52 306.84,262.32 308.46,226.91 310.07,235.77 311.69,206.69 313.31,253.38 314.92,221.29 316.54,220.83 318.16,235.90 319.77,252.22 321.39,216.61 323.01,264.90 324.63,225.01 326.24,246.79 327.86,251.37 329.48,234.44 331.09,248.96 332.71,252.73 334.33,243.20 335.94,253.07 337.56,216.54 339.18,244.37 340.79,235.36 342.41,244.52 344.03,208.45 345.65,256.24 347.26,227.26 348.88,259.68 350.50,230.49 352.11,239.84 353.73,249.57 355.35,215.57 356.96,242.75 358.58,213.49 360.20,242.32 361.81,256.05 363.43,225.94 365.05,221.18 366.67,232.67 368.28,249.77 369.90,245.09 371.52,228.31 373.13,258.15 374.75,223.33 376.37,256.41 377.98,221.96 379.60,242.54 381.22,222.80 382.83,235.93 384.45,248.08 386.07,237.21 387.69,232.96 389.30,237.69 390.92,214.47 392.54,260.98 394.15,229.27 395.77,253.54 397.39,238.37 399.00,259.15 400.62,229.71 402.24,237.88 403.85,244.34 405.47,251.51 407.09,229.75 408.71,250.65 410.32,235.15 411.94,256.58 413.56,238.05 415.17,256.89 416.79,237.56 418.41,240.99 420.02,256.75 421.64,252.67 423.26,245.17 424.87,246.04 426.49,228.76 428.11,227.34 429.73,246.97 431.34,260.87 432.96,246.72 434.58,262.52 436.19,221.20 437.81,248.51 439.43,265.34 441.04,243.89 442.66,246.91 444.28,252.18 445.89,220.54 447.51,231.57 449.13,228.42 450.75,231.99 452.36,244.54 453.98,229.78 455.60,239.85 457.21,221.53 458.83,240.78 460.45,226.28 462.06,255.49 463.68,251.98 465.30,268.30 466.91,215.75 468.53,243.21 470.15,253.51 471.77,246.26 473.38,253.60 475.00,232.12 476.62,244.79 478.23,243.96 479.85,221.33 481.47,231.53 483.08,259.38 484.70,240.15 486.32,212.53 487.93,255.48 489.55,261.37 491.17,212.49 492.79,256.81 494.40,245.49 496.02,239.65 497.64,228.04 499.25,217.21 500.87,249.42 502.49,218.47 504.10,236.86 505.72,251.01 507.34,234.11 508.95,242.72 510.57,231.49 512.19,267.55 513.81,221.54 515.42,250.62 517.04,253.63 518.66,227.15 520.27,252.00 521.89,227.86 523.51,250.04 525.12,261.70 526.74,224.05 528.36,227.53 529.97,216.98 531.59,232.44 533.21,265.91 534.83,221.16 536.44,247.23 538.06,227.61 539.68,245.72 541.29,254.84 542.91,248.14 544.53,232.88 546.14,237.81 547.76,256.10 549.38,228.48 550.99,267.38 552.61,227.50 554.23,237.12 555.85,221.99 557.46,245.09 559.08,225.73 560.70,228.83 562.31,217.65 563.93,247.57 565.55,216.29 567.16,249.79 568.78,233.33 570.40,262.12 572.01,223.93 573.63,248.01 575.25,222.70 576.87,218.98 578.48,247.90 580.10,255.42 581.72,223.96 583.33,259.47 584.95,223.70 586.57,226.66 588.18,267.04 589.80,212.34 591.42,247.62 593.03,233.37 594.65,243.78 596.27,247.18 597.89,250.41 599.50,239.36 601.12,238.90 602.74,233.45 604.35,247.43 605.97,246.54 607.59,236.22 609.20,251.53 610.82,223.79 612.44,223.54 614.05,262.04 615.67,220.08 617.29,226.69 618.91,222.69 620.52,203.89 622.14,245.47 623.76,244.86 625.37,226.44 626.99,233.56 628.61,220.67 630.22,232.16 631.84,207.91 633.46,226.06 635.07,225.07 636.69,219.94 638.31,206.14 639.93,233.65 641.54,244.83 643.16,253.25 644.78,202.06 646.39,249.39 648.01,230.64 649.63,216.02 651.24,240.23 652.86,205.19 654.48,229.69 656.09,231.63 657.71,212.72 659.33,216.12 660.95,256.10 662.56,214.03 664.18,203.82 665.80,238.37 667.41,209.63 669.03,234.76 670.65,232.49' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(0,0,1); AIC = 1453.31; BIC = 1465.01' data-id='ARIMA(0,0,1)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e369' points='82.09,200.83 83.70,172.64 85.32,218.68 86.94,212.10 88.56,179.75 90.17,207.31 91.79,206.90 93.41,190.45 95.02,169.28 96.64,194.41 98.26,223.20 99.87,192.81 101.49,181.74 103.11,216.09 104.72,194.21 106.34,222.14 107.96,204.58 109.58,179.31 111.19,207.03 112.81,207.87 114.43,213.77 116.04,202.72 117.66,229.76 119.28,208.10 120.89,181.21 122.51,200.61 124.13,198.70 125.74,193.23 127.36,211.74 128.98,219.00 130.60,186.44 132.21,197.00 133.83,225.09 135.45,199.80 137.06,182.38 138.68,228.23 140.30,226.91 141.91,182.52 143.53,240.66 145.15,227.24 146.76,199.36 148.38,233.71 150.00,225.74 151.62,192.01 153.23,191.32 154.85,224.73 156.47,229.73 158.08,194.95 159.70,194.04 161.32,227.18 162.93,236.72 164.55,203.22 166.17,218.35 167.78,223.07 169.40,212.74 171.02,218.19 172.64,243.58 174.25,235.03 175.87,220.27 177.49,215.03 179.10,198.26 180.72,209.18 182.34,247.83 183.95,199.79 185.57,200.50 187.19,247.14 188.80,240.05 190.42,195.85 192.04,209.06 193.66,203.04 195.27,200.11 196.89,243.15 198.51,246.74 200.12,228.04 201.74,214.41 203.36,242.82 204.97,240.00 206.59,241.92 208.21,229.27 209.82,248.16 211.44,233.58 213.06,234.48 214.68,245.41 216.29,206.32 217.91,218.37 219.53,251.77 221.14,226.87 222.76,204.78 224.38,211.43 225.99,211.39 227.61,239.43 229.23,256.19 230.84,252.32 232.46,204.15 234.08,229.19 235.70,251.91 237.31,237.74 238.93,235.58 240.55,229.50 242.16,226.72 243.78,208.25 245.40,222.36 247.01,212.12 248.63,207.05 250.25,211.64 251.86,227.83 253.48,241.02 255.10,247.40 256.72,213.72 258.33,233.45 259.95,241.04 261.57,225.39 263.18,248.33 264.80,247.64 266.42,223.82 268.03,204.99 269.65,212.39 271.27,235.10 272.88,225.51 274.50,238.69 276.12,229.98 277.74,250.19 279.35,229.76 280.97,225.82 282.59,225.69 284.20,247.63 285.82,219.81 287.44,249.89 289.05,237.04 290.67,251.17 292.29,256.82 293.90,234.13 295.52,243.21 297.14,230.12 298.76,214.67 300.37,226.78 301.99,234.21 303.61,233.43 305.22,245.66 306.84,260.78 308.46,232.63 310.07,219.47 311.69,219.10 313.31,251.09 314.92,213.96 316.54,220.74 318.16,247.26 319.77,239.57 321.39,234.07 323.01,258.53 324.63,229.06 326.24,255.95 327.86,249.46 329.48,239.65 331.09,255.86 332.71,255.02 334.33,256.01 335.94,244.13 337.56,220.54 339.18,245.14 340.79,244.69 342.41,230.18 344.03,220.13 345.65,249.33 347.26,241.43 348.88,256.63 350.50,232.11 352.11,250.13 353.73,240.31 355.35,224.86 356.96,231.58 358.58,213.78 360.20,254.95 361.81,255.79 363.43,220.77 365.05,217.75 366.67,239.95 368.28,255.74 369.90,239.87 371.52,241.47 373.13,250.45 374.75,236.20 376.37,249.95 377.98,229.18 379.60,237.30 381.22,221.83 382.83,243.04 384.45,249.42 386.07,238.13 387.69,238.04 389.30,224.88 390.92,228.38 392.54,256.89 394.15,237.83 395.77,251.87 397.39,252.03 399.00,257.60 400.62,229.74 402.24,241.01 403.85,253.71 405.47,247.94 407.09,237.63 408.71,248.67 410.32,245.51 411.94,255.05 413.56,249.59 415.17,258.18 416.79,237.43 418.41,249.57 420.02,264.06 421.64,256.21 423.26,252.94 424.87,245.93 426.49,223.63 428.11,230.45 429.73,256.97 431.34,263.02 432.96,264.22 434.58,254.08 436.19,223.32 437.81,263.04 439.43,267.66 441.04,247.75 442.66,258.52 444.28,246.81 445.89,221.44 447.51,229.54 449.13,226.61 450.75,237.83 452.36,242.72 453.98,234.86 455.60,233.95 457.21,226.77 458.83,235.13 460.45,232.51 462.06,259.17 463.68,272.74 465.30,258.57 466.91,217.86 468.53,250.40 470.15,256.91 471.77,257.08 473.38,250.77 475.00,235.91 476.62,253.59 478.23,239.05 479.85,214.88 481.47,244.29 483.08,267.76 484.70,225.09 486.32,219.65 487.93,273.05 489.55,247.88 491.17,221.72 492.79,261.33 494.40,251.07 496.02,239.51 497.64,218.11 499.25,228.55 500.87,241.87 502.49,217.63 504.10,245.09 505.72,251.70 507.34,238.01 508.95,236.93 510.57,250.87 512.19,258.55 513.81,227.55 515.42,261.09 517.04,248.29 518.66,238.35 520.27,246.08 521.89,231.45 523.51,265.12 525.12,258.79 526.74,225.98 528.36,219.10 529.97,211.99 531.59,250.03 533.21,258.40 534.83,229.71 536.44,242.98 538.06,229.68 539.68,253.89 541.29,263.26 542.91,246.63 544.53,232.82 546.14,249.20 547.76,248.51 549.38,245.62 550.99,265.15 552.61,231.41 554.23,230.09 555.85,228.81 557.46,243.72 559.08,225.79 560.70,220.05 562.31,227.39 563.93,238.11 565.55,223.48 567.16,246.06 568.78,248.73 570.40,254.87 572.01,234.00 573.63,246.54 575.25,212.57 576.87,223.85 578.48,259.68 580.10,246.39 581.72,240.25 583.33,254.98 584.95,215.84 586.57,246.61 588.18,255.17 589.80,219.73 591.42,246.56 593.03,236.23 594.65,247.91 596.27,254.46 597.89,253.24 599.50,242.60 601.12,236.54 602.74,239.17 604.35,252.99 605.97,244.65 607.59,248.55 609.20,248.15 610.82,214.61 612.44,240.13 614.05,256.27 615.67,220.15 617.29,227.57 618.91,206.41 620.52,208.23 622.14,252.26 623.76,242.48 625.37,228.37 626.99,228.40 628.61,225.10 630.22,221.70 631.84,205.53 633.46,225.17 635.07,224.32 636.69,208.14 638.31,202.81 639.93,237.37 641.54,260.12 643.16,235.26 644.78,211.55 646.39,250.44 648.01,221.96 649.63,225.15 651.24,227.19 652.86,203.57 654.48,234.54 656.09,224.36 657.71,200.04 659.33,231.90 660.95,251.60 662.56,199.93 664.18,210.87 665.80,227.06 667.41,210.79 669.03,235.58 670.65,232.60' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(0,0,2); AIC = 1439.1; BIC = 1454.7' data-id='ARIMA(0,0,2)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e370' points='82.09,157.08 83.70,77.75 85.32,175.28 86.94,183.20 88.56,89.79 90.17,161.03 91.79,149.14 93.41,116.14 95.02,73.92 96.64,124.90 98.26,188.86 99.87,141.85 101.49,81.11 103.11,195.96 104.72,130.50 106.34,185.56 107.96,164.93 109.58,84.64 111.19,169.66 112.81,154.88 114.43,191.76 116.04,151.68 117.66,208.43 119.28,169.11 120.89,93.15 122.51,139.93 124.13,141.35 125.74,134.77 127.36,163.87 128.98,189.33 130.60,119.27 132.21,133.49 133.83,196.32 135.45,152.08 137.06,112.36 138.68,192.30 140.30,234.86 141.91,103.90 143.53,237.05 145.15,228.47 146.76,147.35 148.38,224.56 150.00,205.18 151.62,129.84 153.23,130.39 154.85,200.18 156.47,217.06 158.08,136.21 159.70,141.67 161.32,203.93 162.93,245.86 164.55,156.77 166.17,191.72 167.78,202.93 169.40,189.81 171.02,196.18 172.64,257.88 174.25,238.48 175.87,195.02 177.49,175.50 179.10,162.81 180.72,156.91 182.34,263.10 183.95,157.91 185.57,156.84 187.19,253.46 188.80,253.70 190.42,134.34 192.04,157.84 193.66,167.22 195.27,150.79 196.89,262.26 198.51,259.15 200.12,233.01 201.74,182.86 203.36,267.91 204.97,245.28 206.59,268.11 208.21,223.62 209.82,270.65 211.44,248.58 213.06,225.88 214.68,262.16 216.29,177.89 217.91,193.67 219.53,273.03 221.14,220.14 222.76,151.73 224.38,184.86 225.99,180.78 227.61,264.45 229.23,277.79 230.84,287.50 232.46,169.89 234.08,220.99 235.70,287.75 237.31,243.66 238.93,245.86 240.55,213.68 242.16,222.58 243.78,163.91 245.40,202.35 247.01,174.58 248.63,169.25 250.25,177.79 251.86,232.70 253.48,242.53 255.10,275.51 256.72,191.68 258.33,227.10 259.95,263.78 261.57,219.85 263.18,271.26 264.80,267.02 266.42,203.38 268.03,171.04 269.65,171.36 271.27,249.99 272.88,206.01 274.50,263.05 276.12,219.67 277.74,283.36 279.35,216.79 280.97,233.14 282.59,198.57 284.20,288.13 285.82,195.74 287.44,283.05 289.05,256.29 290.67,275.45 292.29,303.66 293.90,237.99 295.52,254.04 297.14,228.55 298.76,188.36 300.37,218.59 301.99,237.95 303.61,249.70 305.22,261.48 306.84,308.79 308.46,222.56 310.07,217.26 311.69,185.86 313.31,279.15 314.92,192.52 316.54,206.35 318.16,263.15 319.77,271.37 321.39,224.04 323.01,313.82 324.63,228.47 326.24,291.55 327.86,286.92 329.48,255.96 331.09,298.74 332.71,299.32 334.33,283.60 335.94,273.59 337.56,203.32 339.18,269.03 340.79,253.99 342.41,242.52 344.03,193.46 345.65,292.81 347.26,247.42 348.88,305.28 350.50,236.23 352.11,271.08 353.73,263.21 355.35,199.09 356.96,242.81 358.58,193.06 360.20,284.22 361.81,290.97 363.43,205.35 365.05,204.07 366.67,252.03 368.28,293.27 369.90,263.93 371.52,248.70 373.13,294.01 374.75,233.58 376.37,287.97 377.98,216.04 379.60,251.56 381.22,211.69 382.83,259.08 384.45,280.85 386.07,247.41 387.69,240.72 389.30,230.19 390.92,214.35 392.54,307.54 394.15,246.66 395.77,296.67 397.39,276.71 399.00,303.08 400.62,232.18 402.24,260.59 403.85,285.12 405.47,282.61 407.09,245.06 408.71,285.70 410.32,265.50 411.94,304.51 413.56,273.58 415.17,304.86 416.79,256.37 418.41,281.35 420.02,319.59 421.64,301.14 423.26,281.86 424.87,266.92 426.49,219.10 428.11,234.93 429.73,301.53 431.34,324.70 432.96,302.51 434.58,303.63 436.19,219.97 437.81,310.15 439.43,333.89 441.04,279.30 442.66,292.68 444.28,275.20 445.89,201.25 447.51,227.71 449.13,222.83 450.75,244.97 452.36,263.92 453.98,234.34 455.60,243.07 457.21,213.13 458.83,250.32 460.45,240.58 462.06,315.52 463.68,322.41 465.30,313.69 466.91,200.04 468.53,284.07 470.15,304.47 471.77,292.34 473.38,290.58 475.00,245.58 476.62,278.77 478.23,253.89 479.85,203.33 481.47,258.59 483.08,314.15 484.70,234.88 486.32,203.65 487.93,324.87 489.55,291.81 491.17,204.89 492.79,312.57 494.40,276.33 496.02,249.05 497.64,207.04 499.25,210.15 500.87,265.70 502.49,200.98 504.10,264.52 505.72,285.62 507.34,248.85 508.95,260.74 510.57,266.63 512.19,317.46 513.81,222.99 515.42,304.10 517.04,285.70 518.66,239.42 520.27,282.02 521.89,239.39 523.51,310.60 525.12,304.26 526.74,206.63 528.36,205.44 529.97,192.57 531.59,267.54 533.21,312.67 534.83,218.59 536.44,268.97 538.06,234.39 539.68,292.14 541.29,309.94 542.91,276.67 544.53,240.62 546.14,272.15 547.76,293.65 549.38,258.37 550.99,322.96 552.61,225.50 554.23,236.20 555.85,218.68 557.46,261.35 559.08,212.45 560.70,209.58 562.31,209.50 563.93,258.56 565.55,208.05 567.16,281.56 568.78,266.14 570.40,305.27 572.01,226.80 573.63,266.13 575.25,194.46 576.87,213.40 578.48,298.57 580.10,287.30 581.72,238.33 583.33,297.93 584.95,204.82 586.57,253.14 588.18,306.89 589.80,195.29 591.42,275.77 593.03,247.90 594.65,279.26 596.27,291.64 597.89,289.25 599.50,259.11 601.12,252.43 602.74,251.78 604.35,288.42 605.97,275.72 607.59,263.32 609.20,276.68 610.82,201.95 612.44,239.78 614.05,301.21 615.67,195.15 617.29,208.90 618.91,179.35 620.52,170.44 622.14,281.48 623.76,261.43 625.37,219.09 626.99,225.32 628.61,202.19 630.22,208.78 631.84,161.82 633.46,209.58 635.07,201.78 636.69,174.20 638.31,164.65 639.93,249.73 641.54,288.17 643.16,259.58 644.78,169.44 646.39,277.80 648.01,212.83 649.63,197.78 651.24,227.76 652.86,158.15 654.48,225.88 656.09,212.19 657.71,164.81 659.33,213.93 660.95,279.22 662.56,156.02 664.18,163.14 665.80,227.25 667.41,174.59 669.03,242.34 670.65,242.57' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(0,1,0); AIC = 1514.62; BIC = 1518.52' data-id='ARIMA(0,1,0)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e371' points='82.09,142.96 83.70,136.65 85.32,148.35 86.94,146.28 88.56,136.87 90.17,145.89 91.79,143.72 93.41,141.40 95.02,141.46 96.64,151.22 98.26,157.76 99.87,152.03 101.49,149.25 103.11,163.14 104.72,155.79 106.34,161.64 107.96,158.21 109.58,152.65 111.19,164.66 112.81,163.18 114.43,166.28 116.04,161.16 117.66,165.51 119.28,159.01 120.89,153.46 122.51,162.42 124.13,164.87 125.74,166.86 127.36,171.78 128.98,174.28 130.60,168.66 132.21,174.43 133.83,182.42 135.45,178.56 137.06,178.81 138.68,190.17 140.30,192.80 141.91,180.51 143.53,196.14 145.15,191.64 146.76,182.92 148.38,191.10 150.00,186.72 151.62,180.49 153.23,185.59 154.85,195.28 156.47,195.98 158.08,189.13 159.70,194.78 161.32,203.74 162.93,206.40 164.55,197.08 166.17,203.25 167.78,205.13 169.40,204.60 171.02,206.31 172.64,210.96 174.25,205.09 175.87,199.18 177.49,198.52 179.10,200.23 180.72,203.45 182.34,214.70 183.95,203.65 185.57,208.06 187.19,218.92 188.80,215.70 190.42,204.82 192.04,213.48 193.66,219.88 195.27,224.08 196.89,238.12 198.51,235.49 200.12,231.69 201.74,228.42 203.36,238.12 204.97,233.64 206.59,233.88 208.21,227.62 209.82,230.79 211.44,225.45 213.06,221.62 214.68,223.51 216.29,214.61 217.91,219.10 219.53,226.45 221.14,218.77 222.76,214.60 224.38,223.08 225.99,226.57 227.61,236.11 229.23,233.87 230.84,230.22 232.46,217.36 234.08,225.03 235.70,229.41 237.31,220.80 238.93,218.64 240.55,213.92 242.16,214.64 243.78,210.33 245.40,217.45 247.01,217.45 248.63,221.59 250.25,227.36 251.86,235.65 253.48,236.41 255.10,237.92 256.72,229.10 258.33,234.91 259.95,237.97 261.57,232.61 263.18,236.89 264.80,233.25 266.42,226.18 268.03,226.71 269.65,232.28 271.27,243.28 272.88,239.88 274.50,246.82 276.12,242.43 277.74,248.66 279.35,241.17 280.97,244.77 282.59,243.71 284.20,253.84 285.82,244.71 287.44,254.90 289.05,250.38 290.67,250.77 292.29,250.01 293.90,240.61 295.52,241.87 297.14,239.27 298.76,237.99 300.37,244.84 301.99,248.65 303.61,250.41 305.22,251.00 306.84,252.88 308.46,242.12 310.07,243.96 311.69,244.65 313.31,256.38 314.92,249.01 316.54,255.52 318.16,263.86 319.77,264.57 321.39,260.86 323.01,270.11 324.63,260.47 326.24,267.44 327.86,264.76 329.48,260.55 331.09,263.47 332.71,259.88 334.33,254.89 335.94,251.55 337.56,245.02 339.18,253.18 340.79,250.69 342.41,249.85 344.03,247.49 345.65,258.97 347.26,252.65 348.88,256.73 350.50,247.61 352.11,250.81 353.73,248.44 355.35,243.09 356.96,250.34 358.58,248.00 360.20,258.92 361.81,256.92 363.43,248.48 365.05,252.76 366.67,260.45 368.28,263.78 369.90,259.08 371.52,257.55 373.13,261.26 374.75,254.21 376.37,259.70 377.98,252.52 379.60,258.48 381.22,256.78 382.83,264.12 384.45,265.99 386.07,262.54 387.69,263.76 389.30,265.62 390.92,268.11 392.54,279.25 394.15,272.66 395.77,278.30 397.39,275.15 399.00,276.71 400.62,269.82 402.24,275.30 403.85,278.24 405.47,277.49 407.09,274.68 408.71,280.17 410.32,278.31 411.94,281.98 413.56,277.76 415.17,280.13 416.79,274.71 418.41,277.94 420.02,279.83 421.64,274.68 423.26,270.86 424.87,269.00 426.49,266.44 428.11,272.08 429.73,279.70 431.34,278.82 432.96,272.75 434.58,269.98 436.19,261.41 437.81,270.82 439.43,268.26 441.04,258.25 442.66,256.87 444.28,252.36 445.89,245.66 447.51,251.82 449.13,253.98 450.75,258.44 452.36,261.02 453.98,258.99 455.60,262.15 457.21,262.30 458.83,269.60 460.45,270.82 462.06,278.25 463.68,274.77 465.30,269.67 466.91,258.31 468.53,269.14 470.15,268.79 471.77,264.38 473.38,261.51 475.00,255.80 476.62,258.83 478.23,255.49 479.85,252.60 481.47,260.72 483.08,264.36 484.70,254.74 486.32,254.65 487.93,267.00 489.55,259.35 491.17,250.66 492.79,261.71 494.40,254.40 496.02,250.68 497.64,248.44 499.25,252.75 500.87,260.58 502.49,256.15 504.10,265.47 505.72,266.89 507.34,262.79 508.95,264.92 510.57,265.52 512.19,268.62 513.81,257.87 515.42,266.20 517.04,261.35 518.66,256.02 520.27,260.33 521.89,255.42 523.51,261.17 525.12,255.95 526.74,245.32 528.36,249.33 529.97,252.95 531.59,263.39 533.21,265.84 534.83,255.40 536.44,262.24 538.06,259.39 539.68,265.24 541.29,263.56 542.91,256.93 544.53,252.72 546.14,255.72 547.76,255.36 549.38,249.14 550.99,252.17 552.61,239.18 554.23,241.27 555.85,240.66 557.46,245.54 559.08,241.10 560.70,243.93 562.31,247.34 563.93,254.26 565.55,250.69 567.16,259.41 568.78,256.08 570.40,257.52 572.01,247.92 573.63,252.59 575.25,247.03 576.87,253.33 578.48,262.40 580.10,258.15 581.72,252.11 583.33,257.25 584.95,247.53 586.57,254.58 588.18,258.15 589.80,246.43 591.42,256.46 593.03,252.76 594.65,255.03 596.27,253.22 597.89,249.17 599.50,243.29 601.12,241.26 602.74,239.95 604.35,240.86 605.97,235.21 607.59,230.23 609.20,227.77 610.82,218.28 612.44,221.95 614.05,223.94 615.67,209.75 617.29,212.14 618.91,210.83 620.52,213.27 622.14,224.17 623.76,217.18 625.37,210.08 626.99,209.55 628.61,206.54 630.22,207.51 631.84,204.54 633.46,211.79 635.07,211.70 636.69,211.21 638.31,214.35 639.93,224.49 641.54,224.21 643.16,215.90 644.78,205.39 646.39,215.53 648.01,205.19 649.63,202.88 651.24,205.34 652.86,198.56 654.48,207.00 656.09,204.79 657.71,200.50 659.33,207.73 660.95,212.51 662.56,194.37 664.18,196.49 665.80,211.33 667.41,209.32 669.03,226.08 670.65,242.57' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(0,1,2); AIC = 1317.62; BIC = 1329.31' data-id='ARIMA(0,1,2)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e372' points='82.09,194.45 83.70,154.88 85.32,203.54 86.94,207.49 88.56,160.88 90.17,196.42 91.79,190.49 93.41,174.03 95.02,152.96 96.64,178.40 98.26,210.31 99.87,186.86 101.49,156.55 103.11,213.85 104.72,181.19 106.34,208.67 107.96,198.37 109.58,158.31 111.19,200.73 112.81,193.36 114.43,211.76 116.04,191.76 117.66,220.07 119.28,200.46 120.89,162.56 122.51,185.90 124.13,186.61 125.74,183.33 127.36,197.84 128.98,210.55 130.60,175.59 132.21,182.69 133.83,214.03 135.45,191.96 137.06,172.14 138.68,212.03 140.30,233.26 141.91,167.92 143.53,234.36 145.15,230.08 146.76,189.60 148.38,228.12 150.00,218.46 151.62,180.86 153.23,181.14 154.85,215.96 156.47,224.38 158.08,184.04 159.70,186.77 161.32,217.83 162.93,238.75 164.55,194.30 166.17,211.74 167.78,217.33 169.40,210.78 171.02,213.96 172.64,244.75 174.25,235.07 175.87,213.38 177.49,203.65 179.10,197.32 180.72,194.37 182.34,247.35 183.95,194.87 185.57,194.33 187.19,242.54 188.80,242.66 190.42,183.11 192.04,194.84 193.66,199.51 195.27,191.32 196.89,246.93 198.51,245.38 200.12,232.34 201.74,207.32 203.36,249.75 204.97,238.46 206.59,249.85 208.21,227.65 209.82,251.12 211.44,240.11 213.06,228.78 214.68,246.88 216.29,204.84 217.91,212.71 219.53,252.31 221.14,225.92 222.76,191.78 224.38,208.31 225.99,206.28 227.61,248.03 229.23,254.68 230.84,259.53 232.46,200.85 234.08,226.34 235.70,259.65 237.31,237.66 238.93,238.75 240.55,222.70 242.16,227.14 243.78,197.86 245.40,217.04 247.01,203.19 248.63,200.53 250.25,204.79 251.86,232.18 253.48,237.09 255.10,253.54 256.72,211.72 258.33,229.39 259.95,247.69 261.57,225.77 263.18,251.43 264.80,249.31 266.42,217.55 268.03,201.42 269.65,201.58 271.27,240.81 272.88,218.87 274.50,247.33 276.12,225.69 277.74,257.46 279.35,224.24 280.97,232.40 282.59,215.16 284.20,259.84 285.82,213.75 287.44,257.31 289.05,243.95 290.67,253.52 292.29,267.59 293.90,234.82 295.52,242.83 297.14,230.11 298.76,210.06 300.37,225.15 301.99,234.81 303.61,240.67 305.22,246.54 306.84,270.15 308.46,227.12 310.07,224.48 311.69,208.82 313.31,255.36 314.92,212.14 316.54,219.04 318.16,247.38 319.77,251.48 321.39,227.86 323.01,272.66 324.63,230.07 326.24,261.55 327.86,259.24 329.48,243.79 331.09,265.14 332.71,265.43 334.33,257.58 335.94,252.59 337.56,217.53 339.18,250.31 340.79,242.81 342.41,237.09 344.03,212.61 345.65,262.18 347.26,239.53 348.88,268.40 350.50,233.95 352.11,251.33 353.73,247.41 355.35,215.42 356.96,237.23 358.58,212.41 360.20,257.89 361.81,261.26 363.43,218.54 365.05,217.90 366.67,241.83 368.28,262.41 369.90,247.77 371.52,240.17 373.13,262.77 374.75,232.63 376.37,259.76 377.98,223.87 379.60,241.60 381.22,221.70 382.83,245.35 384.45,256.21 386.07,239.52 387.69,236.19 389.30,230.93 390.92,223.03 392.54,269.53 394.15,239.15 395.77,264.10 397.39,254.14 399.00,267.30 400.62,231.92 402.24,246.10 403.85,258.34 405.47,257.09 407.09,238.35 408.71,258.63 410.32,248.55 411.94,268.01 413.56,252.58 415.17,268.19 416.79,243.99 418.41,256.46 420.02,275.54 421.64,266.33 423.26,256.71 424.87,249.26 426.49,225.40 428.11,233.30 429.73,266.53 431.34,278.09 432.96,267.02 434.58,267.57 436.19,225.83 437.81,270.83 439.43,282.67 441.04,255.43 442.66,262.11 444.28,253.39 445.89,216.49 447.51,229.70 449.13,227.26 450.75,238.31 452.36,247.76 453.98,233.00 455.60,237.36 457.21,222.42 458.83,240.98 460.45,236.12 462.06,273.51 463.68,276.94 465.30,272.59 466.91,215.89 468.53,257.82 470.15,267.99 471.77,261.94 473.38,261.06 475.00,238.61 476.62,255.17 478.23,242.76 479.85,217.53 481.47,245.10 483.08,272.82 484.70,233.27 486.32,217.69 487.93,278.17 489.55,261.68 491.17,218.31 492.79,272.04 494.40,253.95 496.02,240.34 497.64,219.38 499.25,220.93 500.87,248.65 502.49,216.36 504.10,248.06 505.72,258.59 507.34,240.24 508.95,246.17 510.57,249.11 512.19,274.48 513.81,227.34 515.42,267.81 517.04,258.63 518.66,235.54 520.27,256.79 521.89,235.52 523.51,271.05 525.12,267.89 526.74,219.18 528.36,218.59 529.97,212.16 531.59,249.57 533.21,272.09 534.83,225.14 536.44,250.28 538.06,233.03 539.68,261.84 541.29,270.72 542.91,254.12 544.53,236.14 546.14,251.87 547.76,262.59 549.38,244.99 550.99,277.22 552.61,228.59 554.23,233.93 555.85,225.19 557.46,246.48 559.08,222.08 560.70,220.65 562.31,220.61 563.93,245.09 565.55,219.89 567.16,256.56 568.78,248.87 570.40,268.39 572.01,229.24 573.63,248.87 575.25,213.10 576.87,222.55 578.48,265.05 580.10,259.42 581.72,234.99 583.33,264.73 584.95,218.27 586.57,242.39 588.18,269.20 589.80,213.52 591.42,253.67 593.03,239.77 594.65,255.42 596.27,261.59 597.89,260.40 599.50,245.36 601.12,242.03 602.74,241.71 604.35,259.99 605.97,253.65 607.59,247.46 609.20,254.13 610.82,216.84 612.44,235.72 614.05,266.37 615.67,213.45 617.29,220.31 618.91,205.57 620.52,201.12 622.14,256.52 623.76,246.52 625.37,225.39 626.99,228.50 628.61,216.96 630.22,220.25 631.84,196.82 633.46,220.65 635.07,216.76 636.69,203.00 638.31,198.23 639.93,240.68 641.54,259.86 643.16,245.59 644.78,200.62 646.39,254.69 648.01,222.27 649.63,214.76 651.24,229.72 652.86,194.99 654.48,228.78 656.09,221.95 657.71,198.31 659.33,222.82 660.95,255.40 662.56,193.93 664.18,197.48 665.80,229.47 667.41,203.19 669.03,236.99 670.65,233.09' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(1,0,0); AIC = 1417.99; BIC = 1429.69' data-id='ARIMA(1,0,0)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e373' points='82.09,122.01 83.70,120.86 85.32,178.78 86.94,141.91 88.56,121.28 90.17,155.77 91.79,134.55 93.41,97.48 95.02,96.45 96.64,153.17 98.26,168.08 99.87,115.00 101.49,131.87 103.11,167.03 104.72,154.84 106.34,176.44 107.96,129.44 109.58,122.22 111.19,163.13 112.81,171.18 114.43,174.04 116.04,176.77 117.66,191.05 119.28,135.54 120.89,113.83 122.51,140.56 124.13,138.44 125.74,147.63 127.36,175.12 128.98,158.36 130.60,125.55 132.21,161.26 133.83,176.76 135.45,134.52 137.06,147.70 138.68,211.11 140.30,176.97 141.91,162.76 143.53,233.26 145.15,192.62 146.76,181.48 148.38,216.00 150.00,171.88 151.62,130.08 153.23,161.24 154.85,207.64 156.47,181.32 158.08,138.62 159.70,169.19 161.32,222.46 162.93,206.48 164.55,172.22 166.17,196.68 167.78,197.13 169.40,192.62 171.02,223.45 172.64,249.31 174.25,219.27 175.87,186.39 177.49,169.89 179.10,160.20 180.72,203.85 182.34,216.61 183.95,157.44 185.57,199.55 187.19,253.56 188.80,200.94 190.42,144.73 192.04,161.99 193.66,159.96 195.27,200.06 196.89,260.89 198.51,247.60 200.12,210.84 201.74,220.45 203.36,257.91 204.97,255.37 206.59,248.44 208.21,244.41 209.82,260.90 211.44,238.55 213.06,241.92 214.68,224.91 216.29,184.87 217.91,228.75 219.53,249.65 221.14,189.90 222.76,166.37 224.38,183.06 225.99,217.76 227.61,270.34 229.23,282.08 230.84,235.52 232.46,192.48 234.08,250.50 235.70,268.26 237.31,244.63 238.93,231.64 240.55,217.62 242.16,196.65 243.78,180.90 245.40,190.07 247.01,172.22 248.63,173.03 250.25,202.06 251.86,237.04 253.48,257.11 255.10,238.46 256.72,207.34 258.33,243.31 259.95,244.36 261.57,242.58 263.18,269.39 264.80,238.89 266.42,189.08 268.03,171.18 269.65,206.11 271.27,230.55 272.88,231.23 274.50,243.88 276.12,247.82 277.74,253.93 279.35,224.01 280.97,217.86 282.59,238.16 284.20,247.29 285.82,234.33 287.44,271.22 289.05,264.76 290.67,287.92 292.29,274.63 293.90,245.09 295.52,242.78 297.14,210.78 298.76,201.72 300.37,227.15 301.99,243.15 303.61,254.91 305.22,282.39 306.84,270.67 308.46,220.22 310.07,203.38 311.69,227.10 313.31,240.86 314.92,198.63 316.54,231.45 318.16,266.78 319.77,250.45 321.39,263.72 323.01,276.09 324.63,256.35 326.24,289.50 327.86,273.23 329.48,274.87 331.09,299.00 332.71,292.38 334.33,279.18 335.94,242.53 337.56,232.37 339.18,262.39 340.79,248.92 342.41,220.84 344.03,237.38 345.65,272.75 347.26,272.99 348.88,274.76 350.50,251.64 352.11,267.60 353.73,234.87 355.35,218.41 356.96,220.82 358.58,233.36 360.20,287.20 361.81,253.13 363.43,204.78 365.05,225.27 366.67,270.26 368.28,280.30 369.90,257.20 371.52,268.73 373.13,267.30 374.75,257.63 376.37,256.18 377.98,231.74 379.60,233.94 381.22,232.64 382.83,268.70 384.45,266.07 386.07,244.45 387.69,236.07 389.30,223.19 390.92,255.54 392.54,280.63 394.15,268.77 395.77,287.85 397.39,288.36 399.00,271.74 400.62,244.74 402.24,271.43 403.85,284.01 405.47,266.01 407.09,263.02 408.71,276.77 410.32,282.75 411.94,290.84 413.56,287.41 415.17,283.43 416.79,267.41 418.41,298.25 420.02,311.44 421.64,292.62 423.26,275.25 424.87,245.78 426.49,226.09 428.11,264.37 429.73,311.77 431.34,314.89 432.96,303.01 434.58,266.65 436.19,259.83 437.81,320.64 439.43,309.76 441.04,285.21 442.66,284.95 444.28,242.51 445.89,212.95 447.51,225.56 449.13,232.62 450.75,253.35 452.36,250.85 453.98,238.20 455.60,229.84 457.21,229.57 458.83,246.02 460.45,273.70 462.06,318.56 463.68,318.56 465.30,263.46 466.91,237.18 468.53,293.09 470.15,299.10 471.77,291.56 473.38,270.69 475.00,260.25 476.62,267.77 478.23,231.54 479.85,227.76 481.47,283.15 483.08,279.11 484.70,221.08 486.32,257.24 487.93,310.26 489.55,253.39 491.17,252.48 492.79,296.55 494.40,264.27 496.02,230.48 497.64,208.41 499.25,234.70 500.87,237.09 502.49,229.06 504.10,273.85 505.72,269.37 507.34,254.11 508.95,263.34 510.57,289.10 512.19,275.70 513.81,258.84 515.42,295.97 517.04,265.24 518.66,258.25 520.27,263.18 521.89,270.87 523.51,307.80 525.12,261.10 526.74,206.11 528.36,199.75 529.97,225.71 531.59,287.49 533.21,271.08 534.83,240.86 536.44,253.69 538.06,259.92 539.68,300.01 541.29,295.23 542.91,260.74 544.53,254.56 546.14,281.65 547.76,278.05 549.38,286.92 550.99,279.88 552.61,230.23 554.23,228.46 555.85,237.54 557.46,239.73 559.08,211.18 560.70,209.55 562.31,231.19 563.93,236.24 565.55,240.54 567.16,274.74 568.78,283.44 570.40,270.59 572.01,244.19 573.63,234.45 575.25,202.83 576.87,251.04 578.48,293.59 580.10,265.65 581.72,264.68 583.33,256.78 584.95,226.18 586.57,276.90 588.18,257.56 589.80,230.86 591.42,263.45 593.03,261.76 594.65,284.73 596.27,290.58 597.89,275.93 599.50,256.16 601.12,252.15 602.74,267.98 604.35,282.81 605.97,270.24 607.59,269.23 609.20,243.65 610.82,218.67 612.44,266.93 614.05,254.33 615.67,201.23 617.29,195.84 618.91,175.41 620.52,219.52 622.14,272.62 623.76,242.72 625.37,221.84 626.99,215.09 628.61,205.10 630.22,188.02 631.84,182.93 633.46,206.13 635.07,189.59 636.69,169.98 638.31,202.26 639.93,266.72 641.54,275.53 643.16,219.74 644.78,217.34 646.39,249.08 648.01,206.18 649.63,211.03 651.24,196.99 652.86,188.09 654.48,219.83 656.09,191.25 657.71,186.52 659.33,242.79 660.95,224.77 662.56,159.17 664.18,191.48 665.80,203.97 667.41,204.54 669.03,235.36 670.65,242.57' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(1,1,0); AIC = 1437.63; BIC = 1445.43' data-id='ARIMA(1,1,0)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e374' points='82.09,142.57 83.70,137.16 85.32,148.18 86.94,145.73 88.56,137.20 90.17,145.68 91.79,143.49 93.41,141.27 95.02,141.84 96.64,151.48 98.26,157.41 99.87,151.80 101.49,149.86 103.11,162.72 104.72,156.01 106.34,161.40 107.96,157.85 109.58,153.16 111.19,164.53 112.81,163.27 114.43,165.96 116.04,161.28 117.66,165.07 119.28,158.59 120.89,153.75 122.51,162.42 124.13,164.85 125.74,166.99 127.36,171.80 128.98,173.89 130.60,168.83 132.21,174.73 133.83,182.15 135.45,178.49 137.06,179.31 138.68,190.27 140.30,192.14 141.91,181.22 143.53,195.88 145.15,191.14 146.76,183.23 148.38,190.79 150.00,186.30 151.62,180.61 153.23,185.97 154.85,195.24 156.47,195.55 158.08,189.32 159.70,195.16 161.32,203.84 162.93,205.89 164.55,197.32 166.17,203.29 167.78,205.06 169.40,204.64 171.02,206.51 172.64,210.62 174.25,204.70 175.87,199.02 177.49,198.48 179.10,200.28 180.72,203.92 182.34,214.07 183.95,203.76 185.57,208.52 187.19,218.69 188.80,215.05 190.42,205.13 192.04,213.72 193.66,220.05 195.27,224.80 196.89,238.04 198.51,235.34 200.12,231.56 201.74,228.95 203.36,237.96 204.97,233.71 206.59,233.61 208.21,227.80 209.82,230.55 211.44,225.26 213.06,221.69 214.68,223.03 216.29,214.79 217.91,219.44 219.53,226.04 221.14,218.51 222.76,214.98 224.38,223.25 225.99,227.08 227.61,236.05 229.23,233.72 230.84,229.57 232.46,217.71 234.08,225.24 235.70,228.97 237.31,220.63 238.93,218.32 240.55,213.84 242.16,214.30 243.78,210.55 245.40,217.33 247.01,217.54 248.63,221.79 250.25,227.73 251.86,235.67 253.48,236.47 255.10,237.45 256.72,229.34 258.33,235.03 259.95,237.67 261.57,232.77 263.18,236.65 264.80,232.80 266.42,226.09 268.03,226.88 269.65,232.76 271.27,243.08 272.88,240.19 274.50,246.58 276.12,242.72 277.74,248.27 279.35,241.31 280.97,244.69 282.59,244.21 284.20,253.40 285.82,245.21 287.44,254.70 289.05,250.41 290.67,250.73 292.29,249.52 293.90,240.61 295.52,241.67 297.14,239.12 298.76,238.25 300.37,244.98 301.99,248.70 303.61,250.41 305.22,251.06 306.84,252.29 308.46,242.12 310.07,243.92 311.69,245.17 313.31,255.98 314.92,249.30 316.54,255.94 318.16,263.94 319.77,264.42 321.39,261.37 323.01,269.70 324.63,260.87 326.24,267.37 327.86,264.61 329.48,260.74 331.09,263.35 332.71,259.66 334.33,254.72 335.94,251.19 337.56,245.39 339.18,253.04 340.79,250.62 342.41,249.70 344.03,248.05 345.65,258.66 347.26,252.84 348.88,256.25 350.50,247.71 352.11,250.63 353.73,248.10 355.35,243.36 356.96,250.16 358.58,248.49 360.20,258.78 361.81,256.44 363.43,248.61 365.05,253.09 366.67,260.60 368.28,263.52 369.90,258.96 371.52,257.69 373.13,260.86 374.75,254.41 376.37,259.27 377.98,252.72 379.60,258.32 381.22,257.07 382.83,264.16 384.45,265.76 386.07,262.54 387.69,263.80 389.30,265.70 390.92,268.65 392.54,278.93 394.15,272.94 395.77,278.16 397.39,275.23 399.00,276.37 400.62,270.08 402.24,275.46 403.85,278.23 405.47,277.38 407.09,274.99 408.71,280.12 410.32,278.55 411.94,281.83 413.56,277.93 415.17,279.92 416.79,274.92 418.41,278.10 420.02,279.64 421.64,274.53 423.26,270.79 424.87,268.90 426.49,266.76 428.11,272.54 429.73,279.76 431.34,278.61 432.96,272.68 434.58,269.60 436.19,261.92 437.81,270.75 439.43,267.78 441.04,258.16 442.66,256.59 444.28,251.95 445.89,245.87 447.51,251.86 449.13,254.16 450.75,258.53 452.36,260.89 453.98,259.12 455.60,262.15 457.21,262.68 458.83,269.71 460.45,271.27 462.06,278.18 463.68,274.59 465.30,269.14 466.91,258.88 468.53,269.19 470.15,268.63 471.77,264.27 473.38,261.23 475.00,255.94 476.62,258.66 478.23,255.32 479.85,252.99 481.47,260.92 483.08,263.89 484.70,254.72 486.32,255.27 487.93,266.65 489.55,258.91 491.17,251.18 492.79,261.33 494.40,254.17 496.02,250.51 497.64,248.60 499.25,253.11 500.87,260.36 502.49,256.62 504.10,265.58 505.72,266.73 507.34,262.94 508.95,265.00 510.57,265.74 512.19,268.14 513.81,258.32 515.42,266.01 517.04,261.11 518.66,256.25 520.27,260.11 521.89,255.72 523.51,260.93 525.12,255.40 526.74,245.48 528.36,249.50 529.97,253.49 531.59,263.57 533.21,265.37 534.83,255.78 536.44,262.16 538.06,259.75 539.68,265.25 541.29,263.30 542.91,256.77 544.53,252.91 546.14,255.75 547.76,255.08 549.38,249.30 550.99,251.52 552.61,239.23 554.23,241.19 555.85,240.86 557.46,245.28 559.08,241.19 560.70,244.07 562.31,247.69 563.93,254.11 565.55,251.16 567.16,259.30 568.78,256.20 570.40,257.08 572.01,248.17 573.63,252.34 575.25,247.38 576.87,253.86 578.48,262.30 580.10,257.93 581.72,252.44 583.33,256.85 584.95,247.97 586.57,254.87 588.18,257.69 589.80,247.04 591.42,256.43 593.03,253.03 594.65,255.11 596.27,253.18 597.89,249.03 599.50,243.32 601.12,241.33 602.74,240.14 604.35,240.71 605.97,235.07 607.59,230.19 609.20,227.37 610.82,218.51 612.44,222.11 614.05,223.27 615.67,209.86 617.29,212.08 618.91,210.96 620.52,213.85 622.14,223.88 623.76,216.84 625.37,210.04 626.99,209.38 628.61,206.55 630.22,207.33 631.84,204.87 633.46,211.78 635.07,211.68 636.69,211.38 638.31,214.90 639.93,224.55 641.54,223.87 643.16,215.41 644.78,205.90 646.39,215.04 648.01,205.07 649.63,202.99 651.24,204.99 652.86,198.94 654.48,206.86 656.09,204.57 657.71,200.79 659.33,207.90 660.95,211.75 662.56,194.49 664.18,196.83 665.80,211.03 667.41,209.62 669.03,225.92 670.65,242.57' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(1,1,1); AIC = 1317.72; BIC = 1329.42' data-id='ARIMA(1,1,1)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e375' points='82.09,145.22 83.70,136.06 85.32,145.90 86.94,149.03 88.56,135.35 90.17,145.87 91.79,143.44 93.41,142.76 95.02,141.36 96.64,150.44 98.26,156.33 99.87,155.59 101.49,146.05 103.11,164.54 104.72,155.54 106.34,159.73 107.96,161.48 109.58,150.40 111.19,165.92 112.81,161.38 114.43,167.52 116.04,160.07 117.66,164.43 119.28,161.05 120.89,152.78 122.51,162.33 124.13,165.01 125.74,167.11 127.36,170.36 128.98,175.16 130.60,169.67 132.21,172.91 133.83,182.64 135.45,179.51 137.06,179.59 138.68,186.49 140.30,196.73 141.91,178.92 143.53,194.24 145.15,194.00 146.76,181.60 148.38,190.20 150.00,187.82 151.62,181.22 153.23,185.25 154.85,193.58 156.47,197.52 158.08,189.41 159.70,195.19 161.32,201.25 162.93,208.41 164.55,196.67 166.17,203.08 167.78,204.94 169.40,205.45 171.02,204.84 172.64,210.63 174.25,205.06 175.87,200.03 177.49,197.52 179.10,202.71 180.72,200.13 182.34,216.03 183.95,204.77 185.57,207.13 187.19,216.90 188.80,217.78 190.42,205.57 192.04,212.42 193.66,222.44 195.27,221.89 196.89,238.61 198.51,234.30 200.12,234.51 201.74,226.17 203.36,239.32 204.97,232.16 206.59,235.20 208.21,226.84 209.82,230.06 211.44,226.82 213.06,219.76 214.68,224.64 216.29,215.58 217.91,217.59 219.53,225.70 221.14,221.16 222.76,213.49 224.38,224.90 225.99,224.74 227.61,236.68 229.23,231.70 230.84,232.25 232.46,218.05 234.08,222.62 235.70,230.42 237.31,219.52 238.93,219.85 240.55,212.26 242.16,216.53 243.78,209.18 245.40,218.23 247.01,217.32 248.63,222.79 250.25,226.01 251.86,236.55 253.48,234.44 255.10,239.37 256.72,229.57 258.33,233.23 259.95,239.13 261.57,231.98 263.18,235.60 264.80,234.42 266.42,225.81 268.03,229.05 269.65,229.36 271.27,245.89 272.88,237.20 274.50,249.46 276.12,239.51 277.74,251.10 279.35,239.11 280.97,248.05 282.59,240.01 284.20,257.00 285.82,242.95 287.44,255.01 289.05,250.88 290.67,248.89 292.29,251.15 293.90,240.28 295.52,241.41 297.14,240.60 298.76,237.58 300.37,245.14 301.99,247.69 303.61,251.72 305.22,248.11 306.84,255.07 308.46,240.68 310.07,246.83 311.69,241.69 313.31,258.20 314.92,249.28 316.54,255.86 318.16,261.84 319.77,267.81 321.39,257.62 323.01,272.47 324.63,259.71 326.24,266.78 327.86,265.73 329.48,260.09 331.09,262.53 332.71,260.76 334.33,253.11 335.94,254.23 337.56,243.13 339.18,254.28 340.79,248.95 342.41,253.18 344.03,244.29 345.65,261.08 347.26,250.37 348.88,257.86 350.50,247.65 352.11,249.08 353.73,250.94 355.35,241.15 356.96,252.33 358.58,247.05 360.20,257.43 361.81,258.21 363.43,249.19 365.05,252.50 366.67,259.79 368.28,263.01 369.90,260.96 371.52,254.76 373.13,263.90 374.75,251.50 376.37,261.92 377.98,251.09 379.60,259.75 381.22,256.42 382.83,263.39 384.45,266.06 386.07,263.39 387.69,262.67 389.30,268.41 390.92,264.93 392.54,281.59 394.15,270.88 395.77,279.67 397.39,273.32 399.00,278.47 400.62,269.51 402.24,275.50 403.85,277.14 405.47,279.40 407.09,273.19 408.71,281.71 410.32,276.71 411.94,283.41 413.56,276.47 415.17,281.13 416.79,274.96 418.41,277.08 420.02,279.63 421.64,275.10 423.26,270.49 424.87,270.08 426.49,267.04 428.11,271.83 429.73,278.29 431.34,280.00 432.96,270.89 434.58,272.49 436.19,260.55 437.81,269.29 439.43,268.87 441.04,258.19 442.66,255.61 444.28,254.10 445.89,245.30 447.51,251.95 449.13,254.41 450.75,257.28 452.36,262.30 453.98,257.97 455.60,264.01 457.21,261.03 458.83,271.41 460.45,269.02 462.06,278.97 463.68,272.55 465.30,272.79 466.91,257.50 468.53,268.21 470.15,269.29 471.77,263.35 473.38,262.68 475.00,255.24 476.62,258.38 478.23,256.80 479.85,252.82 481.47,259.05 483.08,264.56 484.70,256.88 486.32,253.15 487.93,265.08 489.55,262.73 491.17,248.25 492.79,261.77 494.40,254.60 496.02,250.78 497.64,250.10 499.25,250.67 500.87,262.44 502.49,256.01 504.10,264.16 505.72,268.29 507.34,261.80 508.95,266.65 510.57,262.62 512.19,271.28 513.81,256.86 515.42,265.07 517.04,263.35 518.66,254.29 520.27,261.74 521.89,254.70 523.51,259.46 525.12,257.81 526.74,245.40 528.36,250.06 529.97,253.59 531.59,260.43 533.21,268.80 534.83,253.83 536.44,263.65 538.06,258.80 539.68,264.49 541.29,263.61 542.91,257.42 544.53,253.35 546.14,253.59 547.76,257.76 549.38,245.87 550.99,254.38 552.61,238.09 554.23,242.78 555.85,238.87 557.46,246.92 559.08,240.44 560.70,245.81 562.31,244.94 563.93,257.04 565.55,248.39 567.16,261.16 568.78,253.48 570.40,259.99 572.01,246.34 573.63,253.93 575.25,247.94 576.87,252.56 578.48,261.00 580.10,260.55 581.72,249.97 583.33,258.61 584.95,249.06 586.57,251.13 588.18,261.91 589.80,244.61 591.42,257.56 593.03,252.38 594.65,255.01 596.27,252.72 597.89,249.77 599.50,243.23 601.12,241.83 602.74,239.35 604.35,240.39 605.97,235.94 607.59,228.96 609.20,228.67 610.82,219.40 612.44,218.92 614.05,225.84 615.67,209.68 617.29,211.90 618.91,212.79 620.52,211.42 622.14,223.34 623.76,218.27 625.37,209.13 626.99,210.62 628.61,205.31 630.22,209.28 631.84,203.68 633.46,212.06 635.07,211.65 636.69,212.98 638.31,213.04 639.93,224.42 641.54,222.14 643.16,219.37 644.78,202.92 646.39,215.58 648.01,206.74 649.63,200.79 651.24,207.64 652.86,197.55 654.48,206.66 656.09,205.61 657.71,201.45 659.33,205.02 660.95,213.86 662.56,196.41 664.18,194.13 665.80,213.83 667.41,208.13 669.03,226.34 670.65,242.57' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(1,1,2); AIC = 1318.31; BIC = 1333.9' data-id='ARIMA(1,1,2)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e376' points='82.09,164.47 83.70,159.83 85.32,198.16 86.94,177.27 88.56,160.67 90.17,184.16 91.79,171.31 93.41,148.25 95.02,145.54 96.64,180.85 98.26,192.66 99.87,159.66 101.49,166.36 103.11,192.41 104.72,182.09 106.34,197.33 107.96,169.15 109.58,160.96 111.19,188.84 112.81,192.75 114.43,196.25 116.04,195.82 117.66,206.91 119.28,172.88 120.89,156.54 122.51,174.32 124.13,173.17 125.74,178.15 127.36,195.48 128.98,187.08 130.60,164.62 132.21,185.95 133.83,198.05 135.45,171.44 137.06,177.06 138.68,217.68 140.30,200.11 141.91,185.33 143.53,232.72 145.15,208.82 146.76,198.32 148.38,222.12 150.00,195.68 151.62,167.77 153.23,185.78 154.85,216.08 156.47,201.72 158.08,173.01 159.70,190.94 161.32,224.82 162.93,217.69 164.55,193.45 166.17,209.32 167.78,210.14 169.40,206.88 171.02,225.00 172.64,243.03 174.25,224.71 175.87,203.55 177.49,193.04 179.10,186.81 180.72,211.71 182.34,224.41 183.95,184.97 185.57,209.23 187.19,245.26 188.80,214.89 190.42,176.45 192.04,187.59 193.66,186.89 195.27,209.22 196.89,249.93 198.51,242.10 200.12,219.57 201.74,222.60 203.36,248.50 204.97,245.90 206.59,243.04 208.21,238.48 209.82,250.36 211.44,236.35 213.06,237.16 214.68,229.16 216.29,201.81 217.91,227.94 219.53,243.99 221.14,206.83 222.76,189.82 224.38,201.11 225.99,220.95 227.61,255.51 229.23,262.95 230.84,236.55 232.46,205.80 234.08,241.87 235.70,255.47 237.31,239.62 238.93,232.22 240.55,222.51 242.16,210.85 243.78,198.82 245.40,206.04 247.01,194.34 248.63,194.54 250.25,211.73 251.86,234.68 253.48,246.76 255.10,237.65 256.72,215.47 258.33,238.02 259.95,240.47 261.57,237.23 263.18,255.29 264.80,237.47 266.42,205.52 268.03,193.56 269.65,213.75 271.27,231.80 272.88,229.99 274.50,240.15 276.12,240.26 277.74,246.98 279.35,226.36 280.97,223.63 282.59,233.62 284.20,243.38 285.82,231.27 287.44,256.94 289.05,251.87 290.67,266.21 292.29,259.95 293.90,239.59 295.52,239.06 297.14,219.31 298.76,212.06 300.37,228.26 301.99,238.47 303.61,245.85 305.22,262.31 306.84,257.92 308.46,224.46 310.07,214.47 311.69,226.59 313.31,239.22 314.92,210.49 316.54,230.13 318.16,253.38 319.77,244.36 321.39,249.65 323.01,261.30 324.63,245.62 326.24,267.93 327.86,258.30 329.48,257.69 331.09,273.77 332.71,269.97 334.33,261.57 335.94,239.90 337.56,230.51 339.18,251.14 340.79,242.61 342.41,225.82 344.03,232.91 345.65,258.31 347.26,256.18 348.88,260.10 350.50,243.29 352.11,254.25 353.73,234.96 355.35,222.24 356.96,225.82 358.58,230.57 360.20,266.23 361.81,246.89 363.43,214.69 365.05,226.45 366.67,254.83 368.28,262.70 369.90,247.89 371.52,253.78 373.13,255.23 374.75,246.61 376.37,248.51 377.98,230.79 379.60,233.84 381.22,231.09 382.83,254.29 384.45,253.86 386.07,239.70 387.69,234.52 389.30,226.56 390.92,244.45 392.54,263.61 394.15,253.70 395.77,267.23 397.39,266.52 399.00,258.25 400.62,239.10 402.24,255.94 403.85,264.43 405.47,253.91 407.09,250.31 408.71,260.28 410.32,262.72 411.94,269.35 413.56,265.82 415.17,265.08 416.79,253.41 418.41,272.47 420.02,282.00 421.64,270.21 423.26,259.21 424.87,241.45 426.49,227.68 428.11,250.57 429.73,281.29 431.34,284.25 432.96,276.27 434.58,255.34 436.19,247.20 437.81,286.84 439.43,281.74 441.04,264.83 442.66,265.35 444.28,239.97 445.89,219.19 447.51,227.80 449.13,231.64 450.75,244.71 452.36,244.22 453.98,235.43 455.60,231.04 457.21,229.39 458.83,240.75 460.45,256.25 462.06,285.91 463.68,286.25 465.30,254.00 466.91,233.13 468.53,269.62 470.15,274.12 471.77,269.15 473.38,257.01 475.00,248.73 476.62,254.74 478.23,232.57 479.85,227.85 481.47,262.60 483.08,263.06 484.70,225.57 486.32,244.89 487.93,281.58 489.55,247.09 491.17,242.21 492.79,273.05 494.40,252.60 496.02,231.72 497.64,216.87 499.25,232.20 500.87,236.37 502.49,228.49 504.10,257.53 505.72,256.00 507.34,245.35 508.95,251.28 510.57,266.44 512.19,261.26 513.81,246.78 515.42,272.29 517.04,253.62 518.66,247.26 520.27,252.25 521.89,254.55 523.51,279.45 525.12,252.17 526.74,215.51 528.36,211.79 529.97,226.13 531.59,265.56 533.21,258.35 534.83,236.18 536.44,246.11 538.06,247.98 539.68,274.02 541.29,272.16 542.91,250.57 544.53,245.19 546.14,262.42 547.76,261.42 549.38,264.77 550.99,263.94 552.61,230.39 554.23,229.90 555.85,234.27 557.46,237.68 559.08,218.74 560.70,217.65 562.31,230.14 563.93,235.52 565.55,235.47 567.16,258.90 568.78,263.15 570.40,257.69 572.01,238.51 573.63,234.86 575.25,213.01 576.87,241.80 578.48,270.64 580.10,253.94 581.72,250.92 583.33,249.35 584.95,227.01 586.57,258.72 588.18,250.25 589.80,229.24 591.42,252.09 593.03,249.72 594.65,264.55 596.27,268.55 597.89,259.97 599.50,247.05 601.12,244.39 602.74,253.50 604.35,263.90 605.97,256.01 607.59,254.80 609.20,240.70 610.82,222.53 612.44,252.30 614.05,248.10 615.67,212.12 617.29,209.70 618.91,196.42 620.52,221.44 622.14,257.67 623.76,239.40 625.37,225.22 626.99,221.64 628.61,214.71 630.22,205.18 631.84,199.88 633.46,215.68 635.07,205.73 636.69,193.03 638.31,211.19 639.93,252.67 641.54,259.69 643.16,226.04 644.78,220.13 646.39,243.90 648.01,215.87 649.63,217.91 651.24,211.31 652.86,202.68 654.48,224.40 656.09,207.21 657.71,202.11 659.33,237.06 660.95,229.93 662.56,185.87 664.18,204.88 665.80,215.32 667.41,213.00 669.03,234.76 670.65,233.09' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(2,0,0); AIC = 1395.6; BIC = 1411.2' data-id='ARIMA(2,0,0)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e377' points='82.09,144.66 83.70,139.30 85.32,150.13 86.94,147.70 88.56,139.28 90.17,147.59 91.79,145.40 93.41,143.18 95.02,143.70 96.64,153.16 98.26,158.98 99.87,153.43 101.49,151.49 103.11,164.13 104.72,157.51 106.34,162.79 107.96,159.27 109.58,154.63 111.19,165.80 112.81,164.54 114.43,167.17 116.04,162.54 117.66,166.25 119.28,159.83 120.89,155.02 122.51,163.50 124.13,165.85 125.74,167.92 127.36,172.63 128.98,174.65 130.60,169.63 132.21,175.40 133.83,182.68 135.45,179.05 137.06,179.83 138.68,190.60 140.30,192.44 141.91,181.67 143.53,196.09 145.15,191.41 146.76,183.60 148.38,191.02 150.00,186.57 151.62,180.93 153.23,186.17 154.85,195.26 156.47,195.54 158.08,189.38 159.70,195.11 161.32,203.63 162.93,205.64 164.55,197.18 166.17,203.04 167.78,204.77 169.40,204.34 171.02,206.17 172.64,210.21 174.25,204.37 175.87,198.74 177.49,198.17 179.10,199.90 180.72,203.45 182.34,213.42 183.95,203.24 185.57,207.89 187.19,217.88 188.80,214.28 190.42,204.48 192.04,212.90 193.66,219.11 195.27,223.76 196.89,236.81 198.51,234.18 200.12,230.46 201.74,227.90 203.36,236.79 204.97,232.62 206.59,232.55 208.21,226.84 209.82,229.56 211.44,224.36 213.06,220.84 214.68,222.16 216.29,214.03 217.91,218.59 219.53,225.09 221.14,217.66 222.76,214.16 224.38,222.29 225.99,226.06 227.61,234.91 229.23,232.64 230.84,228.56 232.46,216.88 234.08,224.28 235.70,227.95 237.31,219.74 238.93,217.44 240.55,213.01 242.16,213.42 243.78,209.69 245.40,216.33 247.01,216.50 248.63,220.65 250.25,226.47 251.86,234.27 253.48,235.05 255.10,236.02 256.72,228.02 258.33,233.61 259.95,236.19 261.57,231.36 263.18,235.17 264.80,231.36 266.42,224.72 268.03,225.46 269.65,231.22 271.27,241.36 272.88,238.50 274.50,244.79 276.12,240.98 277.74,246.44 279.35,239.58 280.97,242.90 282.59,242.41 284.20,251.46 285.82,243.40 287.44,252.74 289.05,248.53 290.67,248.85 292.29,247.66 293.90,238.87 295.52,239.90 297.14,237.36 298.76,236.48 300.37,243.08 301.99,246.73 303.61,248.40 305.22,249.04 306.84,250.24 308.46,240.21 310.07,241.94 311.69,243.16 313.31,253.78 314.92,247.18 316.54,253.71 318.16,261.59 319.77,262.08 321.39,259.08 323.01,267.30 324.63,258.63 326.24,265.05 327.86,262.35 329.48,258.56 331.09,261.15 332.71,257.53 334.33,252.68 335.94,249.20 337.56,243.48 339.18,251.00 340.79,248.61 342.41,247.70 344.03,246.06 345.65,256.50 347.26,250.78 348.88,254.14 350.50,245.72 352.11,248.58 353.73,246.06 355.35,241.37 356.96,248.04 358.58,246.37 360.20,256.50 361.81,254.18 363.43,246.45 365.05,250.84 366.67,258.22 368.28,261.09 369.90,256.59 371.52,255.33 373.13,258.43 374.75,252.07 376.37,256.83 377.98,250.35 379.60,255.83 381.22,254.58 382.83,261.54 384.45,263.10 386.07,259.91 387.69,261.12 389.30,262.98 390.92,265.87 392.54,275.99 394.15,270.09 395.77,275.23 397.39,272.35 399.00,273.48 400.62,267.28 402.24,272.58 403.85,275.30 405.47,274.48 407.09,272.13 408.71,277.20 410.32,275.66 411.94,278.91 413.56,275.09 415.17,277.07 416.79,272.16 418.41,275.31 420.02,276.84 421.64,271.84 423.26,268.17 424.87,266.31 426.49,264.21 428.11,269.91 429.73,277.05 431.34,275.95 432.96,270.14 434.58,267.13 436.19,259.58 437.81,268.29 439.43,265.38 441.04,255.91 442.66,254.37 444.28,249.78 445.89,243.77 447.51,249.64 449.13,251.88 450.75,256.17 452.36,258.48 453.98,256.73 455.60,259.70 457.21,260.23 458.83,267.15 460.45,268.71 462.06,275.53 463.68,272.03 465.30,266.69 466.91,256.59 468.53,266.75 470.15,266.22 471.77,261.94 473.38,258.97 475.00,253.76 476.62,256.43 478.23,253.15 479.85,250.84 481.47,258.65 483.08,261.58 484.70,252.55 486.32,253.09 487.93,264.30 489.55,256.69 491.17,249.08 492.79,259.07 494.40,252.01 496.02,248.39 497.64,246.49 499.25,250.92 500.87,258.04 502.49,254.35 504.10,263.18 505.72,264.32 507.34,260.60 508.95,262.64 510.57,263.39 512.19,265.77 513.81,256.12 515.42,263.70 517.04,258.89 518.66,254.12 520.27,257.92 521.89,253.60 523.51,258.74 525.12,253.30 526.74,243.52 528.36,247.46 529.97,251.39 531.59,261.32 533.21,263.12 534.83,253.69 536.44,259.99 538.06,257.63 539.68,263.07 541.29,261.18 542.91,254.77 544.53,250.98 546.14,253.79 547.76,253.15 549.38,247.47 550.99,249.66 552.61,237.55 554.23,239.47 555.85,239.12 557.46,243.46 559.08,239.42 560.70,242.24 562.31,245.79 563.93,252.12 565.55,249.22 567.16,257.24 568.78,254.21 570.40,255.10 572.01,246.33 573.63,250.44 575.25,245.56 576.87,251.96 578.48,260.29 580.10,256.02 581.72,250.64 583.33,255.01 584.95,246.28 586.57,253.11 588.18,255.93 589.80,245.48 591.42,254.76 593.03,251.46 594.65,253.55 596.27,251.71 597.89,247.67 599.50,242.10 601.12,240.18 602.74,239.04 604.35,239.66 605.97,234.15 607.59,229.38 609.20,226.62 610.82,217.92 612.44,221.48 614.05,222.64 615.67,209.45 617.29,211.63 618.91,210.53 620.52,213.37 622.14,223.26 623.76,216.34 625.37,209.66 626.99,209.00 628.61,206.22 630.22,206.97 631.84,204.54 633.46,211.32 635.07,211.21 636.69,210.90 638.31,214.35 639.93,223.82 641.54,223.15 643.16,214.88 644.78,205.59 646.39,214.58 648.01,204.79 649.63,202.81 651.24,204.78 652.86,198.85 654.48,206.58 656.09,204.24 657.71,200.60 659.33,207.41 660.95,210.91 662.56,194.62 664.18,197.34 665.80,209.62 667.41,207.10 669.03,218.06 670.65,214.58' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(2,0,1); AIC = 1324.83; BIC = 1344.33' data-id='ARIMA(2,0,1)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e378' points='82.09,146.28 83.70,140.57 85.32,143.93 86.94,153.58 88.56,128.82 90.17,140.96 91.79,112.56 93.41,110.03 95.02,129.94 96.64,145.87 98.26,136.52 99.87,149.15 101.49,126.10 103.11,177.20 104.72,156.24 106.34,141.81 107.96,148.69 109.58,131.00 111.19,174.90 112.81,161.94 114.43,189.20 116.04,171.21 117.66,154.91 119.28,140.71 120.89,122.36 122.51,138.25 124.13,148.60 125.74,162.46 127.36,152.31 128.98,151.92 130.60,152.33 132.21,154.84 133.83,153.82 135.45,158.73 137.06,177.83 138.68,167.62 140.30,206.23 141.91,182.15 143.53,200.36 145.15,208.70 146.76,187.14 148.38,183.49 150.00,159.24 151.62,157.22 153.23,179.69 154.85,179.19 156.47,169.64 158.08,163.68 159.70,196.06 161.32,195.09 162.93,204.82 164.55,182.53 166.17,193.50 167.78,197.36 169.40,217.62 171.02,226.46 172.64,229.15 174.25,204.29 175.87,178.15 177.49,165.44 179.10,200.35 180.72,181.20 182.34,198.24 183.95,194.70 185.57,216.12 187.19,207.35 188.80,189.68 190.42,152.37 192.04,157.22 193.66,200.35 195.27,217.88 196.89,250.23 198.51,223.70 200.12,235.25 201.74,226.19 203.36,262.89 204.97,242.03 206.59,259.08 208.21,243.88 209.82,248.33 211.44,248.73 213.06,215.45 214.68,216.65 216.29,218.31 217.91,221.79 219.53,214.12 221.14,191.07 222.76,170.44 224.38,214.78 225.99,237.21 227.61,276.39 229.23,238.16 230.84,235.25 232.46,227.07 234.08,244.80 235.70,261.59 237.31,232.54 238.93,229.60 240.55,196.40 242.16,201.53 243.78,176.70 245.40,183.27 247.01,174.62 248.63,195.76 250.25,215.24 251.86,251.50 253.48,230.25 255.10,237.88 256.72,227.59 258.33,232.55 259.95,256.79 261.57,249.70 263.18,244.00 264.80,215.50 266.42,183.69 268.03,201.71 269.65,202.49 271.27,245.15 272.88,224.15 274.50,261.16 276.12,232.89 277.74,248.91 279.35,213.41 280.97,246.67 282.59,217.63 284.20,265.36 285.82,238.86 287.44,274.08 289.05,278.96 290.67,267.28 292.29,269.65 293.90,237.95 295.52,222.85 297.14,215.64 298.76,214.38 300.37,235.01 301.99,249.71 303.61,275.25 305.22,257.04 306.84,253.91 308.46,207.14 310.07,234.18 311.69,209.44 313.31,231.44 314.92,223.00 316.54,244.33 318.16,249.84 319.77,277.17 321.39,245.96 323.01,285.98 324.63,265.32 326.24,276.72 327.86,284.53 329.48,282.39 331.09,293.01 332.71,285.81 334.33,250.24 335.94,256.01 337.56,237.75 339.18,255.37 340.79,227.95 342.41,250.97 344.03,236.73 345.65,287.42 347.26,256.11 348.88,276.48 350.50,254.53 352.11,241.41 353.73,240.87 355.35,206.60 356.96,247.66 358.58,251.52 360.20,255.18 361.81,238.02 363.43,223.15 365.05,249.43 366.67,265.92 368.28,269.40 369.90,272.16 371.52,253.04 373.13,278.07 374.75,239.03 376.37,257.67 377.98,222.35 379.60,245.50 381.22,249.16 382.83,259.46 384.45,257.77 386.07,239.23 387.69,228.13 389.30,256.60 390.92,247.85 392.54,289.63 394.15,269.56 395.77,294.66 397.39,265.39 399.00,270.65 400.62,259.09 402.24,274.64 403.85,269.03 405.47,275.35 407.09,262.13 408.71,288.44 410.32,277.41 411.94,297.68 413.56,273.95 415.17,284.83 416.79,286.49 418.41,297.63 420.02,300.81 421.64,283.54 423.26,254.17 424.87,243.76 426.49,254.60 428.11,284.71 429.73,307.13 431.34,311.54 432.96,270.78 434.58,287.32 436.19,284.41 437.81,303.53 439.43,305.63 441.04,280.72 442.66,253.31 444.28,240.15 445.89,215.57 447.51,233.30 449.13,243.74 450.75,245.12 452.36,249.18 453.98,228.09 455.60,239.14 457.21,232.14 458.83,273.39 460.45,289.16 462.06,316.36 463.68,273.02 465.30,276.63 466.91,259.42 468.53,291.86 470.15,296.35 471.77,273.82 473.38,275.87 475.00,256.27 476.62,243.94 478.23,244.33 479.85,258.71 481.47,261.91 483.08,253.48 484.70,262.73 486.32,265.10 487.93,270.93 489.55,280.29 491.17,256.81 492.79,279.80 494.40,243.34 496.02,224.52 497.64,230.47 499.25,219.10 500.87,250.68 502.49,248.08 504.10,263.20 505.72,267.70 507.34,258.42 508.95,284.05 510.57,261.16 512.19,291.02 513.81,265.55 515.42,274.89 517.04,273.86 518.66,249.00 520.27,283.50 521.89,280.56 523.51,268.88 525.12,243.99 526.74,200.92 528.36,226.61 529.97,255.99 531.59,258.73 533.21,274.56 534.83,236.05 536.44,270.17 538.06,276.67 539.68,290.15 541.29,275.59 542.91,266.80 544.53,268.27 546.14,271.65 547.76,297.06 549.38,260.17 550.99,267.40 552.61,225.27 554.23,242.00 555.85,225.87 557.46,230.28 559.08,210.66 560.70,228.55 562.31,219.98 563.93,256.10 565.55,247.11 567.16,287.28 568.78,259.70 570.40,272.44 572.01,223.12 573.63,229.56 575.25,239.07 576.87,261.21 578.48,272.69 580.10,280.40 581.72,238.76 583.33,259.62 584.95,255.25 586.57,242.82 588.18,269.71 589.80,233.79 591.42,270.85 593.03,271.91 594.65,285.92 596.27,278.49 597.89,268.20 599.50,254.77 601.12,266.23 602.74,269.31 604.35,275.83 605.97,273.30 607.59,242.54 609.20,245.56 610.82,248.93 612.44,236.31 614.05,241.56 615.67,192.12 617.29,187.34 618.91,216.92 620.52,230.70 622.14,252.79 623.76,237.90 625.37,213.94 626.99,213.70 628.61,188.03 630.22,198.52 631.84,188.05 633.46,194.11 635.07,181.18 636.69,201.32 638.31,231.67 639.93,262.20 641.54,235.72 643.16,246.35 644.78,210.65 646.39,232.16 648.01,215.23 649.63,189.17 651.24,211.36 652.86,194.34 654.48,199.13 656.09,202.20 657.71,220.20 659.33,206.19 660.95,206.50 662.56,185.23 664.18,182.01 665.80,221.24 667.41,206.28 669.03,230.63 670.65,242.57' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(2,1,0); AIC = 1380.8; BIC = 1392.49' data-id='ARIMA(2,1,0)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e379' points='82.09,146.37 83.70,133.92 85.32,143.81 86.94,148.07 88.56,134.31 90.17,143.42 91.79,143.58 93.41,145.15 95.02,142.80 96.64,147.79 98.26,156.92 99.87,156.30 101.49,146.17 103.11,163.91 104.72,152.93 106.34,159.48 107.96,162.20 109.58,151.72 111.19,164.34 112.81,160.07 114.43,165.44 116.04,156.00 117.66,161.96 119.28,161.14 120.89,153.37 122.51,162.35 124.13,165.66 125.74,165.94 127.36,168.71 128.98,176.10 130.60,170.63 132.21,172.18 133.83,182.95 135.45,182.94 137.06,178.51 138.68,186.05 140.30,197.82 141.91,177.23 143.53,191.94 145.15,192.89 146.76,179.05 148.38,187.46 150.00,188.49 151.62,183.53 153.23,184.25 154.85,192.05 156.47,198.44 158.08,192.63 159.70,194.50 161.32,199.80 162.93,208.31 164.55,197.44 166.17,202.78 167.78,205.64 169.40,204.94 171.02,201.88 172.64,206.55 174.25,203.20 175.87,198.88 177.49,199.66 179.10,202.59 180.72,198.36 182.34,216.20 183.95,206.57 185.57,204.39 187.19,213.84 188.80,218.97 190.42,208.23 192.04,217.31 193.66,226.24 195.27,223.49 196.89,236.84 198.51,235.81 200.12,236.15 201.74,227.54 203.36,238.09 204.97,231.71 206.59,234.64 208.21,224.97 209.82,228.77 211.44,224.74 213.06,217.96 214.68,225.40 216.29,216.40 217.91,215.08 219.53,225.11 221.14,222.96 222.76,217.93 224.38,227.56 225.99,225.63 227.61,233.16 229.23,229.07 230.84,233.32 232.46,217.99 234.08,220.23 235.70,226.37 237.31,217.05 238.93,216.59 240.55,211.07 242.16,215.97 243.78,209.72 245.40,219.06 247.01,220.46 248.63,225.11 250.25,227.64 251.86,234.96 253.48,232.87 255.10,239.94 256.72,229.47 258.33,232.36 259.95,238.05 261.57,228.98 263.18,232.41 264.80,232.83 266.42,228.93 268.03,230.76 269.65,231.47 271.27,245.43 272.88,238.45 274.50,248.17 276.12,239.41 277.74,249.78 279.35,241.80 280.97,248.33 282.59,241.40 284.20,257.71 285.82,243.22 287.44,254.55 289.05,248.47 290.67,245.92 292.29,248.88 293.90,238.39 295.52,241.17 297.14,241.97 298.76,239.55 300.37,245.30 301.99,248.30 303.61,249.03 305.22,245.46 306.84,252.51 308.46,242.82 310.07,247.68 311.69,242.41 313.31,260.04 314.92,253.25 316.54,256.42 318.16,263.98 319.77,268.34 321.39,258.69 323.01,273.43 324.63,260.02 326.24,266.64 327.86,266.02 329.48,258.74 331.09,260.67 332.71,257.39 334.33,252.54 335.94,254.23 337.56,243.72 339.18,252.39 340.79,250.75 342.41,254.00 344.03,245.25 345.65,259.11 347.26,248.47 348.88,256.56 350.50,245.11 352.11,248.18 353.73,250.43 355.35,242.79 356.96,253.89 358.58,245.75 360.20,255.07 361.81,258.92 363.43,251.86 365.05,253.45 366.67,257.85 368.28,262.58 369.90,259.00 371.52,254.05 373.13,261.46 374.75,250.67 376.37,260.81 377.98,251.81 379.60,260.82 381.22,256.47 382.83,261.90 384.45,265.86 386.07,263.32 387.69,265.87 389.30,269.83 390.92,266.27 392.54,281.20 394.15,271.52 395.77,278.11 397.39,272.90 399.00,279.34 400.62,271.38 402.24,275.19 403.85,278.10 405.47,280.28 407.09,275.18 408.71,282.07 410.32,277.61 411.94,283.14 413.56,276.77 415.17,282.39 416.79,275.73 418.41,275.85 420.02,278.18 421.64,274.06 423.26,271.28 424.87,273.40 426.49,271.04 428.11,272.40 429.73,277.57 431.34,277.40 432.96,270.73 434.58,273.87 436.19,259.56 437.81,265.85 439.43,265.93 441.04,254.48 442.66,253.44 444.28,254.17 445.89,246.58 447.51,253.50 449.13,254.59 450.75,257.71 452.36,262.44 453.98,260.41 455.60,266.39 457.21,264.98 458.83,273.49 460.45,269.70 462.06,275.53 463.68,271.67 465.30,274.64 466.91,258.56 468.53,267.18 470.15,266.92 471.77,262.05 473.38,262.05 475.00,254.11 476.62,258.49 478.23,259.28 479.85,253.40 481.47,256.97 483.08,265.49 484.70,259.15 486.32,250.82 487.93,263.93 489.55,262.50 491.17,246.66 492.79,258.87 494.40,253.24 496.02,252.89 497.64,251.48 499.25,252.37 500.87,265.00 502.49,257.15 504.10,264.64 505.72,268.52 507.34,264.05 508.95,265.86 510.57,262.48 512.19,271.78 513.81,255.87 515.42,264.50 517.04,262.84 518.66,254.75 520.27,261.79 521.89,251.78 523.51,256.54 525.12,257.87 526.74,248.60 528.36,254.71 529.97,253.95 531.59,260.51 533.21,269.22 534.83,256.15 536.44,265.51 538.06,258.97 539.68,262.68 541.29,262.56 542.91,258.54 544.53,252.33 546.14,253.18 547.76,254.63 549.38,243.18 550.99,251.92 552.61,238.69 554.23,242.15 555.85,238.65 557.46,246.77 559.08,243.37 560.70,247.02 562.31,247.47 563.93,258.15 565.55,250.06 567.16,259.25 568.78,252.70 570.40,259.09 572.01,247.22 573.63,257.16 575.25,251.75 576.87,252.34 578.48,261.26 580.10,260.38 581.72,250.28 583.33,261.76 584.95,249.78 586.57,252.62 588.18,263.89 589.80,247.71 591.42,259.47 593.03,253.81 594.65,254.70 596.27,252.40 597.89,250.06 599.50,244.71 601.12,242.76 602.74,238.52 604.35,239.37 605.97,233.97 607.59,227.33 609.20,229.69 610.82,217.98 612.44,216.26 614.05,224.59 615.67,210.09 617.29,214.88 618.91,215.21 620.52,209.99 622.14,220.72 623.76,216.10 625.37,208.44 626.99,209.30 628.61,205.92 630.22,210.53 631.84,204.97 633.46,212.71 635.07,215.11 636.69,216.48 638.31,214.23 639.93,220.43 641.54,220.77 643.16,218.78 644.78,201.03 646.39,214.39 648.01,205.32 649.63,200.90 651.24,208.22 652.86,197.35 654.48,206.09 656.09,207.47 657.71,200.44 659.33,201.92 660.95,214.36 662.56,197.28 664.18,195.23 665.80,214.14 667.41,208.14 669.03,225.80 670.65,242.57' fill='none' stroke='#BDC3C7' stroke-opacity='0.6' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' title='model: ARIMA(2,1,1); AIC = 1318.03; BIC = 1333.62' data-id='ARIMA(2,1,1)'/>\n   <polyline id='svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_e380' points='82.09,141.17 83.70,139.37 85.32,146.36 86.94,143.08 88.56,138.52 90.17,144.05 91.79,142.13 93.41,141.33 95.02,144.19 96.64,152.17 98.26,155.26 99.87,151.45 101.49,152.54 103.11,160.65 104.72,156.64 106.34,159.60 107.96,156.66 109.58,155.72 111.19,163.79 112.81,163.12 114.43,164.05 116.04,160.91 117.66,161.96 119.28,156.68 120.89,155.27 122.51,162.32 124.13,164.86 125.74,167.53 127.36,171.25 128.98,172.09 130.60,170.13 132.21,175.90 133.83,180.72 135.45,178.95 137.06,182.00 138.68,189.90 140.30,189.63 141.91,184.49 143.53,193.64 145.15,188.71 146.76,184.20 148.38,188.38 150.00,184.27 151.62,181.90 153.23,187.81 154.85,194.33 156.47,193.66 158.08,191.01 159.70,197.23 161.32,203.53 162.93,203.49 164.55,198.68 166.17,203.43 167.78,204.76 169.40,204.97 171.02,206.69 172.64,207.88 174.25,202.21 175.87,198.09 177.49,198.44 179.10,201.05 180.72,205.39 182.34,210.89 183.95,204.96 185.57,210.30 187.19,216.37 188.80,212.16 190.42,207.45 192.04,215.74 193.66,222.31 195.27,228.57 196.89,237.39 198.51,234.57 200.12,231.78 201.74,231.64 203.36,237.18 204.97,233.69 206.59,232.38 208.21,228.32 209.82,228.85 211.44,224.11 213.06,221.33 214.68,220.82 216.29,216.12 217.91,220.46 219.53,223.50 221.14,217.88 222.76,217.63 224.38,225.11 225.99,229.67 227.61,235.22 229.23,231.91 230.84,226.70 232.46,219.80 234.08,225.46 235.70,225.97 237.31,218.96 238.93,216.15 240.55,212.78 242.16,212.68 243.78,211.56 245.40,216.97 247.01,218.62 248.63,223.62 250.25,229.79 251.86,235.70 253.48,236.04 255.10,235.30 256.72,230.74 258.33,235.17 259.95,236.09 261.57,232.94 263.18,234.43 264.80,230.25 266.42,226.07 268.03,228.65 269.65,235.19 271.27,242.43 272.88,241.58 274.50,245.61 276.12,243.63 277.74,246.35 279.35,242.15 280.97,245.03 282.59,246.38 284.20,251.81 285.82,247.68 287.44,253.58 289.05,250.23 290.67,249.55 292.29,246.61 293.90,240.13 295.52,240.38 297.14,238.82 298.76,239.99 300.37,245.85 301.99,248.94 303.61,250.19 305.22,250.25 306.84,248.97 308.46,242.18 310.07,244.41 311.69,247.49 313.31,254.49 314.92,251.69 316.54,258.40 318.16,264.31 319.77,264.45 321.39,263.66 323.01,268.16 324.63,262.97 326.24,266.89 327.86,264.09 329.48,261.50 331.09,262.13 332.71,257.97 334.33,253.28 335.94,249.84 337.56,247.15 339.18,252.12 340.79,250.20 342.41,249.77 344.03,250.59 345.65,257.07 347.26,253.02 348.88,253.65 350.50,247.80 352.11,249.11 353.73,246.62 355.35,244.73 356.96,249.91 358.58,250.72 360.20,257.26 361.81,254.20 363.43,250.03 365.05,255.10 366.67,260.89 368.28,261.90 369.90,258.33 371.52,257.70 373.13,258.72 374.75,254.72 376.37,257.11 377.98,253.61 379.60,257.88 381.22,258.59 382.83,263.91 384.45,264.46 386.07,262.60 387.69,264.33 389.30,267.01 390.92,271.18 392.54,277.63 394.15,274.24 395.77,277.37 397.39,275.18 399.00,275.01 400.62,271.82 402.24,276.32 403.85,278.10 405.47,277.31 407.09,276.71 408.71,280.30 410.32,279.69 411.94,281.30 413.56,278.66 415.17,279.24 416.79,276.33 418.41,278.59 420.02,278.28 421.64,273.59 423.26,270.47 424.87,269.17 426.49,269.43 428.11,275.14 429.73,279.71 431.34,277.23 432.96,271.84 434.58,268.36 436.19,264.36 437.81,269.40 439.43,264.77 441.04,256.93 442.66,254.39 444.28,250.04 445.89,247.19 447.51,252.40 449.13,255.21 450.75,258.88 452.36,260.46 453.98,260.07 455.60,262.99 457.21,265.25 458.83,271.16 460.45,273.53 462.06,277.26 463.68,272.92 465.30,267.31 466.91,262.04 468.53,269.08 470.15,267.38 471.77,263.17 473.38,259.86 475.00,256.37 476.62,257.60 478.23,255.20 479.85,255.34 481.47,261.25 483.08,261.55 484.70,255.58 486.32,257.93 487.93,264.09 489.55,257.19 491.17,253.26 492.79,258.75 494.40,252.64 496.02,249.95 497.64,250.05 499.25,254.94 500.87,260.02 502.49,259.37 504.10,266.00 505.72,266.17 507.34,263.96 508.95,265.68 510.57,266.24 512.19,266.19 513.81,260.37 515.42,264.62 517.04,260.13 518.66,257.23 520.27,259.26 521.89,256.67 523.51,258.63 525.12,252.73 526.74,246.89 528.36,251.46 529.97,256.68 531.59,263.95 533.21,263.55 534.83,257.97 536.44,262.44 538.06,261.70 539.68,264.80 541.29,261.70 542.91,256.22 544.53,253.90 546.14,255.41 547.76,253.51 549.38,248.95 550.99,247.88 552.61,239.36 554.23,240.94 555.85,241.47 557.46,244.06 559.08,242.10 560.70,245.46 562.31,249.54 563.93,254.08 565.55,253.57 567.16,258.74 568.78,256.15 570.40,255.01 572.01,249.31 573.63,251.86 575.25,250.24 576.87,256.57 578.48,261.47 580.10,257.26 581.72,253.85 583.33,255.62 584.95,250.81 586.57,256.03 588.18,256.36 589.80,250.63 591.42,256.91 593.03,254.77 594.65,255.55 596.27,252.86 597.89,248.45 599.50,243.82 601.12,242.09 602.74,240.91 604.35,239.68 605.97,234.15 607.59,229.42 609.20,225.58 610.82,219.77 612.44,221.79 614.05,219.75 615.67,210.50 617.29,212.24 618.91,212.62 620.52,216.40 622.14,221.63 623.76,214.83 625.37,209.52 626.99,208.41 628.61,206.49 630.22,206.97 631.84,206.76 633.46,211.90 635.07,212.20 636.69,213.40 638.31,217.92 639.93,224.13 641.54,221.28 643.16,213.43 644.78,207.81 646.39,212.15 648.01,204.53 649.63,203.11 651.24,203.70 652.86,200.75 654.48,205.88 656.09,203.94 657.71,202.41 659.33,207.58 660.95,207.95 662.56,195.72 664.18,198.40 665.80,209.96 667.41,210.97 669.03,225.00 670.65,242.57' fill='none' stroke='#E74C3C' stroke-opacity='0.9' stroke-width='3.2' stroke-linejoin='round' stroke-linecap='butt' title='best model: ARIMA(0,1,1); AIC = 1316.33; BIC = 1324.13' data-id='ARIMA(0,1,1)'/>\n  <\/g>\n  <g clip-path='url(#svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6_c1)'>\n   <text x='36.73' y='338.24' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>5.0<\/text>\n   <text x='36.73' y='269.83' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>7.5<\/text>\n   <text x='32.33' y='201.41' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>10.0<\/text>\n   <text x='32.33' y='133' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>12.5<\/text>\n   <text x='32.33' y='64.58' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>15.0<\/text>\n   <text transform='translate(90.82,377.20) rotate(-45.00)' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>Jul 2024<\/text>\n   <text transform='translate(189.00,379.27) rotate(-45.00)' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>Sep 2024<\/text>\n   <text transform='translate(286.25,380.65) rotate(-45.00)' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>Nov 2024<\/text>\n   <text transform='translate(387.30,378.23) rotate(-45.00)' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>Jan 2025<\/text>\n   <text transform='translate(480.63,380.31) rotate(-45.00)' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>Mar 2025<\/text>\n   <text transform='translate(578.22,381.34) rotate(-45.00)' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>May 2025<\/text>\n   <text transform='translate(680.99,377.20) rotate(-45.00)' font-size='6.6pt' font-family='Times New Roman' fill='#34495E' fill-opacity='1'>Jul 2025<\/text>\n   <text x='349.42' y='392.71' font-size='8.25pt' font-weight='bold' font-family='Times New Roman' fill='#2C3E50' fill-opacity='1'>Time Index<\/text>\n   <text transform='translate(27.21,267.79) rotate(-90.00)' font-size='8.25pt' font-weight='bold' font-family='Times New Roman' fill='#2C3E50' fill-opacity='1'>Simulated Response Values<\/text>\n   <text x='52.66' y='43.82' font-size='7.5pt' font-family='Times New Roman' fill='#7F8C8D' fill-opacity='1'>Comparing 14 successful ARIMA models • Best performing models highlighted<\/text>\n   <text x='52.66' y='29.19' font-size='10.5pt' font-weight='bold' font-family='Times New Roman' fill='#2C3E50' fill-opacity='1'>ARIMA Model Grid Search: Simulated Time Series Analysis<\/text>\n   <text x='372.45' y='410.35' font-size='6pt' font-family='Times New Roman' fill='#95A5A6' fill-opacity='1'>Models tested: p∈[0,2], d∈[0,1], q∈[0,2] • Original data shown in dark gray • Total observations: 365<\/text>\n  <\/g>\n <\/g>\n<\/svg>","js":null,"uid":"svg_0a3ab3ba_c237_4206_9316_b6f8a10224e6","ratio":1.666666666666667,"settings":{"tooltip":{"css":".tooltip_SVGID_ { padding:5px;background:black;color:white;border-radius:2px;text-align:left; ; position:absolute;pointer-events:none;z-index:999;}","placement":"doc","opacity":0.9,"offx":10,"offy":10,"use_cursor_pos":true,"use_fill":false,"use_stroke":false,"delay_over":200,"delay_out":500},"hover":{"css":".hover_data_SVGID_ { cursor:pointer;stroke-width:4;stroke-opacity:1;fill-opacity:1;r:4px; }","reactive":false,"nearest_distance":null},"hover_inv":{"css":".hover_inv_SVGID_ { opacity:0.1; }"},"hover_key":{"css":".hover_key_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_key_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_key_SVGID_ { fill:orange;stroke:black; }\nline.hover_key_SVGID_, polyline.hover_key_SVGID_ { fill:none;stroke:orange; }\nrect.hover_key_SVGID_, polygon.hover_key_SVGID_, path.hover_key_SVGID_ { fill:orange;stroke:none; }\nimage.hover_key_SVGID_ { stroke:orange; }","reactive":true},"hover_theme":{"css":".hover_theme_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_theme_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_theme_SVGID_ { fill:orange;stroke:black; }\nline.hover_theme_SVGID_, polyline.hover_theme_SVGID_ { fill:none;stroke:orange; }\nrect.hover_theme_SVGID_, polygon.hover_theme_SVGID_, path.hover_theme_SVGID_ { fill:orange;stroke:none; }\nimage.hover_theme_SVGID_ { stroke:orange; }","reactive":true},"select":{"css":".select_data_SVGID_ { fill:red;stroke:gray; }","type":"none","only_shiny":true,"selected":[]},"select_inv":{"css":""},"select_key":{"css":".select_key_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_key_SVGID_ { stroke:none;fill:red; }\ncircle.select_key_SVGID_ { fill:red;stroke:black; }\nline.select_key_SVGID_, polyline.select_key_SVGID_ { fill:none;stroke:red; }\nrect.select_key_SVGID_, polygon.select_key_SVGID_, path.select_key_SVGID_ { fill:red;stroke:none; }\nimage.select_key_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"select_theme":{"css":".select_theme_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_theme_SVGID_ { stroke:none;fill:red; }\ncircle.select_theme_SVGID_ { fill:red;stroke:black; }\nline.select_theme_SVGID_, polyline.select_theme_SVGID_ { fill:none;stroke:red; }\nrect.select_theme_SVGID_, polygon.select_theme_SVGID_, path.select_theme_SVGID_ { fill:red;stroke:none; }\nimage.select_theme_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"zoom":{"min":1,"max":1,"duration":300},"toolbar":{"position":"topright","pngname":"diagram","tooltips":null,"fixed":false,"hidden":[],"delay_over":200,"delay_out":500},"sizing":{"rescale":true,"width":1}}},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<hr></section><section id="disclaimer" class="level2" data-number="1.6"><h2 data-number="1.6" class="anchored" data-anchor-id="disclaimer">
<span class="header-section-number">1.6</span> Disclaimer</h2>
<p>This is just a toy example of leveraging functional programming and basic hyperparameter tuning for time series in R, and some of my learning competencies about data visualization in R and how to get deeper in it.</p>
<p>If you are interested to learn more, check out my other gists.</p>


</section></section> ]]></description>
  <category>R</category>
  <category>time-series</category>
  <category>machine-learning</category>
  <category>grid-search</category>
  <guid>https://joshuamarie.com/posts/02-arima-grid-search/</guid>
  <pubDate>Fri, 26 Sep 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/02-arima-grid-search/image.png" medium="image" type="image/png" height="86" width="144"/>
</item>
<item>
  <title>Automatically generate Deep Feedforward Neural Network (DFFNN) module from torch expression</title>
  <dc:creator>Joshua Marie</dc:creator>
  <link>https://joshuamarie.com/posts/01-meta-nn/</link>
  <description><![CDATA[ <div class="page-columns page-rows-contents page-layout-article"><div class="social-share">
<a href="https://twitter.com/share?url=https://joshuamarie.com/01-meta-nn/&amp;text=Neural%20Networks%20generator%20-%20Joshua%20Marie" target="_blank" class="twitter"><i class="fab fa-twitter fa-fw fa-lg"></i></a><a href="https://www.linkedin.com/shareArticle?url=https://joshuamarie.com/01-meta-nn/&amp;title=Neural%20Networks%20generator%20-%20Joshua%20Marie" target="_blank" class="linkedin"><i class="fa-brands fa-linkedin-in fa-fw fa-lg"></i></a>  <a href="mailto:?subject=Neural%20Networks%20generator%20-%20Joshua%20Marie&amp;body=Check%20out%20this%20link:https://joshuamarie.com/01-meta-nn/" target="_blank" class="email"><i class="fa-solid fa-envelope fa-fw fa-lg"></i></a><a href="https://www.facebook.com/sharer.php?u=https://joshuamarie.com/01-meta-nn/" target="_blank" class="facebook"><i class="fab fa-facebook-f fa-fw fa-lg"></i></a><a href="https://reddit.com/submit?url=https://joshuamarie.com/01-meta-nn/&amp;title=Neural%20Networks%20generator%20-%20Joshua%20Marie" target="_blank" class="reddit">   <i class="fa-brands fa-reddit-alien fa-fw fa-lg"></i></a><a href="https://www.stumbleupon.com/submit?url=https://joshuamarie.com/01-meta-nn/&amp;title=Neural%20Networks%20generator%20-%20Joshua%20Marie" target="_blank" class="stumbleupon"><i class="fa-brands fa-stumbleupon fa-fw fa-lg"></i></a><a href="https://www.tumblr.com/share/link?url=https://joshuamarie.com/01-meta-nn/&amp;name=Neural%20Networks%20generator%20-%20Joshua%20Marie" target="_blank" class="tumblr"><i class="fa-brands fa-tumblr fa-fw fa-lg"></i></a><a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' &amp;&amp; mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=Neural Networks generator - Joshua Marie https://joshuamarie.com/01-meta-nn/'}else{return false;}" target="_blank" class="mastodon"><i class="fa-brands fa-mastodon fa-fw fa-lg"></i></a><a href="https://bsky.app/intent/compose?text=https://joshuamarie.com/01-meta-nn/%20Neural%20Networks%20generator%20-%20Joshua%20Marie" target="_blank" class="bsky"><i class="fa-brands fa-bluesky"></i></a>
</div></div>


<section id="generating-torch-dffnn-expression" class="level1" data-number="1"><h1 data-number="1">
<span class="header-section-number">1</span> Generating torch DFFNN expression</h1>
<p>I have a course tutorial, which I discuss the things to get “advance” in R. Code generation is part of it. My blog compiles <em>pointless codes</em> in <em>pointless code series</em>, and this is one of them.</p>
<p>Now, the question is: How do you define neural network architectures in your deep learning projects? Manually write out each layer? Copy-paste and modify existing code? In this part, I wanted to discuss it to you on how to leverage code generation technique that generates ‘torch’ neural network modules in a <em>programmatic</em> approach. This is a handy approach to building flexible, reusable neural network architectures without repetitive code.</p>
<p>I’ll walk through a function that creates DFFNN expressions with customizable architectures, layer by layer, explaining each step along the way.</p>
<hr>
<section id="introduction" class="level2" data-number="1.1"><h2 data-number="1.1" class="anchored" data-anchor-id="introduction">
<span class="header-section-number">1.1</span> Introduction</h2>
<p>The <code>create_nn_module()</code> function dynamically generates <strong>torch neural network module definitions</strong>. Instead of manually writing out layer definitions and forward pass logic, this function builds the code expressions for you.</p>
<p><strong>Key benefits:</strong></p>
<ul>
<li>
<strong>Flexibility</strong>: Change network architecture with a single function call</li>
<li>
<strong>Automation</strong>: Generate multiple network configurations programmatically</li>
<li>
<strong>Experimentation</strong>: Quickly test different architectures in hyperparameter searches</li>
</ul>
<p>This is how it’s done:</p>
<ol type="1">
<li>Define the network architecture (input size, hidden layers, output size)</li>
<li>Specify activation functions for each layer</li>
<li>Programmatically generate the <code>initialize</code> method (layer definitions)</li>
<li>Programmatically generate the <code>forward</code> method (forward pass logic)</li>
<li>Return an <code>nn_module</code> expression ready to be evaluated</li>
</ol>
<p>The packages used:</p>
<ol type="1">
<li>
<a href="https://rlang.r-lib.org/">rlang (v1.1.4)</a> - For metaprogramming tools</li>
<li>
<a href="https://purrr.tidyverse.org/">purrr (v1.0.2)</a> - For functional programming</li>
<li>
<a href="https://glue.tidyverse.org/">glue (v1.7.0)</a> - For string interpolation</li>
<li>
<a href="https://magrittr.tidyverse.org/">magrittr</a> - For pipe operator</li>
<li>
<a href="https://github.com/klmr/box">box (v1.2.0)</a> - For modular code organization</li>
</ol></section><section id="the-complete-function" class="level2" data-number="1.2"><h2 data-number="1.2" class="anchored" data-anchor-id="the-complete-function">
<span class="header-section-number">1.2</span> The Complete Function</h2>
<p>I created <code>create_nn_module()</code> function a while ago and shared it on <a href="https://gist.github.com/kisha126/c19c209fbe5e0532e16520649bf142c2">GitHub Gist</a>. Here’s the function we’ll be analyzing:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">create_nn_module</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nn_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;">"DeepFFN"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hd_neurons</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">no_x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">no_y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="http://klmr.me/box/reference/use.html">use</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rlang</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">new_function</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call2</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caller_env</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">expr</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exprs</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sym</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">is_function</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">env_get_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">purrr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map2</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">reduce</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">set_names</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">compact</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_if</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">keep</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_lgl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">magrittr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">`%&gt;%`</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">no_x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hd_neurons</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">no_y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</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;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call_args</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;"><a href="https://rdrr.io/r/base/match.call.html">match.call</a></span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call_args</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span></span>
<span>    </span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</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;"><a href="https://rdrr.io/r/base/c.html">c</a></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;"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nnf_relu"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hd_neurons</span><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 class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span><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 class="fl" 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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/is.function.html">is.function</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">single_activation</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</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;"><a href="https://rdrr.io/r/base/c.html">c</a></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;"><a href="https://rdrr.io/r/base/rep.html">rep</a></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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">single_activation</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;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hd_neurons</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><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>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</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;">map2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq_along</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span><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>        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/is.function.html">is.function</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><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;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/call.html">is.call</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><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;">&amp;&amp;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" 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="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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/substitute.html">quote</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">c</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</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;"><a href="https://rdrr.io/r/base/character.html">as.character</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" 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="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>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><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;">&amp;&amp;</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;"><a href="https://rdrr.io/r/base/name.html">is.symbol</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/character.html">is.character</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><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 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>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</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;"><a href="https://rdrr.io/r/base/character.html">as.character</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parent_env</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;"><a href="https://rdrr.io/r/base/sys.parent.html">parent.frame</a></span><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>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">env_names</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;"><a href="https://rdrr.io/r/base/ls.html">ls</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>envir <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parent_env</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">matching_names</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">env_names</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span>                    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">keep</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">obj</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;">env_get_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parent_env</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><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 class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span>                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/identical.html">identical</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">obj</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>                    <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>                </span>
<span>                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">matching_names</span><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;">&gt;</span> <span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">matching_names</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/stop.html">stop</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Could not determine function name for activation function"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/character.html">is.character</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/name.html">is.symbol</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/logical.html">is.logical</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/stop.html">stop</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Activation must be a function, string, symbol, NA, or NULL"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>    <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>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">init_body</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;">map2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</span><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 class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">c</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dims</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_name</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;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"out"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fc{i}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call2</span><span 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call2</span><span 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_name</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nn_linear"</span>, <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dims</span><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>    <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>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">init</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;">new_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        args <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><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>        body <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;">call2</span><span 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 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">init_body</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_calls</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;">map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_name</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;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"out"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fc{i}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_fn</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;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span><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 class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span>        </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</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;"><a href="https://rdrr.io/r/base/list.html">list</a></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;">call2</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;">call2</span><span 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_name</span><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 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>        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_fn</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</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;"><a href="https://rdrr.io/r/base/append.html">append</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></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;">call2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_fn</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</span></span>
<span>    <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/unlist.html">unlist</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recursive = FALSE is also valid</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compact</span><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>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forward_body</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;">reduce</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_calls</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acc</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call</span><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>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expr</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acc</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>, .init <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;">expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forward</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;">new_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        args <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">expr</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>        body <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;">call2</span><span 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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forward_body</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nn_module"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nn_name</span>, initialize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">init</span>, forward <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forward</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</details>
</div>
<hr>
<section id="why-box" class="level3" data-number="1.2.1"><h3 data-number="1.2.1" class="anchored" data-anchor-id="why-box">
<span class="header-section-number">1.2.1</span> Why box?</h3>
<p>You’ll notice that I’ve been using another approach to load namespace in R. But, why ‘box’? You need to check out my mini book dedicated on <em>modular programming in R</em>.</p>
</section><section id="but-why-load-dependencies-using-boxuse-inside-a-function" class="level3" data-number="1.2.2"><h3 data-number="1.2.2" class="anchored" data-anchor-id="but-why-load-dependencies-using-boxuse-inside-a-function">
<span class="header-section-number">1.2.2</span> <em>But why load dependencies using <code>box::use()</code> inside a function?</em>
</h3>
<p>Well, a function, or a function call, creates an <strong>environment</strong>, which <em>encloses</em> the objects and operations within it. In other words, we create a <em>closure</em>. This is actually a good practice for several reasons:</p>
<ol type="1">
<li><p><strong>Namespace isolation</strong>: Dependencies loaded inside the function will not make pollution the global environment, or conflicts with any packages loaded. When you load packages required with <code><a href="https://rdrr.io/r/base/library.html">library()</a></code>, inside a function or not, it attaches those packages to your search path, and will mask functions from other packages. With <code><a href="http://klmr.me/box/reference/use.html">box::use()</a></code> inside a function, the imports are scoped only to that function’s or call’s environment.</p></li>
<li><p><strong>Explicit dependencies</strong>: Anyone reading the function immediately knows what external tools it uses. You don’t have to scroll to the top of a script to see what’s loaded.</p></li>
<li><p><strong>Reproducibility</strong>: The function becomes self-contained. If you share just this function, others know exactly what packages they need less hunting through documentation.</p></li>
<li><p><strong>Avoiding conflicts</strong>: Different functions can use different versions or implementations without conflicts. For example, one function might use <code><a href="https://dplyr.tidyverse.org/reference/filter.html">dplyr::filter()</a></code> while another uses <code><a href="https://rdrr.io/r/stats/filter.html">stats::filter()</a></code>, and they won’t interfere with each other.</p></li>
<li><p><strong>Lazy loading</strong>: The packages are only loaded when the function is actually called, not when it’s defined. This can improve script startup time if you have many functions but only use a few.</p></li>
</ol>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>In a nutshell: The ‘box’ package provides explicit, granular imports, making it transparent which namespace to be imported from which packages. It’s like having a well-organized toolbox where each tool is labeled.</p>
</div>
</div>
<hr></section></section><section id="explanations" class="level2" data-number="1.3"><h2 data-number="1.3" class="anchored" data-anchor-id="explanations">
<span class="header-section-number">1.3</span> Explanations</h2>
<p>I’ll be trying to be concise on explaining each layers of the function so that you’ll understand what I did</p>
<section id="step-1-loading-dependencies" class="level3" data-number="1.3.1"><h3 data-number="1.3.1" class="anchored" data-anchor-id="step-1-loading-dependencies">
<span class="header-section-number">1.3.1</span> Step 1: Loading Dependencies</h3>
<p>I use <code><a href="http://klmr.me/box/reference/use.html">box::use()</a></code> to load dependencies:</p>
<ul>
<li>
<strong>rlang</strong>: Improvised Core R programming. One of the core R programming, <em>metaprogramming</em> which includes creating expressions and functions programmatically, are less painful than what base R have.</li>
<li>
<strong>purrr</strong>: Improvised Functional programming utilities.</li>
<li>
<strong>glue</strong>: R lacks Python’s f-string for string interpolation, although we have <code><a href="https://rdrr.io/r/base/sprintf.html">sprintf()</a></code> and <code><a href="https://rdrr.io/r/base/paste.html">paste()</a></code> for that. <code>glue</code> makes string interpolation more readable with <code>glue("fc{i}")</code> instead of <code>paste0("fc", i)</code> or <code>sprintf("fc%d", i)</code>.</li>
<li>
<strong>magrittr</strong>: The pipe operator <code>%&gt;%</code> for chaining operations. This is optional, by the way — R 4.1+ has the native pipe <code>|&gt;</code>, but <code>%&gt;%</code> offers better flexibility with the dot placeholder.</li>
</ul></section><section id="step-2-defining-network-architecture" class="level3" data-number="1.3.2"><h3 data-number="1.3.2" class="anchored" data-anchor-id="step-2-defining-network-architecture">
<span class="header-section-number">1.3.2</span> Step 2: Defining Network Architecture</h3>
<p>In DFFNN architecture, it is defined by the input layer, the hidden layer, and the output layer.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://joshuamarie.com/posts/01-meta-nn/nn-struc.png" class="img-fluid figure-img"></p>
<figcaption>Source: https://medium.com/data-science/designing-your-neural-networks-a5e4617027ed</figcaption></figure>
</div>
<p>The number of nodes are defined by integers, except for input and output layer nodes which they are <em>fixed</em> and determined by the data you provided, and they are defined by <code>no_x</code> and <code>no_y</code>. The number of hidden layers is defined by the length of input in <code>hd_neurons</code> argument.</p>
<p>Combine <code>no_x</code>, <code>hd_neurons</code>, <code>no_y</code> in order:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">no_x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hd_neurons</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">no_y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>And then calculate the length of <code>nodes</code>, which is <img src="https://latex.codecogs.com/png.latex?1%20+%20n_%7B%5Ctext%7Bhidden%20layres%7D%7D%20+%201">, and then subtract it by 1 because the applied activation functions is invoked <em>between each layer</em>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</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;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span></code></pre></div></div>
<section id="example" class="level4" data-number="1.3.2.1"><h4 data-number="1.3.2.1" class="anchored" data-anchor-id="example">
<span class="header-section-number">1.3.2.1</span> Example</h4>
<p>When you have:</p>
<ul>
<li><p>10 predictors</p></li>
<li>
<p>5 hidden layers, and for each layer:</p>
<ol type="1">
<li>20 nodes</li>
<li>30 nodes</li>
<li>20 nodes</li>
<li>15 nodes</li>
<li>20 nodes</li>
</ol>
</li>
<li><p>1 response variable</p></li>
</ul>
<p><em>Total number of layers: 7</em></p>
<p>This means we need 7 - 1 linear transformations, and here is my diagram:</p>
<p><img src="https://latex.codecogs.com/png.latex?10_%7B%5Ctext%7Binputs%7D%7D%20%5Crightarrow20_%7B%5Ctext%7Bhd1%7D%7D%20%5Crightarrow30_%7B%5Ctext%7Bhd2%7D%7D%20%5Crightarrow20_%7B%5Ctext%7Bhd3%7D%7D%20%5Crightarrow15_%7B%5Ctext%7Bhd4%7D%7D%20%5Crightarrow20_%7B%5Ctext%7Bhd5%7D%7D%20%5Crightarrow1_%7B%5Ctext%7Bouput%7D%7D"></p>
</section></section><section id="step-3-setting-activation-functions" class="level3" data-number="1.3.3"><h3 data-number="1.3.3" class="anchored" data-anchor-id="step-3-setting-activation-functions">
<span class="header-section-number">1.3.3</span> Step 3: Setting Activation Functions</h3>
<p>The <code>activations</code> argument holds the account of the activation function. It could be a string, a literal function, or a mix of it in a vector of inputs.</p>
<p>Then, set <code>activations = NULL</code>, where <code>NULL</code> is the default value, which leads to set ReLU (<code>nnf_relu</code>) as the activation function for all hidden layers</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</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;"><a href="https://rdrr.io/r/base/c.html">c</a></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;"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nnf_relu"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hd_neurons</span><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 class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</details>
</div>
<p>Every <code>activations</code> will have <code>NA</code> as the last element because we need to ensure no activation function <em>after</em> the output. The output layer often doesn’t need an activation (for regression) or needs a specific one based on the task (softmax for classification, sigmoid for binary classification). By defaulting to <code>NA</code>, the user can decide.</p>
<div class="callout callout-style-default callout-note callout-titled" title="Length of inputs">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Length of inputs
</div>
</div>
<div class="callout-body-container callout-body">
<p>To provide values in <code>activations</code> argument, it needs to be equal to the size of hidden layers, or if you provide only 1 act. function, this will be the activation function across the transformations.</p>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled" title="Default">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Default
</div>
</div>
<div class="callout-body-container callout-body">
<p>The default is <code>NULL</code>. That is, if <code>activations</code> is not provided, the activation function is set to ReLU function.</p>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled" title="Instead of NULL">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Instead of NULL
</div>
</div>
<div class="callout-body-container callout-body">
<p>Now, if you’re asking “Why needs to set <code>activations</code> to <code>"nnf_relu"</code> instead of <code>NULL</code>”? Don’t worry, I did consider that, but this is just a <em>pure</em> demo.</p>
</div>
</div>
</section><section id="step-4-processing-activation-functions" class="level3" data-number="1.3.4"><h3 data-number="1.3.4" class="anchored" data-anchor-id="step-4-processing-activation-functions">
<span class="header-section-number">1.3.4</span> Step 4: Processing Activation Functions</h3>
<p>This part (re)processes the activation function inputs in the <code>activations</code> argument. This kept tracks the argument you are putting, especially when you the input you are writing in <code>activations</code> argument has different types.</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call_args</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;"><a href="https://rdrr.io/r/base/match.call.html">match.call</a></span><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><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call_args</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</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;">map2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/seq.html">seq_along</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</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;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span><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>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/is.function.html">is.function</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><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;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/call.html">is.call</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><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;">&amp;&amp;</span> </span>
<span>           <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="fl" 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="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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/substitute.html">quote</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">c</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</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;"><a href="https://rdrr.io/r/base/character.html">as.character</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_arg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" 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="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>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</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;"><a href="https://rdrr.io/r/base/names.html">names</a></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;"><a href="https://rdrr.io/r/base/which.html">which</a></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;"><a href="https://rdrr.io/r/base/lapply.html">sapply</a></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;"><a href="https://rdrr.io/r/base/ls.html">ls</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>envir <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;"><a href="https://rdrr.io/r/base/sys.parent.html">parent.frame</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name</span><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>                    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/identical.html">identical</a></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;"><a href="https://rdrr.io/r/base/get.html">get</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name</span>, envir <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;"><a href="https://rdrr.io/r/base/sys.parent.html">parent.frame</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>                <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 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" 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>
<span>            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">func_name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/stop.html">stop</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Could not determine function name for activation function"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/character.html">is.character</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/name.html">is.symbol</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span></span>
<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;">else</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;"><a href="https://rdrr.io/r/base/logical.html">is.logical</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<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;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/stop.html">stop</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Activation must be a function, string, symbol, NA, or NULL"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span><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></code></pre></div></div>
</details>
</div>
</section><section id="step-5-building-the-initialize-method-body" class="level3" data-number="1.3.5"><h3 data-number="1.3.5" class="anchored" data-anchor-id="step-5-building-the-initialize-method-body">
<span class="header-section-number">1.3.5</span> Step 5: Building the <code>initialize</code> Method Body</h3>
<p>The <em>body</em> I am referring in <code>initialize</code> method is the <em>body</em> of the <em>function</em> for the <code>initialize</code> implemented method. This part is a bit far from trivial. I named it <code>init_body</code> to keep track the expression I am trying to build.</p>
<div class="callout callout-style-default callout-note callout-titled" title="In reality">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>In reality
</div>
</div>
<div class="callout-body-container callout-body">
<p>Keep in mind that there’s no <code>initialize</code> and <code>forward</code> parameters within <code>nn_module()</code> torch namespace or whatsoever. However, it is expected you to create them to create a module inside <code>nn_module()</code>. These parameters are kept within the <code>...</code> wildcard parameter.</p>
</div>
</div>
<section id="creation-of-the-expressions-inside-the-body" class="level4" data-number="1.3.5.1"><h4 data-number="1.3.5.1" class="anchored" data-anchor-id="creation-of-the-expressions-inside-the-body">
<span class="header-section-number">1.3.5.1</span> Creation of the expressions inside the body</h4>
<p>Here is the part I am tracking inside <code>create_nn_module</code> body expression:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">init_body</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;">map2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nodes</span><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 class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">c</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dims</span><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>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_name</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;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"out"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fc{i}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call2</span><span 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call2</span><span 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_name</span><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>              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nn_linear"</span>, <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dims</span><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>    <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></code></pre></div></div>
</details>
</div>
<p>What it does is it creates assignment expressions for each layer in the network.</p>
<p>For instance, <code>c(20, 30, 20, 15, 20)</code> is your argument for the <code>activations</code>:</p>
<ol type="1">
<li>
<p><code>map2(nodes[-length(nodes)], nodes[-1], c)</code> pairs consecutive layer sizes:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" 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>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="fl" 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>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" 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>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details>
</div>
</li>
<li>
<p>For each pair, generates a layer assignment expression:</p>
<ul>
<li>Layer names: <code>fc1</code>, <code>fc2</code>, …, <code>out</code> (last layer)</li>
<li>Creates: <code>self$fc1 = nn_linear(10, 20)</code>
</li>
</ul>
</li>
</ol>
<p><em>This will be the generated expression:</em></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc1</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc2</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc3</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="fl" 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc4</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc5</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">out</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled" title="How is it done?">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>How is it done?
</div>
</div>
<div class="callout-body-container callout-body">
<p>I need you to understand <code><a href="https://rlang.r-lib.org/reference/call2.html">rlang::call2()</a></code> a bit:</p>
<p>The <code>call2()</code> function is a glorified <code><a href="https://rdrr.io/r/base/call.html">call()</a></code> from base R that builds function call expressions.</p>
<p>From what I did within <code>init_body</code>:</p>
<ul>
<li><p><code>call2("$", expr(self), sym("fc1"))</code> constructs <code>self$fc1</code></p></li>
<li>
<p><code>call2("nn_linear", !!!dims)</code> is a bit complex:</p>
<ul>
<li>It splices <code>dims</code> from what I created in <code>map2(nodes[-length(nodes)], nodes[-1], c)</code>.</li>
<li>
<code>call2()</code> function accepts rlang’s quasiquotation API, then splices the dimensions, i.e.&nbsp;<code>call2("nn_linear", !!!c(10, 20))</code> to <code>call2("nn_linear", 10, 20)</code>.</li>
<li>Then finally constructs <code>nn_linear(10, 20)</code>
</li>
</ul>
</li>
<li><p><code>call2("=", lhs, rhs)</code> parses an expression: <code>lhs = rhs</code>. This part yields an expression I want: <code>self$fc1 = nn_linear(10, 20)</code>.</p></li>
</ul>
<p><em>Note: You can use <code>&lt;-</code> if you want, instead of <code>=</code>. After all, <code>=</code> within <code>call2()</code>’s <code>.fn</code> argument tokenize <code>=</code> as an assignment operator. </em></p>
</div>
</div>
</section><section id="building-an-actual-body-and-function" class="level4" data-number="1.3.5.2"><h4 data-number="1.3.5.2" class="anchored" data-anchor-id="building-an-actual-body-and-function">
<span class="header-section-number">1.3.5.2</span> Building an <em>actual</em> body and function</h4>
<p>Now, for this part:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">init</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;">new_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    args <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><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>    body <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;">call2</span><span 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 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">init_body</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details>
</div>
<p>Don’t forget to put curly brackets <code>{</code> around the built expression because it becomes necessary in R when composing a function with multiple lines. Still using <code>call2()</code> for that, particularly <code>call2("{", !!!init_body)</code> creates a code block <code>{ ... }</code> containing all initialization statements. The <code>!!!</code> operator “splices” the list of expressions into the block, because <code>init_body</code> forms a list of expressions.</p>
<p>After building the expression I want for the body of <code>initialize</code>, let’s take further by utilizing it as a <em>body</em> to create a function with <code><a href="https://rlang.r-lib.org/reference/new_function.html">rlang::new_function</a></code>. I just simply wraps all the layer initialization expressions into a complete function for <code>initialize</code> method for <code>nn_module()</code>.</p>
<div class="callout callout-style-default callout-note callout-titled" title="Inputs in `initialize`">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Inputs in <code>initialize</code>
</div>
</div>
<div class="callout-body-container callout-body">
<p>Notice that the argument for <code>initialize</code> is <em>empty</em>? I could’ve place <code>input_size</code> and <code>output_size</code> if I wanted to, but it seems unnecessary since I already placed the sizes of the input and output within the expression I built. To make a function expression with empty arguments, place the <code>args</code> argument of <code>new_function</code> with empty <code><a href="https://rdrr.io/r/base/list.html">list()</a></code>.</p>
</div>
</div>
<p>Here’s the result:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc1</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" 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>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc2</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc3</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="fl" 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>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc4</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fc5</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" 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>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">out</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;">nn_linear</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" 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>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p><em>Store this expression into <code>init</code> because we still have to finalize the expression we want to create. </em></p>
</section></section><section id="step-6-building-layer-calls-for-forward-pass" class="level3" data-number="1.3.6"><h3 data-number="1.3.6" class="anchored" data-anchor-id="step-6-building-layer-calls-for-forward-pass">
<span class="header-section-number">1.3.6</span> Step 6: Building Layer Calls for Forward Pass</h3>
<p>The same process as <code>initialize</code>, except we are not building multiple lines of expression, just building a <em>chained expression</em> with ‘magrittr’ pipe from the initial value.</p>
<section id="creating-layer-of-calls" class="level4" data-number="1.3.6.1"><h4 data-number="1.3.6.1" class="anchored" data-anchor-id="creating-layer-of-calls">
<span class="header-section-number">1.3.6.1</span> Creating layer of calls</h4>
<p>To form this expression is also complex</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_calls</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;">map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_name</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;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n_layers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"out"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fc{i}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_fn</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;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span><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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activations</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span><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 class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span>    </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</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;"><a href="https://rdrr.io/r/base/list.html">list</a></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;">call2</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;">call2</span><span 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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">sym</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_name</span><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 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>    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/NULL.html">is.null</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_fn</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</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;"><a href="https://rdrr.io/r/base/append.html">append</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></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;">call2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">activation_fn</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">result</span></span>
<span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/unlist.html">unlist</a></span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compact</span><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></code></pre></div></div>
</details>
</div>
<p>What it does is it builds a sequence of operations for the forward pass: layer calls and their activation functions. I stored the output into <code>layer_calls</code> so that we can keep track of it.</p>
<p><strong>The process:</strong></p>
<ol type="1">
<li>
<p>For each layer, create a list containing:</p>
<ul>
<li>The layer call: <code>self$fc1()</code>
</li>
<li>The activation call (if exists): <code>nnf_relu()</code>
</li>
</ul>
</li>
<li><p>Flatten all lists into a single sequence with <code><a href="https://rdrr.io/r/base/unlist.html">unlist()</a></code>.</p></li>
<li><p>Filter the list we created away from any <code>NULL</code> values with <code><a href="https://purrr.tidyverse.org/reference/keep.html">purrr::compact()</a></code>.</p></li>
</ol>
<p>Thus, we form a list of expressions:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc1</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nnf_relu</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc2</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nnf_relu</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc3</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nnf_relu</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc4</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nnf_relu</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc5</span><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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nnf_relu</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">out</span><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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details>
</div>
<p><strong>Note:</strong> The last layer (<code>out</code>) has no activation because we set it to <code>NA</code>.</p>
</section><section id="building-an-actual-body-and-function-1" class="level4" data-number="1.3.6.2"><h4 data-number="1.3.6.2" class="anchored" data-anchor-id="building-an-actual-body-and-function-1">
<span class="header-section-number">1.3.6.2</span> Building an actual body and function</h4>
<p>I choose to chain them, <code>x</code> or the input as the initial value, and choose not to break lines and forms multiple assignments. This is what I preferred, and besides, it’s so easy to form chained expression when the output is a defused call with <code>reduce()</code>.</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forward_body</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;">reduce</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">layer_calls</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acc</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call</span><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>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expr</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acc</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">call</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>, .init <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;">expr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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></code></pre></div></div>
</details>
</div>
<p>I choose to chain all operations together with pipe operator <code>%&gt;%</code> from ‘magrittr’.</p>
<p>Then, with <code>reduce()</code> works:</p>
<ol type="1">
<li>
<p>Starting with <code>x</code>, it progressively adds each operation:</p>
<ul>
<li>Step 1: <code>x %&gt;% self$fc1()</code>
</li>
<li>Step 2: <code>(x %&gt;% self$fc1()) %&gt;% nnf_relu()</code>
</li>
<li>Step 3: <code>(x %&gt;% self$fc1() %&gt;% nnf_relu()) %&gt;% self$fc2()</code>
</li>
<li>…and so on</li>
</ul>
</li>
<li>
<p>As for the final output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc1</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc2</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc3</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc4</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc5</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">out</span><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></code></pre></div></div>
</li>
</ol>
<div class="callout callout-style-default callout-tip callout-titled" title="Why pipes?">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Why pipes?
</div>
</div>
<div class="callout-body-container callout-body">
<p>The pipe operator makes the forward pass logic read like a natural sequence: “take input x, pass through <code>fc1</code>, apply <code>nnf_relu</code> to invoke ReLU activation function, then pass through <code>fc2</code>, apply <code>nnf_relu</code> to invoke ReLU activation function, …, it kepts repeating until we reach to <code>out</code>”</p>
</div>
</div>
<p>After that, I stored it into <code>forward_body</code>, then make use of it to build the function for <code>forward</code> method with <code><a href="https://rlang.r-lib.org/reference/new_function.html">rlang::new_function()</a></code>:</p>
<div class="cell">
<details class="code-fold"><summary>Code</summary><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forward</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;">new_function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    args <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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <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;">expr</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>    body <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;">call2</span><span 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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forward_body</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</details>
</div>
<p>The <code>args</code> for <code>forward</code> method has <code>x</code> with empty value. Then, wrap the piped forward pass into a function that accepts input <code>x</code>.</p>
<p>And here’s the result:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><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>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc1</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc2</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc3</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc4</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">fc5</span><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 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;">nnf_relu</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span>        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</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;">out</span><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><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p><em>Store this expression into <code>forward</code> because we still have to finalize the expression we want to create. </em></p>
</section></section><section id="step-7-finalizing-the-nn_module-expression-generation" class="level3" data-number="1.3.7"><h3 data-number="1.3.7" class="anchored" data-anchor-id="step-7-finalizing-the-nn_module-expression-generation">
<span class="header-section-number">1.3.7</span> Step 7: Finalizing the <code>nn_module</code> Expression generation</h3>
<p>Here we are for the final part: generating the <code>nn_module</code> expression, by puzzling each part: <code>initialize</code> and <code>forward</code>.</p>
<p>The final part is built from this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">call2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nn_module"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nn_name</span>, <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_names</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;"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">init</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">forward</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"initialize"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"forward"</span><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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>I mean, you still have to use <code>call2()</code> to build a call. The inputs should be:</p>
<ol type="1">
<li><p><code>.fn = "nn_module"</code> -&gt;</p></li>
<li>
<p>The rest of the arguments:</p>
<ul>
<li>
<code>nn_name</code> which is equivalent to “DeepFFN”. You can set any names whatever you want, though.</li>
<li><code>initialize = init</code></li>
<li><code>forward = forward</code></li>
<li>Originally, I formed this in this expression: <code>!!!set_names(list(init, forward), c("initialize", "forward"))</code>. But then, I realized that we only need <code>initialize</code> and <code>forward</code>, and besides, this is a bit overkill.</li>
</ul>
</li>
</ol>
<p>Thus, the final expression that defines the neural network module.</p>
<p>And hence, I form a function that generates a, perhaps, template:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hd_nodes</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">act_fns</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;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nnf_relu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nnf_relu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nnf_relu"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nnf_relu"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_nn_module</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    hd_neurons <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hd_nodes</span>, </span>
<span>    activations <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">act_fns</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>nn_module("DeepFFN", initialize = function () 
{
    self$fc1 = nn_linear(10, 20)
    self$fc2 = nn_linear(20, 30)
    self$fc3 = nn_linear(30, 20)
    self$fc4 = nn_linear(20, 15)
    self$fc5 = nn_linear(15, 20)
    self$out = nn_linear(20, 1)
}, forward = function (x) 
{
    x %&gt;% self$fc1() %&gt;% nnf_relu() %&gt;% self$fc2() %&gt;% nnf_relu() %&gt;% 
        self$fc3() %&gt;% nnf_relu() %&gt;% self$fc4() %&gt;% nnf_relu() %&gt;% 
        self$fc5() %&gt;% self$out()
})</code></pre>
</div>
</div>
</section></section><section id="disclaimer" class="level2" data-number="1.4"><h2 data-number="1.4" class="anchored" data-anchor-id="disclaimer">
<span class="header-section-number">1.4</span> Disclaimer</h2>
<p>This is an advanced example of metaprogramming in R, demonstrating how to leverage functional programming and <code>rlang</code> for code generation. I don’t mind you to replicate what I did, but sometimes this technique should be used judiciously—sometimes simpler, more explicit code is better.</p>
<p>This example showcases:</p>
<ul>
<li>Deep understanding of R’s evaluation model</li>
<li>Functional programming with <code>purrr</code>
</li>
<li>Expression manipulation with <code>rlang</code>
</li>
<li>Practical application to deep learning workflows</li>
</ul>
<p>And also, I am aware to the fact that the function I made is ugly if you said so.</p>


</section></section> ]]></description>
  <category>R</category>
  <category>machine-learning</category>
  <category>torch</category>
  <category>pointless-code</category>
  <guid>https://joshuamarie.com/posts/01-meta-nn/</guid>
  <pubDate>Mon, 22 Sep 2025 16:00:00 GMT</pubDate>
  <media:content url="https://joshuamarie.com/posts/01-meta-nn/tmbnl.jpg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
