<?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>~ jim</title>
<link>https://jimgar.github.io/posts.html</link>
<atom:link href="https://jimgar.github.io/posts.xml" rel="self" type="application/rss+xml"/>
<description>Jim Gardner&#39;s personal website and blog: DuckDB, R, Python, Quarto, and whatever else is on my mind.</description>
<image>
<url>https://jimgar.github.io/cheddar.JPG</url>
<title>~ jim</title>
<link>https://jimgar.github.io/posts.html</link>
</image>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Wed, 04 Feb 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Six months of Bandcamp’s Jazz-ish Jazz Club</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/jazz-ish-six-months/post.html</link>
  <description><![CDATA[ <p>Last Autumn Bandcamp launched <a href="https://bandcamp.com/about_clubs">Clubs</a>:</p>
<blockquote class="blockquote">
<p>[Clubs are a] subscribe-to-own music discovery experience, curated by trusted experts and rooted in community. Inspired by old-school record clubs and online spaces where passionate fans come together to share and explore music, bringing together human curation with community connection.</p>
</blockquote>
<p>They started with a handful of clubs, and I joined <a href="https://bandcamp.com/club/jazz-ish-jazz">Jazz-ish Jazz</a>, curated and hosted by the absolutely delightful DJ, broadcaster, and music journalist <a href="https://www.tinaedwardsmusic.com/">Tina Edwards</a>. For the price of an album each month, I get a record hand picked by Tina, a listening party (with Tina and usually the artist/s or re-issuer), a written interview, and a community page of sorts where members can leave comments.</p>
<section id="general-praise-for-bandcamp" class="level2"><h2 class="anchored" data-anchor-id="general-praise-for-bandcamp">General praise for Bandcamp</h2>
<p>These club albums are treated in exactly the same way as a normal purchase on Bandcamp. A “normal purchase” that will surprise many in the Spotify age, especially young millenials downward.</p>
<p><strong>You get to download the music</strong>. You own it. No DRM, no time or download limits, no ads. You can also stream it if you like. Oh, and the artist receives something like 75% or more of the sales revenue.</p>
<p>If you care about the artist you are buying from, and can afford it, I don’t know why you wouldn’t use Bandcamp over Spotify or Apple Music. It is <strong>clearly</strong> the more ethical choice at the time I write this, especially as they have just <a href="https://blog.bandcamp.com/2026/01/13/keeping-bandcamp-human/">banned</a> AI-generated music in a bid to keep the platform a celebration of human artistry and connection. I’ve been using Bandcamp for nearly 15 years, and all I can say is, fuck yeah.</p>
</section><section id="why-jazz" class="level2"><h2 class="anchored" data-anchor-id="why-jazz">Why jazz</h2>
<p>Anyway. I joined the jazz club to broaden my horizons in a genre that’s always tantalised me, but with which I’ve only ever flirted. There’s an enormous amount of variety within jazz, so for a record club it seemed like the perfect choice.</p>
</section><section id="reflections-on-the-first-six-months" class="level2"><h2 class="anchored" data-anchor-id="reflections-on-the-first-six-months">Reflections on the first six months</h2>
<p>It’s been fantastic. A truly wonderful mixture of records. I’ll give each a mini-review below, but first want to talk about the listening parties.</p>
<section id="listening-parties" class="level3"><h3 class="anchored" data-anchor-id="listening-parties">Listening parties</h3>
<p>I didn’t expect to enjoy them so much. It’s nice to see some of the same names each month, hang out with Tina and the artists, and generally be a bit silly. But every month is also a profound opportunity. These are incredible musicians… and they’re just there. Answering our questions. Enjoying the feedback. It’s so <strong>human</strong>, and not an opportunity you often get, let alone every month.</p>
<p>It can be touching, too. Recently I was away from home for a funeral, with extended family, and it was of course quite a sad and discombobulating few days. I’m autistic, so identifying my feelings and expressing them around something like a funeral can be challenging, to say the least. The evening before the funeral I went upstairs to the little guest room, put my headphones on, logged into the listening party, and had the most incredible experience across the 45 minutes.</p>
<p>The emotional fullness turned my body into what felt like an electrified pool of jelly. Partly because of the incredible music, and partly because of <strong>how</strong> I was experiencing it - the commensality. The artists were in a great mood, joking around and sharing their process, clearly appreciating our comments. Gratitude was flowing back and forth. It was a special experience.</p>
<p>They’re not always so intense. They have all been fun, though, and the focus it brings to your listening, the way people point out stuff you didn’t - or that you did! - consider, or enjoy, or find curiosity in, is very stimulating.</p>
</section></section><section id="album-mini-reviews" class="level2"><h2 class="anchored" data-anchor-id="album-mini-reviews">Album mini-reviews</h2>
<p>In chronological order of receipt. Click the album title to go to the Bandcamp page.</p>
<p>I’ve put together a <a href="https://bandcamp.com/jimgardner2/playlist/jazz-ish-jazz-the-first-six-months">playlist on Bandcamp</a>, made up of my favourite track from each album. Bandcamp account holders can listen to each track up to 3 times.</p>
<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><a href="https://thecirclingsun.bandcamp.com/album/orbits"><img src="https://jimgar.github.io/posts/jazz-ish-six-months/orbits.jpg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Album cover for 'Orbits', by The Circling Sun. A black background representing space appears to be peppered with small celestial bodies of light, with a large blue-purple-white smear across the centre."></a></p>
</figure>
</div>
<section id="orbits-by-the-circling-sun-2025" class="level3"><h3 class="anchored" data-anchor-id="orbits-by-the-circling-sun-2025">Orbits, by The Circling Sun (2025)</h3>
<p>This is exactly the kind of album that I wouldn’t pick up of my own accord. It has a 70s vibe, despite being contemporary, and prominently features the cuíca (a Brazilian friction drum that sounds like a monkey), which I love. In some ways it’s a utopian album, full of samba charm. The opening track <em>Constellation</em> includes a choir singing, “Freedoooom, freedoooom!” over warm brass and synths. It evokes the optimism of a late spring train journey from Inverness to Kyle of Lochalsh, sparkling still water passing by the window. Good times ahead.</p>
<p>Favourite track: <em>Flying</em>. I can imagine the USA-1950s-retro-futurism-feminist music video for this in my mind’s eye. It takes place in a shopping mall. And involves a baseball bat.</p>
<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><a href="https://amitafra.bandcamp.com/album/the-prophet-and-the-madman"><img src="https://jimgar.github.io/posts/jazz-ish-six-months/the_prophet_and_the_madman.jpg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Album cover for 'The Prophet and The Madman' by Ami Taf Ra. Ami is dressed in white and gold robes, with 5 long ginger braids in her hair. Her right hand reaches towards the viewer as she looks up, and the braids extend away from her head and body as if held on strings."></a></p>
</figure>
</div>
</section><section id="the-prophet-and-the-madman-by-ami-taf-ra-2025" class="level3"><h3 class="anchored" data-anchor-id="the-prophet-and-the-madman-by-ami-taf-ra-2025">The Prophet and The Madman, by Ami Taf Ra (2025)</h3>
<p>Another that I wouldn’t have picked myself. It’s hard to describe! In her debut album Ami draws on her Arab North African roots, fusion jazz, and as the title suggests, lyrically and thematically from author Khalil Gibran. She sings mostly in English, but also classical Arabic, and the Moroccan dialect on <em>Gnawa</em>, which is a catchy, strutting-on-tiptoes affair during verses, sliding into almost-woozy group sung (chanted?) choruses. The following track, <em>Gibran</em>, is a haunting piano-led affair, mysterious, transporting you directly into a dark, comfortable, hazy jazz club. The solos on this album are all masterful and build the tracks up.</p>
<p>It’s a heady record, a lot to take in.</p>
<p>Favourite track: <em>Love (feat. Ryan Porter)</em>. Almost sickly sweet, but it sends shivers down my legs every time. One to slow dance with your sweetheart:</p>
<blockquote class="blockquote">
<p>Desire love to be like a running brook that sings to the night<br>
To know the pain of too much tenderness<br>
To be wounded by your own understanding<br>
Willingly to bleed joyfully<br>
To wake at dawn with a winged heart<br>
To give thanks to another day of love<br>
To return home with gratitude<br>
To sleep with a prayer for your beloved<br>
In your heart, a song of praise<br>
To rest upon your lips<br>
Speak to us of love</p>
</blockquote>
<p><del>I’m not crying you’re crying</del></p>
<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><a href="https://omasta.bandcamp.com/album/jazz-report-from-the-hood"><img src="https://jimgar.github.io/posts/jazz-ish-six-months/jazz_report_from_the_hood.jpg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Album cover for 'Jazz Report From The Hood', by Omasta. It has a bright red background, and in the foreground in black is a chain link fence with a large hole."></a></p>
</figure>
</div>
</section><section id="jazz-report-from-the-hood-by-omasta-2025" class="level3"><h3 class="anchored" data-anchor-id="jazz-report-from-the-hood-by-omasta-2025">Jazz Report from the Hood, by Omasta (2025)</h3>
<p>An album from a Krakow outfit, whose band name means “fat added to food for flavour”. I love that. It perfectly encapsulates the oily-thick feel of this hip-hop inspired jazz report. All the instruments used during recording were vintage, and the mixing and mastering was done on analogue equipment, so the sound is warm and deep as home made roasted chicken broth. The record is intended to be listened to as a whole piece, and it works. To me, it’s reminiscent of a particularly long and slightly scary Friday evening out drinking in my early 20s. It has confidence, cool, curiosity, danger, maybe some scraps and scrapes, before a slightly hyper return home as the sun comes up.</p>
<p>Special shout out to Antoni (soprano sax, flute), who had pneumonia while recording the album. Blimey.</p>
<p>Favourite track: <em>Kazimierz</em>. The closing track is just absolute cool sunglasses-wearing alternative hip-hop personified. The trumpet hook is sick, the guys deliver energetic solo after energetic solo, and the drum kit throughout is tightly yet expressively played, driving throughout. There’s a KILLER beat skip 35 seconds before the end of the track.</p>
<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><a href="https://levitationorchestra.bandcamp.com/album/sanctuary"><img src="https://jimgar.github.io/posts/jazz-ish-six-months/sanctuary.jpg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Album cover for 'Sanctuary', by Levitation Orchestra. A figure stands before a red background. The figure looks kind of scary, dressed in all black leather, a crimson cloak, an almost crow-like helmet with a single golden diamond-shaped 'eye' in the centre."></a></p>
</figure>
</div>
</section><section id="sanctuary-by-levitation-orchestra-2025" class="level3"><h3 class="anchored" data-anchor-id="sanctuary-by-levitation-orchestra-2025">Sanctuary, by Levitation Orchestra (2025)</h3>
<p>This is the album that drew such a strong emotional reaction out of me during the listening party. At times intensely warm and comforting, like a chocolatey dark roast cortado, elsewhere as electrifying and heady as the best jRPG soundtracks. All delivered with exceptionally tight playing and a cohesive feel throughout, despite slow burners being mixed in with livelier tracks.</p>
<p>For a while, I was telling all my friends to listen to <em>Embrace</em>, because it has some of the most incredible voice work (by the mononymous Plumm), almost reminiscent of Rajasthani folk music. Quite impressive. Then on the next track, <em>Breathe It In</em>, the piano absolutely dominates. It sounds like the <em>Spirited Away</em> OST meets the “Zelda’s Awakening” memory from <em>Zelda: Breath of the Wild</em>, with a dash of the saxophone from the final <em>Zelda: Tears of the Kingdom</em> trailer. Restrained drama, resolve in the face of darkness. Following that, <em>Home</em> acts as a gorgeous guided meditation, led by a male voice, “You know me / Within and without / In your embrace / I am safe to be”. The penultimate track, <em>Premedetatio Malorum</em>, sounds like it was made by three goblins wearing shades and trenchcoats, an assessment that went down well in the listening party: One band member said “Definitely goblin energy in the room when we came up with this one”, and another, “Where were you when we were naming these!?”.</p>
<p>Favourite track: <em>Another Way</em>. The closing track, which comes in at 9 minutes, has the best sax harmony I’ve ever heard. It’s also… scary. It illicts a strange fear in me, a fear that somehow thickens into defiance, before releasing into hopefulness.</p>
<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><a href="https://robinjonesquintet.bandcamp.com/album/denga"><img src="https://jimgar.github.io/posts/jazz-ish-six-months/denga.jpg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Album cover for 'Denga', by Robin Jones Quintet. The image is abstract. It could be the view from inside a tunnel, painted with broad brush strokes in red and black."></a></p>
</figure>
</div>
</section><section id="denga-by-robin-jones-quintet-1971" class="level3"><h3 class="anchored" data-anchor-id="denga-by-robin-jones-quintet-1971">Denga, by Robin Jones Quintet (1971)</h3>
<p>A reissue (2022), this early 70s banger is best described by its Bandcamp page: “a scintillating fusion of Afro-Cuban and Afro-Brazillian rhythms laden with heavy Fender Rhodes sounds and no less than three Afro-Latin Percussionists”. The record still sounds fresh, with an absolutely superb mix. All the percussionists are spaced out such that you can hear them distinctively doing their thing. And what a thing they do! It makes you want to move. I joked in the listening party that <em>El Sueno</em> is a Latin James Bond theme, but I really do think there’s something to it! It wouldn’t be out of place in Series 2 of The Night Manager. Overall, this is probably the most crowd pleasing album of the club. I can’t imagine someone taking a dislike to it. Several of the tracks are a blast. Robin’s albums are hard to find, but luckily the reissuer has another, <em><a href="https://robinjonesseven.bandcamp.com/album/el-maja">EL MAJA</a></em>, also up on Bandcamp.</p>
<p>Favourite track: <em>Goodbye Batucada</em>. This one would pair really well with <em>Flying</em>, from Orbits, as both share the same retro-futurist shopping mall vibe. Incredibly upbeat, almost glassy-smiled.</p>
<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><a href="https://ilhanersahin.bandcamp.com/album/istanbul-sessions-mahalle-2"><img src="https://jimgar.github.io/posts/jazz-ish-six-months/istanbul_sessions_mahalle.jpg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Album cover for 'Istanbul Sessions: Mahalle', by Ilhan Ersahin. A candid black-and-white photograph taken by Bercin Gultekin, presumably of Istanbul. Various people stand around near water, with a large boat in the background."></a></p>
</figure>
</div>
</section><section id="istanbull-sessions-mahalle-by-ilhan-ersahin-2026" class="level3"><h3 class="anchored" data-anchor-id="istanbull-sessions-mahalle-by-ilhan-ersahin-2026">Istanbull Sessions: Mahalle, by Ilhan Ersahin (2026)</h3>
<p>I’ve only had a few days with this one, but wow. This album is a celebration of Istanbul’s diverse neighbourhoods, and is described as cinematic, which it certainly is. The record is quite rocky, and grungey, with powerful, almost forceful drumming at times, such as on the opener <em>Yedi Tepe</em>. I’m struck by Ersahin’s sax playing, which can be pretty wild, and has some seductive Turkish-sounding solos (like on <em>Asmalı</em>) that almost twist the ear into hearing a bass clarinet or such. And yet, the restraint shown on <em>Galata</em>, the soft playing, the dedication to the riff, it’s delightful, as is the entire song, which has an 80s flavour that arrived to me through early home video games, suddenly dropping into fully Nirvana-esque grunge at various points. Absolutely brilliant.</p>
<p>Favourite track: <em>Galata</em>. I just love that riff. The whole track sounds the way a vintage game cover looked, think Battletoads, Streets of Rage, night time, sunglasses.</p>


</section></section> ]]></description>
  <category>Music</category>
  <category>Bandcamp</category>
  <category>Jazz</category>
  <guid>https://jimgar.github.io/posts/jazz-ish-six-months/post.html</guid>
  <pubDate>Wed, 04 Feb 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>CPD for July 2025</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/cpd-jul-2025/post.html</link>
  <description><![CDATA[ <p>My team (Data Science) at Smart Data Foundry have started to spend an hour of CPD time together once a week. July is our first month doing it, and I figured I would use it as an excuse to blog a little bit.</p>
<section id="section" class="level2"><h2 class="anchored" data-anchor-id="section">2025-07-10</h2>
<section id="advent-of-code-2024-day-1-part-1" class="level3"><h3 class="anchored" data-anchor-id="advent-of-code-2024-day-1-part-1">Advent of Code 2024 Day 1, Part 1</h3>
<p>I <a href="https://github.com/jimgar/advent-of-code/blob/main/2024/01.R">did AoC Day 1 last year</a> and challenged myself to only use base R. We figured this would be a nice little problem to start our sessions with.</p>
<p>This time, I wanted to try using Python. I’m kinda enjoying the language now, and between boot.dev and work am using it more than R. But! Instead of the standard library, I wanted to try using Polars and compare the solution to my base R one. Recently I’ve used Polars a fair bit for work but the operations I needed have been <strong>very</strong> simple. My spidey senses told me that if I used Polars then I’d end up learning something new.</p>
<p>I also used uv, the hot new Python project/package manager, to start the project off:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> init</span>
<span id="cb1-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> venv</span>
<span id="cb1-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> venv/bin/activate</span>
<span id="cb1-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> add ruff</span>
<span id="cb1-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> add polars</span></code></pre></div></div>
</div>
<p>That creates a few files, including a <code>uv.lock</code> and <code>pyproject.toml</code>. These essentially contain metadata such as the Python and package versions. The files can be copied into another project, or a container image, to install the same version of Python and packages with a simple <code>uv sync</code>.</p>
<section id="solution" class="level4"><h4 class="anchored" data-anchor-id="solution">Solution</h4>
<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="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb2-2"></span>
<span id="cb2-3"></span>
<span id="cb2-4">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb2-5">    pl.read_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aoc_day1/input.txt"</span>, has_header<span 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>
<span id="cb2-6">    .with_columns(</span>
<span id="cb2-7">        column_1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column_1"</span>)</span>
<span id="cb2-8">        .<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.split_exact(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"   "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-9">        .struct.rename_fields([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>])</span>
<span id="cb2-10">    )</span>
<span id="cb2-11">    .unnest(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column_1"</span>)</span>
<span id="cb2-12">)</span>
<span id="cb2-13"></span>
<span id="cb2-14">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.with_columns(</span>
<span id="cb2-15">    l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>).cast(pl.Int32).sort(),</span>
<span id="cb2-16">    r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>).cast(pl.Int32).sort(),</span>
<span id="cb2-17">)</span>
<span id="cb2-18"></span>
<span id="cb2-19">res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.select((pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>)).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>().<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()).item()</span>
<span id="cb2-20"></span>
<span id="cb2-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(res)</span></code></pre></div></div>
</div>
<p>Comparing to the R version of the 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;">df</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/utils/read.table.html">read.table</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-input.txt"</span>, col.names <span class="op" style="color: #5E5E5E;
background-color: 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;">"l"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span><span class="op" style="color: #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;">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;">l</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/sort.html">sort</a></span><span 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">l</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;">r</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/sort.html">sort</a></span><span 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="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>
<span></span>
<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="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;">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;">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;">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;">l</span><span class="op" 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>It’s more verbose. Most of that comes down to fundamental differences between base R and Polars. In this case - which is common - the R solution uses functions. We also benefit from data.frames being native data structures, and <code>read.table</code> which does two things for us: identifies the delimiter (three whitespace characters), and infers the types correctly (numeric).</p>
<p>The Polars solution is fiddlier. The csv parser can only take a single byte delimiter, meaning no regex, no multiple whitespace. I had to look this up, because it seemed frankly idiotic for a modern data processing package, and read the author saying they did this on purpose: It puts speed before anything else, and speed is at the heart of Polars. Doing anything else would go against that. Not so idiotic after all!!!</p>
<p>Anyway this leads to extra processing because the table gets read as a single string column. When it gets split, it becomes a Polars <strong><a href="https://docs.pola.rs/user-guide/expressions/structs/">struct</a></strong>. I didn’t know what a struct actually was, though I saw the name appear in the user guide as I hacked on projects for work. Turns out they’re essentially a typed dictionary.</p>
<p><code>str.split_exact()</code> returns “a struct of <code>n+1</code> fields”. So the result of</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">split_exact(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"   "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
</div>
<p>on a row of data like <code>50123   10023</code> in a column called <code>column_1</code> is</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">{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column_1"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"50123"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"column_2"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10023"</span>}</span></code></pre></div></div>
</div>
<p>Every row gets a dict like that, which taken together are the values in the struct.</p>
<p>Calling <code>unnest()</code> splits a struct into columns. The column names come from whatever the fields are called.</p>
<p>So, all in all, it’s a little bit like an R list-column situation.</p>
<p>Once the columns were unnested they were still string and have to be cast to the correct integer type. And then finally we get to do the calculation, which also looks way more verbose to me. One benefit of chaining methods is that you get to see what happens sequentially. That’s not the case with nested function calls, like in the R code. I don’t like the amount of noise in having to refer to columns with <code>pl.col("l")</code>, but it’s not that bad in this case to be honest.</p>
</section></section></section><section id="section-1" class="level2"><h2 class="anchored" data-anchor-id="section-1">2025-07-17</h2>
<section id="advent-of-code-2024-day-1-part-2" class="level3"><h3 class="anchored" data-anchor-id="advent-of-code-2024-day-1-part-2">Advent of Code 2024 Day 1, Part 2</h3>
<p>Same deal as last week, but part two of the problem.</p>
<section id="solution-1" class="level4"><h4 class="anchored" data-anchor-id="solution-1">Solution</h4>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># df was defined in the week 1 section</span></span>
<span id="cb6-2"></span>
<span id="cb6-3">counts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.select(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>).to_series().value_counts()</span>
<span id="cb6-4"></span>
<span id="cb6-5">joined <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.join(counts, how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inner"</span>, left_on<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, right_on<span 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"</span>)</span>
<span id="cb6-6"></span>
<span id="cb6-7">joined <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> joined.with_columns(multiplied<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>))</span>
<span id="cb6-8"></span>
<span id="cb6-9">summed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> joined.select(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multiplied"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>().item()</span>
<span id="cb6-10"></span>
<span id="cb6-11"><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"Part 2 result: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>summed<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></code></pre></div></div>
</div>
<p>Comparing to the R version:</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># df already defined in the week 1 section</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">freq</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.data.frame.html">as.data.frame</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/table.html">table</a></span><span 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="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;">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;">r</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="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;">l</span><span class="op" style="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/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;">freq</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="st" style="color: #20794D;
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;">df</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/merge.html">merge</a></span><span 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;">freq</span>, by <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, all.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;">TRUE</span>, sort <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>
<span></span>
<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="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;">l</span> <span 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">f</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></code></pre></div></div>
</div>
<p>It’s a similar approach: Make a frequency table of the values in the right-hand column, join those values onto the original table, multiply the left-hand values against the frequency values and sum up the results.</p>
<p>I couldn’t remember what I did in the R version, but my mind went to almost exactly the same places. In both versions I started by making a frequency table, which was trivial. However, in the R version I first filtered the values in the right-hand column by those of the left-hand column, so that the frequency table would be much smaller (there were only 37 shared values between the columns in my dataset). This seemed more efficient for further steps because with a smaller dataframe there will be fewer comparisons to make.</p>
<p>This was what I wanted to do in the Polars version, too, but it was fiddly. There might be a better way of doing this, but my code ended up looking like:</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">mask <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.select(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>).to_series().is_in(df.select(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>).to_series())</span>
<span id="cb8-2">counts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.select(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>).to_series().<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(mask).value_counts()</span></code></pre></div></div>
</div>
<p>That’s not terrible, but to me the R way of subsetting a vector looks <strong>far</strong> more elegant. Once again Polars is more verbose; I get lost trying to read the repetition of <code>select</code> and <code>to_series</code> and the brackets. There <strong>is</strong> a <code>df.to_series()</code> method, so the code could be streamlined slightly, but it takes a column’s position as it’s arg, rather than a name. I stand resolute and refuse to do <code>df.to_series(0)</code>. That… is opaque bullshit!</p>
<p>Instead, I did an inner join between the two DataFrames. An inner join is a “filtering join”, where the left-hand df gets filtered by the contents of the right-hand df. And in the R version I did the same thing using <code><a href="https://rdrr.io/r/base/merge.html">merge()</a></code>.</p>
<p>At this juncture of writing the blog post something quite nice happened. I realise that the R code was doing some redundant things. Because <code><a href="https://rdrr.io/r/base/merge.html">merge()</a></code> does an inner join by default, there was no need to specify <code>all.x = TRUE</code>. In fact, it causes the opposite behaviour of what is needed: It’s like saying “Yes, make sure you keep all the rows from the left-hand df, and if you don’t mind, add NAs whenever there was no matching row in the right-hand df!”. That’s why the subsequent sum requires <code>na.rm = TRUE</code>.</p>
<p>In my opinion the more elegant approach is the Polars version: Don’t bother filtering the frequency table first, simply allow the inner join to do its job and filter for you.</p>
<p>With those changes made, the R code is even slicker (isn’t it satisfying to delete things??):</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;">freq</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.data.frame.html">as.data.frame</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/table.html">table</a></span><span 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="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="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/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;">freq</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="st" style="color: #20794D;
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;">df</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/merge.html">merge</a></span><span 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;">freq</span>, by <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, sort <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>
<span></span>
<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="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;">l</span> <span 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="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>
<p>All in all:</p>
<ul>
<li>Humbling to look back at simple problems and try to solve them with a new tool</li>
<li>Satisfying to revisit concepts and end up streamlining old code</li>
<li>Interesting to observe what <strong>feels nicer to read</strong> as I look at the code. Never underestimate how important feel is to craft</li>
</ul>


</section></section></section> ]]></description>
  <category>Python</category>
  <category>Advent of Code</category>
  <category>Advent of Code 2024</category>
  <category>Polars</category>
  <category>uv</category>
  <guid>https://jimgar.github.io/posts/cpd-jul-2025/post.html</guid>
  <pubDate>Thu, 10 Jul 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Accessing R HTML help pages from outside a container</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/interactive-neovim-development-container-r-quarto-part-2/post.html</link>
  <description><![CDATA[ <div class="cell">
<style type="text/css">
.figure-caption {
    text-align: center;
}

.center {
    text-align: center;
}
</style>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/interactive-neovim-development-container-r-quarto-part-2/r-html-help.png" class="img-fluid figure-img"></p>
<figcaption>The radian R terminal running from inside a container, showing a custom startup message. To the side is a browser with the R HTML help page loaded, which is being served from the container.</figcaption></figure>
</div>
<section id="another-challenge" class="level2"><h2 class="anchored" data-anchor-id="another-challenge">Another challenge!</h2>
<p>This is a follow-on about my <a href="https://jimgar.github.io/posts/interactive-neovim-development-container-r-quarto/post.html">attempt to make</a> a container that can be used as an interactive Neovim development environment with R and Quarto.</p>
<p><a href="https://hcommons.social/@petejones">Pete</a>, who started me dancing down the devil’s road in the first place, had a follow-up request:</p>
<blockquote class="blockquote">
<p>[…] is there a way to run R’s HTML help server in the container and view it in a browser on the host?</p>
</blockquote>
<p>Sounds like that should be easy to do. After all, I was able to get the Quarto HTML preview and hdg graphics viewer for R sorted. I bet it’ll take 30 minutes or so!</p>
<p>How naive of me.</p>
</section><section id="problem-solution" class="level2"><h2 class="anchored" data-anchor-id="problem-solution">Problem / solution</h2>
<p>For those who don’t know - and this included me before this week - you can launch a website containing <strong>all</strong> of the help pages for both R’s standard library (base, tools, utils, Matrix, etc.) and user installed packages. You can launch this website by using the <code><a href="https://rdrr.io/r/utils/help.start.html">help.start()</a></code> function that comes with R. The more you know.</p>
<p>This is useful if, for example, you’re using R in a terminal setting, which as we established last time is what Pete does.</p>
<p>I quickly found out that the root cause of the problem is within the <code><a href="https://rdrr.io/r/utils/help.start.html">help.start()</a></code> function body. Specifically, both <code><a href="https://rdrr.io/r/utils/help.start.html">help.start()</a></code> and a function it calls, <code><a href="https://rdrr.io/r/tools/startDynamicHelp.html">tools::startDynamicHelp()</a></code>, have the IP address <code>127.0.0.1</code> hard coded into their definitions. This is a problem because HTTP servers running inside containers using <code>127.0.0.1</code> can’t be accessed from outside the container. Instead, you have to use <code>0.0.0.0</code>. Thankfully, it is at least possible to tell R that you always want to use the same port when running the server.</p>
<p>A few hours later I hit a brick wall and <a href="https://hachyderm.io/@jimgar/114457423581278999">posted about it</a> on Mastodon. R legend <a href="https://yihui.org/">Yihui Xie</a> jumped in and replied, kindly telling us exactly what we needed to do. This was very much appreciated – I had clearly gone down a dead end. It gave me hope!</p>
<p>What Yihui turned us onto was a command line tool called <code>socat</code>. You can use it to create proxies and bind ports. In our case, the magic command looked like this:</p>
<pre class="shell"><code>socat TCP-LISTEN:20633,bind=0.0.0.0,fork TCP:127.0.0.1:20000 &amp;</code></pre>
<p>This means: Accept traffic (listen) on 0.0.0.0:20633, i.e.&nbsp;receive requests for web pages as we browse the site. Then pass the requests to 127.0.0.1:20000. Fork allows us to handle multiple requests. And the final ampersand tells bash to run the socat process in the background, otherwise socat will make the terminal busy.</p>
<p>To put this into practice I added some more R code to <code>~/.Rprofile</code> during container build time. This is an important file because whenever R starts it runs the code in this file. That means we can use it for performing setup, such as getting socat to forward our traffic. The code looks like this:</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| eval: false</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/interactive.html">interactive</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>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/options.html">options</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;">'help.ports'</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;">20000</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.html">system</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;">'socat TCP-LISTEN:20633,bind=0.0.0.0,fork TCP:127.0.0.1:20000 &amp;'</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;">cli</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://cli.r-lib.org/reference/cli_h1.html">cli_h1</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;">'HTML help pages'</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;">cli</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://cli.r-lib.org/reference/cli_alert.html">cli_alert_info</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;">'Starting httpd help server...'</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;">resp</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/message.html">suppressMessages</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;">tools</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/r/tools/startDynamicHelp.html">startDynamicHelp</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;">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;">resp</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;">20000</span><span class="op" style="color: #5E5E5E;
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;">cli</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://cli.r-lib.org/reference/cli_alert.html">cli_alert_danger</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;">'Failed to start server. Try running {.fn tools::startDynamicHelp} manually to see what happens.'</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;">cli</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://cli.r-lib.org/reference/cli_text.html">cli_text</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>  <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;">cli</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://cli.r-lib.org/reference/cli_alert.html">cli_alert_info</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;">'If container is running with {.code -p 20633:20633}'</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;">cli</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://cli.r-lib.org/reference/cli_alert.html">cli_alert_info</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;">'{.url http://localhost:20633/doc/html/index.html}'</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;">cli</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://cli.r-lib.org/reference/cli_text.html">cli_text</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>  <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/rm.html">rm</a></span><span 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</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;">.Last</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Attempt to clean up after ourselves when R closes.</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># When R closes the help pages are no longer served, so we</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># should also stop socat.</span></span>
<span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A bit of a blunt instrument as it will terminal ALL instances</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># of socat. But I'm assuming for this container that R help will</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># be the sole user of the command, so it's safe to do</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;-</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/system.html">system</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;">'pgrep socat'</span>, intern <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="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;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1L</span><span class="op" style="color: #5E5E5E;
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/system.html">system</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;">'pgrep socat | xargs kill -9'</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>
<p>When starting R with the radian terminal, we get this lovely startup message:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/interactive-neovim-development-container-r-quarto-part-2/radian-startup.png" class="img-fluid figure-img"></p>
<figcaption>The radian terminal immediately following boot up. Brief messages tell the user that the http server has been started for the HTML help pages.</figcaption></figure>
</div>
<p>As my comments say in the code block, this is a rather inelegant solution. There is the possibility of using the bash command <code>$!</code> in order to get the PID of the socat process (<code>$!</code> gets the PID of the most recent background process) and do a targeted kill. But I found that socat was a bit erratic, sometimes spawning a few processes. Unfortunately the second and third processes weren’t children of the first, otherwise we could have captured the value of <code>$!</code>, say as <code>panret_pid</code> and used <code>pkill -P parent_pid</code>. On the plus side I think this is a very clean solution, insofar as socat will always be started and killed in tandem with R.</p>
</section><section id="how-its-going" class="level2"><h2 class="anchored" data-anchor-id="how-its-going">How it’s going</h2>
<p>Well, I wrote this blog post using Neovim from the container in question! That includes previewing the post :)</p>
<p>The Containerfile <a href="https://github.com/jimgar/nvim-r-py-quarto-container">now exists in a GitHub repo</a>. At time of writing there’s no documentation, but I will get to it soon. One thing I’ve noticed is that the container image size is rather large, about 2.7GB. Maybe that’s ok - there is a <strong>lot</strong> of stuff being installed into it, after all. It’s also very tiresome having the plugins install every time the container is restarted and Neovim gets invoked for the first time. I think it might be possible to simply copy everything from <code>.local/share/nvim/lazy</code> into the GitHub repo and copy it into the image at build time instead, but that requires some experimentation.</p>


</section> ]]></description>
  <category>R</category>
  <category>Quarto</category>
  <category>Python</category>
  <category>Neovim</category>
  <category>Containers</category>
  <category>Docker</category>
  <category>Podman</category>
  <guid>https://jimgar.github.io/posts/interactive-neovim-development-container-r-quarto-part-2/post.html</guid>
  <pubDate>Thu, 08 May 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>A containerised Neovim dev environment for R and Quarto</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/interactive-neovim-development-container-r-quarto/post.html</link>
  <description><![CDATA[ <div class="cell">
<style type="text/css">
.figure-caption {
    text-align: center;
}

.center {
    text-align: center;
}
</style>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/interactive-neovim-development-container-r-quarto/nvim_container_1.png" class="img-fluid figure-img"></p>
<figcaption>Neovim running from inside a container showing a Quarto document and radian REPL. A browser is viewing the live HTML preview being served by Quarto</figcaption></figure>
</div>
<section id="the-question" class="level2"><h2 class="anchored" data-anchor-id="the-question">The question</h2>
<p>Over on Mastodon <a href="https://hcommons.social/@petejones">Pete Jones</a> asked the following:</p>
<blockquote class="blockquote">
<p>[…] I’ll jump straight to my noob questions which I’m hoping the #Rstats community can help me answer:</p>
<ul>
<li>Does anyone have any good recommendations for primers/video tutorials etc that are particularly geared towards (social science) research uses of containers?</li>
<li>I can see how containers make sense for sharing your research environment at the end of a project for reproducibility, but how sensible is it to be doing all your iterating and exploratory stuff inside a container during the research? Do folks just do all that on their host system and then containerise it when they’re done?</li>
<li>What’s the advantage of using, say, Apptainer versus podman versus something like rix? There seems to be a lot of overlap so it’s hard to know which one to dive into. For context, my coding work is almost entirely in the form of interactive sessions throwing stuff at the REPL and making data visualisations etc, and rarely a scripts-as-jobs type of workflow. In other words, I want to be sat inside a command line environment writing and evaluating code on the fly.</li>
</ul>
</blockquote>
<p>My response was basically that it’s straightforward-ish to use an image with RStudio Server, or VS Code. That’s how I work these days, barely touching the system installation of R.</p>
<p>It turns out that Pete uses Neovim as his IDE, so those images aren’t that useful. Thankfully, he recently wrote <a href="https://petejon.es/posts/2025-01-29-using-neovim-for-r/">a nice blog post</a> specifically about using Neovim for interactive R and Quarto work. Reading over that post again, and looking at his nvim config files, the desire to prove it was possible took me over.</p>
<p>A couple of days later I’m happy to say it is possible. The rest of this post is dedicated to explaining how.</p>
</section><section id="terminology" class="level2"><h2 class="anchored" data-anchor-id="terminology">Terminology</h2>
<p>Containers aren’t tied to Docker, like most people think. There’s an open source alternative called <a href="https://podman.io/">Podman</a>, and that’s what I used to build and run the container in question. Podman is essentially a drop-in replacement for Docker.</p>
<p>I also refer to the recipe for the container image not as a Dockerfile, but a <a href="https://github.com/containers/buildah/discussions/3170">Containerfile</a>.</p>
</section><section id="the-containerfile-explanation" class="level2"><h2 class="anchored" data-anchor-id="the-containerfile-explanation">The Containerfile explanation</h2>
<p>Here it is in full.</p>
<p>One note to mention is that I haven’t consistently pinned versions throughout. That would definitely be worthwhile, but it takes a bit of time and I don’t have it right now.</p>
<p>From Pete’s config, I knew he would need Python (because the radian R REPL is written in it), R, Quarto, and of course, Neovim. You can see that installing and configuring these different tools happen in short blocks, most of which are heredocs. It’s fine to have small scripts that get copied in and run instead, but I used heredocs so that everything is together in the same file.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> docker.io/library/python:3.13-bookworm</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Python setup. Makes a venv and sets it as the default Python interpreter</span></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> VIRTUAL_ENV=/root/.venv</span>
<span id="cb1-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> venv <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$VIRTUAL_ENV</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> PATH=<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$VIRTUAL_ENV/bin:$PATH"</span></span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install radian, a Julia-inspired R REPL written in Python</span></span>
<span id="cb1-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-U</span> radian</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> R_VERSION=<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4.5.0"</span></span>
<span id="cb1-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> QUARTO_VERSION=<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.7.29"</span></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb1-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Install R</span></span>
<span id="cb1-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Taken from https://github.com/r-lib/rig?tab=readme-ov-file#installing-rig-on-linux-</span></span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Get their certificate</span></span>
<span id="cb1-19"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">curl -L https://rig.r-pkg.org/deb/rig.gpg -o /etc/apt/trusted.gpg.d/rig.gpg</span></span>
<span id="cb1-20"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">sh -c 'echo "deb http://rig.r-pkg.org/deb rig main" &gt; /etc/apt/sources.list.d/rig.list'</span></span>
<span id="cb1-21"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">apt-get update</span></span>
<span id="cb1-22"></span>
<span id="cb1-23"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Now rig is available to install</span></span>
<span id="cb1-24"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">apt-get install r-rig</span></span>
<span id="cb1-25"></span>
<span id="cb1-26"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Install this version of R</span></span>
<span id="cb1-27"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rig add </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${R_VERSION}</span></span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">echo "options(repos = c(CRAN = 'https://packagemanager.posit.co/cran/__linux__/bookworm/latest'))</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" &gt;&gt; ~/.Rprofile</span></span>
<span id="cb1-30"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Rscript -e "options(warn = 2); pak::pak(c('knitr', 'rmarkdown', 'languageserver', 'nx10/httpgd'))"</span></span>
<span id="cb1-31"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span>
<span id="cb1-32"></span>
<span id="cb1-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb1-34"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Set up the R graphics driver</span></span>
<span id="cb1-35"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">echo "if (interactive()) {</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb1-36"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  if ('httpgd' %in% .packages(all.available = TRUE)) {</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb1-37"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    options(device = function(...) {</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb1-38"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      httpgd::hgd(host = '0.0.0.0', port = 9001, token = FALSE)</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb1-39"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    })</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb1-40"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  }</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb1-41"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">}</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" &gt;&gt; ~/.Rprofile</span></span>
<span id="cb1-42"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span>
<span id="cb1-43"></span>
<span id="cb1-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb1-45"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Install Quarto.</span></span>
<span id="cb1-46"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Slightly modified from </span></span>
<span id="cb1-47"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># https://docs.posit.co/resources/install-quarto.html</span></span>
<span id="cb1-48"></span>
<span id="cb1-49"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">mkdir -p /opt/quarto/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span></span>
<span id="cb1-50"></span>
<span id="cb1-51"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">curl -o quarto.tar.gz -L </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-52"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    "https://github.com/quarto-dev/quarto-cli/releases/download/v</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/quarto-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">-linux-amd64.tar.gz"</span></span>
<span id="cb1-53"></span>
<span id="cb1-54"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">tar -zxvf quarto.tar.gz </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-55"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    -C "/opt/quarto/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-56"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    --strip-components=1</span></span>
<span id="cb1-57"></span>
<span id="cb1-58"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rm quarto.tar.gz</span></span>
<span id="cb1-59"></span>
<span id="cb1-60"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Symlink so that </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">quarto</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> can be used as a command</span>
<span id="cb1-61"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ln -s /opt/quarto/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/bin/quarto /usr/local/bin/quarto</span></span>
<span id="cb1-62"></span>
<span id="cb1-63"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Verify successful installation</span></span>
<span id="cb1-64"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">quarto check</span></span>
<span id="cb1-65"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span>
<span id="cb1-66"></span>
<span id="cb1-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># deno is required for one of the plugins</span></span>
<span id="cb1-68"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COPY</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--from=denoland/deno:bin</span> /deno /usr/local/bin/deno</span>
<span id="cb1-69"></span>
<span id="cb1-70"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb1-71"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Neovim plugin system deps</span></span>
<span id="cb1-72"></span>
<span id="cb1-73"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">apt-get update</span></span>
<span id="cb1-74"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">apt-get install -y --no-install-recommends ripgrep fd-find</span></span>
<span id="cb1-75"></span>
<span id="cb1-76"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Instructions for fd from: https://github.com/sharkdp/fd?tab=readme-ov-file#on-debian</span></span>
<span id="cb1-77"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">mkdir -p ~/.local/bin/fd</span></span>
<span id="cb1-78"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ln -s </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span> fdfind<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> ~/.local/bin/fd</span></span>
<span id="cb1-79"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">echo 'export PATH="</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</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;">$HOME</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/.local/bin"' &gt;&gt; ~/.bashrc</span></span>
<span id="cb1-80"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span>
<span id="cb1-81"></span>
<span id="cb1-82"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb1-83"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Get Neovim config</span></span>
<span id="cb1-84"></span>
<span id="cb1-85"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">mkdir -p ~/.config/nvim</span></span>
<span id="cb1-86"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">curl -o config.tar.gz -L "https://codeberg.org/pjphd/neovim_config/archive/main.tar.gz"</span></span>
<span id="cb1-87"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">tar -zxvf config.tar.gz -C ~/.config/nvim --strip-components=1</span></span>
<span id="cb1-88"></span>
<span id="cb1-89"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rm config.tar.gz</span></span>
<span id="cb1-90"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span>
<span id="cb1-91"></span>
<span id="cb1-92"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb1-93"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Install Neovim</span></span>
<span id="cb1-94"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Taken from https://github.com/neovim/neovim/blob/master/INSTALL.md#pre-built-archives-2</span></span>
<span id="cb1-95"></span>
<span id="cb1-96"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim-linux-x86_64.tar.gz</span></span>
<span id="cb1-97"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rm -rf /opt/nvim</span></span>
<span id="cb1-98"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">tar -C /opt -xzf nvim-linux-x86_64.tar.gz</span></span>
<span id="cb1-99"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">echo 'export PATH="</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">:/opt/nvim-linux-x86_64/bin"' &gt;&gt; ~/.bashrc</span></span>
<span id="cb1-100"></span>
<span id="cb1-101"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rm nvim-linux-x86_64.tar.gz</span></span>
<span id="cb1-102"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span>
<span id="cb1-103"></span>
<span id="cb1-104"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tidy up apt</span></span>
<span id="cb1-105"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> clean <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-rf</span> /var/lib/apt/lists/<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb1-106"></span>
<span id="cb1-107"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CMD</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">/bin/bash</span></span></code></pre></div></div>
<section id="python" class="level3"><h3 class="anchored" data-anchor-id="python">Python</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> docker.io/library/python:3.13-bookworm</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Python setup. Makes a venv and sets it as the default Python interpreter</span></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> VIRTUAL_ENV=/root/.venv</span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> venv <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$VIRTUAL_ENV</span></span>
<span id="cb2-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> PATH=<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$VIRTUAL_ENV/bin:$PATH"</span></span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install radian, a Julia-inspired R REPL written in Python</span></span>
<span id="cb2-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-U</span> radian</span></code></pre></div></div>
<p>I used an “official” Python base image with version 3.13, and Debian Bookworm as the Linux distro. At time of writing bookworm is the latest release of Debian.</p>
<p>Python no longer allows you to install packages into the system version of Python. I think that’s fine - virtual environments are great (I wish R’s ecosystem was better in this regard). So here, I make a venv and add that version of Python to the container’s PATH. This means when you invoke <code>python</code> or <code>pip</code> it uses the venv. If you don’t take the step of adding it to the PATH your subsequent <code>RUN</code> commands, e.g.&nbsp;<code>pip install</code>ing requirements, will use the system Python installation. Not a lot of people know this so I thought it was worth pointing out. Handily, this also means you never need to <code>source /venv/bin/activate</code>.</p>
</section><section id="r" class="level3"><h3 class="anchored" data-anchor-id="r">R</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb3-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Install R</span></span>
<span id="cb3-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Taken from https://github.com/r-lib/rig?tab=readme-ov-file#installing-rig-on-linux-</span></span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Get their certificate</span></span>
<span id="cb3-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">curl -L https://rig.r-pkg.org/deb/rig.gpg -o /etc/apt/trusted.gpg.d/rig.gpg</span></span>
<span id="cb3-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">sh -c 'echo "deb http://rig.r-pkg.org/deb rig main" &gt; /etc/apt/sources.list.d/rig.list'</span></span>
<span id="cb3-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">apt-get update</span></span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Now rig is available to install</span></span>
<span id="cb3-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">apt-get install r-rig</span></span>
<span id="cb3-12"></span>
<span id="cb3-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Install this version of R</span></span>
<span id="cb3-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rig add </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${R_VERSION}</span></span>
<span id="cb3-15"></span>
<span id="cb3-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">echo "options(repos = c(CRAN = 'https://packagemanager.posit.co/cran/__linux__/bookworm/latest'))</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" &gt;&gt; ~/.Rprofile</span></span>
<span id="cb3-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Rscript -e "options(warn = 2); pak::pak(c('knitr', 'rmarkdown', 'languageserver', 'nx10/httpgd'))"</span></span>
<span id="cb3-18"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb3-21"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Set up the R graphics driver</span></span>
<span id="cb3-22"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">echo "if (interactive()) {</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb3-23"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  if ('httpgd' %in% .packages(all.available = TRUE)) {</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb3-24"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    options(device = function(...) {</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb3-25"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      httpgd::hgd(host = '0.0.0.0', port = 9001, token = FALSE)</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb3-26"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    })</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb3-27"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  }</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n</span></span>
<span id="cb3-28"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">}</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" &gt;&gt; ~/.Rprofile</span></span>
<span id="cb3-29"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span></code></pre></div></div>
<p>I use <a href="https://github.com/r-lib/rig">rig</a> to install R. Then, I set the CRAN repo to the Posit Public Package Manager, specifically the version with binaries for Bookworm. This means the packages don’t need to build from source so the installation process is nice and fast.</p>
<p>Rig comes with <a href="https://github.com/r-lib/pak">pak</a>, a package installation manager for R. This fantastic package can find and install Linux system dependencies for R packages, which is a real blessing when making your own container. Here I just install the packages I know are required for Quarto (knitr, rmarkdown) and Neovim (languageserver, httpgd).</p>
<p>Speaking of httpgd, I add some configuration to the Rprofile for this. The purpose of this package is to offer a graphics device, for viewing plots and HTML. Basically, the browser on your computer can be used to view images provided by httpgd while it runs inside the container.</p>
</section><section id="quarto" class="level3"><h3 class="anchored" data-anchor-id="quarto">Quarto</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb4-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Install Quarto.</span></span>
<span id="cb4-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Slightly modified from </span></span>
<span id="cb4-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># https://docs.posit.co/resources/install-quarto.html</span></span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">mkdir -p /opt/quarto/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span></span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">curl -o quarto.tar.gz -L </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    "https://github.com/quarto-dev/quarto-cli/releases/download/v</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/quarto-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">-linux-amd64.tar.gz"</span></span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">tar -zxvf quarto.tar.gz </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    -C "/opt/quarto/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    --strip-components=1</span></span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rm quarto.tar.gz</span></span>
<span id="cb4-16"></span>
<span id="cb4-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Symlink so that </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">quarto</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> can be used as a command</span>
<span id="cb4-18"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ln -s /opt/quarto/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${QUARTO_VERSION}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/bin/quarto /usr/local/bin/quarto</span></span>
<span id="cb4-19"></span>
<span id="cb4-20"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Verify successful installation</span></span>
<span id="cb4-21"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">quarto check</span></span>
<span id="cb4-22"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span></code></pre></div></div>
<p>There’s nothing particularly special going on here. I basically just followed Posit’s instructions.</p>
</section><section id="neovim-dependencies" class="level3"><h3 class="anchored" data-anchor-id="neovim-dependencies">Neovim dependencies</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># deno is required for one of the plugins</span></span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COPY</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--from=denoland/deno:bin</span> /deno /usr/local/bin/deno</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb5-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Neovim plugin system deps</span></span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">apt-get update</span></span>
<span id="cb5-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">apt-get install -y --no-install-recommends ripgrep fd-find</span></span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Instructions for fd from: https://github.com/sharkdp/fd?tab=readme-ov-file#on-debian</span></span>
<span id="cb5-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">mkdir -p ~/.local/bin/fd</span></span>
<span id="cb5-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ln -s </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span> fdfind<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> ~/.local/bin/fd</span></span>
<span id="cb5-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">echo 'export PATH="</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</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;">$HOME</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/.local/bin"' &gt;&gt; ~/.bashrc</span></span>
<span id="cb5-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span></code></pre></div></div>
<p><code>deno</code> is a system dep of <code>peek.nvim</code>, and thankfully they had straightforward instructions that allowed me to copy the binary from one of their own container images.</p>
<p>Pretty much everyone using Neovim will use a plugin called <code>Telescope</code>, which requires <code>fd</code>, and I think <code>ripgrep</code> too.</p>
</section><section id="neovim-config" class="level3"><h3 class="anchored" data-anchor-id="neovim-config">Neovim config</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb6-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Get Neovim config</span></span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">mkdir -p ~/.config/nvim</span></span>
<span id="cb6-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">curl -o config.tar.gz -L "https://codeberg.org/pjphd/neovim_config/archive/main.tar.gz"</span></span>
<span id="cb6-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">tar -zxvf config.tar.gz -C ~/.config/nvim --strip-components=1</span></span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rm config.tar.gz</span></span>
<span id="cb6-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span></code></pre></div></div>
<p>This grabs Pete’s config files from Codeberg and puts them into the expected place in the file system.</p>
</section><section id="neovim" class="level3"><h3 class="anchored" data-anchor-id="neovim">Neovim</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb7-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;EOF</span></span>
<span id="cb7-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Install Neovim</span></span>
<span id="cb7-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Taken from https://github.com/neovim/neovim/blob/master/INSTALL.md#pre-built-archives-2</span></span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim-linux-x86_64.tar.gz</span></span>
<span id="cb7-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rm -rf /opt/nvim</span></span>
<span id="cb7-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">tar -C /opt -xzf nvim-linux-x86_64.tar.gz</span></span>
<span id="cb7-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">echo 'export PATH="</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">:/opt/nvim-linux-x86_64/bin"' &gt;&gt; ~/.bashrc</span></span>
<span id="cb7-9"></span>
<span id="cb7-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rm nvim-linux-x86_64.tar.gz</span></span>
<span id="cb7-11"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">EOF</span></span></code></pre></div></div>
<p>Again, pretty straightforward - followed instructions provided by the Neovim maintainers.</p>
</section><section id="tidy-and-cmd" class="level3"><h3 class="anchored" data-anchor-id="tidy-and-cmd">Tidy and cmd</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tidy up apt</span></span>
<span id="cb8-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> clean <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-rf</span> /var/lib/apt/lists/<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CMD</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">/bin/bash</span></span></code></pre></div></div>
<p>The lists of available packages are just bloat at this point, so I get rid of them. And I add a new cmd, which is what gets run when the container is booted up. In this case it’s just bash, so that the user gets a flexible launch/base point to work from.</p>
</section></section><section id="build-the-container" class="level2"><h2 class="anchored" data-anchor-id="build-the-container">Build the container</h2>
<p>This bit is really simple. In a terminal <code>cd</code> to the directory where the Containerfile lives. Then run:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">podman</span> build <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span> pete . <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--arch</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>linux/amd64</span></code></pre></div></div>
</div>
<p>It will take a minute or two depending on your internet and processing power.</p>
<p><code>pete</code> is the name I’ve given the image. The <code>.</code> just means ‘look in the current directory for the Containerfile’. The <code>--arch=linux/amd64</code> is to “set the architecture of the image to be built, and that of the base image to be pulled, if the build uses one, to the provided value instead of using the architecture of the build host”. I’m on an arm64 Macbook, which is the build host, but most of the things we’re installing into the image are explicitly the amd64 versions.</p>
<p>One thing to bear in mind is the “build context” when you build the image. <strong>All</strong> the files in the directory containing the Containerfile will get copied into the build context, so that they’ve available for the build. Podman will look like it’s frozen if you have large files in the build context. In reality, it’s copying them. You can either add them to a <code>.Containerignore</code> (aka <code>.Dockerignore</code>) or do the build in a different directory.</p>
</section><section id="run-the-container" class="level2"><h2 class="anchored" data-anchor-id="run-the-container">Run the container</h2>
<p>Before running, I made a tiny Quarto file in the same directory as the Containerfile, called <code>index.qmd</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb10-2"><span class="an" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">title:</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> "test"</span></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Quarto</span></span>
<span id="cb10-6"></span>
<span id="cb10-7">Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;https://quarto.org&gt;</span>.</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb10-10">mtcars</span>
<span id="cb10-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb10-12"></span>
<span id="cb10-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb10-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(mtcars)</span>
<span id="cb10-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<p>This is when the magic happens.</p>
<p>In the terminal (from the directory with <code>index.qmd</code>) run:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">podman</span> run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--rm</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-it</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 9000:9000 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 9001:9001 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-v</span> ./:/test_project pete</span></code></pre></div></div>
</div>
<p>The container will start. We’ve set it up to run with an interactive terminal, forwarding ports 9000 and 9001 from within the container to the same ports on the host machine, and also mounted the current directory to <code>/test_project</code> inside the container.</p>
<p>The ports will be used to preview the qmd file and for showing plots made during an interactive R session. The volume mounting means that we can edit the qmd file inside the container and the changes will persist on the host machine - in fact we can also make entirely new files in <code>/test_project</code> and they will appear on the host file system!</p>
<p>With the container running, start neovim:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" 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;">nvim</span></span></code></pre></div></div>
</div>
<p>It will take a moment to install all of the plugins and LSPs. When it’s done follow any prompts that come up.</p>
<p>Once finished, do <code>:tabnew</code>, then <code>:terminal</code>. Go into Insert mode and run:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">quarto</span> preview test_project/index.qmd <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--host</span> 0.0.0.0 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--port</span> 9000</span></code></pre></div></div>
</div>
<p>In your web browser, go to <code>localhost:9000</code>. You should see a live preview of the Quarto document.</p>
<p>Now, in the other nvim tab, let’s <code>:e test_project/index.qmd</code>. Go ahead and change something in the file, and <code>:w</code>. Look at the browser and the doc should update right in front of you.</p>
<p>For the cherry on top, let’s start a REPL at the same time and view a plot. With the config at current time of writing <code>localleader</code> is set to <code>Space</code>. Do <code>&lt;localleader&gt;rS</code>, this should open a radian terminal. Now highlight the line of R code <code>plot(mtcars)</code> with <code>Shift + V</code>, then do <code>&lt;localleader&gt;r</code> followed by <code>Return</code>. Radian should say that it’s running a httpgd server. Go to your browser and in a new tab/window go to <code>localhost:9001/live</code>. There’s your plot!</p>
<p>This shows how Neovim with a plot viewer and Quarto live preview, plus a ‘live link’ between the container and host file systems is possible.</p>
</section><section id="is-it-worth-it" class="level2"><h2 class="anchored" data-anchor-id="is-it-worth-it">Is it worth it?</h2>
<p>The short answer is, I’m not sure.</p>
<p>R itself is quite mature and so are many of its most well-used packages, so developing on your system and then putting the finished project into a container is fine. Creating a dev environment with a lot of dependencies, like in this case, is fairly involved. It’s also annoying that the Neovim packages have to install every time you start the container, though there may be a solution to that I’ve yet to find.</p>
<p>At the same time, once you have a couple of Containerfiles, like this dev one, and another that encapsulates the final project, you’ve got most of what you need for future projects. It’s also amazing how quickly we can adapt to slightly different workflows like this. I think it’s really cool that Neovim can offer the REPL and native terminal. I’m also a believer that the ephemeral nature of containers forces us to be a lot better about capturing code, and making sure it runs from start to end. Too often people rely heavily on the REPL and are scared to restart their IDE in case their analysis no longer works, for some reason.</p>
</section><section id="two-extra-learnings" class="level2"><h2 class="anchored" data-anchor-id="two-extra-learnings">Two extra learnings</h2>
<p>Two extras things I learned during this process:</p>
<ol type="1">
<li><p>Ever wondered about what has gone into a base image that you are using? As in, what did the Containerfile / Dockerfile have in it? That’s possible to get. First run <code>podman image ls</code> and note down the image id of the image you’re interested in. Then use <code>podman history --no-trunc image_id</code>, where <code>image_id</code> is (you guessed it) the image id. The output isn’t clean like a Containerfile - it contains timestamps and suchlike - and it is in reverse order. But it’s still a good one to know, particularly if you find yourself in a situation like I did while making this post: The Containerfile disappeared after a forced laptop restart. Ten minutes of tedious reformatting later, all was repaired.</p></li>
<li><p>How to close buffers. I used to open files in Neovim sometimes using the <code>:e</code> (alias of <code>:edit</code>) command. I knew that these buffers had to be closed somehow but never figured it out. It’s actually very simple. You can list all buffers with <code>:buffers</code>. Then you can do <code>db #</code>, where <code>#</code> is the number of the buffer to be closed.</p></li>
</ol>


</section> ]]></description>
  <category>R</category>
  <category>Quarto</category>
  <category>Python</category>
  <category>Neovim</category>
  <category>Containers</category>
  <category>Docker</category>
  <category>Podman</category>
  <guid>https://jimgar.github.io/posts/interactive-neovim-development-container-r-quarto/post.html</guid>
  <pubDate>Wed, 30 Apr 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Learning Neovim is Hard</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/learning-neovim-is-hard/post.html</link>
  <description><![CDATA[ <div class="cell">
<style type="text/css">
.figure-caption {
    text-align: center;
}

.center {
    text-align: center;
}
</style>
</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>This is NOT a place for you to learn anything. It is NOT a guide. It’s a “traditional” blog post where I waffle on and indulge myself. There may be some accidental nuggets of wisdom… but I cannot be held liable for any strain you experience trying to find them.</p>
</div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/learning-neovim-is-hard/nvim.png" class="img-fluid figure-img"></p>
<figcaption>R script in Neovim running in Wezterm</figcaption></figure>
</div>
<section id="wouldnt-it-be-nice" class="level2"><h2 class="anchored" data-anchor-id="wouldnt-it-be-nice">Wouldn’t it be nice?</h2>
<p>I wonder if this resonates with you? Ever read people’s LinkedIn profiles, or their blogs/websites/CVs, and think:</p>
<blockquote class="blockquote">
<p>Wow. This person is absolutely incredible. They have achieved/know so much.</p>
</blockquote>
<p>Wouldn’t it be nice if that person posted on their LinkedIn about the shitty fucking week they had trying to learn about containers for the first time? Or how that one time they had to work a weekend because they re-ran their Jupyter notebook from top to bottom and the results were completely different to what they showed their boss?</p>
<p>How about if they tried something and spent many days – weeks, months! – only to realise they just couldn’t understand it, and kinda like, gave up?</p>
<p>Well that’s me with Neovim.</p>
<p>I’m a mid-level programmer/Data Scientist. I’m now way beyond a total beginner, and there are actually people who look up to me as a professional. It feels unbelievable at times, but, that’s what happens when you work hard, learn, and care about the craft (and other people).</p>
<p>Read on to hear the struggle.</p>
</section><section id="obliterated" class="level2"><h2 class="anchored" data-anchor-id="obliterated">Obliterated</h2>
<p>When I started programming in March 2020 I was working in evolutionary biology. I don’t know how it is now, but back then we were using a mix of proprietary GUI-based programs and command line tools written and published by academics. That meant my introduction to programming was the command line (specifically using bash), then Python, and then R. If I needed to write a script I’d come up with a little toy dataset, open the terminal, invoke Python/R, and write every line of code myself using the vanilla REPL.</p>
<p>That’s right. There was no</p>
<ul>
<li>Syntax highlighting</li>
<li>Linting</li>
<li>Hovers</li>
<li>Keyboard shortcuts</li>
<li>Formatting</li>
</ul>
<p>There wasn’t even ‘bracketed paste’, which as I understand it just means ‘multi-line code pasting into the terminal’. It was a literal line-by-line workflow where even a for-loop had to be input and sent to the interpreter in parts.</p>
<p>At first I did it because I really didn’t know any better. As I became more aware of the options, I inevitably switched to using Spyder for Python and RStudio for R, and eventually RStudio for Python for a while (yes, really o_O).</p>
<p>I’ve always been grateful for starting in the terminal though. I’ve never been afraid to fire up an R/Python REPL and do quick checks on language behaviour.</p>
<p>In fact, at times I’ve felt a little smug: I’ve had colleagues who don’t know the difference between R and RStudio. Meanwhile, I “graduated” last year to using VS Code as my main IDE. Yeehaw, I’m a cowboy riding out there in the wild west of Microsoft’s Extension Marketplace. I dazzled colleagues by rummaging around in the <code>settings.json</code>, pointing to an R binary for the Radian terminal. I even <a href="https://github.com/lionel-/codegrip/pull/17">exposed addin_reshape()</a> from <a href="https://github.com/lionel-/codegrip">codegrip</a> and contributed to the docs..! Ok, I admit to being genuinely proud of that one, even if it was easy.</p>
<p>Then came the vim extension for VS Code. Wow. So cool. Even though the productivity gains from vim motions are marginal at best, and the time spent trying to adjust will never be won back. But you know, the important thing at the time was it kept me engaged with programming. It also exposed me to more computer sciencey developers and low-level language practitioners. Which in turn, of course, lead me to <a href="https://neovim.io/">Neovim</a>.</p>
<p>And Neovim obliterated any sense of smugness that I had.</p>
<p>It’s good to get in over your head every once in a while. It’s humbling.</p>
</section><section id="neovim" class="level2"><h2 class="anchored" data-anchor-id="neovim">Neovim</h2>
<p>If you don’t know, Neovim (A.K.A <code>nvim</code>) is a text editor that can be used as an IDE. It’s a bit like VS Code in that it’s configurable, but way harder. In the nvim world people juice up the editor with extra functionality by installing plugins, most of which are written in Lua.</p>
<p>Apparently Lua is the easiest language ever to learn and literal children use it to make Roblox games. If you use nvim you’ll also be coding in Lua: it’s the language used for your config file/s.</p>
<p>I’m not going to say that I didn’t understand <strong>anything</strong> in the config files. But I didn’t find Lua super easy to get a flavour of. I just wanted my config to work most of the time, so I would mostly look at other people’s configs – limiting my exposure to Lua as a general language. This meant whenever the config would break or I’d get unexpected behaviour I didn’t really know what to do. It turns out that knowing words is important<sup>1</sup> when you’re learning programming.</p>
<p>I also found the documentation for most plugins threadbare. Developers: Don’t assume too much of your potential users. Many of us are, frankly, morons, and many more will have a TikTok user’s attention span. You do actually need to tell us what your software does and how it can be used.</p>
<p>Clearly I needed more help, so I read and watched a <strong>lot</strong> of tutorials. I found <a href="https://www.youtube.com/@typecraft_dev/videos">typecraft</a>, who I would like to hug. I found the mononymous <a href="https://www.youtube.com/@teej_dv">TJ</a>, nvim contributor and creator of treesitter. There is of course fellow sober addict and programming craftsman <a href="https://www.youtube.com/@ThePrimeTimeagen">ThePrimeagen</a> (“Primeagen mentioned!”, “Let’s go boys!!”, etc.). I watched <a href="https://www.youtube.com/@joseanmartinez">Josean Martinez’s</a> and <a href="https://www.youtube.com/@henrymisc">Henry Misc’s</a> guides. Probably the best series of them all was by <a href="https://www.youtube.com/@vhyrro/videos">Vhyrro</a>, who gave examples and explained the ‘why’ of things<sup>2</sup> for different facets of the editor – but who maddeningly never finished the video series!</p>
<p>They all helped, but in the end, it was still a huge amount of work. My knowledge wasn’t coming together. I couldn’t touch type, which is kind of a vim pre-requisite, so I tried to learn that via <a href="https://www.typingclub.com/">TypingClub</a>. That definitely helped, but didn’t stick because it was too slow for work. Then a young 20-something on Mastodon tooted about how they were able to touch type faster than their old speed after <strong>just a month of TypingClub</strong>, and it completely unlocked nvim for them, and… I died a bit inside. Touch typing for me is honestly just super hard – even after several months.</p>
<p>I’d estimate that all told I probably spent a few weeks dedicated to learning about Neovim as a tool, lots of vim motions and commands, editing and using different configs, the language server protocol, linters, formatters, stuff like that. I must have used it as an IDE for a good 3 months. There were periods that I really enjoyed it.</p>
<p>I’d like to try again one day. There’s just something alluring about it. It reminds me of watching my older brother on the computer in the early 90s. The vibe appeals.</p>
</section><section id="not-all-wasted" class="level2"><h2 class="anchored" data-anchor-id="not-all-wasted">Not all wasted</h2>
<p>So now I’m mostly using VS Code and the vim extension. I’m maybe faster in some ways than pre-nvim. One thing I like to do is open nvim in the terminal VS Code’s integrated terminal. That way I can qickly edit a file without having to open and close a new tab in the main editor.</p>
<p>It’s also given me gems like the vim ‘find and replace within highlighted text’ command: <code>:s/find this phrase/replace with this/g</code>.</p>
<p>Maybe even better is that it started to teach me about the differences between formatters, linters, and LSPs. In reality there’s overlap between the three categories, with some LSPs using linters, or formatters that use linters, and whatever <a href="https://docs.astral.sh/ruff/">ruff</a> does. But at least it’s not a <strong>complete</strong> mystery. Case in point, I was recently able to <a href="https://hachyderm.io/@jimgar/114197304839595680">help someone on Mastodon</a> with an R linting bugbear in the context of nvim – even though it’s been 6 months since I used it in earnest!</p>
<p>Learning is a journey and even when it is hard, or doesn’t work out, we are bound to learn some things along the way.</p>


</section><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p>See <a href="https://www.youtube.com/watch?v=g1ib43q3uXQ">“How to teach programming (and other things)?” by Felienne Hermans</a>↩︎</p></li>
<li id="fn2"><p>Again, see Felienne’s talk. It’s important!↩︎</p></li>
</ol></section></div> ]]></description>
  <category>R</category>
  <category>Python</category>
  <category>Neovim</category>
  <category>VS Code</category>
  <guid>https://jimgar.github.io/posts/learning-neovim-is-hard/post.html</guid>
  <pubDate>Mon, 24 Mar 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>VS Code for R on macOS</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/vs-code-macos-r/post.html</link>
  <description><![CDATA[ <p>I never thought I’d say this, but… I’m enjoying Visual Studio Code (VS Code) as an IDE for R. I’d even recommend it as an alternative to RStudio for the average R user. This is particularly true if you - like me - are getting interested in languages other than R, such as python.</p>
<p>Setting it all up isn’t super hard. The <strong>essential</strong> thing to know is that VS Code is primarily a customisable IDE that relies on extensions to add in support for most languages. For R, there’s a nice, simple guide on the VS Code website <a href="https://code.visualstudio.com/docs/languages/r">specifically for us</a>. Install VS Code, follow the linked guide, and you should be ready to code. There’s also a <a href="https://github.com/REditorSupport/vscode-R/wiki">wiki</a> for the extension that is worth looking at.</p>
<p>What’s more esoteric are the bits and bobs of context you need to start getting the most out of VS Code, particularly if you come from the RStudio world - like I do! So this post contains some of the extra context that I think is worth knowing during/after setup.</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">
<ol type="1">
<li>This post is <strong>specifically for mac users</strong>
</li>
<li>I haven’t done any package development in VS Code yet</li>
<li>I don’t claim to be an advanced R <strong>or</strong> VS Code user</li>
<li>Use any of my suggestions/instructions at your own risk!</li>
</ol>
</div>
</div>
<section id="rproj-vs-folderdirectoryworkspace" class="level1"><h1>Rproj vs Folder/Directory/Workspace</h1>
<p>You’re likely familiar with the <code>.Rproj</code> file generated by RStudio when you create a new project. We like the Rproj because: double-clicking it opens the project in its own RStudio session, with the working directory already set. That’s good.</p>
<p>With VS Code you just open the project folder/directory (also known as a workspace). All terminals, including the R one, will have the working directory set. No Rproj required.</p>
<p>Like RStudio you can have multiple windows open, each for a separate project. It’s worth knowing that if you open another folder with File &gt; Open Folder…, it will close the currently open folder. One window, one folder. If you want to have multiple windows each with their own folder, you first have to open a window with Cmd + Shift + N, and <strong>then</strong> open a folder.</p>
<p>In the radian section you will learn about the settings json, and in the custom keybindings section about keybindings.json. It’s worth knowing that these can be set at the workspace level and will override any user-level settings when anyone works in the folder.</p>
</section><section id="radian" class="level1"><h1>Radian</h1>
<p>While it <strong>is</strong> recommended to install and use <a href="https://github.com/randy3k/radian">radian</a> as the R console for VS Code, a word of warning. This became the biggest pain point during my setup. I would try to run R code and get errors about not being able to find R and/or radian. If the same happens to you, try the following.</p>
<p>First, install radian however the site suggests. Then open VS Code’s <code>settings.json</code>. To do that, hit Cmd + Shift + P (this will open the command palette) and start typing in “open user settings (json)”. When the option is visible to you, click it and <code>settings.json</code> will open.</p>
<p>The settings json is a file of customisable settings for controlling the behaviour of VS Code. In this case, we want to tell it where to look for R and radian. Copy the following into your settings file and then replace the file paths for your machine.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript"><span id="cb1-1">{</span>
<span id="cb1-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r.bracketedPaste"</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;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r.rpath.mac"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/usr/local/bin/R"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r.rterm.mac"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Library/Frameworks/Python.framework/Versions/3.11/bin/radian"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r.rterm.option"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb1-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--no-save"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--no-restore"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb1-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--r-binary=/usr/local/bin/R"</span></span>
<span id="cb1-9">  ]</span>
<span id="cb1-10">}</span></code></pre></div></div>
</div>
<p><code>r.rpath.mac</code> and <code>--r-binary</code> point to your R. To find out where that is, run <code>which R</code> in the terminal.</p>
<p><code>r.rterm.mac</code> points to your radian. You can find that with <code>which radian</code>.</p>
<p>Save the settings file. Now if you open an R file and attempt to run some code it should open a terminal and just “do the thing”.</p>
</section><section id="interactive-viewer-pane" class="level1"><h1>Interactive viewer pane</h1>
<p>You should have installed the R package <a href="https://github.com/nx10/httpgd">httpgd</a> during the initial setup. This package is a pre-requisite for using the interactive viewer pane. In VS Code the viewer works much the same way as in RStudio, but it has a couple of benefits like opening a new tab each time you view something, and better interactive filtering for tables. When running through this setup with a few people I’ve noticed that the interactive viewer is often a place where we encounter errors.</p>
<p>The solution every time is to add the code block below to <code>~/.Rprofile</code>.</p>
<p>First of all, check if you have an <code>~/.Rprofile</code>. If you don’t, make one. If you do, add the contents of this code block.</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="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/interactive.html">interactive</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;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Sys.getenv.html">Sys.getenv</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;">"RSTUDIO"</span><span class="op" style="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;">""</span><span class="op" style="color: #5E5E5E;
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;"><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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/file.path.html">file.path</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/Sys.getenv.html">Sys.getenv</a></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;">.Platform</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">OS.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;">"windows"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USERPROFILE"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HOME"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".vscode-R"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"init.R"</span><span class="op" style="color: #5E5E5E;
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="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/interactive.html">interactive</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;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Sys.getenv.html">Sys.getenv</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;">"TERM_PROGRAM"</span><span class="op" style="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;">"vscode"</span><span class="op" style="color: #5E5E5E;
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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"httpgd"</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/zpackages.html">.packages</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>all.available <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/options.html">options</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vsc.plot <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>
<span>    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/options.html">options</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>device <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;">httpgd</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;">hgd</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>silent <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.vsc.browser</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">httpgd</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;">hgd_url</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>history <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>, viewer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Beside"</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>
<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/options.html">options</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vsc.rstudioapi <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>Strictly speaking the <code>options(vsc.rstudioapi = TRUE)</code> line is not needed for this step. But it is a <a href="https://github.com/REditorSupport/vscode-R/wiki/RStudio-addin-support#enabling-rstudio-addin-support">prerequisite for using RStudio addins</a>, including awesome ones like <a href="https://github.com/lionel-/codegrip">codegrip</a>, so you may as well include it.</p>
</section><section id="format-on-save" class="level1"><h1>Format on save</h1>
<p>Did you know that there’s such a thing as “format on save”? It sounds terrible, right? Someone else’s idea of what looks good absolutely destroying your inspiring, elegant, unique code?</p>
<p>Well for me… It’s actually been kind of liberating. I would never have tried this, but recently at work I’ve been collaborating with colleagues on a python codebase. For consistency’s sake we all have the same settings for python, including use of a formatter with format on save enable. Whenever we save files the code gets formatted.</p>
<p>Yeah, it means your personal style disappears somewhat. But the truth is that most of us code similarly anyways. At work it has been great, because I don’t know all of the python idioms and apparently my code stinks of R.</p>
<p>Once we switched to the formatter? No more stinky typey. Idioms are largely upheld and we get to focus on solving the actual problems, not fretting over formatting and indentation. I’ve spent quite a bit of time with R in VS Code over the past month, and I have found the same sense of freedom.</p>
<p>There is a linter for R (<a href="https://lintr.r-lib.org">lintr</a>) and it is automatically set up when you install the R extension<sup>1</sup>. VS Code can pick this linter up and use it to format your code on save. You’re going to get shouted at a lot by the linter anyway, so if you’re curious about trying format on save, this is all you need to do:</p>
<p>In <code>settings.json</code> add the following items</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript"><span id="cb3-1">[</span>
<span id="cb3-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[r]"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb3-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"editor.formatOnSave"</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;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"editor.tabSize"</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"editor.detectIndentation"</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;">false</span></span>
<span id="cb3-6">  }</span>
<span id="cb3-7">]</span></code></pre></div></div>
</div>
<p>The tab size and detect indentation items are there to stop lintr acting goofy with format on save. If you’re going to change lintr’s indentation settings (e.g.&nbsp;you prefer 4-space indentation), remove the <code>tabsize</code> one (or change it to 4).</p>
<p>With RStudio people use lintr in combination with <a href="https://github.com/r-lib/styler">styler</a>, and sometimes GitHub actions, to apply style to their code. That’s fine. But it’s not for me, I prefer format on save. I literally just press Cmd + S and the formatting is taken care of, no matter what the issue is. It’s simple and immediate.</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">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you hate the idea of format on save, or even a linter, and see them as an affront to the craft of programming: It’s ok. I get it. Everything you have to say is valid - you do you!</p>
</div>
</div>
</section><section id="extensions" class="level1"><h1>Extensions</h1>
<p>These are the notable extensions that I have been using. You can find them by searching the extension marketplace with their name - they’re all rather distinct.</p>
<p>None of them are inherently needed if you’re working with plain R scripts.</p>
<section id="rainbow-csv" class="level2"><h2 class="anchored" data-anchor-id="rainbow-csv">Rainbow CSV</h2>
<p>I work with a lot of csv and dat files for work, and they require regular inspection. If I needed to look at them outside of R, usually I’d open the file in TextEdit or TextMate if small/simple, or Excel if not.</p>
<p>But that’s been massively reduced thanks to this extension, which is very fast and simple. It works on csv, tsv, and dat, with good instructions in the readme for customising behaviour.</p>
<p><img src="https://jimgar.github.io/posts/vs-code-macos-r/images/rainbow-csv.png" class="img-fluid" style="width:80.0%" alt="A VS Code window showing the R dataset 'mtcars' open as a standalone csv file. The Rainbow CSV extension is installed and enabled, applying a series of alternating colours to the columns of mtcars.csv"></p>
<p>Yay! That’s nice, right? 🌈</p>
<p>There’s a few non-obvious tools available as well. First, when hovering over values it tells you the column name.</p>
<p>Second, in the toolbar at the bottom of VS Code, you can see “Query”, and “Align”. These are awesome.</p>
<p><strong>Query</strong> will open a table-viewer and access to RBQL (rainbow query language), which is basically SQL.</p>
<p><strong>Align</strong> will align all of the colums by inserting whitespace, and you can then click the same button to shrink the table back. If you are going to do this the docs (and I!) recommend that you put the following in your settings json: It will create a temp file where the alignment and shrinking happens, leaving the original file untouched.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript"><span id="cb4-1">{</span>
<span id="cb4-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rainbow_csv.align_in_scratch_file"</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;">true</span></span>
<span id="cb4-3">}</span></code></pre></div></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>If you work with AWS there’s an extension for that, too. You can interact with all your usual services, including S3. Unlike the GUI, you don’t have to download (smaller) files from S3 just to view them. So if you have some csv that needs checking, you can literally just use the file explorer, click the filename, it will open in preview here in VS Code, <strong>and it will get the rainbow csv treatment!</strong> I think this is very cool!!!</p>
</div>
</div>
</section><section id="gitlens" class="level2"><h2 class="anchored" data-anchor-id="gitlens">GitLens</h2>
<p>If you work with git this is nice. When you click on a line of code in the editor you get to see the git blame (ugh why is it called that) and commit message. There’s also a GitLens tab with history and graph in the bottom panel.</p>
</section><section id="quarto" class="level2"><h2 class="anchored" data-anchor-id="quarto">Quarto</h2>
<p>Pretty self-explanatory. There’s great support for Quarto in VS Code. That’s how I’m writing this blog post :)</p>
</section><section id="peacock" class="level2"><h2 class="anchored" data-anchor-id="peacock">Peacock</h2>
<p>Peacock adds colour to various parts of the VS Code window.</p>
<p>The real use for it is in colouring different workspaces. The settings get added to the workspace-specific settings json so they persist over time. If you have a couple of projects that you tend to flick between, the colour can help to orient you by differentiating between the windows.</p>
</section><section id="r-debugger" class="level2"><h2 class="anchored" data-anchor-id="r-debugger">R Debugger</h2>
<p>To be honest I’ve not played with this much yet, or used it at all in earnest. I usually use the native debugger in the REPL. But it does have one enticing feature, which is a variable viewer.</p>
</section></section><section id="keyboard-shortcuts" class="level1"><h1>Keyboard shortcuts</h1>
<section id="general" class="level2"><h2 class="anchored" data-anchor-id="general">General</h2>
<p>These are the ones that I find notable.</p>
<div class="cell">
<div class="cell-output-display">
<div id="xxzyycmqpb" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#xxzyycmqpb table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#xxzyycmqpb thead, #xxzyycmqpb tbody, #xxzyycmqpb tfoot, #xxzyycmqpb tr, #xxzyycmqpb td, #xxzyycmqpb th {
  border-style: none;
}

#xxzyycmqpb p {
  margin: 0;
  padding: 0;
}

#xxzyycmqpb .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #FFFFFF;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #07193B;
  width: auto;
  border-top-style: hidden;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: hidden;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#xxzyycmqpb .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#xxzyycmqpb .gt_title {
  color: #FFFFFF;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #07193B;
  border-bottom-width: 0;
}

#xxzyycmqpb .gt_subtitle {
  color: #FFFFFF;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #07193B;
  border-top-width: 0;
}

#xxzyycmqpb .gt_heading {
  background-color: #07193B;
  text-align: center;
  border-bottom-color: #07193B;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#xxzyycmqpb .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xxzyycmqpb .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#xxzyycmqpb .gt_col_heading {
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#xxzyycmqpb .gt_column_spanner_outer {
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#xxzyycmqpb .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#xxzyycmqpb .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#xxzyycmqpb .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#xxzyycmqpb .gt_spanner_row {
  border-bottom-style: hidden;
}

#xxzyycmqpb .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#xxzyycmqpb .gt_empty_group_heading {
  padding: 0.5px;
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#xxzyycmqpb .gt_from_md > :first-child {
  margin-top: 0;
}

#xxzyycmqpb .gt_from_md > :last-child {
  margin-bottom: 0;
}

#xxzyycmqpb .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#xxzyycmqpb .gt_stub {
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#xxzyycmqpb .gt_stub_row_group {
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#xxzyycmqpb .gt_row_group_first td {
  border-top-width: 2px;
}

#xxzyycmqpb .gt_row_group_first th {
  border-top-width: 2px;
}

#xxzyycmqpb .gt_summary_row {
  color: #FFFFFF;
  background-color: #07193B;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#xxzyycmqpb .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#xxzyycmqpb .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#xxzyycmqpb .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xxzyycmqpb .gt_grand_summary_row {
  color: #FFFFFF;
  background-color: #07193B;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#xxzyycmqpb .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#xxzyycmqpb .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#xxzyycmqpb .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#xxzyycmqpb .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#xxzyycmqpb .gt_footnotes {
  color: #FFFFFF;
  background-color: #07193B;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#xxzyycmqpb .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#xxzyycmqpb .gt_sourcenotes {
  color: #FFFFFF;
  background-color: #07193B;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#xxzyycmqpb .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#xxzyycmqpb .gt_left {
  text-align: left;
}

#xxzyycmqpb .gt_center {
  text-align: center;
}

#xxzyycmqpb .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#xxzyycmqpb .gt_font_normal {
  font-weight: normal;
}

#xxzyycmqpb .gt_font_bold {
  font-weight: bold;
}

#xxzyycmqpb .gt_font_italic {
  font-style: italic;
}

#xxzyycmqpb .gt_super {
  font-size: 65%;
}

#xxzyycmqpb .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#xxzyycmqpb .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#xxzyycmqpb .gt_indent_1 {
  text-indent: 5px;
}

#xxzyycmqpb .gt_indent_2 {
  text-indent: 10px;
}

#xxzyycmqpb .gt_indent_3 {
  text-indent: 15px;
}

#xxzyycmqpb .gt_indent_4 {
  text-indent: 20px;
}

#xxzyycmqpb .gt_indent_5 {
  text-indent: 25px;
}

#xxzyycmqpb .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}
</style>
<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false"><tbody class="gt_table_body">
<tr class="gt_group_heading_row odd">
<th colspan="2" id="Show/hide" class="gt_group_heading" data-quarto-table-cell-role="th" style="border-top-width: 2px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: #103884; color: #FFD977; font-weight: bold" scope="colgroup">Show/hide</th>
</tr>
<tr class="gt_row_group_first even">
<td class="gt_row gt_left" headers="Show/hide  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + J</td>
<td class="gt_row gt_left" headers="Show/hide  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Show/hide the terminal</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Show/hide  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + B</td>
<td class="gt_row gt_left" headers="Show/hide  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Show/hide the sidebar</td>
</tr>
<tr class="gt_group_heading_row even">
<th colspan="2" id="Editor" class="gt_group_heading" data-quarto-table-cell-role="th" style="border-top-width: 2px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: #103884; color: #FFD977; font-weight: bold" scope="colgroup">Editor</th>
</tr>
<tr class="gt_row_group_first odd">
<td class="gt_row gt_left" headers="Editor  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Ctrl + 1</td>
<td class="gt_row gt_left" headers="Editor  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Focus on editor</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Editor  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Shift + [ / ]</td>
<td class="gt_row gt_left" headers="Editor  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cycle left / right through editor windows</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Editor  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + W</td>
<td class="gt_row gt_left" headers="Editor  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Close current editor window</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Editor  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Option + ↑ / ↓</td>
<td class="gt_row gt_left" headers="Editor  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Move current line (or highlighted block) of text up/down</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Editor  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Option + ↑ / ↓</td>
<td class="gt_row gt_left" headers="Editor  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Insert cursor above or below current position</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Editor  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Shift + V</td>
<td class="gt_row gt_left" headers="Editor  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Preview markdown file</td>
</tr>
<tr class="gt_group_heading_row odd">
<th colspan="2" id="Explorer" class="gt_group_heading" data-quarto-table-cell-role="th" style="border-top-width: 2px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: #103884; color: #FFD977; font-weight: bold" scope="colgroup">Explorer</th>
</tr>
<tr class="gt_row_group_first even">
<td class="gt_row gt_left" headers="Explorer  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Shift + E</td>
<td class="gt_row gt_left" headers="Explorer  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Focus on explorer</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Explorer  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + ↓</td>
<td class="gt_row gt_left" headers="Explorer  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Open highlighted file from explorer</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Explorer  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Ctrl + Enter</td>
<td class="gt_row gt_left" headers="Explorer  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Open highlighted file from explorer (to the side)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Explorer  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + ←</td>
<td class="gt_row gt_left" headers="Explorer  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Collapse all folders</td>
</tr>
<tr class="gt_group_heading_row even">
<th colspan="2" id="Terminal" class="gt_group_heading" data-quarto-table-cell-role="th" style="border-top-width: 2px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: #103884; color: #FFD977; font-weight: bold" scope="colgroup">Terminal</th>
</tr>
<tr class="gt_row_group_first odd">
<td class="gt_row gt_left" headers="Terminal  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Ctrl + ↑ / ↓</td>
<td class="gt_row gt_left" headers="Terminal  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Grow/shrink the terminal pane</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Terminal  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Shift + [ / ]</td>
<td class="gt_row gt_left" headers="Terminal  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cycle left / right through terminals</td>
</tr>
<tr class="gt_group_heading_row odd">
<th colspan="2" id="Workspace" class="gt_group_heading" data-quarto-table-cell-role="th" style="border-top-width: 2px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: #103884; color: #FFD977; font-weight: bold" scope="colgroup">Workspace</th>
</tr>
<tr class="gt_row_group_first even">
<td class="gt_row gt_left" headers="Workspace  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Shift + N</td>
<td class="gt_row gt_left" headers="Workspace  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Open a new window</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Workspace  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + O</td>
<td class="gt_row gt_left" headers="Workspace  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Open a file or folder</td>
</tr>
</tbody></table>
</div>
</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>Shrinking the terminal will <a href="https://kevinyank.com/posts/fix-system-beep-vscode/#:~:text=The%20issue%20seems%20to%20be,warn%20you%20that%20your%20keystrokes">cause the macOS system beep to kick in</a>, which is annoying as hell. I think this shortcut is so useful that it’s actually worth deactivating the beep. It’s simple to do. First check if you have a file at <code>~/Library/KeyBindings/DefaultKeyBinding.dict</code></p>
<p>If you do, add the line from the code block below. If you don’t:</p>
<ol type="1">
<li>Create the directory <code>~/Library/KeyBindings</code>
</li>
<li>Create the file <code>DefaultKeyBinding.dict</code> in the new directory</li>
<li>Add the below to the file<sup>2</sup>
</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript"><span id="cb5-1">{</span>
<span id="cb5-2">  <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;">\U</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">F701"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"noop:"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb5-3">}</span></code></pre></div></div>
</div>
<p>Alternatively, you can make custom keybindings in VS Code (see below for examples of how to do that).</p>
</div>
</div>
</section><section id="r-specific" class="level2"><h2 class="anchored" data-anchor-id="r-specific">R specific</h2>
<p>These should all be familiar if you’re used to RStudio. The Quarto ones come with the Quarto extension.</p>
<div class="cell">
<div class="cell-output-display">
<div id="vlfbynqhsf" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#vlfbynqhsf table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#vlfbynqhsf thead, #vlfbynqhsf tbody, #vlfbynqhsf tfoot, #vlfbynqhsf tr, #vlfbynqhsf td, #vlfbynqhsf th {
  border-style: none;
}

#vlfbynqhsf p {
  margin: 0;
  padding: 0;
}

#vlfbynqhsf .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #FFFFFF;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #07193B;
  width: auto;
  border-top-style: hidden;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: hidden;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#vlfbynqhsf .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#vlfbynqhsf .gt_title {
  color: #FFFFFF;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #07193B;
  border-bottom-width: 0;
}

#vlfbynqhsf .gt_subtitle {
  color: #FFFFFF;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #07193B;
  border-top-width: 0;
}

#vlfbynqhsf .gt_heading {
  background-color: #07193B;
  text-align: center;
  border-bottom-color: #07193B;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#vlfbynqhsf .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#vlfbynqhsf .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#vlfbynqhsf .gt_col_heading {
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#vlfbynqhsf .gt_column_spanner_outer {
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#vlfbynqhsf .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#vlfbynqhsf .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#vlfbynqhsf .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#vlfbynqhsf .gt_spanner_row {
  border-bottom-style: hidden;
}

#vlfbynqhsf .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#vlfbynqhsf .gt_empty_group_heading {
  padding: 0.5px;
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#vlfbynqhsf .gt_from_md > :first-child {
  margin-top: 0;
}

#vlfbynqhsf .gt_from_md > :last-child {
  margin-bottom: 0;
}

#vlfbynqhsf .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#vlfbynqhsf .gt_stub {
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#vlfbynqhsf .gt_stub_row_group {
  color: #FFFFFF;
  background-color: #07193B;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#vlfbynqhsf .gt_row_group_first td {
  border-top-width: 2px;
}

#vlfbynqhsf .gt_row_group_first th {
  border-top-width: 2px;
}

#vlfbynqhsf .gt_summary_row {
  color: #FFFFFF;
  background-color: #07193B;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#vlfbynqhsf .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#vlfbynqhsf .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#vlfbynqhsf .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#vlfbynqhsf .gt_grand_summary_row {
  color: #FFFFFF;
  background-color: #07193B;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#vlfbynqhsf .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#vlfbynqhsf .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#vlfbynqhsf .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#vlfbynqhsf .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#vlfbynqhsf .gt_footnotes {
  color: #FFFFFF;
  background-color: #07193B;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#vlfbynqhsf .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#vlfbynqhsf .gt_sourcenotes {
  color: #FFFFFF;
  background-color: #07193B;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#vlfbynqhsf .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#vlfbynqhsf .gt_left {
  text-align: left;
}

#vlfbynqhsf .gt_center {
  text-align: center;
}

#vlfbynqhsf .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#vlfbynqhsf .gt_font_normal {
  font-weight: normal;
}

#vlfbynqhsf .gt_font_bold {
  font-weight: bold;
}

#vlfbynqhsf .gt_font_italic {
  font-style: italic;
}

#vlfbynqhsf .gt_super {
  font-size: 65%;
}

#vlfbynqhsf .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#vlfbynqhsf .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#vlfbynqhsf .gt_indent_1 {
  text-indent: 5px;
}

#vlfbynqhsf .gt_indent_2 {
  text-indent: 10px;
}

#vlfbynqhsf .gt_indent_3 {
  text-indent: 15px;
}

#vlfbynqhsf .gt_indent_4 {
  text-indent: 20px;
}

#vlfbynqhsf .gt_indent_5 {
  text-indent: 25px;
}

#vlfbynqhsf .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}
</style>
<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false"><tbody class="gt_table_body">
<tr class="gt_group_heading_row odd">
<th colspan="2" id="Run code" class="gt_group_heading" data-quarto-table-cell-role="th" style="border-top-width: 2px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: #103884; color: #FFD977; font-weight: bold" scope="colgroup">Run code</th>
</tr>
<tr class="gt_row_group_first even">
<td class="gt_row gt_left" headers="Run code  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Enter</td>
<td class="gt_row gt_left" headers="Run code  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Run the current line or highlighted code</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="Run code  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Shift + Enter</td>
<td class="gt_row gt_left" headers="Run code  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Run the entire script</td>
</tr>
<tr class="gt_group_heading_row even">
<th colspan="2" id="Quarto" class="gt_group_heading" data-quarto-table-cell-role="th" style="border-top-width: 2px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: #103884; color: #FFD977; font-weight: bold" scope="colgroup">Quarto</th>
</tr>
<tr class="gt_row_group_first odd">
<td class="gt_row gt_left" headers="Quarto  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Shift + K</td>
<td class="gt_row gt_left" headers="Quarto  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Render and show preview</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="Quarto  Keys" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Cmd + Shift + I</td>
<td class="gt_row gt_left" headers="Quarto  Description" style="border-top-width: 1px; border-top-style: solid; border-top-color: #103884; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #103884">Insert code chunk</td>
</tr>
</tbody></table>
</div>
</div>
</div>
</section><section id="custom-keybindings" class="level2"><h2 class="anchored" data-anchor-id="custom-keybindings">Custom keybindings</h2>
<p>Like the settings file, there’s a <code>keybindings.json</code> for any bespoke keyboard shortcuts that you want to make. Open the command palette as before, but this time you’re looking for “Open Keyboard Shortcuts (JSON)”.</p>
<p>It is super useful to have something that can toggle between the terminal and editor. You can choose whatever keystrokes you like but Ctrl + ` is a common choice.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript"><span id="cb6-1">[</span>
<span id="cb6-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// toggle between the terminal and editor</span></span>
<span id="cb6-3">  {</span>
<span id="cb6-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"key"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctrl+`"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"command"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"workbench.action.terminal.focus"</span></span>
<span id="cb6-6">  }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-7">  {</span>
<span id="cb6-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"key"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ctrl+`"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"command"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"workbench.action.focusActiveEditorGroup"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb6-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"when"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"terminalFocus"</span></span>
<span id="cb6-11">  }</span>
<span id="cb6-12">]</span></code></pre></div></div>
</div>
<p>For R, most of us have used RStudio extensively and are familiar with three shortcuts, namely</p>
<ul>
<li>
<strong>Insert pipe</strong> (Cmd + Shift + M)</li>
<li>
<strong>Insert assignment operator</strong> (Option + -)</li>
<li>
<strong>Restart R session</strong> (Cmd + Shift + 0)</li>
</ul>
<p>The keybindings below will add all of these to VS Code. It might look like there’s some duplication here for the pipe and assignment operator, and there kind of is. It’s just that you need to make the keybindings available to the editor and terminal separately (I tried to combine them but it didn’t work - if anyone knows how please get in touch!).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript"><span id="cb7-1">[</span>
<span id="cb7-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// keybindings for r/rmd/qmd</span></span>
<span id="cb7-3">  {</span>
<span id="cb7-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"key"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cmd+Shift+m"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"command"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"args"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb7-7">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" |&gt;"</span></span>
<span id="cb7-8">    }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"when"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"editorTextFocus &amp;&amp; editorLangId =~ /r|rmd|quarto/"</span></span>
<span id="cb7-10">  }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-11">  {</span>
<span id="cb7-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"key"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alt+-"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"command"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"args"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb7-15">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span 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;- "</span></span>
<span id="cb7-16">    }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-17">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"when"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"editorTextFocus &amp;&amp; editorLangId =~ /r|rmd|quarto/"</span></span>
<span id="cb7-18">  }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// keybindings for R terminal (radian included)</span></span>
<span id="cb7-20">  {</span>
<span id="cb7-21">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"key"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cmd+Shift+m"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-22">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"command"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"workbench.action.terminal.sendSequence"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-23">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"args"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb7-24">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" |&gt;"</span></span>
<span id="cb7-25">    }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-26">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"when"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"terminalFocus"</span></span>
<span id="cb7-27">  }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-28">  {</span>
<span id="cb7-29">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"key"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alt+-"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-30">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"command"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"workbench.action.terminal.sendSequence"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-31">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"args"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb7-32">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span 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;- "</span></span>
<span id="cb7-33">    }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-34">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"when"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"terminalFocus"</span></span>
<span id="cb7-35">  }<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-36">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// restart R session</span></span>
<span id="cb7-37">  {</span>
<span id="cb7-38">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"key"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cmd+Shift+0"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-39">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"command"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"runCommands"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-40">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"args"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb7-41">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"commands"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb7-42">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"workbench.action.terminal.kill"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb7-43">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r.createRTerm"</span></span>
<span id="cb7-44">      ]</span>
<span id="cb7-45">    }</span>
<span id="cb7-46">  }</span>
<span id="cb7-47">]</span></code></pre></div></div>
</div>


</section></section><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p>If you want to change default lintr settings see https://github.com/REditorSupport/vscode-R/wiki/Code-linting↩︎</p></li>
<li id="fn2"><p>If you’re on a version of macOS Sonoma &lt; 14.4, the line should read <code>"^@\UF701" = "noop";</code>↩︎</p></li>
</ol></section></div> ]]></description>
  <category>VS Code</category>
  <category>R</category>
  <category>macOS</category>
  <guid>https://jimgar.github.io/posts/vs-code-macos-r/post.html</guid>
  <pubDate>Wed, 20 Dec 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Day 3 - Advent of Code 2023</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/advent-of-code-2023-day-3/post.html</link>
  <description><![CDATA[ <p>AoC day 3 using base R. I got caught in the matrix 🥲</p>
<p>Spoilers ahead.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/advent-of-code-2023-day-3/matrix.jpeg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:60.0%" alt="Green text streaming vertically down a black background. It looks like the code seen in The Matrix movies."></p>
</figure>
</div>
<section id="solution-without-commentary" class="level1"><h1>Solution without commentary</h1>
<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;">input</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/readLines.html">readLines</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;">"input.txt"</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;">schematic</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/matrix.html">matrix</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/unlist.html">unlist</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/strsplit.html">strsplit</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input</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>  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;">140</span>,</span>
<span>  nrow <span 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>
<span>  byrow <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>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input_as_string</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/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input</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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_locations</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/grep.html">gregexpr</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;">"\\d+"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input_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;">numbers</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/integer.html">as.integer</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input_as_string</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_locations</span><span class="op" style="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 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;">symbols</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/regmatches.html">regmatches</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;">input_as_string</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gregexpr</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;">"[^0-9.]"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input_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 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/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;"><a href="https://rdrr.io/r/base/unique.html">unique</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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</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/funprog.html">Map</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_location</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number</span><span class="op" style="color: #5E5E5E;
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;"># Convert the first digit's index from the stringified input to its</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># equivalent position in the schematic/matrix</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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/Round.html">ceiling</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_location</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="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;">column</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;">number_location</span> <span class="op" style="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;">row</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">140</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;"># Bounding box. Account for boxes falling outside matrix edges</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">column</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="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> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">column</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">right</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">column</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nchar.html">nchar</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number</span><span class="op" style="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;">141</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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">column</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nchar.html">nchar</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number</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;">top</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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="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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bottom</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">141</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collect up characters in the box</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">above</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">top</span><span class="op" style="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">schematic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">top</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">right</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;">middle</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;">schematic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">right</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;">below</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">bottom</span><span class="op" style="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">schematic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bottom</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">right</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;">chars</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">above</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">middle</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">below</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;">part</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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/any.html">any</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">symbols</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">chars</span><span class="op" style="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;">number</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;">NA</span></span>
<span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find index of *, if it exists</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">star</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</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="st" style="color: #20794D;
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/match.html">%in%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">above</span><span class="op" style="color: #5E5E5E;
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;">star</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/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">top</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span> <span class="op" style="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/which.html">which</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">above</span><span class="op" style="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="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="st" style="color: #20794D;
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/match.html">%in%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">middle</span><span class="op" style="color: #5E5E5E;
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;">star</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/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span> <span class="op" style="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/which.html">which</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">middle</span><span class="op" style="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="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="st" style="color: #20794D;
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/match.html">%in%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">below</span><span class="op" style="color: #5E5E5E;
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;">star</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/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bottom</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span> <span class="op" style="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/which.html">which</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">below</span><span class="op" style="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="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/data.frame.html">data.frame</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>parts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">part</span>, stars <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">star</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;">number_locations</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</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/base/do.call.html">do.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;">rbind</span>, args <span 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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Part 1</span></span>
<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="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts</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>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Part 2</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dupes</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;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/duplicated.html">duplicated</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stars</span>, incomparables <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="va" style="color: #111111;
background-color: null;
font-style: inherit;">gears</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;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stars</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">dupes</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/sum.html">sum</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/aggregate.html">aggregate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gears</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts</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;"><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>Stars <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gears</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">prod</span><span class="op" style="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></code></pre></div></div>
</details>
</div>
</section><section id="part-1" class="level1"><h1>Part 1</h1>
<section id="the-problem" class="level2"><h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>— Day 3: Gear Ratios —</p>
<p>You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the <strong>water source</strong>, but this is as far as he can bring you. You go inside.</p>
<p>It doesn’t take long to find the gondolas, but there seems to be a problem: they’re not moving.</p>
<p>“Aaah!”</p>
<p>You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. “Sorry, I wasn’t expecting anyone! The gondola lift isn’t working right now; it’ll still be a while before I can fix it.” You offer to help.</p>
<p>The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can <strong>add up all the part numbers</strong> in the engine schematic, it should be easy to work out which part is missing.</p>
<p>The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don’t really understand, but apparently <strong>any number adjacent to a symbol</strong>, even diagonally, is a “part number” and should be included in your sum. (Periods (.) do not count as a symbol.)</p>
<p>Here is an example engine schematic:</p>
<pre><code>467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..</code></pre>
<p>In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is <strong>4361</strong>.</p>
<p>Of course, the actual engine schematic is much larger. <strong>What is the sum of all of the part numbers in the engine schematic?</strong></p>
</section><section id="solution" class="level2"><h2 class="anchored" data-anchor-id="solution">Solution</h2>
<p>Right, so in AoC day 1 I saw my first ever matrix. That means I now see matrices everywhere. reading the problem, I envision the schematic as a matrix, and there’s a bounding box drawn around each cluster of digits. The box encompasses all of the characters surrounding the digits, too. All I have to do is get the indices of the digits and it should be pretty simple maths from there.</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;">input</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/readLines.html">readLines</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;">"input.txt"</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;">input</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "............................................411.....................363..134.........463.775..........................506..................."
[2] "......429...836..$............../..960........*.............+..........*...=....381.....*........67......426.....=..../...304..............."
[3] ".........*...&amp;...641..........924..*.........855....492..495.......476.927.......*.........680...../.&amp;....*.....713......*.................."</code></pre>
</div>
<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/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;">input</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] 140</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/all.html">all</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/nchar.html">nchar</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input</span><span class="op" style="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="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] TRUE</code></pre>
</div>
</div>
<p>Yeah, so this thing is 140 x 140 characters. I’m now absolutely committed to the idea of it being a matrix.</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;">schematic</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/matrix.html">matrix</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/unlist.html">unlist</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/strsplit.html">strsplit</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input</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>  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;">140</span>,</span>
<span>  nrow <span 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>
<span>  byrow <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>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">schematic</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;">6</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;">12</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] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] "."  "."  "."  "."  "."  "."  "."  "."  "."  "."   "."   "."  
[2,] "."  "."  "."  "."  "."  "."  "4"  "2"  "9"  "."   "."   "."  
[3,] "."  "."  "."  "."  "."  "."  "."  "."  "."  "*"   "."   "."  
[4,] "."  "."  "."  "."  "."  "."  "."  "."  "."  "9"   "0"   "0"  
[5,] "."  "."  "."  "."  "."  "."  "."  "."  "."  "."   "."   "."  
[6,] "."  "."  "."  "."  "."  "."  "."  "."  "7"  "2"   "1"   "."  </code></pre>
</div>
</div>
<p>And this is where my brain ground to a halt. I completely boxed myself in - pun not intended but very much appropriate - because I became fixated on the matrix. I didn’t want to do another list extravaganza. I kept printing the data and thinking, “It’s a fucking cube. A cube! MATRIX IS THE ANSWER!!!”</p>
<p>So yeah after a couple of hours trying to figure out how I would capture the digits, which reached a low point when I considered lapplying through every index in the matrix 💀, I knew it was time to look for “inspiration.”</p>
<p>Which came in the form of Adam Austin <a href="https://github.com/ataustin/advent-of-code/blob/main/y2023/d03/solution.R">again</a>. Did he use a matrix? No.&nbsp;But our ideas for how to solve were actually very similar. I used his approach of turning the input into a single string to unlock everything else that would make the matrix work.</p>
<p>That’s right. I still kept on with the matrix idea. We learn by doing hard things, right? Or by making things hard? Something like that… 😅</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;">input_as_string</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/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input</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>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The first position for each group of numbers</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_locations</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/grep.html">gregexpr</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;">"\\d+"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input_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;">number_locations</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="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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1]  45  69  74  86  90 119 147 153 176 221</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The actual numbers in each group</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</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/integer.html">as.integer</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input_as_string</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_locations</span><span class="op" style="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 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;">numbers</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 411 363 134 463 775 506 429 836 960 381</code></pre>
</div>
</div>
<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;">symbols</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/regmatches.html">regmatches</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;">input_as_string</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gregexpr</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;">"[^0-9.]"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">input_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 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/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;"><a href="https://rdrr.io/r/base/unique.html">unique</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>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">symbols</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "$" "/" "*" "+" "=" "&amp;" "%" "#" "@" "-"</code></pre>
</div>
</div>
<p>Once I’ve got access to all the characters in a bounding box, I’ll need to check if at least one of them is in this vector of symbols. If so, the numbers are a “part”.</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;">parts_and_stars</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/funprog.html">Map</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_location</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number</span><span class="op" style="color: #5E5E5E;
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;"># Convert the first digit's index from the stringified input to its</span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># equivalent position in the schematic/matrix</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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/Round.html">ceiling</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_location</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="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;">column</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;">number_location</span> <span class="op" style="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;">row</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">140</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;"># Bounding box. Account for boxes falling outside matrix edges</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">column</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="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> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">column</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">right</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">column</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nchar.html">nchar</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number</span><span class="op" style="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;">141</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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">column</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nchar.html">nchar</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number</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;">top</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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="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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bottom</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">141</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collect up characters in the box</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">above</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">top</span><span class="op" style="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">schematic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">top</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">right</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;">middle</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;">schematic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">right</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;">below</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">bottom</span><span class="op" style="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">schematic</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bottom</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">right</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;">chars</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">above</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">middle</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">below</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;">part</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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/any.html">any</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">symbols</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">chars</span><span class="op" style="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;">number</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;">NA</span></span>
<span></span>
<span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For part 2. Find index of *, if it exists</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">star</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</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="st" style="color: #20794D;
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/match.html">%in%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">above</span><span class="op" style="color: #5E5E5E;
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;">star</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/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">top</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span> <span class="op" style="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/which.html">which</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">above</span><span class="op" style="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="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="st" style="color: #20794D;
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/match.html">%in%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">middle</span><span class="op" style="color: #5E5E5E;
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;">star</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/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">row</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span> <span class="op" style="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/which.html">which</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">middle</span><span class="op" style="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="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="st" style="color: #20794D;
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/match.html">%in%</a></span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">below</span><span class="op" style="color: #5E5E5E;
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;">star</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/paste.html">paste</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bottom</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span> <span class="op" style="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/which.html">which</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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">below</span><span class="op" style="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="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/data.frame.html">data.frame</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>parts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">part</span>, stars <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">star</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;">number_locations</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</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/base/do.call.html">do.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;">rbind</span>, args <span 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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</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>  parts stars
1   411  2 47
2   363  2 72
3   134  &lt;NA&gt;
4   463  2 89
5   775  2 89
6   506  &lt;NA&gt;</code></pre>
</div>
</div>
<p>To be fair, I think this code is actually fine. It works and makes logical sense. I wasn’t too happy about including the star bit, which is for part 2 of the problem. Originally it wasn’t a part of the <code>Map</code> and I had to go back and add it. I’ll explain more in a moment.</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;"><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="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 536202</code></pre>
</div>
</div>
</section></section><section id="part-2" class="level1"><h1>Part 2</h1>
<section id="the-problem-1" class="level2"><h2 class="anchored" data-anchor-id="the-problem-1">The problem</h2>
<p>— Part Two —</p>
<p>The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.</p>
<p>You don’t seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled “help”, so you pick it up and the engineer answers.</p>
<p>Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You’re going so slowly that you haven’t even left the station. You exit the gondola.</p>
<p>The missing part wasn’t the only issue - one of the gears in the engine is wrong. A <strong>gear</strong> is any <code>*</code> symbol that is adjacent to <strong>exactly two part numbers</strong>. Its <strong>gear ratio</strong> is the result of multiplying those two numbers together.</p>
<p>This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.</p>
<p>Consider the same engine schematic again:</p>
<pre><code>467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..</code></pre>
<p>In this schematic, there are <strong>two</strong> gears. The first is in the top left; it has part numbers <code>467</code> and <code>35</code>, so its gear ratio is <code>16345</code>. The second gear is in the lower right; its gear ratio is <code>451490</code>. (The <code>*</code> adjacent to <code>617</code> is not a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces <strong><code>467835</code></strong>.</p>
<p><strong>What is the sum of all of the gear ratios in your engine schematic?</strong></p>
</section><section id="solution-1" class="level2"><h2 class="anchored" data-anchor-id="solution-1">Solution</h2>
<p>A gear is a star that’s in the bounding box of two parts. I was already tracking the potential parts in the <code>Map</code>, so I also grabbed the indices of any star that appeared. As a reminder, here’s the dataframe I’m working with</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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</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>  parts stars
1   411  2 47
2   363  2 72
3   134  &lt;NA&gt;
4   463  2 89
5   775  2 89
6   506  &lt;NA&gt;</code></pre>
</div>
</div>
<p>The index of a star is unique. It’s basically an ID. So if a star’s ID appears more than once next to a part, it is a gear.</p>
<div class="cell">
<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="va" style="color: #111111;
background-color: null;
font-style: inherit;">dupes</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;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/duplicated.html">duplicated</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stars</span>, incomparables <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="va" style="color: #111111;
background-color: null;
font-style: inherit;">dupes</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "2 89"  "2 47"  "2 72"  "3 10"  "3 36"  "3 82"  "3 107" "3 122" "4 56" 
[10] "4 70" </code></pre>
</div>
<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;">gears</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;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts_and_stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stars</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">dupes</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;">gears</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   parts stars
1    411  2 47
2    363  2 72
4    463  2 89
5    775  2 89
7    429  3 10
9    960  3 36
10   381  3 82
12   426 3 107
13   304 3 122
16   855  2 47</code></pre>
</div>
<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/sum.html">sum</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/aggregate.html">aggregate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gears</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">parts</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;"><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>Stars <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gears</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">stars</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">prod</span><span class="op" style="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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 78272573</code></pre>
</div>
</div>
<p>Ending on an Adam Austin double-whammy: <code>aggregate</code> with <code>prod</code> inside.</p>


</section></section> ]]></description>
  <category>R</category>
  <category>base R</category>
  <category>Advent of Code</category>
  <category>Advent of Code 2023</category>
  <guid>https://jimgar.github.io/posts/advent-of-code-2023-day-3/post.html</guid>
  <pubDate>Fri, 15 Dec 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Day 2 - Advent of Code 2023</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/advent-of-code-2023-day-2/post.html</link>
  <description><![CDATA[ <p>AoC day 2 using base R.</p>
<p>I spent multiple evenings working on this and trying to understand three other programmer’s solutions.</p>
<p>If you’re interested in the comparative piece - which you should be because it’s the best part - go to the last section of this post.</p>
<p>Spoilers ahead.</p>
<section id="solution-without-commentary" class="level1"><h1>Solution without commentary</h1>
<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;">games_raw</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/readLines.html">readLines</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;">"input.txt"</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;">games_raw</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/setNames.html">setNames</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_raw</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;">)</span></span>
<span></span>
<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;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gregexpr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>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;">"\\d+ ((red)|(blue)|(green))"</span>, text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_raw</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;">games</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_raw</span>, m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_with_counts_for_colours</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span><span class="op" style="color: #5E5E5E;
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;">colours</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>red <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, blue <span 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>, green <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">colours</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">colour</span><span class="op" style="color: #5E5E5E;
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/grep.html">grep</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">colour</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span>, value <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;">|&gt;</span></span>
<span>      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gsub</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;">" [a-z]+"</span>, <span class="st" style="color: #20794D;
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;">_</span><span 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/integer.html">as.integer</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;">)</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;">possible_games</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/lapply.html">sapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_with_counts_for_colours</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game_rbg_counts</span><span class="op" style="color: #5E5E5E;
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;">limits</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>red <span 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>, blue <span 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>, green <span 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>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/funprog.html">Map</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">count</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">limit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/all.html">all</a></span><span 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;">&lt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">limit</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;">game_rbg_counts</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">limits</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/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;"><a href="https://rdrr.io/r/base/all.html">all</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;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">possible_games</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">possible_games</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 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/names.html">names</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/integer.html">as.integer</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/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></code></pre></div></div>
</details>
</div>
</section><section id="part-1" class="level1"><h1>Part 1</h1>
<section id="the-problem" class="level2"><h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>— Day 2: Cube Conundrum —</p>
<p>You’re launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile of leaves. It’s quite cold, but you don’t see much snow. An Elf runs over to greet you.</p>
<p>The Elf explains that you’ve arrived at Snow Island and apologizes for the lack of snow. He’ll be happy to explain the situation, but it’s a bit of a walk, so you have some time. They don’t get many visitors up here; would you like to play a game in the meantime?</p>
<p>As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to figure out information about the number of cubes.</p>
<p>To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random cubes, show them to you, and then put them back in the bag. He’ll do this a few times per game.</p>
<p>You play several games and record the information from each game (your puzzle input). Each game is listed with its ID number (like the 11 in Game 11: …) followed by a semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 red, 5 green, 4 blue).</p>
<p>For example, the record of a few games might look like this:</p>
<pre><code>Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green</code></pre>
<p>In game 1, three sets of cubes are revealed from the bag (and then put back again). The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is only 2 green cubes.</p>
<p>The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?</p>
<p>In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly, game 4 would also have been impossible because the Elf showed you 15 blue cubes at once. If you add up the IDs of the games that would have been possible, you get 8.</p>
<p>Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those games?</p>
</section><section id="solution" class="level2"><h2 class="anchored" data-anchor-id="solution">Solution</h2>
<p>For each game, I just need to know if <strong>any</strong> of the counts for a colour exceed the possible limits. The subsets of the games are irrelevant, though maybe they’ll be important for part 2 - we’ll see!</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;">games_raw</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/readLines.html">readLines</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;">"input.txt"</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;">games_raw</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/setNames.html">setNames</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_raw</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;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_raw</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                                                                                                                                                      1 
                                    "Game 1: 1 red, 3 blue, 11 green; 1 blue, 5 red; 3 blue, 5 green, 13 red; 6 red, 1 blue, 4 green; 16 red, 12 green" 
                                                                                                                                                      2 
       "Game 2: 3 red, 13 blue, 5 green; 14 green, 14 blue; 9 blue, 10 green, 3 red; 2 green, 5 blue; 11 green, 3 blue, 3 red; 16 blue, 2 red, 9 green" 
                                                                                                                                                      3 
"Game 3: 17 blue, 5 red; 3 red, 11 green, 17 blue; 1 red, 6 blue, 9 green; 3 blue, 11 green, 1 red; 3 green, 10 red, 11 blue; 12 red, 3 green, 15 blue" </code></pre>
</div>
</div>
<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;">matches</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/grep.html">gregexpr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>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;">"\\d+ ((red)|(blue)|(green))"</span>, text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_raw</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;">games</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_raw</span>, m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$`1`
 [1] "1 red"    "3 blue"   "11 green" "1 blue"   "5 red"    "3 blue"  
 [7] "5 green"  "13 red"   "6 red"    "1 blue"   "4 green"  "16 red"  
[13] "12 green"

$`2`
 [1] "3 red"    "13 blue"  "5 green"  "14 green" "14 blue"  "9 blue"  
 [7] "10 green" "3 red"    "2 green"  "5 blue"   "11 green" "3 blue"  
[13] "3 red"    "16 blue"  "2 red"    "9 green" 

$`3`
 [1] "17 blue"  "5 red"    "3 red"    "11 green" "17 blue"  "1 red"   
 [7] "6 blue"   "9 green"  "3 blue"   "11 green" "1 red"    "3 green" 
[13] "10 red"   "11 blue"  "12 red"   "3 green"  "15 blue" </code></pre>
</div>
</div>
<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;">games_with_counts_for_colours</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span><span class="op" style="color: #5E5E5E;
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;">colours</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>red <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, blue <span 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>, green <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">colours</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">colour</span><span class="op" style="color: #5E5E5E;
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/grep.html">grep</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">colour</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span>, value <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;">|&gt;</span></span>
<span>      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gsub</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;">" [a-z]+"</span>, <span class="st" style="color: #20794D;
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;">_</span><span 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/integer.html">as.integer</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;">)</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;">games_with_counts_for_colours</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$`1`
$`1`$red
[1]  1  5 13  6 16

$`1`$blue
[1] 3 1 3 1

$`1`$green
[1] 11  5  4 12


$`2`
$`2`$red
[1] 3 3 3 2

$`2`$blue
[1] 13 14  9  5  3 16

$`2`$green
[1]  5 14 10  2 11  9


$`3`
$`3`$red
[1]  5  3  1  1 10 12

$`3`$blue
[1] 17 17  6  3 11 15

$`3`$green
[1] 11  9 11  3  3</code></pre>
</div>
</div>
<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;">possible_games</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/lapply.html">sapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_with_counts_for_colours</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game_rbg_counts</span><span class="op" style="color: #5E5E5E;
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;">limits</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>red <span 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>, blue <span 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>, green <span 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>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/funprog.html">Map</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">count</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">limit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/all.html">all</a></span><span 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;">&lt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">limit</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;">game_rbg_counts</span>,</span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">limits</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/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;"><a href="https://rdrr.io/r/base/all.html">all</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;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">possible_games</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>    1     2     3     4     5     6     7     8     9    10 
FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE </code></pre>
</div>
</div>
<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;">possible_games</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">possible_games</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 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/names.html">names</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/integer.html">as.integer</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/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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1931</code></pre>
</div>
</div>
</section></section><section id="part-2" class="level1"><h1>Part 2</h1>
<section id="the-problem-1" class="level2"><h2 class="anchored" data-anchor-id="the-problem-1">The problem</h2>
<p>— Part Two —</p>
<p>The Elf says they’ve stopped producing snow because they aren’t getting any water! He isn’t sure why the water stopped; however, he can show you how to get to the water source to check it out for yourself. It’s just up ahead!</p>
<p>As you continue your walk, the Elf poses a second question: in each game you played, what is the fewest number of cubes of each color that could have been in the bag to make the game possible?</p>
<p>Again consider the example games from earlier:</p>
<pre><code>Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green</code></pre>
<ul>
<li>In game 1, the game could have been played with as few as 4 red, 2 green, and 6 blue cubes. If any color had even one fewer cube, the game would have been impossible.</li>
<li>Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.</li>
<li>Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.</li>
<li>Game 4 required at least 14 red, 3 green, and 15 blue cubes.</li>
<li>Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.</li>
</ul>
<p>The power of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. The power of the minimum set of cubes in game 1 is 48. In games 2-5 it was 12, 1560, 630, and 36, respectively. Adding up these five powers produces the sum 2286.</p>
<p>For each game, find the minimum set of cubes that must have been present. What is the sum of the power of these sets?</p>
</section><section id="solution-1" class="level2"><h2 class="anchored" data-anchor-id="solution-1">Solution</h2>
<p>This part is straightforward. For each game I just need to take the maximum value for each colour and multiply them together. Then add those multiplied values up.</p>
<p>I’ve already done most of the leg work by creating lists of values for each colour, per game.</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;">games_with_counts_for_colours</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$`1`
$`1`$red
[1]  1  5 13  6 16

$`1`$blue
[1] 3 1 3 1

$`1`$green
[1] 11  5  4 12</code></pre>
</div>
</div>
<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;">fewest_cubes_per_game</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games_with_counts_for_colours</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span><span class="op" style="color: #5E5E5E;
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/lapply.html">sapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span>, \<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">colour_values</span><span class="op" style="color: #5E5E5E;
background-color: 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;">colour_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>
<span><span class="op" style="color: #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;">fewest_cubes_per_game</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$`1`
  red  blue green 
   16     3    12 

$`2`
  red  blue green 
    3    16    14 

$`3`
  red  blue green 
   12    17    11 </code></pre>
</div>
</div>
<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;"><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="va" style="color: #111111;
background-color: null;
font-style: inherit;">fewest_cubes_per_game</span>, \<span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span><span class="op" style="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 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 class="op" style="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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span><span class="op" style="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/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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 83105</code></pre>
</div>
</div>
</section></section><section id="other-base-r-solutions" class="level1"><h1>Other base R solutions</h1>
<p>I thought it was super interesting comparing with other people’s base R solutions. I’d summarise my own approach as</p>
<ul>
<li>Keep stuff in lists</li>
<li>Iterate through those lists</li>
<li>Write ‘procedurally’</li>
<li>Err on the side of verbose for variable names</li>
</ul>
<p>Below you can find my thoughts on three other solutions, which taken together showcase how diverse problem-solving can be amongst programmer-types.</p>
<p>Please note that I don’t know what the author motivations were when it comes to style, aesthetics, readability, testability, etc., or indeed how much time they spent on the code or how happy with it they are. Everything I have written below is my ignorant interpretation of their work.</p>
<p>Thanks to <a href="https://johnmackintosh.net">John Mackintosh</a> for making me aware of the authors.</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="">jonocarroll</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="">ataustin</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="">ursulams</a></li>
</ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<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;">f02a</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">checked</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/lapply.html">sapply</a></span><span 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;">f02_helper</span>, USE.NAMES <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>
<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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">checked</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="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>
<span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f02b</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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/sum.html">sum</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f02b_helper</span><span class="op" style="color: #5E5E5E;
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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f02_helper</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">game</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/integer.html">as.integer</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/grep.html">sub</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;">"Game ([0-9]*).*"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\\1"</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;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">sub</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="st" style="color: #20794D;
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;">sets</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/unlist.html">unlist</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/strsplit.html">strsplit</a></span><span 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="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="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/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/strsplit.html">strsplit</a></span><span 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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">trimws</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, simplify <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;">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;">possible</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">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;">{</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">vals</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/strsplit.html">strsplit</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">s</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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">vals</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</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/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;">vals</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;">&lt;=</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 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;">vals</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</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/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;">vals</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;">&lt;=</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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">vals</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</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/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;">vals</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;">&lt;=</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 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;">game</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/all.html">all</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">sets</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">possible</span><span class="op" style="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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">f02b_helper</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">game</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/integer.html">as.integer</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/grep.html">sub</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;">"Game ([0-9]*).*"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\\1"</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;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">sub</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="st" style="color: #20794D;
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;">sets</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/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/strsplit.html">strsplit</a></span><span 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="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="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/base/lapply.html">lapply</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/strsplit.html">strsplit</a></span><span 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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">trimws</span><span class="op" style="color: #5E5E5E;
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;">totals</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sets</span>, \<span 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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unglue</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;">unglue_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;">z</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{n=\\d+} {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>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">check_max</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">d1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d2</span><span class="op" style="color: #5E5E5E;
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;">r1</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/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;">d1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d1</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">r2</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/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;">d2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d2</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">b1</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/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;">d1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d1</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">b2</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/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;">d2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d2</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">g1</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/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;">d1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d1</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">g2</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/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;">d2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d2</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">maxr</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/Extremes.html">max</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="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;">r1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r2</span><span class="op" style="color: #5E5E5E;
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;">maxb</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/Extremes.html">max</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="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;">b1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b2</span><span class="op" style="color: #5E5E5E;
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;">maxg</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/Extremes.html">max</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="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;">g1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">g2</span><span class="op" style="color: #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;">d3</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/data.frame.html">data.frame</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/integer.html">integer</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>, c <span class="op" style="color: #5E5E5E;
background-color: 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">character</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">d3</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/cbind.html">rbind</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d3</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>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">maxr</span>, c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span><span class="op" style="color: #5E5E5E;
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;">d3</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/cbind.html">rbind</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d3</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>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">maxb</span>, c <span 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 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;">d3</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/cbind.html">rbind</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d3</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>n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">maxg</span>, c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span><span class="op" style="color: #5E5E5E;
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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/prod.html">prod</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/funprog.html">Reduce</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">check_max</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">totals</span><span class="op" style="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;">n</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>Note: I have excluded the roxygen tags from this solution.</p>
<p>Jonathan has gone for a <strong>very</strong> functional style, with tightly named variables. Looking at this I can imagine that, if you wanted to, you could write tests for all the pieces.</p>
<p>Ample use of the <code>apply</code> family is on show, and <code>strsplit</code>, which is not something I went for at all but makes perfect sense for the problem at hand.</p>
<p>He also did something that crossed my mind - whereas I looked at the data and chose to simply name my vector items from 1 to 100, Jonathan has used the actual values in the data <code>sub("Game ([0-9]*).*", "\\1", x)</code>. I like that. And I enjoy seeing the value from a regex capture group actually being utilised!</p>
<p>I get into a frenzy sometimes keeping objects in lists and <code>apply</code>ing / <code><a href="https://purrr.tidyverse.org/reference/map.html">purrr::map</a></code>ping over them. Sometimes I wonder if it obfuscates what is happening. I like the explicitness of code like this. It is repetitive but not overly so</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;">r1</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/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;">d1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d1</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">r2</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/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;">d2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d2</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">b1</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/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;">d1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d1</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">b2</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/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;">d2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d2</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">g1</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/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;">d1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d1</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="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;">g2</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/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;">d2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">d2</span><span 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>At the same time, I find the terse naming and highly functional approach more difficult to parse than procedural scripting.</p>
<p>Source <a href="https://github.com/jonocarroll/advent-of-code/blob/main/2023/R/R/day02.R"><i class="fa-brands fa-github" aria-label="github"></i></a></p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<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;">input</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/readLines.html">readLines</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;">"input.txt"</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;"># for each game, create a data frame structure</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">structure_game</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">sets</span><span class="op" style="color: #5E5E5E;
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;">count</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sets</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gregexpr</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;">"\\d+"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sets</span><span class="op" style="color: #5E5E5E;
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;">color</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sets</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gregexpr</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;">"[a-z]+"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sets</span><span class="op" style="color: #5E5E5E;
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;">set</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/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="op" style="color: #5E5E5E;
background-color: 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;">sets</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, times <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/lengths.html">lengths</a></span><span 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></span>
<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>set   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">set</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;"><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="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>
<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://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="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="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><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;"># combine all games and find the max cubes of each color per game</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sets</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/strsplit.html">strsplit</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/grep.html">gsub</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;">"Game \\d+: "</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;">input</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, split <span 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">games</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/setNames.html">setNames</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">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sets</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">structure_game</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/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;">sets</span><span class="op" style="color: #5E5E5E;
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;">game_df</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/do.call.html">do.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;">rbind</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">games</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;">game_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;">game</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/grep.html">gsub</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="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/colnames.html">rownames</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game_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>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_cubes</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/aggregate.html">aggregate</a></span><span 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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game_df</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>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># part 1</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">limit</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/data.frame.html">data.frame</a></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;"><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;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</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>
<span>                    max   <span class="op" style="color: #5E5E5E;
background-color: 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;">12</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">merged</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/merge.html">merge</a></span><span 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_cubes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">limit</span>, by <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</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;">merged</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">possible</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;">merged</span><span 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;">&lt;=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">merged</span><span 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>
<span></span>
<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;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/aggregate.html">aggregate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">possible</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">merged</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">all</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/sum.html">sum</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/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;">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;">game</span><span 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;">possible</span><span class="op" style="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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># part 2</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">powers</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/aggregate.html">aggregate</a></span><span 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_cubes</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">prod</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/sum.html">sum</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">powers</span><span 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></code></pre></div></div>
</div>
<p>Adam is procedurally scripting, and also has a named function whereas I went for anonymous.</p>
<p>The data structure of choice is the dataframe. Probably the most notable parts for me are <code>merge</code> and <code>aggregate</code>. I’m trying to improve my base R and there’s tons of stuff I’ve never used, including these two functions.</p>
<p><code>merge</code> is a join between two dataframes, with control for keeping rows in the x/y dataset/s.</p>
<p><code>aggregate</code> is super cool! It’s a bit like doing a group_by and summarise with dplyr.</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;">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="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/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;">tidyselect</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://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 class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<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;">Species</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>     Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1     setosa        5.006       3.428        1.462       0.246
2 versicolor        5.936       2.770        4.260       1.326
3  virginica        6.588       2.974        5.552       2.026</code></pre>
</div>
</div>
<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/stats/aggregate.html">aggregate</a></span><span 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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris</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="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>Species <span 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean</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>     Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1     setosa        5.006       3.428        1.462       0.246
2 versicolor        5.936       2.770        4.260       1.326
3  virginica        6.588       2.974        5.552       2.026</code></pre>
</div>
</div>
<p>You can also use formula notation, which is also not something I’m familiar with so will convert here as an example. Where Adam has this dataframe</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;"><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="va" style="color: #111111;
background-color: null;
font-style: inherit;">game_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>    set color count game
1.1   1   red     1    1
1.2   1  blue     3    1
1.3   1 green    11    1
1.4   2  blue     1    1
1.5   2   red     5    1
1.6   3  blue     3    1</code></pre>
</div>
</div>
<p>And does</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;">max_cubes</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/aggregate.html">aggregate</a></span><span 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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">game</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">game_df</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></code></pre></div></div>
</div>
<p>It makes this</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;"><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="va" style="color: #111111;
background-color: null;
font-style: inherit;">max_cubes</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>  color game count
1  blue    1     3
2 green    1    12
3   red    1    16
4  blue   10     1
5 green   10     7
6   red   10    10</code></pre>
</div>
</div>
<p>Which is the equivalent of doing this</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;">game_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;">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>    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://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;">count</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</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;"><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;">game</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>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>In part 2 of his answer Adam also uses the <code>prod</code> function, which multiplies together elements of a vector. It’s way better than something I considered doing, <code>Reduce(`*`, max_cubes)</code> 😅</p>
<p>Source <a href="https://github.com/ataustin/advent-of-code/blob/main/y2023/d02/solution.R"><i class="fa-brands fa-github" aria-label="github"></i></a></p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<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;"># part 1</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;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/read.table.html">read.table</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">puzzle_input</span>, header <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>, 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;">"\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;">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;">game</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/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/row.html">row</a></span><span 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>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">get_max</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">string</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pattern</span><span class="op" style="color: #5E5E5E;
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;">cubes</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/grep.html">gsub</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;">"[^0-9,]"</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">string</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gregexpr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pattern</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">string</span><span class="op" style="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/lapply.html">sapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cubes</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="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/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/unlist.html">unlist</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/strsplit.html">strsplit</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/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;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, split <span 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="op" style="color: #5E5E5E;
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;">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;">red_max</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;">get_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;">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;">V1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\\d+\\s+red\\b"</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;">green_max</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;">get_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;">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;">V1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\\d+\\s+green\\b"</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;">blue_max</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;">get_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;">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;">V1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\\d+\\s+blue\\b"</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/sum.html">sum</a></span><span 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="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;">red_max</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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;">&amp;</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;">green_max</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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;">&amp;</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;">blue_max</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"game"</span><span class="op" style="color: #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;"># part 2</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;">product</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;">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;">red_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;">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;">green_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;">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;">blue_max</span></span>
<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="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;">product</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>I really like this solution! Concise enough to be on the right side of golfed, and with just two objects created, this is still (to me) a very readable solution.</p>
<p>Like Austin, Ursula has a function but the code is otherwise procedural. And the data structure is a dataframe. So basically I’m the odd one out for having used vanilla lists 🙃</p>
<p>Again, I quite like the straightforward approach of defining a function and calling it three times, in contrast to my inclination of <code>apply</code>ing. You read the code and just know what is happening.</p>
<p>The <code>get_max</code> function taught me that <code>gsub</code> can operate on a list. I really didn’t expect that! It deserves an explanation. Here’s the line in question</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;">cubes</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/grep.html">gsub</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;">"[^0-9,]"</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">string</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gregexpr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pattern</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">string</span><span class="op" style="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>
<p>For the red cubes, this is what <code>regmatches(string, gregexpr(pattern, string))</code> returns</p>
<div class="cell">
<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;"># Just the first three items in the list...</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;"><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;">"1 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"13 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"16 red"</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2 red"</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12 red"</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>Then <code>gsub</code>, which is stripping out anything not a digit or comma, operates directly on the list and returns this</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;"><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;">"1,5,13,6,16"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3,3,3,2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5,3,1,1,10,12"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<p>Allow me to explain, because to me it wasn’t obvious where these commas were coming from in the first place. The answer is actually pretty simple: <code>gsub</code> converts non-string arguments to strings. It looks to me like it does so with <code>as.character</code>, so the regmatches return ends up looking like this before the subbing happens</p>
<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="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;"><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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"13 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"16 red"</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2 red"</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10 red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12 red"</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] "c(\"1 red\", \"5 red\", \"13 red\", \"6 red\", \"16 red\")"           
[2] "c(\"3 red\", \"3 red\", \"3 red\", \"2 red\")"                        
[3] "c(\"5 red\", \"3 red\", \"1 red\", \"1 red\", \"10 red\", \"12 red\")"</code></pre>
</div>
</div>
<p>I love it. Ursula is really showing how well she knows her R data types and their coercion!</p>
<p>Another thing to highlight for me is a difference between dataframes and tibbles. I’m used to the tidyverse, where subsetting a tibble with square brackets still returns a tibble</p>
<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="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris_tibbilicus</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;">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/as_tibble.html">as_tibble</a></span><span 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris_tibbilicus</span><span 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_tibbilicus</span><span 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="st" style="color: #20794D;
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># A tibble: 50 × 1
   Sepal.Length
          &lt;dbl&gt;
 1          5.1
 2          4.9
 3          4.7
 4          4.6
 5          5  
 6          5.4
 7          4.6
 8          5  
 9          4.4
10          4.9
# ℹ 40 more rows</code></pre>
</div>
</div>
<p>If I want that filtered Sepal.Length column as a vector I might do it like this</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">iris_tibbilicus</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="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;">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/pull.html">pull</a></span><span 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> [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7
[20] 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9
[39] 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0</code></pre>
</div>
</div>
<p>But with a vanilla dataframe you get a vector simply by specifying the column of interest. Like so</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="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;">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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"setosa"</span>, <span class="st" style="color: #20794D;
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> [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7
[20] 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9
[39] 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0</code></pre>
</div>
</div>
<p>Source <a href="https://gist.github.com/ursulams/34f51c4120fad10e38c21891a47fb612"><i class="fa-brands fa-github" aria-label="github"></i></a></p>
</div>
</div>
</div>


</section> ]]></description>
  <category>R</category>
  <category>base R</category>
  <category>Advent of Code</category>
  <category>Advent of Code 2023</category>
  <guid>https://jimgar.github.io/posts/advent-of-code-2023-day-2/post.html</guid>
  <pubDate>Sun, 10 Dec 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Day 1 - Advent of Code 2023</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/advent-of-code-2023/post.html</link>
  <description><![CDATA[ <p>I did AoC day 1 using base R.</p>
<p>Spoilers ahead. Also, while this code works, I don’t know how elegant it is or if there are better ways to do parts of it in base. If you have any tips or pointers I’d love to hear them!</p>
<section id="part-1" class="level1"><h1>Part 1</h1>
<section id="the-problem" class="level2"><h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>— Day 1: Trebuchet?! —</p>
<p>Something is wrong with global snow production, and you’ve been selected to take a look. The Elves have even given you a map; on it, they’ve used stars to mark the top fifty locations that are likely to be having problems.</p>
<p>You’ve been doing this long enough to know that to restore snow operations, you need to check all fifty stars by December 25th.</p>
<p>Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!</p>
<p>You try to ask why they can’t just use a weather machine (“not powerful enough”) and where they’re even sending you (“the sky”) and why your map looks mostly blank (“you sure ask a lot of questions”) and hang on did you just say the sky (“of course, where do you think snow comes from”) when you realize that the Elves are already loading you into a trebuchet (“please hold still, we need to strap you in”).</p>
<p>As they’re making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.</p>
<p>The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.</p>
<p>For example:</p>
<ul>
<li>1abc2</li>
<li>pqr3stu8vwx</li>
<li>a1b2c3d4e5f</li>
<li>treb7uchet</li>
</ul>
<p>In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.</p>
<p>Consider your entire calibration document. What is the sum of all of the calibration values?</p>
</section><section id="solution" class="level2"><h2 class="anchored" data-anchor-id="solution">Solution</h2>
<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;">lines</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/readLines.html">readLines</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;">"input.txt"</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;">lines</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "mxmkjvgsdzfhseightonetwoeight7" "3five4s84four9rtbzllggz"       
[3] "75sevenzdrpkv1onetwo"          </code></pre>
</div>
</div>
<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;">matches</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/grep.html">gregexpr</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>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;">"\\d"</span>, text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lines</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;">digits</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lines</span>, m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">digits</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] "7"

[[2]]
[1] "3" "4" "8" "4" "9"

[[3]]
[1] "7" "5" "1"</code></pre>
</div>
<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;">concatenated</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">digits</span>, \<span 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;">number_of_digits</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;">x</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;"># if only one number in a line it gets counted as both the</span></span>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first AND last number</span></span>
<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>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">number_of_digits</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 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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="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="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="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;">number_of_digits</span><span class="op" style="color: #5E5E5E;
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;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">concatenated</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] "77"

[[2]]
[1] "39"

[[3]]
[1] "71"</code></pre>
</div>
</div>
<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/sum.html">sum</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/integer.html">as.integer</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/unlist.html">unlist</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">concatenated</span><span class="op" style="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>[1] 55621</code></pre>
</div>
</div>
</section></section><section id="part-2" class="level1"><h1>Part 2</h1>
<section id="the-problem-1" class="level2"><h2 class="anchored" data-anchor-id="the-problem-1">The problem</h2>
<p>— Part Two —</p>
<p>Your calculation isn’t quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid “digits”.</p>
<p>Equipped with this new information, you now need to find the real first and last digit on each line. For example:</p>
<ul>
<li>two1nine</li>
<li>eightwothree</li>
<li>abcone2threexyz</li>
<li>xtwone3four</li>
<li>4nineeightseven2</li>
<li>zoneight234</li>
<li>7pqrstsixteen</li>
</ul>
<p>In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.</p>
<p>What is the sum of all of the calibration values?</p>
</section><section id="solution-1" class="level2"><h2 class="anchored" data-anchor-id="solution-1">Solution</h2>
<p>This is the point at which I cursed my commitment to doing this in base R. After a few hours I had to look up a hint, and in the process also checked for other solutions in R. Everyone else is just using stringr, dplyr, and/or tidyr. As a result they have reasonably simple regex and are mutating tibbles. I’m out here using functions I don’t fully understand, trying to understand lookarounds, lapplying my way over lists 🥲</p>
<p>Many of us who got stuck encountered the same issue: overlapping digits in strings. For example, the first two digits from <code>zqtwonethreekcz3seven2</code> are <code>two</code> and <code>one</code>. In this case it wouldn’t really matter, but without getting overlapping strings separated like this, it could make me count a string like <code>mwnineight</code> as just containing <code>nine</code>, and then I’d end up with the answer as <code>99</code>.</p>
<p>And unfortunately the example doesn’t mention this at all.</p>
<p>I managed to get a working regex on regex101.com but of course it didn’t work with <code>gregexpr</code>. Honestly, at this point I’m out of my depth, so just systematically worked through the small number of functions available to me seeing what looked like it worked.</p>
<p>And…</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expand the regex to include word versions and a positive lookahead assertion, to accommodate for overlapping matches</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pattern</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(?=(\\d)|(one)|(two)|(three)|(four)|(five)|(six)|(seven)|(eight)|(nine))"</span></span>
<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;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/grep.html">gregexec</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>pattern <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">pattern</span>, text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lines</span>, perl <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="va" style="color: #111111;
background-color: null;
font-style: inherit;">res</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/regmatches.html">regmatches</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lines</span>, m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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>
<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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">985</span><span class="op" 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]  [,2]  [,3]    [,4] [,5]    [,6]
 [1,] ""    ""    ""      ""   ""      ""  
 [2,] ""    ""    ""      "3"  ""      "2" 
 [3,] ""    "one" ""      ""   ""      ""  
 [4,] "two" ""    ""      ""   ""      ""  
 [5,] ""    ""    "three" ""   ""      ""  
 [6,] ""    ""    ""      ""   ""      ""  
 [7,] ""    ""    ""      ""   ""      ""  
 [8,] ""    ""    ""      ""   ""      ""  
 [9,] ""    ""    ""      ""   "seven" ""  
[10,] ""    ""    ""      ""   ""      ""  
[11,] ""    ""    ""      ""   ""      ""  </code></pre>
</div>
</div>
<p>The ffff??? a MATRIX?</p>
<p>Like most people who use R to work with data but never do statistics, I’ve never. Ever. Interacted with a matrix. I have a vague idea about how to index them.</p>
<p>But it turns out they’re easy to collapse and I also got to use one of my fave base functions, <code>nzchar</code>. It returns TRUE for a non-zero length ‘scalar’ character vector, which makes it excellent for logical subsetting.</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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collapse the results into just the matches</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">digits</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/lapply.html">lapply</a></span><span 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="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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nchar.html">nzchar</a></span><span 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>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">digits</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;">985</span><span class="op" 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] "two"   "one"   "three" "3"     "seven" "2"    </code></pre>
</div>
</div>
<p>From here it’s fairly straightforward.</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;">mapping</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;">stats</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/r/stats/setNames.html">setNames</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</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;">9</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"three"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"four"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"five"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"six"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seven"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eight"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nine"</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="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;">all_numeric</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">digits</span>, \<span 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;">mapped</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;">mapping</span><span 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>
<span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Backfill the NAs with the original numeric values</span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numeric_indices</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/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/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;">mapped</span><span class="op" style="color: #5E5E5E;
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;">mapped</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numeric_indices</span><span 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;">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;">numeric_indices</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;">mapped</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>
<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/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;">all_numeric</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/lengths.html">lengths</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">all_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="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>[1] 62</code></pre>
</div>
</div>
<p>There are still a small number of lines where there is only a single number, so I’ll stick with the same code from before… yes I am going to copy and paste considering this is so trivial.</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;">concatenated</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/lapply.html">lapply</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">all_numeric</span>, \<span 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;">number_of_digits</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;">x</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/ifelse.html">ifelse</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;">number_of_digits</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 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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="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="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="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;">number_of_digits</span><span class="op" style="color: #5E5E5E;
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;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">concatenated</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></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] "87"

[[2]]
[1] "39"

[[3]]
[1] "72"</code></pre>
</div>
</div>
<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;"><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="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="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="va" style="color: #111111;
background-color: null;
font-style: inherit;">concatenated</span><span class="op" style="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>[1] 53592</code></pre>
</div>
</div>


</section></section> ]]></description>
  <category>R</category>
  <category>Advent of Code</category>
  <category>Advent of Code 2023</category>
  <guid>https://jimgar.github.io/posts/advent-of-code-2023/post.html</guid>
  <pubDate>Mon, 04 Dec 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Making the terminal zsh-tosh on macOS</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/post.html</link>
  <description><![CDATA[ <div class="cell">
<style type="text/css">
.figure-caption {
    text-align: center;
}

.center {
    text-align: center;
}
</style>
</div>
<section id="overview" class="level2" data-number="1"><h2 data-number="1" class="anchored" data-anchor-id="overview">
<span class="header-section-number">1</span> Overview</h2>
<p>In this post I’m going to share a few tools that have made my zsh terminal experience on macOS a little bit nicer.</p>
<p>There’s two separate sets of tools. One is to <a href="https://github.com/ohmyzsh/ohmyzsh">Oh My Zsh</a> with a couple of plugins. The other are so-called modern versions of a couple of commonly used command line tools. The purpose of all these tools is to make the terminal colourful and easier to use.</p>
<p>All of the changes here will also apply to terminals in VS Code. If you’re an R programmer using RStudio, I’m <del>sorry, that’s not a nice thing people like us get to have :(</del> a fool! Of course this is possible - though not the default. See Section&nbsp;4 to see how to make it so.</p>
</section><section id="installation-quick-ref" class="level2" data-number="2"><h2 data-number="2" class="anchored" data-anchor-id="installation-quick-ref">
<span class="header-section-number">2</span> Installation quick ref</h2>
<p>Assuming you are putting installs in default locations</p>
<section id="zsh-stuff" class="level3" data-number="2.1"><h3 data-number="2.1" class="anchored" data-anchor-id="zsh-stuff">
<span class="header-section-number">2.1</span> zsh stuff</h3>
<ul>
<li>Install <a href="https://github.com/ohmyzsh/ohmyzsh#basic-installation">zsh</a> (if you need to)</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sh</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-c</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;">$(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">curl</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-fsSL</span> https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div></div>
</div>
<ul>
<li>Install <a href="https://github.com/zsh-users/zsh-autosuggestions">zsh-autosuggestions</a>
</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> clone https://github.com/zsh-users/zsh-autosuggestions <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ZSH_CUSTOM</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:-</span>~/.oh-my-zsh/custom<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span>/plugins/zsh-autosuggestions</span></code></pre></div></div>
</div>
<ul>
<li>Install <a href="https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/INSTALL.md">zsh-syntax-highlighting</a>
</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> clone https://github.com/zsh-users/zsh-syntax-highlighting.git</span>
<span id="cb3-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"source </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(q-)PWD</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ZDOTDIR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$HOME}</span>/.zshrc</span></code></pre></div></div>
</div>
<ul>
<li>Update zshrc</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the autosuggestions and highliting to the plugins variable like so</span></span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plugins</span><span 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>
<span id="cb4-4">   git</span>
<span id="cb4-5">   zsh-autosuggestions</span>
<span id="cb4-6">   zsh-syntax-highlighting</span>
<span id="cb4-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
</div>
<ul>
<li>Source</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> ~/.zshrc</span></code></pre></div></div>
</div>
</section><section id="cli-tools" class="level3" data-number="2.2"><h3 data-number="2.2" class="anchored" data-anchor-id="cli-tools">
<span class="header-section-number">2.2</span> cli tools</h3>
<p>Require <a href="https://brew.sh">homebrew</a></p>
<ul>
<li>Install <a href="https://github.com/eza-community/eza/blob/main/INSTALL.md#brew-macos">eza</a>
</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> install eza</span></code></pre></div></div>
</div>
<ul>
<li>Install <a href="https://github.com/sharkdp/bat#on-macos-or-linux-via-homebrew">bat</a>
</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" 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;">brew</span> install bat</span></code></pre></div></div>
</div>
<p><code>exit</code> your terminal session and start a new one.</p>
</section></section><section id="context" class="level2" data-number="3"><h2 data-number="3" class="anchored" data-anchor-id="context">
<span class="header-section-number">3</span> Context</h2>
<section id="zsh" class="level3" data-number="3.1"><h3 data-number="3.1" class="anchored" data-anchor-id="zsh">
<span class="header-section-number">3.1</span> zsh</h3>
<p>zsh has come as the default shell for mac’s terminal since October 2019, with the release of macOS Catalina.</p>
<p>The standard experience is perfectly fine, just like bash, but is rather bare bones. There’s no syntax highlighting even for the native shell scripting. In fact, everything is black-and-white by default. I’m not down on zsh or bash as they are. I just prefer some colour to help visually parse wtf I’m looking at.</p>
<p>If you’re unsure you have it, run the following to check. You should see something similar to my output.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zsh</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--version</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>zsh 5.9 (x86_64-apple-darwin23.0)</code></pre>
</div>
</div>
<p>Also run this to check that it’s your default shell. You should see the same output as I have here.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$SHELL</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>/bin/zsh</code></pre>
</div>
</div>
<p>If you get <code>command not found</code> or the default shell is something else (like bash), follow the instructions <a href="https://github.com/ohmyzsh/ohmyzsh/wiki/Installing-ZSH">here</a> to install zsh and make it your default.</p>
</section><section id="oh-my-zsh" class="level3" data-number="3.2"><h3 data-number="3.2" class="anchored" data-anchor-id="oh-my-zsh">
<span class="header-section-number">3.2</span> Oh My Zsh</h3>
<p><a href="https://github.com/ohmyzsh/ohmyzsh">Oh My Zsh</a> makes the terminal a bit more colourful and can show you things like which git branch you’re on. I’ve found it quite useful for that alone, and it is really easy to install, with a great default theme.</p>
<p>zsh has a config file, typically found at <code>~/.zshrc</code>. What Oh My Zsh does is essentially modify your .zshrc, putting some defaults in and setting things up to further configure as you see fit.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/zsh.png" class="img-fluid figure-img"></p>
<figcaption>Once installed, you will see something like this</figcaption></figure>
</div>
<p>It doesn’t look like a lot, true.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/zsh2.png" class="img-fluid figure-img"></p>
<figcaption>But once you actually start doing something…</figcaption></figure>
</div>
<p>You can see the shortened current directory, which git branch I’m on (“zsh”, that I’m working in for this blog post), and some colour when calling <code>ls -l</code>.</p>
<p>Oh My Zsh also has loads of <a href="https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins">plugins</a> to enhance functionality. Most are for working with common tools, languages and frameworks for developers. The plugins I’ve enabled are for syntax highlighting and autosuggestions. The syntax highlighting is nice because if I’m writing a little shell script, or piping a bunch of commands together, it helps me to more quickly visually parse the content. The autosuggestions are a bit like tab completion, but they use your execution history to autosuggest what might come next. This can be useful if you’re doing a repetitive workflow like making regular git commits, or moving back and forth between a small number of directories.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/syntax-highlighting.png" class="img-fluid figure-img"></p>
<figcaption>After adding the plugins to my zshrc using the <code>nano</code> editor, you can see there’s now syntax highlighting in this <code>for</code> loop.</figcaption></figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/autosuggestions.png" class="img-fluid figure-img"></p>
<figcaption>Here’s an example of an autosuggestion. If I hit the right arrow key it would accept the suggestion.</figcaption></figure>
</div>
</section><section id="bat-and-eza" class="level3" data-number="3.3"><h3 data-number="3.3" class="anchored" data-anchor-id="bat-and-eza">
<span class="header-section-number">3.3</span> bat and eza</h3>
<p>Both <a href="https://github.com/sharkdp/bat">bat</a> and <a href="https://github.com/eza-community/eza">eza</a> are “modern” versions of the classic command line tools <code>cat</code> and <code>ls</code>, respectively. The former is for printing the contents of files, and the latter is for listing files and directories.</p>
<p>bat is particularly nice for several reasons. Let’s look at the contents of my zshrc with cat first</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/cat.png" class="img-fluid figure-img"></p>
<figcaption>zshrc printed with cat</figcaption></figure>
</div>
<p>The output is single-colour, printed via stdout, in full. It’s literally just a dump of the file contents to the screen. Compare that to bat</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/bat.png" class="img-fluid figure-img"></p>
<figcaption>zshrc printed with bat</figcaption></figure>
</div>
<p>That’s more like it. We’ve got syntax highlighting, and the print is paginated a la <code>less</code>. There’s more though - if you have git it shows modifications in the sidebar, it can show all non-printable characters, concatenate files together, and when used in pipes will behave as a drop-in replacement for cat. So all around pretty awesome.</p>
<p>eza I like for two reasons.</p>
<p>First, yes, it’s colour again. You can see in the image below that the return values for <code>eza -l</code> are colourised in comparison to <code>ls -l</code>. And I always use the <code>-l</code> flag, so that matters to me.</p>
<p>Second is the tree flag <code>-T</code>. Sometimes a simple tree is all you need to see a project’s structure. The really nice thing about <code>-T</code> is you can combine it with <code>-L</code> (level), and specify a depth. For example if I only wanted to show stuff in the root of my current directory, I can do <code>eza -TL=1</code>. You may also have noticed that the trees are colourised 👀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/eza.png" class="img-fluid figure-img"></p>
<figcaption>Examples of using eza</figcaption></figure>
</div>
</section></section><section id="sec-rstudio" class="level2" data-number="4"><h2 data-number="4" class="anchored" data-anchor-id="sec-rstudio">
<span class="header-section-number">4</span> RStudio settings</h2>
<p>It’s really simple to make zsh the default terminal in RStudio.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/rstudio1.png" class="img-fluid figure-img"></p>
<figcaption>In the Terminal tab, click the down arrow next to <code>Terminal n</code>. Then click <code>Terminal Options</code></figcaption></figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/rstudio2.png" class="img-fluid figure-img"></p>
<figcaption>New terminals open with: zsh. Then click <code>Apply</code>, and <code>OK</code></figcaption></figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/images/rstudio3.png" class="img-fluid figure-img"></p>
<figcaption>Start a new terminal and there you go!</figcaption></figure>
</div>
</section><section id="conclusion" class="level2" data-number="5"><h2 data-number="5" class="anchored" data-anchor-id="conclusion">
<span class="header-section-number">5</span> Conclusion</h2>
<p>Go get some colour in your terminal ;)</p>


</section> ]]></description>
  <category>Terminal</category>
  <category>zsh</category>
  <category>Command Line</category>
  <category>macOS</category>
  <category>bat</category>
  <category>eza</category>
  <category>Oh My Zsh</category>
  <guid>https://jimgar.github.io/posts/zsh-tosh-pretty-terminal/post.html</guid>
  <pubDate>Wed, 11 Oct 2023 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Comfy keyboard shortcuts for RStudio</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/post.html</link>
  <description><![CDATA[ <div class="cell">
<style type="text/css">
.figure-caption {
    text-align: center;
}

.center {
    text-align: center;
}
</style>
</div>
<section id="intro" class="level2"><h2 class="anchored" data-anchor-id="intro">Intro</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/helvetica-found.png" class="img-fluid figure-img"></p>
<figcaption>Found, by <a href="https://helveticablanc.com">Helvetica Blanc</a></figcaption></figure>
</div>
<p>I know quite a few keyboard shortcuts in RStudio these days.</p>
<p>That said, such knowledge has been hard won. My background is not in programming, so a lot of the time I wasn’t aware that certain shortcuts were possible. Or if I did, I may not know how to describe them/their names.</p>
<p>Much of my learning has been done by bumbling around, giving it a go, and pairing with colleagues. That’s all good, but it is nice to have a practical list for reference.</p>
<p>So, with <a href="https://fosstodon.org/@jimgar/110832714377237658">help</a> from some good peeps over at mastodon, I’m happy to share just such a reference list. The aim is to help get you feeling confident, comfy, and fancy-free when coding in RStudio. It’s nice to feel at home in your IDE of choice ☺️</p>
<p>I’m a Mac user currently running RStudio 2023.06.1. Please let me know if there’s anything wrong with the Windows/Linux defaults listed.</p>
</section><section id="keyboard-shortcut-quick-reference" class="level2"><h2 class="anchored" data-anchor-id="keyboard-shortcut-quick-reference">Keyboard shortcut quick reference</h2>
<div class="center">
<p> : <code>Option</code> + <code>Shift</code> + <code>K</code></p>
</div>
<div class="center">
<p> /  : <code>Alt</code> + <code>Shift</code> + <code>K</code></p>
</div>
<p>Brings up the keyboard shortcut quick ref.</p>
<p>However, I can never remember the layout of the panel and get mentally overloaded seeing so many symbols and columns on screen at once. I prefer the <a href="https://support.posit.co/hc/en-us/articles/200711853-Keyboard-Shortcuts-in-the-RStudio-IDE">web version</a> instead (though it is a bit out of date).</p>
</section><section id="restart-r-session" class="level2"><h2 class="anchored" data-anchor-id="restart-r-session">Restart R session</h2>
<div class="center">
<p> : <code>Command</code> + <code>Shift</code> + <code>0</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Shift</code> + <code>F10</code></p>
</div>
<p>Generally, it’s a good habit to restart the R session regularly. If your global environment is a graveyard of variables and functions - and you can’t tell which are living or dead - this is what you need.</p>
</section><section id="executing-code" class="level2"><h2 class="anchored" data-anchor-id="executing-code">Executing code</h2>
<div class="center">
<p> : <code>Command</code> + <code>Enter</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Enter</code></p>
</div>
<p>Executes the line, or <strong>lines</strong>, your cursor is on.</p>
<p>Do you highlight an entire series of piped functions with your mouse and then click the <code>Run</code> button?</p>
<p>What if I told you that instead, you just put your cursor wherever the hell you like in a pipe and press <code>Command</code> + <code>Enter</code>? And it just runs the whole thing?</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/piped-highlight.gif" class="img-fluid figure-img"></p>
<figcaption>Behold!</figcaption></figure>
</div>
<p>You can even work your way through a script by hitting <code>Command</code> + <code>Enter</code> on repeat. Don’t trust me?</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/run-steps.gif" class="img-fluid figure-img"></p>
<figcaption>Try this for size.</figcaption></figure>
</div>
<p>That’s right: Even when the focus is in the console (which you can tell because the cursor is blinking there) you can continue hitting <code>Command</code> + <code>Enter</code> to run subsequent lines of your R script.</p>
</section><section id="switch-focus-between-sourceconsole" class="level2"><h2 class="anchored" data-anchor-id="switch-focus-between-sourceconsole">Switch focus between Source/Console</h2>
<div class="center">
<p> /  /  : <code>Your</code> + <code>choice!</code></p>
</div>
<p>Toggle the keyboard focus between your script and the console.</p>
<p>This is an unbound keyboard shortcut by default. While there are shortcuts to focus individually on the source and console panels, I like this one because it acts like a toggle. Easier to remember than two separate shortcuts. Also my brain small… like cute cat :)</p>
<p>Go to <code>Tools</code>, <code>Modify Keyboard Shortcuts...</code> and search for “focus”.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/switch-focus.gif" class="img-fluid figure-img"></p>
<figcaption>Click into the empty middle column for <code>Switch Focus Between Source/Console</code>. Press whatever shortcut keys you want to use, then hit enter. I used <code>Control</code> + <code>1</code>, but this was already assigned to <code>Move Focus to Source</code> so I had to delete it there too.</figcaption></figure>
</div>
</section><section id="indenting-code" class="level2"><h2 class="anchored" data-anchor-id="indenting-code">Indenting code</h2>
<div class="center">
<p> : <code>Command</code> + <code>I</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>I</code></p>
</div>
<p>Standardises indentation for highlighted code.</p>
<p>You’re typing, copying from the web, moving stuff about. I know it gets messy. Forget tab/space/backspace tedium and use this shortcut.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/indent.gif" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
</section><section id="commenting-crap" class="level2"><h2 class="anchored" data-anchor-id="commenting-crap">Commenting crap</h2>
<div class="center">
<p> : <code>Command</code> + <code>Shift</code> + <code>C</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Shift</code> + <code>C</code></p>
</div>
<p>Comment/uncomment the line your cursor is on, or highlighted lines. Trust me that this one is worth memorising. You’re going to be using it <strong>a lot</strong>.</p>
</section><section id="finding-crap" class="level2"><h2 class="anchored" data-anchor-id="finding-crap">Finding crap</h2>
<div class="center">
<p> : <code>Command</code> + <code>F</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>F</code></p>
</div>
<p>Brings up the find/replace menu for the script currently open. The <code>In selection</code> check box is very useful.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/find-replace.gif" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
<div class="center">
<p> : <code>Command</code> + <code>Shift</code> + <code>F</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Shift</code> + <code>F</code></p>
</div>
<p>Brings up the <code>Find in Files</code> dialogue box. I use this when it’s been a while since I worked on a project. For example, I might want to know which files a specific function gets used in.</p>
</section><section id="inserting-operators" class="level2"><h2 class="anchored" data-anchor-id="inserting-operators">Inserting operators</h2>
<div class="center">
<p> : <code>Option</code> + <code>-</code></p>
</div>
<div class="center">
<p> /  : <code>Alt</code> + <code>-</code></p>
</div>
<p>Drops in the assignment operator <code>&lt;-</code></p>
<div class="center">
<p> : <code>Command</code> + <code>Shift</code> + <code>M</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Shift</code> + <code>M</code></p>
</div>
<p>Drops in the pipe operator.</p>
<p>If you’ve had RStudio installed for a while the default might be the <code>magrittr</code> pipe <code>%&gt;%</code>. These days, the base R pipe <code>|&gt;</code> has got you covered for 99% of use cases, and doesn’t add yet another dependency or library call to your projects, so it is my preferred pipe.</p>
<p>If you want to make it the default, go to <code>Tools</code>, <code>Global Options</code>, and check the box as in the image below.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/pipe.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
</section><section id="using-multiple-caretscursors" class="level2"><h2 class="anchored" data-anchor-id="using-multiple-caretscursors">Using multiple carets/cursors</h2>
<p>These are for when you want to do stuff like ‘find and replace’, but the text you’re finding/adding/removing is heterogeneous in some way. You might be tidying up some lines of text after pasting them from a web page, for example.</p>
<div class="center">
<p> : <code>Control</code> + <code>Option</code> + <code>Up/Down</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Alt</code> + <code>Up/Down</code></p>
</div>
<p>Adds carets above/below your current position.</p>
<div class="center">
<p> : <code>Option</code> + <code>Click and drag</code></p>
</div>
<div class="center">
<p> /  : <code>Alt</code> + <code>Click and drag</code></p>
</div>
<p>Highlight text or place carets in contiguous chunks.</p>
<ul>
<li>Click/move your cursor into the first place you want a caret</li>
<li>Hold down <code>Option</code>
</li>
<li>Click and drag the cursor</li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/carets1.gif" class="img-fluid figure-img"></p>
<figcaption>In this example I add carets to multiple contiguous lines of text.</figcaption></figure>
</div>
<div class="center">
<p> : <code>Control</code> + <code>Option</code> + <code>Click</code></p>
</div>
<div class="center">
<p> /  : <code>Control</code> + <code>Alt</code> + <code>Click</code></p>
</div>
<p>This is for when things have started to get a little bit messier and you can’t easily highlight text in contiguous chunks.</p>
<ul>
<li>Click/move your cursor into the first place you want a caret</li>
<li>Then hold down <code>Control</code> + <code>Option</code>
</li>
<li>Each mouse click will place another caret wherever your mouse is hovering</li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/carets2.gif" class="img-fluid figure-img"></p>
<figcaption>In this example I delete several lines across a few blocks of text.</figcaption></figure>
</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 my experience the terms “caret” and “cursor” are are synonymous in programming IDEs like RStudio. However, some IDEs will use “caret” and others “cursor”. One to be aware of.</p>
</div>
</div>
</section><section id="moving-the-cursor" class="level2"><h2 class="anchored" data-anchor-id="moving-the-cursor">Moving the cursor</h2>
<div class="center">
<p> : <code>Command</code> + <code>Up/Down/Left/Right</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Up/Down/Left/Right</code></p>
</div>
<p>The up/down keys get you to the top/bottom of a document.</p>
<p>The left/right keys get you to the start/end of the current line.</p>
<div class="center">
<p> : <code>Option</code> + <code>Left/Right</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Left/Right</code></p>
</div>
<p>Skip over whole words. Whee!</p>
</section><section id="highlighting-and-deleting-text" class="level2"><h2 class="anchored" data-anchor-id="highlighting-and-deleting-text">Highlighting and deleting text</h2>
<div class="center">
<p> : <code>Option</code> + <code>Shift</code> + <code>Left/Right</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Shift</code> + <code>Left/Right</code></p>
</div>
<p>Highlights whole words. Oooh 😮</p>
<div class="center">
<p> : <code>Option</code> + <code>Backspace</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Backspace</code></p>
</div>
<p>Deletes the word preceding the cursor position. Cya 🙋</p>
</section><section id="summon-the-command-palette" class="level2"><h2 class="anchored" data-anchor-id="summon-the-command-palette">Summon the command palette 👹</h2>
<div class="center">
<p> : <code>Control</code> + <code>Shift</code> + <code>P</code></p>
</div>
<div class="center">
<p> /  : <code>Ctrl</code> + <code>Shift</code> + <code>P</code></p>
</div>
<p>Bringing up the command palette lets you search for commands (and settings) by name. Here I bring up the palette because I can never remember the shortcut to turn some text into a function.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/command-palette.gif" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
</section><section id="enclosing" class="level2"><h2 class="anchored" data-anchor-id="enclosing">Enclosing</h2>
<p>Highlight some text and enclose it with e.g.&nbsp;parentheses <code>()</code> or quotes <code>""</code>.</p>
<p>If you highlight some text, you only need to give the opening character of the enclosing pair. RStudio will add the closing character.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/images/enclosing.gif" class="img-fluid figure-img"></p>
<figcaption>Using just <code>"</code>, <code>(</code>, and <code>[</code> to enclose some text.</figcaption></figure>
</div>


</section> ]]></description>
  <category>R</category>
  <category>RStudio</category>
  <guid>https://jimgar.github.io/posts/rstudio-keyboard-shortcuts/post.html</guid>
  <pubDate>Thu, 03 Aug 2023 23:00:00 GMT</pubDate>
</item>
<item>
  <title>Publishing content in Quarto using conditionals and if/else code flow</title>
  <dc:creator>Jim Gardner</dc:creator>
  <link>https://jimgar.github.io/posts/quarto-code-conditionals/post.html</link>
  <description><![CDATA[ <div class="cell">
<style type="text/css">
.figure-caption {
    text-align: center;
}
</style>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/quarto-code-conditionals/images/damsel1.png" class="img-fluid figure-img" style="width:80.0%" alt="A cropped photograph of the eyes and surrounding fur of a cat's face. The cat is a chimera with green eyes, the left side of her face coloured black and the right side orange. The cat's name is Damsel."></p>
<figcaption>Damsel the chimera. Copyright Andy Stacey.</figcaption></figure>
</div>
<p>In this post I will describe some of the ways I’ve found to conditionally include content in a Quarto-generate HTML document.</p>
<p>I’m using RStudio and the knitr rendering engine. If you know of Jupyter solutions please let me know! I will happily link to them for the disappointed Pythonistas who have ended up on my blog ;)</p>
<section id="quarto" class="level1"><h1>Quarto</h1>
<section id="the-shortcode" class="level2"><h2 class="anchored" data-anchor-id="the-shortcode">The {{&lt; include &gt;}} shortcode</h2>
<p>This isn’t conditional, but it is really useful.</p>
<p>If you have company marketing boilerplate, or headers that start all of your reports, a neat solution is to keep these in their own qmd file and bring them into any other qmd using the <a href="https://quarto.org/docs/authoring/includes.html">include shortcode</a>.</p>
<p>In your report you would use the code below</p>
<pre data-shortcodes="false"><code>{{&lt; include _content.qmd &gt;}}</code></pre>
<p>This is akin to manually copying and pasting in the contents of <code>content.qmd</code> prior to the report getting rendered.</p>
<p>We’ll come back to the shortcode later.</p>
</section><section id="conditional-content" class="level2"><h2 class="anchored" data-anchor-id="conditional-content">Conditional content</h2>
<p>To my mind this one unfairly hogs the “conditional” title.</p>
<p>This is for when you have parts of a document that you only want rendered (or not) to a specific <strong>format</strong>, such as HTML, PDF, etc.</p>
<p>If you’re interested in this, here’s the <a href="https://quarto.org/docs/authoring/conditional.html">Quarto guide</a>.</p>
</section><section id="project-profiles" class="level2"><h2 class="anchored" data-anchor-id="project-profiles">Project profiles</h2>
<p>In this scenario you will have content to be included (or excluded) based on the profile/s specified. The div below would only be included if the “advanced” profile is in use</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb2-1">::: {.content-visible when-profile="advanced"}</span>
<span id="cb2-2">This content will only appear in the advanced version.</span>
<span id="cb2-3">:::</span></code></pre></div></div>
<p>You can also specify configurations in a profile-specific YAML that can be fully, or selectively used, to override those in the default profile. Here are <a href="https://quarto.org/docs/projects/profiles.html">the docs.</a></p>
</section></section><section id="code-flow" class="level1"><h1>Code flow</h1>
<p>Most of the following examples follow the usual if/else structure.</p>
<p>I’ll be referring to <code>params$my_boolean</code> throughout this section so let me take a moment to explain.</p>
<p>In the YAML header of a Quarto document you can pass in parameters. These get stored in a list <code>params</code> that can be called throughout the document, just like any variables you make and keep in the global environment.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb3-2"><span class="an" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">params:</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  my_boolean: FALSE</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div></div>
<p>Reports made this way are known as <a href="https://quarto.org/docs/computations/parameters.html">parameterised reports</a>.</p>
<p>If you’re using a Quarto project it is also possible to include variables in a root directory file <code>_variables.yml</code>. I’m not doing that here, but if it sounds appropriate for you, <a href="https://quarto.org/docs/authoring/variables.html">here are the docs</a>.</p>
<section id="inline-code" class="level2"><h2 class="anchored" data-anchor-id="inline-code">Inline code</h2>
<p>You can conditionally execute inline code. If <code>my_boolean</code> is <code>TRUE</code> the example below results in a level 2 heading that says “Plots for mtcars”.</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">## Plots `r if (params$my_boolean) "for mtcars" else "for iris"`</span></span></code></pre></div></div>
</section><section id="in-functions" class="level2"><h2 class="anchored" data-anchor-id="in-functions">In functions</h2>
<p>Sometimes you just need a bit of perspective. Rather than something convoluted, do you just need to make a function with flow control of some kind?</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">plotting_func</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">dat</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col2</span><span class="op" style="color: #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="va" style="color: #111111;
background-color: null;
font-style: inherit;">params</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">my_boolean</span><span class="op" style="color: #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;">dat</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;">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="op" style="color: #5E5E5E;
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="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="op" style="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;">col1</span> <span class="op" style="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;">col2</span> <span class="op" style="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>
<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>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dat</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;">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="op" style="color: #5E5E5E;
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_boxplot.html">geom_boxplot</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="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="op" style="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;">col1</span> <span class="op" style="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;">col2</span> <span class="op" style="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>
<span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>  </span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Or if the content is amenable to it, something like this</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="va" style="color: #111111;
background-color: null;
font-style: inherit;">plotting_func</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</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;">dat</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col1</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">col2</span><span class="op" style="color: #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;">dat</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;">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="op" style="color: #5E5E5E;
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;">params</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">my_boolean</span><span class="op" style="color: #5E5E5E;
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="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="op" style="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;">col1</span> <span class="op" style="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;">col2</span> <span class="op" style="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="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;">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_boxplot.html">geom_boxplot</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="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="op" style="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;">col1</span> <span class="op" style="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;">col2</span> <span class="op" style="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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span>  </span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>You could put the function into a code block. Or you could hide it away in a folder <code>R/</code> and a script <code>functions.R</code>. That’s useful if you write long ugly functions like me (lol), but also if you have multiple functions. In this case, you could just source the functions silently along with library calls at the start of the Quarto doc.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb7-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| include: false</span></span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(downloadthis)</span>
<span id="cb7-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(reactable)</span>
<span id="cb7-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"functions.R"</span>))</span>
<span id="cb7-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
<p>If your content is too complicated for flow control inside of a function, then just put the content into separate functions in <code>functions.R</code>, <code>source</code> them as above, and use flow control in the Quarto doc</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb8-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb8-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (params<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>my_boolean) {</span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fancy_model_a</span>(mtcars, wt, mpg) </span>
<span id="cb8-4">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fancy_model_b</span>(iris, Species, Sepal.Length)</span>
<span id="cb8-6">}</span>
<span id="cb8-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</section><section id="knitrknit_child" class="level2"><h2 class="anchored" data-anchor-id="knitrknit_child">knitr::knit_child</h2>
<p>Saving the best for last, because this one is really cool!</p>
<p>Remember how I started with the <code>{{&lt; include &gt;}}</code> shortcode and said that it wasn’t conditional?</p>
<pre data-shortcodes="false"><code>{{&lt; include _content.qmd &gt;}}</code></pre>
<p>If anyone knows how to (simply) use the shortcode in a conditional manner I’d love to hear about it. In the meantime I consider the conditional version of this is using <code><a href="https://rdrr.io/pkg/knitr/man/knit_child.html">knitr::knit_child</a></code>.</p>
<p>Let’s say I make interactive plots of my data using Observable code blocks and keep this code in a separate Quarto doc <code>ojs-cells.qmd</code>. The code can be added to the main Quarto doc under certain conditions, as dictated by a boolean param <code>ojs_include</code> in the YAML header</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb10-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| output: asis</span></span>
<span id="cb10-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (params<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ojs_include) { </span>
<span id="cb10-4">  </span>
<span id="cb10-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb10-6">      knitr<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;">knit_child</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb10-7">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> readr<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;">read_file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ojs-cells.qmd"</span>)</span>
<span id="cb10-8">      )</span>
<span id="cb10-9">  )</span>
<span id="cb10-10">  </span>
<span id="cb10-11">}</span>
<span id="cb10-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
<p>What’s happening here is</p>
<ol type="1">
<li>
<code><a href="https://readr.tidyverse.org/reference/read_file.html">readr::read_file</a></code> brings in <code>ojs-cells.qmd</code> as a string</li>
<li>
<code>cat</code> restores the line-by-line formatting and ‘prints’ the content</li>
<li>The <code>output: asis</code> chunk option means Quarto treats the content as raw markdown output</li>
</ol>
<p>Worth a mention - though not about conditionality - is this <a href="https://mickael.canouil.fr/posts/2023-03-05-quarto-auto-table-crossref/">demo</a> by Mickaël Canouil. It shows how the <code>knit_child</code> method can be called in a for loop to create <em>n</em> code chunks, and therefore <em>n</em> outputs (like tables and plots). This is great when you don’t know how many plots there will be or even what they are called.</p>
</section></section><section id="summary" class="level1"><h1>Summary</h1>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://jimgar.github.io/posts/quarto-code-conditionals/images/damsel2.JPG" class="img-fluid figure-img" style="width:80.0%" alt="A blurry, close-up photograph of a cat's face, taken unflatteringly from below. Her mouth is slightly ajar and the overall look is goofy. The cat is a chimera with green eyes, the left side of her face coloured black and the right side orange. The cat's name is Damsel."></p>
<figcaption>Damsel the chimera. Copyright me!</figcaption></figure>
</div>
<p>I hope these approaches give you a practical set of tools for including conditional content in Quarto-published HTML documents.</p>
<p>You can use</p>
<ol type="1">
<li><p>The shortcode <code>{{&lt; include &gt;}}</code> to bring in a sub-document</p></li>
<li><p>Publish content dependent on output format using “conditional content”</p></li>
<li>
<p>if/else statements either inline or in code chunks. This can be combined with</p>
<ol type="1">
<li>Functional programming by putting code into functions - either in code blocks in the document, or in a separate sourced file</li>
<li>With knitr’s <code>knit_child</code> to bring in entire sub-documents as required</li>
</ol>
</li>
</ol>
<p>Thank you to <a href="brodrigues.co">Bruno Rodrigues</a> and <a href="johnmackintosh.net">John MacKintosh</a> for proofreading a draft of this post.</p>


</section> ]]></description>
  <category>Quarto</category>
  <category>R</category>
  <category>knitr</category>
  <guid>https://jimgar.github.io/posts/quarto-code-conditionals/post.html</guid>
  <pubDate>Sun, 23 Apr 2023 23:00:00 GMT</pubDate>
</item>
</channel>
</rss>
