<?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>Data Science Bytes</title>
<link>https://www.javierorracadeatcu.com/blog.html</link>
<atom:link href="https://www.javierorracadeatcu.com/blog.xml" rel="self" type="application/rss+xml"/>
<description>Javier Orraca-Deatcu&#39;s data science website built with R and Quarto</description>
<generator>quarto-1.9.35</generator>
<lastBuildDate>Sun, 15 Mar 2026 07:00:00 GMT</lastBuildDate>
<item>
  <title>Three Open Source Tools to Bookmark</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2026-03-15-open-source-tools-data-ai/</link>
  <description><![CDATA[ 





<section id="the-open-source-data-ai-tooling-ecosystem-is-having-a-moment" class="level1">
<h1>The Open-Source Data + AI Tooling Ecosystem Is Having a Moment</h1>
<p>Every few weeks, a handful of tools land in my bookmarks at the same time and I realize they’re all pointing at the same underlying shift. These three projects - all open-source, all practitioner-first - all push back against the idea that good infrastructure has to be complicated or cloud-hosted.</p>
<p>Here’s what I’ve been digging into.</p>
<section id="msgvault-your-email-archive-powered-by-duckdb" class="level2">
<h2 class="anchored" data-anchor-id="msgvault-your-email-archive-powered-by-duckdb">msgvault: Your Email Archive, Powered by DuckDB</h2>
<p>I’ve spent more time than I’d like to admit trying to search my own email. Gmail’s built-in search is fine for recent messages, but try querying three years of threads about a specific project and you’ll quickly hit its limits.</p>
<p><a href="https://www.msgvault.io/" target="_blank">msgvault</a>, developed by Wes McKinney (the creator of Python’s pandas!), is a local email archiving tool that syncs your Gmail (with IMAP support coming) to your machine - raw MIME, labels, attachments and all - and then lets you search it via a terminal UI, a REST API, or an MCP server for AI assistant integration. Under the hood it uses <strong>SQLite FTS5</strong> for full-text search and <strong>DuckDB over Parquet</strong> for analytics, which the project claims is “hundreds of times faster than SQL JOINs” for aggregate queries over large archives.</p>
<p>The MCP angle is the part I find most interesting. Plugging your complete email history into a local AI assistant context - without shipping any of it to a third-party API - is exactly the kind of privacy-preserving workflow I want to see more of. Installation is a single <code>curl</code> command, and it’s MIT-licensed and open-source on GitHub.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>DuckDB + Parquet keeps showing up everywhere
</div>
</div>
<div class="callout-body-container callout-body">
<p>msgvault, Arrow-backed data frames, DuckLake (see below) - if you haven’t made DuckDB a core part of your analytics stack yet, 2026 is the year.</p>
</div>
</div>
</section>
<section id="rtk-rust-token-killer-stop-feeding-your-ai-agent-noise" class="level2">
<h2 class="anchored" data-anchor-id="rtk-rust-token-killer-stop-feeding-your-ai-agent-noise">RTK (Rust Token Killer): Stop Feeding Your AI Agent Noise</h2>
<p>If you’re running AI coding assistants like Claude Code, Cursor, or Codex on the regular, you’ve probably hit the wall where a long session starts to degrade because the context window fills up with verbose CLI output. <code>git status</code> alone can dump hundreds of lines. Multiply that by a full dev workflow and you’re paying for a lot of tokens that aren’t helping anyone reason about your code.</p>
<p><a href="https://www.rtk-ai.app/" target="_blank">RTK</a> is a Rust-built CLI proxy that intercepts command output before it hits your AI agent’s context window and compresses it. The project reports <strong>89% average noise reduction</strong> across 2,900+ real commands, with one user clocking 138 million tokens saved. On pay-per-token models, the project estimates up to 70% of your bill is “noise the LLM doesn’t need” - that resonates with my own intuition after months of Claude Code-heavy development.</p>
<p>It works via a <code>PreToolUse</code> hook (<code>rtk init --global</code>), and supports git, cargo, npm, docker, kubectl filtering out of the box. A <code>rtk gain</code> dashboard shows you exactly how much you’re saving. I’m planning to try it against some of my data pipeline workflows where <code>dbt</code> logs get verbose fast.</p>
</section>
<section id="ducklake-r-versioned-data-lakes-in-r-built-on-duckdb" class="level2">
<h2 class="anchored" data-anchor-id="ducklake-r-versioned-data-lakes-in-r-built-on-duckdb">ducklake-r: Versioned Data Lakes in R, Built on DuckDB</h2>
<p>If you work in regulated industries or on any project where reproducibility matters - and really, what data science project <em>doesn’t</em> benefit from reproducibility - you’ve probably cobbled together some version of a “save a dated CSV” workflow and felt vaguely uncomfortable about it.</p>
<p><a href="https://tgerke.github.io/ducklake-r/" target="_blank">ducklake-r</a>, maintained by Travis Gerke, is an experimental R package that brings proper versioned data lake infrastructure into your R workflow. It’s built on DuckDB and implements ACID transactions, automatic change tracking, medallion architecture (bronze/silver/gold layers), and time travel queries - all with <code>dplyr</code>-compatible syntax.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ducklake)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Write to a bronze layer with author attribution</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with_transaction</span>(con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">author =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"javier"</span>, {</span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace_table</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"raw_claims"</span>, new_data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layer =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bronze"</span>)</span>
<span id="cb1-6">})</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Time-travel: query the table as it was last Tuesday</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_ducklake_table_version</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"raw_claims"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">as_of =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-03-08"</span>)</span></code></pre></div></div>
</div>
<p>The combination of <code>with_transaction()</code> for authorship and <code>list_table_snapshots()</code> for auditing gives you the kind of audit trail that’s genuinely useful for collaborative work - or for the moment six months from now when someone asks “what changed between these two model runs?”</p>
<p>It’s early-stage (experimental lifecycle), but the design is thoughtful and Travis’s documentation is excellent. Worth watching closely if you do any serious data pipeline work in R.</p>
</section>
<section id="the-common-thread" class="level2">
<h2 class="anchored" data-anchor-id="the-common-thread">The Common Thread</h2>
<p>What strikes me about all three of these tools is that they’re not selling you a platform - they’re handing you back control. Your email. Your token budget. Your data history. The best open-source tooling right now is quietly making it easier to own your own stack, keep your data local, and work more intentionally with AI agents rather than just throwing more compute at the problem.</p>
<p>These are exactly the kinds of projects I want to highlight here more regularly. If you’re building something in this spirit, drop me a note - I’d love to hear about it.</p>
</section>
<section id="learn-more" class="level2">
<h2 class="anchored" data-anchor-id="learn-more">Learn More</h2>
<ul>
<li><a href="https://www.msgvault.io/" target="_blank">msgvault</a> - local email archive with DuckDB analytics and MCP integration</li>
<li><a href="https://www.rtk-ai.app/" target="_blank">RTK (Rust Token Killer)</a> - CLI proxy for AI agent token compression</li>
<li><a href="https://tgerke.github.io/ducklake-r/" target="_blank">ducklake-r</a> - versioned R data lake package by Travis Gerke</li>
</ul>
<hr>
<p>Happy Sunday, and happy coding!</p>


</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>duckdb</category>
  <category>r</category>
  <category>arrow</category>
  <guid>https://www.javierorracadeatcu.com/posts/2026-03-15-open-source-tools-data-ai/</guid>
  <pubDate>Sun, 15 Mar 2026 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2026-03-15-open-source-tools-data-ai/rtkgain02.png" medium="image" type="image/png" height="88" width="144"/>
</item>
<item>
  <title>Intro to Tidymodels</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2025-03-24-intro-to-tidymodels/</link>
  <description><![CDATA[ 





<p><img src="https://www.javierorracadeatcu.com/posts/2025-03-24-intro-to-tidymodels/intro_to_tidymodels.png" class="preview-image img-fluid"></p>
<section id="about-tidymodels" class="level1">
<h1>About Tidymodels</h1>
<p>The <a href="https://tidymodels.org/">tidymodels</a> framework for R is a collection of packages that brings tidy principles and a unified syntax to machine learning (“ML”) for R programmers, enabling end-to-end reproducibility for your ML workflows. I’ve been using this framework for five years and it continues to improve. Posit PBC funds a software engineering team dedicated to the development of this framework so its packages are feature-rich, regularly maintained, and current with ML trends. For Python users unfamiliar with R tools, the <code>tidymodels</code> framework is very similar to Python’s <code>scikit-learn</code>.</p>
<p>The core <code>tidymodels</code> packages include the following:</p>
<ul>
<li><p><a href="https://rsample.tidymodels.org/">rsample</a>: provides infrastructure for efficient data splitting and resampling</p></li>
<li><p><a href="https://parsnip.tidymodels.org/">parsnip</a>: a tidy, unified interface to models that can be used to try a range of models without getting bogged down in the syntactical minutiae of the underlying packages</p></li>
<li><p><a href="https://recipes.tidymodels.org/">recipes</a>: a tidy interface to data pre-processing tools for feature engineering</p></li>
<li><p><a href="https://workflows.tidymodels.org/">workflows</a>: expands the traditional model-only recipe to a much more holistic blueprint for pre-processing, modeling, post-processing, and evaluation</p></li>
<li><p><a href="https://dials.tidymodels.org/">dials</a>: creates and manages tuning parameters and parameter grids</p></li>
<li><p><a href="https://tune.tidymodels.org/">tune</a>: helps you optimize the hyperparameters of your model and pre-processing steps</p></li>
<li><p><a href="https://yardstick.tidymodels.org/">yardstick</a>: measures the effectiveness of models using performance metrics</p></li>
<li><p><a href="https://broom.tidymodels.org/">broom</a>: converts the information in common statistical R objects into user-friendly, predictable formats</p></li>
</ul>
<p>I was thrilled to present about <code>tidymodels</code> last week to the <em>Department of Mathematics, Statistics and Data Science</em> at Loyola Marymount University. Their students and faculty were engaging and I had a great time covering a logistic regression problem with this framework.</p>
</section>
<section id="other-tools-explored" class="level1">
<h1>Other Tools Explored</h1>
<ul>
<li><a href="https://positron.posit.co/">Positron</a>: A fresh, open-source coding environment purpose-built for data analysis and modeling, including all the best bells and whistles from VS Code and RStudio.</li>
</ul>
</section>
<section id="embedded-presentation" class="level1">
<h1>Embedded Presentation</h1>
<iframe width="780" height="500" src="intro_to_tidymodels.html" title="Intro to Tidymodels">
</iframe>
<ul>
<li>Fullscreen web slides: <a href="http://rrr.is/IntroToTidymodels">Intro to Tidymodels</a></li>
<li>GitHub repo: <a href="https://github.com/JavOrraca/intro-to-tidymodels/">JavOrraca/intro-to-tidymodels</a></li>
</ul>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>tidymodels</category>
  <category>positron</category>
  <guid>https://www.javierorracadeatcu.com/posts/2025-03-24-intro-to-tidymodels/</guid>
  <pubDate>Mon, 24 Mar 2025 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2025-03-24-intro-to-tidymodels/intro_to_tidymodels.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Vector DB + RAG Maker</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2025-03-07-vector-db-and-rags/</link>
  <description><![CDATA[ 





<p><img src="https://www.javierorracadeatcu.com/posts/2025-03-07-vector-db-and-rags/retro_cartoon_robot.jpg" class="preview-image img-fluid"></p>
<section id="boosting-llm-performance-with-vector-db-rag-systems" class="level1">
<h1>Boosting LLM Performance with Vector DB + RAG Systems</h1>
<p>If you’re looking to create your own knowledge base from technical documentation, check out my <a href="https://github.com/JavOrraca/Vector-DB-and-RAG-Maker" target="_blank">newly released GitHub project</a>. My Vector DB and RAG Maker tool offers a straightforward yet powerful method to process, store, and query documentation using vector embeddings and Retrieval-Augmented Generation (“RAG”). In my example, I apply it to recent R documentation and tutorials, making it an ideal solution for individuals and enterprises alike.</p>
</section>
<section id="benefits-of-vector-db-rag-vs-direct-llm-querying" class="level1">
<h1>Benefits of Vector DB + RAG vs Direct LLM Querying</h1>
<p>Let’s say you’re working with specialized documentation (e.g., 30 books on the topics of the R programming language, applied statistics, and machine learning) and you wish your LLM responses had access to this curated information… The benefits of using a vector database with RAG, instead of directly querying an LLM, are substantial:</p>
<section id="significantly-lower-api-costs" class="level3">
<h3 class="anchored" data-anchor-id="significantly-lower-api-costs">1. Significantly Lower API Costs</h3>
<p>Direct LLM queries with large context windows can get expensive, quickly, especially when working with extensive documentation. The vector DB approach only sends the most relevant chunks to the LLM, drastically reducing token usage and API costs.</p>
</section>
<section id="superior-query-performance" class="level3">
<h3 class="anchored" data-anchor-id="superior-query-performance">2. Superior Query Performance</h3>
<p>RAG systems deliver faster responses given that:</p>
<ul>
<li>The vector search quickly identifies the most relevant content</li>
<li>The LLM only processes small, highly relevant chunks rather than entire documents</li>
<li>The system caches embeddings, making subsequent similar queries even faster</li>
</ul>
</section>
<section id="scalability" class="level3">
<h3 class="anchored" data-anchor-id="scalability">3. Scalability</h3>
<p>As your collection grows (imagine adding more specialized packages or books to your library), a vector DB scales gracefully. The search performance remains consistent whether you have 3 books or 300, while direct LLM approaches would require increasingly larger context windows.</p>
</section>
<section id="higher-accuracy-for-domain-specific-questions" class="level3">
<h3 class="anchored" data-anchor-id="higher-accuracy-for-domain-specific-questions">4. Higher Accuracy for Domain-Specific Questions</h3>
<p>By retrieving specific, relevant context from your documentation, RAG systems provide more precise answers tailored to your exact versions and implementations rather than the LLM’s general knowledge.</p>
</section>
</section>
<section id="try-it-out" class="level1">
<h1>Try It Out!</h1>
<p>I’ve designed this system to be as straightforward as possible to setup and use. You can have it running with your own documentation in just a few minutes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> clone JavOrraca/Vector-DB-and-RAG-Maker</span>
<span id="cb1-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> Vector-DB-and-RAG-Maker</span>
<span id="cb1-3"><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;">-r</span> requirements.txt</span></code></pre></div></div>
<p>After setting your API key, you’re ready to ingest your R related files and start querying. Check out the full project on GitHub to see how you can transform your technical documentation into an intelligent knowledge base that provides bleeding-edge responses based on your exact resources.</p>
<ul>
<li>Vector DB and RAG Maker: <a href="https://github.com/JavOrraca/Vector-DB-and-RAG-Maker" target="_blank">JavOrraca/Vector-DB-and-RAG-Maker</a></li>
</ul>
</section>
<section id="about-this-tool" class="level1">
<h1>About this Tool</h1>
<p>This tool was built in approximately 90 minutes with Anthropic’s <a href="https://www.anthropic.com/news/claude-3-7-sonnet">Claude 3.7 Sonnet and Claude Code</a>. My personal computer is a 2023 MacBook Air, so nothing fancy. I interacted with Claude Code via the macOS Terminal and I incurred $2.28 in API calls to build this entire tool. I consider this first draft of the tool a huge success given the low amount of effort to develop. Please feel free to reach out to me directly on LinkedIn or file a GitHub Issue if you encounter a bug or have feature requests.</p>
<p>The code for this tool was written in Python and makes use of <a href="https://www.langchain.com/">LangChain</a>, <a href="https://huggingface.co/">Hugging Face</a>, <a href="https://www.trychroma.com/">Chroma</a> (for the vector database), and the CLI tool expects you to supply an Anthropic API key in order to hit Claude 3.7 Sonnet as the external LLM. If you’re not quite set up yet for Anthropic API billing, here is some information on <a href="https://www.anthropic.com/pricing#anthropic-api">Anthropic API pricing</a> and an <a href="https://support.anthropic.com/en/articles/8977456-how-do-i-pay-for-my-api-usage">FAQ on their API billing</a>.</p>
<p>Perhaps fine-tuning an LLM is the better long-term solution. There are similar tools out there in the wild, this one is just particularly easy for me to follow and hopefully the same for other R and Python programmers. Eventually, I’d like to expand this tool as a general purpose vector DB + RAG maker, but for now, happy Friday, and happy coding!</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>python</category>
  <category>langchain</category>
  <category>huggingface</category>
  <category>chromadb</category>
  <guid>https://www.javierorracadeatcu.com/posts/2025-03-07-vector-db-and-rags/</guid>
  <pubDate>Fri, 07 Mar 2025 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2025-03-07-vector-db-and-rags/retro_cartoon_robot.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Big Data, Tiny Laptop</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2025-01-30-big-data-tiny-laptop/</link>
  <description><![CDATA[ 





<p><img src="https://www.javierorracadeatcu.com/posts/2025-01-30-big-data-tiny-laptop/Big_Data_Tiny_Laptop.png" class="preview-image img-fluid"></p>
<section id="doing-more-with-less" class="level1">
<h1>Doing More with Less</h1>
<p>Ever wonder how you can work with “bigger than RAM” datasets on your laptop’s memory without needing a supercomputer? Last night, I had a great time presenting a practical approach to modern data science tooling that is reshaping how data is processed, analyzed, and presented, all from your local machine.</p>
<p>I shared how language-agnostic, open-source data storage and processing frameworks like Apache Parquet, Apache Arrow, DuckDB, and Polars make it possible to handle enormous data efficiently, whether you’re working with SQL, R, Python, and more. These next-gen frameworks let you process huge datasets faster than ever, right on your laptop. For example, I live-demoed how I’m able to read a 1.1 billion row, 22 column, 40GB data set on my MacBook Air in <em>25 milliseconds</em>. 🚀</p>
</section>
<section id="other-tools-explored" class="level1">
<h1>Other Tools Explored</h1>
<ul>
<li><a href="https://positron.posit.co/">Positron</a>: A fresh, open-source coding environment purpose-built for data analysis and modeling, including all the best bells and whistles from VS Code and RStudio.</li>
<li><a href="https://quarto.org/">Quarto</a>: An open source technical publishing system similar in feel to notebooks (like Jupyter Notebooks) for creating beautiful articles, websites, slides, dashboards, and with full support for Python, R, Julia, and Observable</li>
</ul>
</section>
<section id="embedded-presentation" class="level1">
<h1>Embedded Presentation</h1>
<iframe width="780" height="500" src="Big_Data_Tiny_Laptop.html" title="Big Data, Tiny Laptop">
</iframe>
<ul>
<li>Fullscreen web slides: <a href="http://rrr.is/BigDataTinyLaptop">Big Data, Tiny Laptop</a></li>
<li>GitHub repo: <a href="https://github.com/JavOrraca/Big-Data-Tiny-Laptop">JavOrraca/Big-Data-Tiny-Laptop</a></li>
</ul>
<p>This event was a collaborative effort between <a href="https://www.meetup.com/tech-by-the-beach/">Tech by the Beach</a>, <a href="https://www.meetup.com/socal-rug/">SoCal R Users Group</a>, and <a href="https://www.csulb.edu/cob-graduate-programs/ms-programs/ms-information-systems">CSULB’s Master of Science in Information Systems (MSIS) program</a>.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>positron</category>
  <category>arrow</category>
  <category>duckdb</category>
  <category>polars</category>
  <category>quarto</category>
  <guid>https://www.javierorracadeatcu.com/posts/2025-01-30-big-data-tiny-laptop/</guid>
  <pubDate>Thu, 30 Jan 2025 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2025-01-30-big-data-tiny-laptop/Big_Data_Tiny_Laptop.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>ETL with Arrow &amp; DuckDB</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2024-04-29-arrow-and-duckdb-talk/</link>
  <description><![CDATA[ 





<section id="ultra-fast-etl" class="level1">
<h1>Ultra-Fast ETL</h1>
<p>Two of my favorite data science tools in recent years include the advent of language agnostic, in-memory analytics frameworks like <a href="https://arrow.apache.org/">Apache Arrow</a> &amp; single-node in-process engines like <a href="https://duckdb.org/">DuckDB</a>. When paired with the <a href="https://parquet.apache.org/">Apache Parquet</a> columnar storage format to read, manipulate, analyze, and write data, these tools improve the R and Python experience in multiple ways:</p>
<ul>
<li>faster than dplyr (R) and pandas (Python)</li>
<li>SQL-friendly larger than memory processing</li>
<li>extremely portable (compile on all major systems)</li>
<li>zero-copy integration between DuckDB and Arrow</li>
</ul>
</section>
<section id="hackathon-talk" class="level1">
<h1>Hackathon Talk</h1>
<p>This past weekend, the <a href="https://www.meetup.com/socal-rug/">Southern California R Users Group</a> and UC Irvine hosted their annual hackathon. Given the larger data sets that we made available to the hackathon participants, I provided a talk on <a href="https://javorraca.github.io/UCI-2024-Hackathon-Workshop-Arrow-DuckDB"><em>ETL with Arrow &amp; DuckDB</em></a>.</p>
<p>Traditionally, you’d be unable to load a data set larger than your available RAM into your R or Python development environment; Arrow and DuckDB help us get around this limitation. For example, my MacBook Air has 24GB RAM and with R + Arrow, I could read and begin analyzing a 1.1 billion row 40GB Parquet data set in 25 milliseconds!</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2024-04-29-arrow-and-duckdb-talk/ArrowHackathonTalk.png" class="img-fluid"></p>
</section>
<section id="presentation-materials" class="level1">
<h1>Presentation Materials</h1>
<ul>
<li>Slides: <a href="https://javorraca.github.io/UCI-2024-Hackathon-Workshop-Arrow-DuckDB">javorraca.github.io/UCI-2024-Hackathon-Workshop-Arrow-DuckDB</a></li>
<li>Source: <a href="https://github.com/JavOrraca/UCI-2024-Hackathon-Workshop-Arrow-DuckDB">github.com/JavOrraca/UCI-2024-Hackathon-Workshop-Arrow-DuckDB</a></li>
</ul>
</section>
<section id="installation-notes" class="level1">
<h1>Installation Notes</h1>
<p>To compile the full Arrow build with easy access to Amazon S3 and GCS, I’d recommend the following pattern for R and Python users:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">R</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install full Arrow package w/ S3 and GCS support:</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set required env vars</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LIBARROW_MINIMAL =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"false"</span>)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ARROW_S3 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ON"</span>)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ARROW_GCS =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ON"</span>)</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download &amp; Install Arrow</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'arrow'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repos =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://apache.r-universe.dev'</span>)</span></code></pre></div></div>
</div>
</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 cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install full PyArrow w/ S3 and GCS support</span></span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set required env vars</span></span>
<span id="cb2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb2-5">os.environ[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LIBARROW_MINIMAL"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FALSE"</span></span>
<span id="cb2-6">os.environ[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ARROW_S3"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ON"</span></span>
<span id="cb2-7">os.environ[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ARROW_GCS"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ON"</span></span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install PyArrow</span></span>
<span id="cb2-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>pip install pyarrow</span></code></pre></div></div>
</div>
</div>
</div>
</div>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>dplyr</category>
  <category>arrow</category>
  <category>duckdb</category>
  <category>duckplyr</category>
  <guid>https://www.javierorracadeatcu.com/posts/2024-04-29-arrow-and-duckdb-talk/</guid>
  <pubDate>Mon, 29 Apr 2024 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2024-04-29-arrow-and-duckdb-talk/ArrowHackathonTalk.png" medium="image" type="image/png" height="75" width="144"/>
</item>
<item>
  <title>You Don’t Need OOP for Data Science</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2023-11-23-oop-with-r/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2023-11-23-oop-with-r/charlie.png" class="preview-image img-fluid figure-img"></p>
<figcaption>Python friends discussing OOP</figcaption>
</figure>
</div>
<section id="oop-with-r" class="level1 page-columns page-full">
<h1>OOP with R</h1>
<div class="columns">
<div class="column" style="width:80%;">
<p>For R users, the <a href="https://r6.r-lib.org/">R6 package</a> - <em>one of several object-oriented systems for R</em> - brings forth the elegance of object-oriented programming (OOP) to a traditionally ‘stats first’ language. My friend the <a href="https://chat.openai.com/g/g-QvPl8z04e-tidy-wizard">Tidy Wizard</a>, a barely-configured GPT tool that I crafted specializing in the R language, says that R6 “bestows upon the user the ability to craft sophisticated and modular software, a boon in various business scenarios.”</p>
</div><div class="column" style="width:5%;">

</div><div class="column" style="width:15%;">
<p><img src="https://www.javierorracadeatcu.com/posts/2023-11-23-oop-with-r/logo.svg" class="img-fluid"></p>
</div>
</div>
<p>When meeting data scientists in the wild, they’re almost always Python users and they often ask me the rhetorical question of “can R do OOP and encapsulation?” I used to be embarrassed about this… Did I miss some mission critical component of data science in grad school? Have I been learning from the wrong books and tutorials?</p>
<p>Having worked alongside amazing software engineers over recent years, I’ve come to realize OOP is fundamental in computer science education. The de facto OOP nature of Python makes the transition from C/C++/Java to Python a breeze and delight. This is one of the many reasons why we see so much OOP development in data science organizations from Python-based data science practitioners and educators. But what if you don’t have a formal software background and gravitated to R for statistics, functional programming, and data science?</p>
<aside>
<a href="https://chat.openai.com/g/g-QvPl8z04e-tidy-wizard">Interact with the Tidy Wizard</a> if you’re a paying ChatGPT Plus customer! I’m developing a Shiny app so anyone can interact with the Wizard directly via API (Gandalf-speak will be optional… trust me when I say it gets old 😅)<br><br>
</aside>
<p>My thoughts on this topic will differ from others in the R community, likely from folks that have formal computer science / software engineering backgrounds. I do not have a formal CS background, so bear with me while I work through this heresy: <em>You don’t need to know OOP to be a successful data scientist.</em></p>
<p>In data science, would knowing OOP help your future career prospects? Probably; <em>Probably</em> in the same way a PhD <em>might</em> help you climb the corporate ladder more quickly. In my experience (and again, as a primary R user), not knowing the intricacies of OOP shouldn’t slow you down from interactive web app development, ML training / production at scale, containerization via Docker, parallelized / vectorized function writing, package development, NLP, deep learning, etc.</p>
<p>On a daily basis, most R users don’t have a need for OOP since they utilize objects and functions. They apply functions to objects to create new objects, and all this is typically done to one class: the data.frame/tibble. OOP in R also isn’t limited to R6; In fact, we regularly apply functions in R that inherit the role of generic-function OO. For example, base R’s <code>sum()</code> and <code>print()</code> - <em>to name just a couple!</em> - are S3 functions.</p>
<p>Copying from Hadley Wickham’s <em>Advanced R</em>, he shares the following insights about different OO systems in R:</p>
<blockquote class="blockquote">
<p>Generally in R, functional programming is much more important than object-oriented programming, because you typically solve complex problems by decomposing them into simple functions, not simple objects. Nevertheless, there are important reasons to learn each of the three systems:</p>
<ul>
<li><p>S3 allows your functions to return rich results with user-friendly display and programmer-friendly internals. S3 is used throughout base R, so it’s important to master if you want to extend base R functions to work with new types of input.<br><br></p></li>
<li><p>R6 provides a standardised way to escape R’s copy-on-modify semantics. This is particularly important if you want to model objects that exist independently of R. Today, a common need for R6 is to model data that comes from a web API, and where changes come from inside or outside of R.<br><br></p></li>
<li><p>S4 is a rigorous system that forces you to think carefully about program design. It’s particularly well-suited for building large systems that evolve over time and will receive contributions from many programmers. This is why it is used by the Bioconductor project, so another reason to learn S4 is to equip you to contribute to that project.</p></li>
</ul>
</blockquote>
<p>Not mentioned in the above is the OOP system <a href="https://rconsortium.github.io/S7/">S7</a>, expected to be the successor to S3 and S4. Given the nature of this early-stage framework, I’ve focused most of this post to highlight R6. S7 “has been designed and implemented collaboratively by the R Consortium Object-Oriented Programming Working Group” and their <a href="https://rconsortium.github.io/S7/articles/motivation.html">high-level motivation for S7 post</a> details how it aims to resolve some of the challenges with R’s two built-in OO systems S3 and S4.</p>
</section>
<section id="additional-resources" class="level1">
<h1>Additional Resources</h1>
<p>After a pattern of writing, modifying, and deleting a more robust background section for object-oriented systems with R, I’ve decided to instead point you to some of the best resources I’ve come across:</p>
<ul>
<li><p>The <em>Advanced R</em> book, by Hadley Wickham, includes several chapters on OOP with R including base types, S3, R6, S4, and a trade-offs chapter. The <a href="https://adv-r.hadley.nz/oo.html"><em>Introduction to OOP</em></a> section is a great place to start for a high-level overview.</p></li>
<li><p>Dario Radečić published a phenomenal <a href="https://appsilon.com/oop-in-r-with-r6/">complete R6 guide</a> for Appsilon’s blog. It’s a bookmark-worthy R6 overview that touches on its classes and constructors/methods including <code>$new()</code>, <code>$initialize()</code>, <code>$print()</code>, and more.</p></li>
</ul>
</section>
<section id="r6-example-use-cases" class="level1">
<h1>R6 Example Use Cases</h1>
<p>With the assistance of the Tidy Wizard, let’s dive into some examples that aim to “illuminate a few practical applications where R6 can lend its prowess.” 🧙</p>
<section id="crm-systems" class="level2">
<h2 class="anchored" data-anchor-id="crm-systems">CRM Systems</h2>
<p>When we ponder Customer Relationship Management, we find ourselves amidst a vast sea of data, encompassing customer interactions, preferences, transaction histories, and much more. In the traditional approach, this data might be stored in spreadsheets or databases as tabular data. While this method is straightforward, it often leads to a fragmented view of the customer, with data scattered across multiple tables or sheets.</p>
<p>Herein lies the profound advantage of employing R6 in a CRM context:</p>
<p><em>Unified Customer View</em>: By defining an R6 class for a customer, you encapsulate all relevant information and behaviors related to that customer in one place. This approach fosters a holistic view of each customer, integrating data from various sources. It allows for more coherent and comprehensive analysis and decision-making.</p>
<p><em>Behavior Encapsulation</em>: With R6, you’re not just storing data; you’re also encapsulating behaviors. For instance, an R6 class for a customer could include methods to calculate lifetime value, predict churn, or even trigger personalized marketing actions. This encapsulation of behavior with data enriches the CRM’s capabilities.</p>
<p><em>Flexibility and Scalability</em>: R6’s object-oriented nature allows for more scalable and adaptable solutions. As business needs evolve, you can extend or modify the R6 classes without overhauling your entire data structure. This flexibility is crucial in the ever-changing business landscape.</p>
<p><em>Interactivity and Automation</em>: R6 classes can interact with each other, enabling automated workflows. For instance, a class representing an email campaign can interact with the customer class to send personalized emails, log interactions, and update customer profiles, all in an automated manner.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Defining a Customer class</span></span>
<span id="cb1-2">Customer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> R6<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;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Customer"</span>,</span>
<span id="cb1-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb1-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb1-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">email =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb1-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">transaction_history =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(),</span>
<span id="cb1-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(name, email) {</span>
<span id="cb1-8">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> name</span>
<span id="cb1-9">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>email <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> email</span>
<span id="cb1-10">        },</span>
<span id="cb1-11">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">record_transaction =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(transaction) {</span>
<span id="cb1-12">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(transaction))</span>
<span id="cb1-13">        },</span>
<span id="cb1-14">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">calculate_lifetime_value =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb1-15">          self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-16">            purrr<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;">map_dbl</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> .x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>amount) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-17">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb1-18">        }</span>
<span id="cb1-19">    )</span>
<span id="cb1-20">)</span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example usage</span></span>
<span id="cb1-23">customer1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Customer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"John Doe"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">email =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"john@example.com"</span>)</span>
<span id="cb1-24">customer1<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;">record_transaction</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-01-01"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">amount =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>))</span>
<span id="cb1-25">customer1<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;">record_transaction</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-02-01"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">amount =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>))</span>
<span id="cb1-26">lifetime_value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> customer1<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;">calculate_lifetime_value</span>()</span>
<span id="cb1-27"></span>
<span id="cb1-28"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(lifetime_value)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 250</code></pre>
</div>
</div>
<p>In this enriched example, our Customer class now not only stores basic information but also tracks transaction history and calculates the lifetime value. This enriched model provides a more dynamic and insightful perspective on the customer, far surpassing the static nature of spreadsheets or basic tabular data.</p>
</section>
<section id="financial-modeling" class="level2">
<h2 class="anchored" data-anchor-id="financial-modeling">Financial Modeling</h2>
<p>Let’s craft an R6 class that models a portfolio of financial instruments, each with its own characteristics and behaviors. This portfolio can include various types of investments like stocks, bonds, and mutual funds, each represented by their own R6 classes.</p>
<p>The beauty of using R6 in this context lies in its ability to encapsulate the complexities of financial instruments within their respective classes, thereby making the overall portfolio management more intuitive and robust. Let’s delve into this with a code example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(R6)</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a generic FinancialInstrument class</span></span>
<span id="cb3-4">FinancialInstrument <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> R6<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;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FinancialInstrument"</span>,</span>
<span id="cb3-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb3-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">identifier =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb3-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">principal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb3-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(identifier, principal) {</span>
<span id="cb3-9">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>identifier <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> identifier</span>
<span id="cb3-10">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>principal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> principal</span>
<span id="cb3-11">        },</span>
<span id="cb3-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">calculate_return =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb3-13">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Placeholder for return calculation</span></span>
<span id="cb3-14">        }</span>
<span id="cb3-15">    )</span>
<span id="cb3-16">)</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extend FinancialInstrument for specific types</span></span>
<span id="cb3-19">Stock <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> R6<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;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stock"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit =</span> FinancialInstrument,</span>
<span id="cb3-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb3-21">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dividend_yield =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb3-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(identifier, principal, dividend_yield) {</span>
<span id="cb3-23">            super<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;">initialize</span>(identifier, principal)</span>
<span id="cb3-24">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dividend_yield <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> dividend_yield</span>
<span id="cb3-25">        },</span>
<span id="cb3-26">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">calculate_return =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb3-27">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>principal <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dividend_yield)</span>
<span id="cb3-28">        }</span>
<span id="cb3-29">    )</span>
<span id="cb3-30">)</span>
<span id="cb3-31"></span>
<span id="cb3-32">Bond <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> R6<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;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bond"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit =</span> FinancialInstrument,</span>
<span id="cb3-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb3-34">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">interest_rate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb3-35">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(identifier, principal, interest_rate) {</span>
<span id="cb3-36">            super<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;">initialize</span>(identifier, principal)</span>
<span id="cb3-37">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interest_rate <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> interest_rate</span>
<span id="cb3-38">        },</span>
<span id="cb3-39">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">calculate_return =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb3-40">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>principal <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>interest_rate)</span>
<span id="cb3-41">        }</span>
<span id="cb3-42">    )</span>
<span id="cb3-43">)</span>
<span id="cb3-44"></span>
<span id="cb3-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define a Portfolio class that can hold multiple financial instruments</span></span>
<span id="cb3-46">Portfolio <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> R6<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;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Portfolio"</span>,</span>
<span id="cb3-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb3-48">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">instruments =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(),</span>
<span id="cb3-49">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add_instrument =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(instrument) {</span>
<span id="cb3-50">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>instruments[[instrument<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>identifier]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> instrument</span>
<span id="cb3-51">        },</span>
<span id="cb3-52">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_return =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb3-53">          self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>instruments <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-54">            purrr<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;">map_dbl</span>(\(x) x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate_return</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb3-55">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb3-56">        }</span>
<span id="cb3-57">    )</span>
<span id="cb3-58">)</span>
<span id="cb3-59"></span>
<span id="cb3-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example usage</span></span>
<span id="cb3-61">portfolio <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Portfolio<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>()</span>
<span id="cb3-62">portfolio<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;">add_instrument</span>(Stock<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAPL"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>))</span>
<span id="cb3-63">portfolio<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;">add_instrument</span>(Bond<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"US-Gov"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>))</span>
<span id="cb3-64"></span>
<span id="cb3-65">total_return <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> portfolio<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;">total_return</span>()</span>
<span id="cb3-66"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(total_return)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 350</code></pre>
</div>
</div>
<p>In this expanded scenario, we have:</p>
<ul>
<li>A base class <code>FinancialInstrument</code> that defines a generic financial instrument.</li>
<li>Derived classes <code>Stock</code> and <code>Bond</code> that inherit from <code>FinancialInstrument</code> and implement specific behaviors, such as different ways of calculating returns.</li>
<li>A <code>Portfolio</code> class that can contain a collection of different financial instruments. It can calculate the total return of the portfolio by summing the returns of each individual instrument.</li>
</ul>
<p>This object-oriented approach provided by R6 allows for clear separation and encapsulation of the logic for each type of financial instrument. It makes the code modular, easier to understand, and maintain. For a business leader or data analyst, this translates to a system that is more adaptable to new types of financial instruments and changing financial models, ultimately aiding in making more informed investment decisions.</p>
</section>
<section id="library-system" class="level2">
<h2 class="anchored" data-anchor-id="library-system">Library System</h2>
<p>Let’s develop a simplified system for managing a library which includes tracking books, patrons, and book loans. This example will demonstrate how R6 can be used to model real-world entities and their interactions.</p>
<p>In our library system, we’ll have three main classes:</p>
<ol type="1">
<li><em>Book</em>: Represents a book in the library.</li>
<li><em>Patron</em>: Represents a library member who can borrow books.</li>
<li><em>Library</em>: Manages the collection of books and patrons, and handles book loans.</li>
</ol>
<p>Here’s the R code for this use case:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(R6)</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Book class</span></span>
<span id="cb5-4">Book <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> R6<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;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Book"</span>,</span>
<span id="cb5-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb5-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">author =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb5-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">is_loaned =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-9">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(title, author) {</span>
<span id="cb5-10">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>title <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> title</span>
<span id="cb5-11">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>author <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> author</span>
<span id="cb5-12">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>is_loaned <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb5-13">        },</span>
<span id="cb5-14">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">loan =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb5-15">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>is_loaned <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb5-16">        },</span>
<span id="cb5-17">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">return_book =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb5-18">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>is_loaned <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb5-19">        }</span>
<span id="cb5-20">    )</span>
<span id="cb5-21">)</span>
<span id="cb5-22"></span>
<span id="cb5-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Patron class</span></span>
<span id="cb5-24">Patron <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> R6<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;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Patron"</span>,</span>
<span id="cb5-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-26">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb5-27">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">books_loaned =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(),</span>
<span id="cb5-28">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">transaction_history =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(),</span>
<span id="cb5-29">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(name) {</span>
<span id="cb5-30">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> name</span>
<span id="cb5-31">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books_loaned <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb5-32">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb5-33">        },</span>
<span id="cb5-34">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">borrow_book =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(book) {</span>
<span id="cb5-35">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>book<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>is_loaned) {</span>
<span id="cb5-36">                book<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;">loan</span>()</span>
<span id="cb5-37">                self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books_loaned[[book<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>title]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> book</span>
<span id="cb5-38">                self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history, </span>
<span id="cb5-39">                                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.time</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">": Borrowed"</span>, book<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>title)))</span>
<span id="cb5-40">            } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb5-41">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This book is currently loaned out."</span>)</span>
<span id="cb5-42">            }</span>
<span id="cb5-43">        },</span>
<span id="cb5-44">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">return_book =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(book) {</span>
<span id="cb5-45">            book<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;">return_book</span>()</span>
<span id="cb5-46">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books_loaned[[book<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>title]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb5-47">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history, </span>
<span id="cb5-48">                                          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.time</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">": Returned"</span>, book<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>title)))</span>
<span id="cb5-49">        },</span>
<span id="cb5-50">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">get_transaction_history =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb5-51">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>transaction_history)</span>
<span id="cb5-52">        }</span>
<span id="cb5-53">    )</span>
<span id="cb5-54">)</span>
<span id="cb5-55"></span>
<span id="cb5-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Library class</span></span>
<span id="cb5-57">Library <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> R6<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;">R6Class</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Library"</span>,</span>
<span id="cb5-58">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">public =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-59">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">books =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(),</span>
<span id="cb5-60">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">patrons =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(),</span>
<span id="cb5-61">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">initialize =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb5-62">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb5-63">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>patrons <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb5-64">        },</span>
<span id="cb5-65">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add_book =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(book) {</span>
<span id="cb5-66">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books[[book<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>title]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> book</span>
<span id="cb5-67">        },</span>
<span id="cb5-68">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add_patron =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(patron) {</span>
<span id="cb5-69">            self<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>patrons[[patron<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> patron</span>
<span id="cb5-70">        }</span>
<span id="cb5-71">    )</span>
<span id="cb5-72">)</span>
<span id="cb5-73"></span>
<span id="cb5-74"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example usage</span></span>
<span id="cb5-75">library_system <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Library<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>()</span>
<span id="cb5-76">book1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Book<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Great Gatsby"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F. Scott Fitzgerald"</span>)</span>
<span id="cb5-77">book2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Book<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1984"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"George Orwell"</span>)</span>
<span id="cb5-78">patron1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Patron<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"John Doe"</span>)</span>
<span id="cb5-79"></span>
<span id="cb5-80">library_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_book</span>(book1)</span>
<span id="cb5-81">library_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_book</span>(book2)</span>
<span id="cb5-82">library_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_patron</span>(patron1)</span>
<span id="cb5-83"></span>
<span id="cb5-84"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># John Doe borrows "The Great Gatsby"</span></span>
<span id="cb5-85">patron1<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;">borrow_book</span>(library_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Great Gatsby"</span>]])</span>
<span id="cb5-86"></span>
<span id="cb5-87"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if the book is loaned</span></span>
<span id="cb5-88"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(library_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Great Gatsby"</span>]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>is_loaned)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># John Doe returns "The Great Gatsby"</span></span>
<span id="cb7-2">patron1<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;">return_book</span>(library_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Great Gatsby"</span>]])</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check if the book is loaned</span></span>
<span id="cb7-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(library_system<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>books[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Great Gatsby"</span>]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>is_loaned)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View John Doe's transaction history</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(patron1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_transaction_history</span>())</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] "2023-11-28 14:49:54.173788 : Borrowed The Great Gatsby"

[[2]]
[1] "2023-11-28 14:49:54.175161 : Returned The Great Gatsby"</code></pre>
</div>
</div>
<p>In the above example:</p>
<ul>
<li>The <code>Book</code> class represents a book with methods to loan and return it</li>
<li>The <code>Patron</code> class represents a library member who can borrow and return books
<ul>
<li>The <code>Patron</code> class includes a <code>transaction_history</code> list to record each borrowing and returning action</li>
<li>Methods <code>borrow_book</code> and <code>return_book</code> append a descriptive string and timestamp to the <code>transaction_history</code> each time they are called</li>
<li><code>get_transaction_history</code> retrieves and displays the transaction history</li>
</ul></li>
<li>The <code>Library</code> class manages the collection of books and patrons
<ul>
<li>It allows adding books and patrons to the library</li>
</ul></li>
</ul>
<p>This code provides a simple yet effective demonstration of how R6 can be used to model and manage real-world entities and their interactions, showcasing the utility of object-oriented programming in R. Any time John Doe borrows or returns a book, the transaction is recorded. You can then view all his transactions by calling the <code>get_transaction_history</code> method on his Patron object.</p>
</section>
</section>
<section id="closing-remarks" class="level1">
<h1>Closing Remarks</h1>
<p>It’s crucial to recognize that OOP, while a powerful paradigm, is not a fundamental requirement for effective data science in R. The essence of R, especially in the realms of statistical programming, data analysis and visualization, is encapsulated in its rich ecosystem of packages and the functional programming approach, which often supersedes the need for a traditional OOP approach.</p>
<p>In the world of R, the <a href="https://www.tidyverse.org/">tidyverse</a> and <a href="https://www.tidymodels.org/">tidymodels</a> collections of packages stand out as quintessential examples of how functional programming can be both efficient and sufficient for most data science tasks. These packages provide a comprehensive suite of tools that are designed to work seamlessly with data objects, enabling users to perform complex data manipulations, analyses, and modeling without delving deeply into the nuances of OOP. Even web app development in R via Shiny allows data scientists to build robust client- and server-side programs without the need for OOP.</p>
<p>The functional programming paradigm empowers R users to apply a plethora of functions to objects in a way that is often more aligned with the typical workflows in reproducible statistics and machine learning. I find myself writing packages (often) and have yet had a need to employ traditional OOP in my package development work; That said, some of my favorite R frameworks leverage OOP (R6) in the backend. Whether you choose to dive into OOP or stick with the functional programming nature of R, you’ll most certainly be offered the tools and frameworks necessary to achieve your data science goals efficiently and effectively.</p>
</section>
<section id="sessioninfo" class="level1">
<h1>sessionInfo()</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-overflow-scroll code-with-copy"><code class="sourceCode r"><span id="cb11-1">R version <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2023-10-31</span>)</span>
<span id="cb11-2">Platform<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> aarch64<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>apple<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;">darwin20</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>bit)</span>
<span id="cb11-3">Running under<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> macOS Sonoma <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span></span>
<span id="cb11-4"></span>
<span id="cb11-5">Matrix products<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> default</span>
<span id="cb11-6">BLAS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>   <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/</span>System<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Library<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Frameworks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Accelerate.framework<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Versions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Frameworks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>vecLib.framework<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Versions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>libBLAS.dylib </span>
<span id="cb11-7">LAPACK<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/</span>Library<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Frameworks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>R.framework<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Versions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>arm64<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Resources<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lib<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>libRlapack.dylib;  LAPACK version <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.0</span></span>
<span id="cb11-8"></span>
<span id="cb11-9">locale<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb11-10">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span></span>
<span id="cb11-11"></span>
<span id="cb11-12">time zone<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> America<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>New_York</span>
<span id="cb11-13">tzcode source<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> internal</span>
<span id="cb11-14"></span>
<span id="cb11-15">attached base packages<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb11-16">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] stats     graphics  grDevices utils     datasets  methods   base     </span>
<span id="cb11-17"></span>
<span id="cb11-18">other attached packages<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb11-19">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] quarto_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>  purrr_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> R6_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.1</span>   </span>
<span id="cb11-20"></span>
<span id="cb11-21">loaded via a <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">namespace</span> (and not attached)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb11-22"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] digest_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.33</span>     later_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.1</span>       fastmap_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>     xfun_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.41</span>         magrittr_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>   </span>
<span id="cb11-23"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>] knitr_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.45</span>        htmltools_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.7</span>   rmarkdown_2<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.25</span>    lifecycle_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>   ps_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.5</span>         </span>
<span id="cb11-24">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>] cli_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.1</span>         processx_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.2</span>    vctrs_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.4</span>       compiler_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>    rstudioapi_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">15.0</span></span>
<span id="cb11-25">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>] tools_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>       evaluate_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.23</span>     yaml_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.7</span>        Rcpp_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.11</span>       rlang_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>      </span>
<span id="cb11-26">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>] jsonlite_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.7</span>   </span></code></pre></div></div>
</div>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>r6</category>
  <category>tidyverse</category>
  <category>tidymodels</category>
  <guid>https://www.javierorracadeatcu.com/posts/2023-11-23-oop-with-r/</guid>
  <pubDate>Thu, 23 Nov 2023 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2023-11-23-oop-with-r/charlie.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>webR via Quarto Extensions</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2023-08-23-webr-via-quarto-extensions/</link>
  <description><![CDATA[ 

<script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.45.0/min/vs/loader.js"></script>
<script type="module" id="qwebr-monaco-editor-init">

  // Configure the Monaco Editor's loader
  require.config({
    paths: {
      'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.45.0/min/vs'
    }
  });
</script>
<script type="module">
// Global dictionary to store Monaco Editor instances
globalThis.qwebrEditorInstances = {};

// Function that builds and registers a Monaco Editor instance    
globalThis.qwebrCreateMonacoEditorInstance = function (cellData) {

  const initialCode = cellData.code;
  const qwebrCounter = cellData.id;

  // Retrieve the previously created document elements
  let runButton = document.getElementById(`qwebr-button-run-${qwebrCounter}`);
  let editorDiv = document.getElementById(`qwebr-editor-${qwebrCounter}`);
  
  // Load the Monaco Editor and create an instance
  let editor;
  require(['vs/editor/editor.main'], function () {
    editor = monaco.editor.create(editorDiv, {
      value: initialCode,
      language: 'r',
      theme: 'vs-light',
      automaticLayout: true,           // Works wonderfully with RevealJS
      scrollBeyondLastLine: false,
      minimap: {
        enabled: false
      },
      fontSize: '17.5pt',              // Bootstrap is 1 rem
      renderLineHighlight: "none",     // Disable current line highlighting
      hideCursorInOverviewRuler: true  // Remove cursor indictor in right hand side scroll bar
    });

    // Store the official counter ID to be used in keyboard shortcuts
    editor.__qwebrCounter = qwebrCounter;

    // Store the official div container ID
    editor.__qwebrEditorId = `qwebr-editor-${qwebrCounter}`;

    // Store the initial code value
    editor.__qwebrinitialCode = initialCode;

    // Set at the model level the preferred end of line (EOL) character to LF.
    // This prevent `\r\n` from being given to the webR engine if the user is on Windows.
    // See details in: https://github.com/coatless/quarto-webr/issues/94
    // Associated error text: 
    // Error: <text>:1:7 unexpected input

    // Retrieve the underlying model
    const model = editor.getModel();
    // Set EOL for the model
    model.setEOL(monaco.editor.EndOfLineSequence.LF);

    // Dynamically modify the height of the editor window if new lines are added.
    let ignoreEvent = false;
    const updateHeight = () => {
      const contentHeight = editor.getContentHeight();
      // We're avoiding a width change
      //editorDiv.style.width = `${width}px`;
      editorDiv.style.height = `${contentHeight}px`;
      try {
        ignoreEvent = true;

        // The key to resizing is this call
        editor.layout();
      } finally {
        ignoreEvent = false;
      }
    };

    // Helper function to check if selected text is empty
    function isEmptyCodeText(selectedCodeText) {
      return (selectedCodeText === null || selectedCodeText === undefined || selectedCodeText === "");
    }

    // Registry of keyboard shortcuts that should be re-added to each editor window
    // when focus changes.
    const addWebRKeyboardShortCutCommands = () => {
      // Add a keydown event listener for Shift+Enter to run all code in cell
      editor.addCommand(monaco.KeyMod.Shift | monaco.KeyCode.Enter, () => {

        // Retrieve all text inside the editor
        qwebrExecuteCode(editor.getValue(), editor.__qwebrCounter);
      });

      // Add a keydown event listener for CMD/Ctrl+Enter to run selected code
      editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () => {

        // Get the selected text from the editor
        const selectedText = editor.getModel().getValueInRange(editor.getSelection());
        // Check if no code is selected
        if (isEmptyCodeText(selectedText)) {
          // Obtain the current cursor position
          let currentPosition = editor.getPosition();
          // Retrieve the current line content
          let currentLine = editor.getModel().getLineContent(currentPosition.lineNumber);

          // Propose a new position to move the cursor to
          let newPosition = new monaco.Position(currentPosition.lineNumber + 1, 1);

          // Check if the new position is beyond the last line of the editor
          if (newPosition.lineNumber > editor.getModel().getLineCount()) {
            // Add a new line at the end of the editor
            editor.executeEdits("addNewLine", [{
            range: new monaco.Range(newPosition.lineNumber, 1, newPosition.lineNumber, 1),
            text: "\n", 
            forceMoveMarkers: true,
            }]);
          }
          
          // Run the entire line of code.
          qwebrExecuteCode(currentLine, editor.__qwebrCounter,
            EvalTypes.Interactive);

          // Move cursor to new position
          editor.setPosition(newPosition);
        } else {
          // Code to run when Ctrl+Enter is pressed with selected code
          qwebrExecuteCode(selectedText, editor.__qwebrCounter, EvalTypes.Interactive);
        }
      });
    }

    // Register an on focus event handler for when a code cell is selected to update
    // what keyboard shortcut commands should work.
    // This is a workaround to fix a regression that happened with multiple
    // editor windows since Monaco 0.32.0 
    // https://github.com/microsoft/monaco-editor/issues/2947
    editor.onDidFocusEditorText(addWebRKeyboardShortCutCommands);

    // Register an on change event for when new code is added to the editor window
    editor.onDidContentSizeChange(updateHeight);

    // Manually re-update height to account for the content we inserted into the call
    updateHeight();

    // Store the editor instance in the global dictionary
    qwebrEditorInstances[editor.__qwebrCounter] = editor;

  });

  // Add a click event listener to the run button
  runButton.onclick = function () {
    qwebrExecuteCode(editor.getValue(), editor.__qwebrCounter, EvalTypes.Interactive);
  };

}
</script>




<p><img src="https://www.javierorracadeatcu.com/posts/2023-08-23-webr-via-quarto-extensions/images/webr_bart.jpeg" class="preview-image img-fluid"></p>
<p>Imagine you’ve written a program in R. It’s simple and does exactly what you want but now, you wish to make it available for many people on the web, allowing them to run it within their web browsers. The challenge is that web browsers don’t natively understand R code.</p>
<section id="webassembly" class="level1">
<h1>WebAssembly</h1>
<p>Traditionally, web browsers understand JavaScript as the primary language to run any dynamic operations. If you wanted to run code in a browser, you’d need to rewrite it in JavaScript. Re-writing code for every language to JavaScript is tedious and not efficient. This is where <a href="https://webassembly.org/">WebAssembly</a> (or “wasm”) comes into play.</p>
<p>WebAssembly introduces a new type of code that can be run in modern web browsers. It is designed as a low-level virtual machine that runs code at near-native speed. What’s fascinating about WebAssembly is that it isn’t written by hand; instead, other languages (like C, C++, Rust, Python, R, and more) can be compiled to WebAssembly.</p>
<p>In essence, WebAssembly allows you to take code from languages other than JavaScript, compile it into a format that the browser can understand, and then run it efficiently.</p>
</section>
<section id="webr" class="level1">
<h1>webR</h1>
<p>Compiling R to wasm was hypothetical even just a few years ago but is now possible via <a href="https://docs.r-wasm.org/webr/latest/">webR</a>. Being able to run R functions within a web browser without the need for an R interpreter on the user’s end feels game changing.</p>
<p>webR is enabling R developers to bring their applications, algorithms, and visualizations to broader audiences via the web. R + wasm is definitely in the early stages of development and new use cases are being released daily by the data science and web development communities. For now, I’m just enjoying learning the basics and thinking about potential use cases.</p>
</section>
<section id="demo" class="level1">
<h1>Demo</h1>
<p>For the examples below, I’m running webR via a Quarto extension. I’m following James Balamuta’s documentation on GitHub to learn how to use his <a href="https://github.com/coatless/quarto-webr">webR Code Extension for Quarto</a> before I dive deeper into the internals of webR. I’m basically using a plug-and-play implementation of webR for Quarto 🚀. My goal is to start here and eventually use the raw webR framework for more robust applications.</p>
<p>And so, enjoy some webr-enabled executable code chunks in a static Quarto doc!</p>
<div id="qwebr-insertion-location-1"></div>
<noscript>Please enable JavaScript to experience the dynamic code cell content on this page.</noscript>
<div id="qwebr-insertion-location-2"></div>
<noscript>Please enable JavaScript to experience the dynamic code cell content on this page.</noscript>
<div id="qwebr-insertion-location-3"></div>
<noscript>Please enable JavaScript to experience the dynamic code cell content on this page.</noscript>
<div id="qwebr-insertion-location-4"></div>
<noscript>Please enable JavaScript to experience the dynamic code cell content on this page.</noscript>
<p>THAT WAS FUN! Enjoy, and happy learning.</p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>webR</category>
  <category>dplyr</category>
  <guid>https://www.javierorracadeatcu.com/posts/2023-08-23-webr-via-quarto-extensions/</guid>
  <pubDate>Wed, 23 Aug 2023 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2023-08-23-webr-via-quarto-extensions/images/webR.png" medium="image" type="image/png" height="143" width="144"/>
</item>
<item>
  <title>Impressing Hiring Teams with a Shiny App Demo</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2023-03-28-shiny-bslib-socal-rug/2023-03-28-shiny-bslib-socal-rug.html</link>
  <description><![CDATA[ 





<section id="background" class="level1">
<h1>Background</h1>
<p>The Southern California R Users Group (“SoCal RUG”) is one of my favorite organizations - Not only is there a lot to be learned from regularly attending our events, it’s a great place to meet and network with other passionate data scientists in the community.</p>
<p>Last week, I presented to SoCal RUG on how to build a Shiny app demo as a resume or cover letter accessory, an extension to the <a href="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/2022-11-15-shiny-bslib-demo.html"><em>Build a Shiny App Demo</em></a> blog post that I wrote in Nov 2022. Empowering yourself with this kind of ‘accessory’ ready to be demoed at your interviews will help impress data science hiring managers and also differentiate you from other applicants.</p>
</section>
<section id="watch-on-youtube" class="level1">
<h1>Watch on YouTube</h1>
<div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/lNn4beRxnbk" title="Build a Shiny App Demo as a Cover Letter Accessory" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
</section>
<section id="presentation-materials" class="level1">
<h1>Presentation Materials</h1>
<ul>
<li>Slides from my SoCal RUG presentation: <a href="https://github.com/socalrug/presentations/tree/master/2023-03-21_Build_a_Shiny_App_Demo">2023-03-21 | Build_a_Shiny_App_Demo</a></li>
<li>Blog post: <a href="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/2022-11-15-shiny-bslib-demo.html">2022-11-15 | Build a Shiny App Demo</a></li>
<li>GitHub repo: <a href="https://github.com/JavOrraca/bslib_demo_shiny">github.com/JavOrraca/bslib_demo_shiny</a></li>
<li>Live Shiny app demo: <a href="https://javierorraca.shinyapps.io/Bloomreach_Shiny_App/">javierorraca.shinyapps.io/Bloomreach_Shiny_App/</a></li>
</ul>
</section>
<section id="resources" class="level1">
<h1>Resources</h1>
<section id="beginner-resources" class="level2">
<h2 class="anchored" data-anchor-id="beginner-resources">Beginner Resources</h2>
<ul>
<li>Official Shiny website: <a href="https://shiny.rstudio.com">shiny.rstudio.com</a>
<ul>
<li>Includes example galleries for inspiration</li>
</ul></li>
<li>Deploying your Shiny app to shinyapps.io: <a href="https://shiny.rstudio.com/articles/shinyapps.html">shiny.rstudio.com/articles/shinyapps.html</a></li>
<li>Shiny extension packages:
<ul>
<li><a href="https://rinterface.github.io/bs4Dash/articles/step-by-step.html">{bs4Dash}: How to start? (step-by-step)</a>
<ul>
<li>Brings Bootstrap 4 support to Shiny themes</li>
<li>Relies on AdminLTE HTML template</li>
</ul></li>
<li><a href="https://rstudio.github.io/bslib/articles/bslib.html">{bslib}: Customizing “stock” Shiny apps</a>
<ul>
<li>Globally style your Shiny Bootstrap themes</li>
<li>Use Sass variables to further customize your apps with “Sassy CSS” (*.scss)</li>
</ul></li>
</ul></li>
</ul>
</section>
<section id="advanced-resources" class="level2">
<h2 class="anchored" data-anchor-id="advanced-resources">Advanced Resources</h2>
<ul>
<li>Hadley Wickham’s Mastering Shiny: <a href="https://mastering-shiny.org/">mastering-shiny.org/</a></li>
<li>{golem} for modularizing and packaging Shiny apps: <a href="https://thinkr-open.github.io/golem/index.html">thinkr-open.github.io/golem/index.html</a></li>
<li>Engineering Production-Grade Shiny Apps: <a href="https://engineering-shiny.org/">engineering-shiny.org/</a></li>
<li>Outstanding User Interfaces with Shiny: <a href="https://unleash-shiny.rinterface.com/">unleash-shiny.rinterface.com/</a></li>
<li>Automating Dockerfile creation for Shiny apps: <a href="https://www.jumpingrivers.com/blog/shiny-auto-docker">www.jumpingrivers.com/blog/shiny-auto-docker</a></li>
</ul>
</section>
<section id="shiny-inspiration" class="level2">
<h2 class="anchored" data-anchor-id="shiny-inspiration">Shiny Inspiration</h2>
<ul>
<li>Posit’s Shiny gallery: <a href="https://shiny.rstudio.com/gallery/">shiny.rstudio.com/gallery/</a></li>
<li>Appsilon’s Shiny demos: <a href="https://demo.appsilon.com/">demo.appsilon.com/</a></li>
<li>Shiny Contest submissions by Stefan Schliebs:
<ul>
<li>2021: <a href="https://nz-stefan.shinyapps.io/commute-explorer-2/">New Zealand Commute Explorer</a></li>
<li>2020: <a href="https://nz-stefan.shinyapps.io/blog-explorer/">R Blog Explorer</a></li>
<li>2019: <a href="https://nz-stefan.shinyapps.io/cran-explorer/">R Package Explorer</a></li>
</ul></li>
</ul>


</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>tidyverse</category>
  <category>shiny</category>
  <category>renv</category>
  <category>bslib</category>
  <guid>https://www.javierorracadeatcu.com/posts/2023-03-28-shiny-bslib-socal-rug/2023-03-28-shiny-bslib-socal-rug.html</guid>
  <pubDate>Tue, 28 Mar 2023 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2023-03-28-shiny-bslib-socal-rug/images/presentation_cover_slide.png" medium="image" type="image/png" height="80" width="144"/>
</item>
<item>
  <title>R Goodies for 2023</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2023-01-15-r-goodies-for-2023/2023-01-15-r-goodies-for-2023.html</link>
  <description><![CDATA[ 





<section id="overview" class="level1">
<h1>Overview</h1>
<p>Data science had an awesome year in 2022. The list below highlights some of my favorites that I hope you test drive in 2023 (<em>if you haven’t already!</em>). Happy belated New Year folks!</p>
</section>
<section id="probably" class="level1">
<h1>1. Probably</h1>
<p>The <a href="https://probably.tidymodels.org/index.html">probably</a> package, developed by Max Kuhn and Davis Vaughan at Posit, is in early stages of development and presents a new (at least, new to me) approach to assigning more realistic discrete class predictions. One initial question you might have is <em>Where exactly should I use <strong>probably</strong> in a modeling pipeline?</em> - <a href="https://probably.tidymodels.org/articles/where-to-use.html">Davis Vaughan addresses this in detail</a> and developed {probably} as a “post processing step <em>after</em> the model has been fit, but <em>before</em> the model performance has been calculated.”</p>
<p>This package helps programmatically identify a more appropriate threshold / cutoff for class predictions using <a href="https://en.wikipedia.org/wiki/Youden%27s_J_statistic">Youden’s J statistic</a> (or “J-index”). The statistic is implemented in the {yardstick} package and can be accessed via <a href="https://yardstick.tidymodels.org/reference/j_index.html"><code>yardstick::j_index()</code></a>. So, what’s with the J statistic?</p>
<ul>
<li>J = sensitivity + specificity - 1</li>
<li>The maximum value of J is 1 when there are no false positives and no false negatives</li>
<li>This technique comes in handy when a threshold of 50% (or 0.5) may not be the most appropriate for assigning a binary classification, especially when class imbalances exist.</li>
</ul>
<p>Let’s create a basic tidymodels workflow to fit Lending Club loan data to a logistic regression and predict whether a loan is good or bad. The code below was pulled directly from the source example written by Davis Vaughan:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource r code-overflow-scroll number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(parsnip)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(probably)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rsample)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(modeldata)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lending_club"</span>)</span>
<span id="cb1-8"></span>
<span id="cb1-9">lending_club_modified <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb1-10">  lending_club <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-11">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Class =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">relevel</span>(Class, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"good"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-12">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Class, annual_inc, verification_status, sub_grade)</span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb1-15">split <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rsample<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;">initial_split</span>(lending_club_modified)</span>
<span id="cb1-16">lending_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rsample<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;">training</span>(split)</span>
<span id="cb1-17">lending_test  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rsample<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;">testing</span>(split)</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note: 7,008 records are "good" loans and 384 are "bad"</span></span>
<span id="cb1-20"></span>
<span id="cb1-21">logi_reg_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb1-22">  parsnip<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;">logistic_reg</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-23">  parsnip<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-24">  parsnip<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;">fit</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Class <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> lending_train)</span>
<span id="cb1-25"></span>
<span id="cb1-26">predictions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(</span>
<span id="cb1-27">  lending_test,</span>
<span id="cb1-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(logi_reg_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> lending_test),</span>
<span id="cb1-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(logi_reg_fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> lending_test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prob"</span>))</span>
<span id="cb1-30"></span>
<span id="cb1-31">threshold_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> predictions <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-32">  probably<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;">threshold_perf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> Class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> .pred_good, </span>
<span id="cb1-33">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">thresholds =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0025</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-34">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.metric <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"distance"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-35">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(.metric <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sens"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>,</span>
<span id="cb1-36">                                         .metric <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spec"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>,</span>
<span id="cb1-37">                                         <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>))</span>
<span id="cb1-38"></span>
<span id="cb1-39">max_j_index_threshold <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> threshold_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-40">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.metric <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"j_index"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-41">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.estimate <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;">max</span>(.estimate)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-42">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(.threshold)</span>
<span id="cb1-43"></span>
<span id="cb1-44">ggplot2<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;">ggplot</span>(threshold_data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> .threshold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> .estimate, </span>
<span id="cb1-45">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> .metric, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-46">  ggplot2<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;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-47">  ggplot2<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;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-48">  ggplot2<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;">scale_color_viridis_d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-49">  ggplot2<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;">scale_alpha_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-50">  ggplot2<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;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> max_j_index_threshold, </span>
<span id="cb1-51">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-52">  ggplot2<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;">labs</span>(</span>
<span id="cb1-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'Good' Threshold</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;">(above this value is considered 'good')"</span>,</span>
<span id="cb1-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Metric Estimate"</span>,</span>
<span id="cb1-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Balancing performance by varying the threshold"</span>,</span>
<span id="cb1-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensitivity or specificity alone might not be"</span>,</span>
<span id="cb1-57">                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" enough!</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;">Vertical line = Max J-Index"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2023-01-15-r-goodies-for-2023/2023-01-15-r-goodies-for-2023_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The <code>max_j_index_threshold</code> has a value of 0.945 and the above viz helps supports the case for changing the discrete classification threshold from the baseline value of 0.5 (where a prediction probability above 0.5 indicates a “good”). Neat package with some other capabilities that I’m going to keep an eye out for.</p>
</section>
<section id="arrow-duckdb" class="level1">
<h1>2. Arrow + DuckDB</h1>
<p>Earlier in 2022, there was a lot of excitement on my Twitter feed with devs and data scientists discussing larger-than-memory data manipulation and ultra-fast ETL with <a href="https://duckdb.org/">DuckDB</a>, a new analytical data management system with bindings for Python and R. DuckDB offers a parallel vectorized execution engine for zero-copy query of <a href="https://arrow.apache.org/">Apache Arrow</a> data sets - Perfect for my world of <em>mostly</em> tabular data. DuckDB streams query results back to Arrow and this is possible via a SQL interface and API. As DuckDB puts it, they provide “an in-process SQL OLAP database management system.”</p>
<p>Below are some of my favorite DuckDB overviews and tutorials:</p>
<ol type="1">
<li><a href="https://duckdb.org/2021/12/03/duck-arrow.html#Benchmark%20Comparison"><em>DuckDB quacks Arrow</em>, Pedro Holanda and Jonathan Keane</a></li>
<li><a href="https://www.christophenicault.com/post/large_dataframe_arrow_duckdb/"><em>Manipulate big data with Arrow &amp; DuckDB</em>, Christophe Nicault</a></li>
<li><a href="https://www.richpauloo.com/post/parquet/"><em>Parquet, SQL, DuckDB, arrow, dbplyr and R</em>, Rich Pauloo</a></li>
</ol>
<p>If the above don’t make it clear, using dplyr with Arrow and DuckDB is a breeze. I haven’t yet mentioned the <a href="https://parquet.apache.org/">Apache Parquet</a> file format since I find it secondary to the above, but Arrow and DuckDB provide an easy approach to working with Parquet (and other) file formats and filesize (compared to a CSV) can be slimmed by an order of magnitude. Let’s dive into some stats, below, benchmarked by Christophe Nicault. The benchmarks relate to an 8GB CSV file with 59 million observations:</p>
<section id="size-on-disk" class="level2">
<h2 class="anchored" data-anchor-id="size-on-disk">Size on Disk</h2>
<p><img src="https://www.javierorracadeatcu.com/posts/2023-01-15-r-goodies-for-2023/images/size_on_disk.png" class="img-fluid"></p>
</section>
<section id="query-performance" class="level2">
<h2 class="anchored" data-anchor-id="query-performance">Query Performance</h2>
<p><img src="https://www.javierorracadeatcu.com/posts/2023-01-15-r-goodies-for-2023/images/performance_on_query.png" class="img-fluid"></p>
</section>
<section id="size-vs-performance" class="level2">
<h2 class="anchored" data-anchor-id="size-vs-performance">Size vs Performance</h2>
<p><img src="https://www.javierorracadeatcu.com/posts/2023-01-15-r-goodies-for-2023/images/size_vs_performance.png" class="img-fluid"></p>
</section>
</section>
<section id="encryption-with-r" class="level1">
<h1>3. Encryption with R</h1>
<p>R’s <a href="https://docs.ropensci.org/sodium/">sodium</a> package provides a binding to <a href="https://doc.libsodium.org/">libsodium</a> for higher-level cryptographic tooling. I first learned of {sodium} while working with Google BigQuery and learning about R’s <a href="https://gargle.r-lib.org/articles/managing-tokens-securely.html">gargle</a> package for securely managing access credentials. Another package I’d like to point attention to is <a href="https://docs.ropensci.org/cyphr/">cyphr</a>. I’ve used a combination of these packages for creating secure access to encrypted data objects.</p>
<p>I’d like to walk through two examples using these encryption tools:</p>
<ol type="1">
<li>Intro to {sodium} for keypair generation, message encrypt / decrpyt</li>
<li>Keypair gen with {sodium} + file encrypt / decrpt with {cyphr}</li>
</ol>
<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">
<span class="screen-reader-only">Warning</span>Installation from source on macOS or Linux requires <a href="https://doc.libsodium.org/">libsodium</a>
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>On macOS, install via homebrew: <code>brew install libsodium</code></li>
<li>On Debian or Ubuntu: <code>sudo apt-get install -y libsodium-dev</code></li>
<li>On Fedora: <code>sudo yum install libsodium-devel</code></li>
<li>On CentOS / RHEL, install via EPEL:
<ul>
<li><code>sudo yum install epel-release</code></li>
<li><code>sudo yum install libsodium-devel</code></li>
</ul></li>
</ul>
</div>
</div>
<section id="intro-to-sodium" class="level2">
<h2 class="anchored" data-anchor-id="intro-to-sodium">Intro to Sodium</h2>
<p>The following example uses {sodium} to generate a keypair and encrpt / decrypt a serialized message (representing the iris data set). One major benefit of encrypting objects like this is that you can publicly expose these encrypted objects since you alone hold the key to decrypt them. If you’re working with GitHub Actions, GitLab CI/CD, or another git-backed repository, you can save these encrypted messages in your public repo - If someone forks or clones your repository, they’ll be unable to decrypt the object without the hash key initially created in the keypair generation step.</p>
<p>The example below was <a href="https://docs.ropensci.org/sodium/#hello-world">written by Jeroen Ooms on rOpenSci</a> and is part of the documentation for the {sodium} package. I added the last step to show how to <code>unserialize()</code> an object previously decrypted.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource r code-overflow-scroll number-lines code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sodium)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate keypair:</span></span>
<span id="cb2-4">key <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sodium<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;">keygen</span>()</span>
<span id="cb2-5">pub <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sodium<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;">pubkey</span>(key)</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Encrypt message with pubkey</span></span>
<span id="cb2-8">msg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">serialize</span>(iris, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb2-9">ciphertext <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sodium<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;">simple_encrypt</span>(msg, pub)</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Decrypt message with private key</span></span>
<span id="cb2-12">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sodium<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;">simple_decrypt</span>(ciphertext, key)</span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unserialize the decrypted message &amp; assign iris data to new obj</span></span>
<span id="cb2-15">iris_new <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unserialize</span>(out)</span></code></pre></div></div>
</div>
</section>
<section id="sodium-cyphr" class="level2">
<h2 class="anchored" data-anchor-id="sodium-cyphr">Sodium + Cyphr</h2>
<p>Using the {sodium} and {cyphr} packages, the code snippets below showcase three key steps: 3a) data encryption, 3b) in-line decryption (to be used as a script intended to run with a GitHub Actions Workflow every six hours), and 3c) the GitHub Actions YAML file to configure the automation workflow.</p>
<p>Using this approach, I can safely expose an encrypted RDS file on my public GitHub repo that contains secret API credentials for accessing Meetup.com and fetching new events that may have been posted. The encrypted RDS file can only be decrypted using the keypair hash saved with the <code>MEETUP_PWD</code> environment variable on my GitHub repo, a secret env var only visible to me.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>GitHub Actions
</div>
</div>
<div class="callout-body-container callout-body">
<p>To learn how to use secrets for use with GitHub Actions, the official <a href="https://docs.github.com/en/actions/security-guides/encrypted-secrets">Actions/Encrypted Secrets</a> documentation is an excellent end-to-end resource. A generalized overview of how to configure your GitHub Actions Workflow is available on GitHub’s <a href="https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions">Actions/Workflow Syntax</a> documentation. A brief overview of major updates to GitHub Actions for R Users was published on the <a href="https://www.tidyverse.org/blog/2022/06/actions-2-0-0/">Tidyverse Blog in June 2022</a>, with the most complete resource for R related GitHub Actions being on <a href="https://github.com/r-lib/actions">r-lib/actions</a>.</p>
</div>
</div>
<p>NOTE: For the purposes of GitHub Actions and the config YAML file covered in Step 3c, this file lives in the following path on my GitHub repo: <code>~/.github/workflows/main.yml</code></p>
</section>
<section id="step-3a-encrypt" class="level2">
<h2 class="anchored" data-anchor-id="step-3a-encrypt">Step 3a: Encrypt</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource r code-overflow-scroll number-lines code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The following is a one-time key encryption setup</span></span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sodium)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cyphr)</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(meetupr)</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># After doing a one-time interactive auth with {meetupr},</span></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># your Meetup token will be stored as an RDS file in the </span></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># same path as your {meetupr} package:</span></span>
<span id="cb3-10">token_path <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">path.expand</span>(</span>
<span id="cb3-12">    <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;">"~"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Library"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Application Support"</span>, </span>
<span id="cb3-13">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"meetupr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"meetup_token.rds"</span>)</span>
<span id="cb3-14">  )</span>
<span id="cb3-15"></span>
<span id="cb3-16">meetupr<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;">meetup_auth</span>(</span>
<span id="cb3-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">token =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb3-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cache =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb3-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">use_appdir =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb3-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">token_path =</span> token_path</span>
<span id="cb3-21">)</span>
<span id="cb3-22"></span>
<span id="cb3-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Encrypt the existing token to safely save </span></span>
<span id="cb3-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># meetupr_secret.rds in a public repo</span></span>
<span id="cb3-25">sodium_key <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sodium<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;">keygen</span>()</span>
<span id="cb3-26"></span>
<span id="cb3-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># In your working environment, save a secret env var called</span></span>
<span id="cb3-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "MEETUPR_PWD" using the randomly generated sodium_key</span></span>
<span id="cb3-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MEETUPR_PWD"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sodium<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;">bin2hex</span>(sodium_key))</span>
<span id="cb3-30"></span>
<span id="cb3-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a key for use with {cyphr}</span></span>
<span id="cb3-32">key <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cyphr<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;">key_sodium</span>(sodium<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;">hex2bin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.getenv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MEETUPR_PWD"</span>)))</span>
<span id="cb3-33"></span>
<span id="cb3-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Encrypt the RDS and save as meetupr_secret.rds</span></span>
<span id="cb3-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in the working environment</span></span>
<span id="cb3-36">cyphr<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;">encrypt_file</span>(</span>
<span id="cb3-37">  token_path,</span>
<span id="cb3-38">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> key,</span>
<span id="cb3-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dest =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"meetupr_secret.rds"</span></span>
<span id="cb3-40">)</span></code></pre></div></div>
</div>
</section>
<section id="step-3b-decrypt" class="level2">
<h2 class="anchored" data-anchor-id="step-3b-decrypt">Step 3b: Decrypt</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource r code-overflow-scroll number-lines code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This script was saved as "meetup_events.R" and it is run as part</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># of the GitHub Actions workflow on line 37 of the YAML file covered</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in "Step 3c: GitHub Actions YAML"</span></span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb4-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb4-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(meetupr)</span>
<span id="cb4-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cyphr)</span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate Key with MEETUPR_PWD env var for MeetupR data CI/CD</span></span>
<span id="cb4-13">key <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cyphr<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;">key_sodium</span>(sodium<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;">hex2bin</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.getenv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MEETUPR_PWD"</span>)))</span>
<span id="cb4-14"></span>
<span id="cb4-15">temptoken <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempfile</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fileext =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".rds"</span>)</span>
<span id="cb4-16"></span>
<span id="cb4-17">cyphr<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;">decrypt_file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"meetupr_secret.rds"</span>,</span>
<span id="cb4-18">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> key,</span>
<span id="cb4-19">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dest =</span> temptoken)</span>
<span id="cb4-20"></span>
<span id="cb4-21">token <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readRDS</span>(temptoken)[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb4-22"></span>
<span id="cb4-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Non-Interactive Meetup Authorization</span></span>
<span id="cb4-24">token <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> meetupr<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;">meetup_auth</span>(</span>
<span id="cb4-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">token =</span> token,</span>
<span id="cb4-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">use_appdir =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb4-27">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cache =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb4-28">)</span>
<span id="cb4-29"></span>
<span id="cb4-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save a temporary, updated Meetup personal access token that will</span></span>
<span id="cb4-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># be used downstream with the meetupr::get_events() function</span></span>
<span id="cb4-32"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">MEETUPR_PAT =</span> temptoken)</span>
<span id="cb4-33"></span>
<span id="cb4-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a character vector of the Meetup accounts for data collection</span></span>
<span id="cb4-35">socal_groups <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOCAL-RUG"</span>, </span>
<span id="cb4-36">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Santa-Barbara-R-Users-Group"</span>, </span>
<span id="cb4-37">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Real-Data-Science-USA-R-Meetup"</span>,</span>
<span id="cb4-38">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"useR-Group-in-San-Luis-Obispo-County"</span>, </span>
<span id="cb4-39">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-irvine"</span>,</span>
<span id="cb4-40">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-la"</span>,</span>
<span id="cb4-41">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-pasadena"</span>,</span>
<span id="cb4-42">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-riverside"</span>,</span>
<span id="cb4-43">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-santa-barbara"</span>,</span>
<span id="cb4-44">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-san-diego"</span>)</span>
<span id="cb4-45"></span>
<span id="cb4-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a function for getting upcoming and past Meetup events</span></span>
<span id="cb4-47">get_meetup_events <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb4-48">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(meetupr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_events</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">urlname =</span> x)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb4-49">    meetupr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_events</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">urlname =</span> x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-50">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chapter =</span> x)</span>
<span id="cb4-51">  }</span>
<span id="cb4-52">}</span>
<span id="cb4-53"></span>
<span id="cb4-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using purrr, iterate through each Meetup account, collect </span></span>
<span id="cb4-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># event data, and row-bind the resulting event listings</span></span>
<span id="cb4-56">events_raw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb4-57">  purrr<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;">map_dfr</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.x =</span> socal_groups, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.f =</span> get_meetup_events) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-58">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(chapter, dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>())</span>
<span id="cb4-59"></span>
<span id="cb4-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data clean up</span></span>
<span id="cb4-61">events <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb4-62">  events_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-63">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb4-64">    status <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"draft"</span>,</span>
<span id="cb4-65">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>stringr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(title, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cross-post|cross-post"</span>),</span>
<span id="cb4-66">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>stringr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(description, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cross-post|cross-post"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-67">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-68">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(time)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-69">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb4-70">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Upcoming =</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(status <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"published"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&amp;#x2713;"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>),</span>
<span id="cb4-71">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Chapter =</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb4-72">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SOCAL-RUG"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SoCal RUG"</span>,</span>
<span id="cb4-73">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Santa-Barbara-R-Users-Group"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Santa Barbara RUG"</span>,</span>
<span id="cb4-74">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Real-Data-Science-USA-R-Meetup"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Los Angeles RUG"</span>,</span>
<span id="cb4-75">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"useR-Group-in-San-Luis-Obispo-County"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SLO RUG"</span>,</span>
<span id="cb4-76">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-irvine"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R-Ladies Irvine"</span>,</span>
<span id="cb4-77">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-la"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R-Ladies Los Angeles"</span>,</span>
<span id="cb4-78">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-pasadena"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R-Ladies Pasadena"</span>,</span>
<span id="cb4-79">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-riverside"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R-Ladies Riverside"</span>,</span>
<span id="cb4-80">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-santa-barbara"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R-Ladies Santa Barbara"</span>,</span>
<span id="cb4-81">      chapter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rladies-san-diego"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R-Ladies San Diego"</span>,</span>
<span id="cb4-82">      <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> chapter),</span>
<span id="cb4-83">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Event =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'&lt;a href='</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"'</span>, link, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"&gt;'</span>, title, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'&lt;/a&gt;'</span>),</span>
<span id="cb4-84">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Date =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(time),</span>
<span id="cb4-85">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Time =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format.POSIXct</span>(time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%I:%M %p"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-86">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Upcoming, Chapter, Event, Date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time (PST)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> Time)</span>
<span id="cb4-87"></span>
<span id="cb4-88"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(events, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"events/events_past_and_upcoming.rds"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="step-3c-github-actions-yaml" class="level2">
<h2 class="anchored" data-anchor-id="step-3c-github-actions-yaml">Step 3c: GitHub Actions YAML</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource bash code-overflow-scroll number-lines code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">name:</span> Render and Deploy Quarto Blog</span>
<span id="cb5-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">on:</span></span>
<span id="cb5-3">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pull_request:</span></span>
<span id="cb5-4">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">branches:</span> [ master ]</span>
<span id="cb5-5">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">push:</span></span>
<span id="cb5-6">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">branches:</span> [ master ]</span>
<span id="cb5-7">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">schedule:</span></span>
<span id="cb5-8">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span> cron:  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0 */6 * * *'</span></span>
<span id="cb5-9"></span>
<span id="cb5-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">jobs:</span></span>
<span id="cb5-11">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">build:</span></span>
<span id="cb5-12">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">runs-on:</span> ubuntu-latest</span>
<span id="cb5-13">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">steps:</span></span>
<span id="cb5-14">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span> name: Checkout Repo</span>
<span id="cb5-15">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uses:</span> actions/checkout@v3</span>
<span id="cb5-16">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">with:</span></span>
<span id="cb5-17">          <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fetch-depth:</span> 0</span>
<span id="cb5-18">          </span>
<span id="cb5-19">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span> name: Install libsodium</span>
<span id="cb5-20">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">run:</span> sudo apt-get install <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-y</span> libsodium-dev</span>
<span id="cb5-21"></span>
<span id="cb5-22">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span> name: Setup Quarto CLI</span>
<span id="cb5-23">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uses:</span> quarto-dev/quarto-actions/setup@v2</span>
<span id="cb5-24">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">with:</span></span>
<span id="cb5-25">          <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">version:</span> 1.2.280</span>
<span id="cb5-26"></span>
<span id="cb5-27">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span> name: Setup R </span>
<span id="cb5-28">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uses:</span> r-lib/actions/setup-r@v2</span>
<span id="cb5-29">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">with:</span></span>
<span id="cb5-30">          <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">r-version:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'4.2.2'</span></span>
<span id="cb5-31">          <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">use-public-rspm:</span> true</span>
<span id="cb5-32">      </span>
<span id="cb5-33">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span> name: Setup renv</span>
<span id="cb5-34">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uses:</span> r-lib/actions/setup-renv@v2</span>
<span id="cb5-35"></span>
<span id="cb5-36">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span> name: Fetch Events from Meetup</span>
<span id="cb5-37">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">run:</span> source<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">here::here</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"helpers"</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"meetup_events.R"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">))</span></span>
<span id="cb5-38">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">env:</span></span>
<span id="cb5-39">          <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">MEETUPR_PWD:</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;">{ secrets.MEETUPR_PWD </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span>}</span>
<span id="cb5-40">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">shell:</span> Rscript {0}</span>
<span id="cb5-41"></span>
<span id="cb5-42">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span> name: Render and Publish to Netlify</span>
<span id="cb5-43">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uses:</span> quarto-dev/quarto-actions/publish@v2</span>
<span id="cb5-44">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">with:</span></span>
<span id="cb5-45">          <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">target:</span> netlify</span>
<span id="cb5-46">          <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">NETLIFY_AUTH_TOKEN:</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;">{ secrets.NETLIFY_AUTH_TOKEN </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">}</span>}</span></code></pre></div></div>
</div>
</section>
</section>
<section id="session-info" class="level1">
<h1>Session Info</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-overflow-scroll code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">R</span> version 4.2.2 <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">2022-10-31</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="cb6-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Platform:</span> aarch64-apple-darwin20 <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">64-bit</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="cb6-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Running</span> under: macOS Ventura 13.1</span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Matrix</span> products: default</span>
<span id="cb6-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">LAPACK:</span> /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib</span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">locale:</span></span>
<span id="cb6-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[1]</span> en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8</span>
<span id="cb6-10"></span>
<span id="cb6-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">attached</span> base packages:</span>
<span id="cb6-12"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[1]</span> stats     graphics  grDevices utils     datasets  methods   base     </span>
<span id="cb6-13"></span>
<span id="cb6-14"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">other</span> attached packages:</span>
<span id="cb6-15"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[1]</span> quarto_1.2          purrr_1.0.1         lubridate_1.9.0    </span>
<span id="cb6-16"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[4]</span> timechange_0.2.0    stringr_1.5.0       meetupr_0.2.99.9002</span>
<span id="cb6-17"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[7]</span> cyphr_1.1.4         sodium_1.2.1        ggplot2_3.4.0      </span>
<span id="cb6-18"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[10]</span> modeldata_1.0.1     rsample_1.1.1       dplyr_1.0.10       </span>
<span id="cb6-19"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[13]</span> probably_0.1.0      parsnip_1.0.3      </span>
<span id="cb6-20"></span>
<span id="cb6-21"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">loaded</span> via a namespace <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">and</span> not attached<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb6-22"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[1]</span> tidyselect_1.2.0  xfun_0.36         listenv_0.9.0    </span>
<span id="cb6-23"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[4]</span> colorspace_2.0-3  vctrs_0.5.1       generics_0.1.3   </span>
<span id="cb6-24"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[7]</span> htmltools_0.5.4   yaml_2.3.6        utf8_1.2.2       </span>
<span id="cb6-25"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[10]</span> rlang_1.0.6       pillar_1.8.1      later_1.3.0      </span>
<span id="cb6-26"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[13]</span> glue_1.6.2        withr_2.5.0       DBI_1.1.3        </span>
<span id="cb6-27"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[16]</span> lifecycle_1.0.3   munsell_0.5.0     gtable_0.3.1     </span>
<span id="cb6-28"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[19]</span> anytime_0.3.9     future_1.30.0     evaluate_0.19    </span>
<span id="cb6-29"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[22]</span> codetools_0.2-18  knitr_1.41        fastmap_1.1.0    </span>
<span id="cb6-30"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[25]</span> ps_1.7.2          parallel_4.2.2    fansi_1.0.3      </span>
<span id="cb6-31"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[28]</span> furrr_0.3.1       Rcpp_1.0.9        scales_1.2.1     </span>
<span id="cb6-32"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[31]</span> jsonlite_1.8.4    parallelly_1.34.0 digest_0.6.31    </span>
<span id="cb6-33"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[34]</span> stringi_1.7.12    processx_3.8.0    grid_4.2.2       </span>
<span id="cb6-34"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[37]</span> hardhat_1.2.0     cli_3.6.0         tools_4.2.2      </span>
<span id="cb6-35"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[40]</span> magrittr_2.0.3    tibble_3.1.8      tidyr_1.2.1      </span>
<span id="cb6-36"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[43]</span> pkgconfig_2.0.3   rmarkdown_2.19    assertthat_0.2.1 </span>
<span id="cb6-37"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[46]</span> yardstick_1.1.0   rstudioapi_0.14   ratelimitr_0.4.1 </span>
<span id="cb6-38"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[49]</span> R6_2.5.1          globals_0.16.2    compiler_4.2.2   </span></code></pre></div></div>
</div>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>tidymodels</category>
  <category>probably</category>
  <category>arrow</category>
  <category>duckdb</category>
  <category>sodium</category>
  <category>cyphr</category>
  <guid>https://www.javierorracadeatcu.com/posts/2023-01-15-r-goodies-for-2023/2023-01-15-r-goodies-for-2023.html</guid>
  <pubDate>Sun, 15 Jan 2023 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2023-01-15-r-goodies-for-2023/images/apache_arrow.png" medium="image" type="image/png" height="65" width="144"/>
</item>
<item>
  <title>Data Science Hangout</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2023-01-01-data-science-hangout/2023-01-01-data-science-hangout.html</link>
  <description><![CDATA[ 





<section id="background" class="level1">
<h1>Background</h1>
<p>I’ve been attending Posit’s weekly <a href="https://posit.co/data-science-hangout/">Data Science Hangout</a> since 2021 and it’s been one of my favorite standing weekly meetings. This is a great platform to hear how data science leaders are using R to drive business results and the host, Rachael Dempsey, does an excellent job fostering an inclusive and open culture. I was thrilled to be the guest several weeks ago in Dec 2022. 🚀</p>
<p>I talked about my transition to data science from a career in financial modeling and consulting, talked about my love for Shiny and the R community, and gave some tips on how to start your own data science community. I also said “you know” an inordinate number of times, please forgive me in advance. 🙃</p>
<p>Whether you’re a data science practitioner, student, looking to make a career pivot, or simply wanting to network, the Data Science Hangout has something for all. If you haven’t already, I highly encourage you to attend Posit’s Data Science Hangout live sometime in the future.</p>
</section>
<section id="watch-on-youtube" class="level1">
<h1>Watch on YouTube</h1>
<div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/-mm5tHQVPY8" title="From Excel to Lead Machine Learning Engineer" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>tidyverse</category>
  <category>shiny</category>
  <category>machine learning</category>
  <category>tidymodels</category>
  <guid>https://www.javierorracadeatcu.com/posts/2023-01-01-data-science-hangout/2023-01-01-data-science-hangout.html</guid>
  <pubDate>Sun, 01 Jan 2023 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2023-01-01-data-science-hangout/images/Posit_Data_Science_Hangout.png" medium="image" type="image/png" height="78" width="144"/>
</item>
<item>
  <title>Build a Shiny App Demo</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/2022-11-15-shiny-bslib-demo.html</link>
  <description><![CDATA[ 





<section id="background" class="level1">
<h1>Background</h1>
<p>If you Google Search “applying for jobs is,” you see auto-completion suggestions that feel about right…<img src="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/images/google_auto_suggestions.png" class="img-fluid"></p>
<p>Applying for jobs is both exhausting <em>and</em> a full-time job. Data scientists are expected to stay on top of their statistical education, have robust GitHub repositories, and they’re expected to know how to apply ever-changing technologies for DevOps and reproducible machine learning… And all this in addition to the endless resume and cover letter tweaks you’re probably making for each job (as you should).</p>
<p>If you’re an academic wanting to pivot to industry or a data scientist looking for a job change, I propose a simple (<em>or simple-ish</em>) solution to standing out from the crowd in a meaningful way: Build an easy-to-access web application for your future employers.</p>
<p>In July 2022, <a href="https://www.linkedin.com/in/travisgerke/">Travis Gerke</a> held a presentation at rstudio::conf(2022) titled <a href="https://www.rstudio.com/conference/2022/talks/wtf-teach-you-industry/"><em>What They Forgot to Teach You About Industry Transitions from Academia</em></a> (or “WTF-AITA” 😬). Travis presented my Shiny app as a “cover letter accessory” and it motivated me to write this tutorial such that anybody familiar with R, GitHub, and the RStudio IDE could deploy their own demo Shiny app for prospective employers.</p>
</section>
<section id="steps-to-edit-the-app" class="level1">
<h1>Steps to Edit the App</h1>
<p>To ensure a smooth installation, I recommend installing the latest version of R (4.2.2) and the latest version of the RStudio IDE. The underlying R scripts used on this Shiny app rely on the native pipe operator (<code>|&gt;</code>) introduced in base R 4.1.0. If you do not have access to the native pipe, you will not be able to run the app without additional code tweaks, i.e., replacing all instances of the native pipe (<code>|&gt;</code>) with the <code>magrittr</code> pipe (<code>%&gt;%</code>).</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">
<span class="screen-reader-only">Warning</span>Git for the R User
</div>
</div>
<div class="callout-body-container callout-body">
<p>The next steps assume that you’re familiar with GitHub, you’ve installed R + <a href="https://github.com/git-guides/install-git">Git</a> + the <a href="https://posit.co/download/rstudio-desktop/">RStudio IDE</a>, and you’ve connected RStudio to GitHub. Jenny Bryan’s <a href="https://happygitwithr.com/rstudio-git-github.html"><em>Happy Git and GitHub for the useR</em></a> is a great resource if you’re stuck on any of these topics.</p>
</div>
</div>
<section id="clone-repo" class="level2">
<h2 class="anchored" data-anchor-id="clone-repo">1: Clone Repo</h2>
<p>We’ll start by forking this GitHub repo into your repository to allow you to experiment with my code without actually changing anything in my original project repo. To do this, navigate to my GitHub repo ( <a href="https://github.com/JavOrraca/bslib_demo_shiny">JavOrraca/bslib_demo_shiny</a>) and click on the <code>Fork</code> button in the top-right of the repo. This will prompt you to “Create a new fork” - Typically, I like to keep all of the defaults intact. When you’re ready to create the new fork, simply hit the <code>Create fork</code> button on the bottom of the screen.</p>
<p>Assuming you kept the original repository name, you should see a newly created repo titled “bslib_demo_shiny” in your GitHub repositories. It contains a full copy of all files and folders found in my original project repo.</p>
<p>Next, you’ll want to clone this repo to your local machine. Using the RStudio IDE, navigate to its menu bar and click on <code>File</code> -&gt; <code>New Project</code> -&gt; <code>Version Control</code> -&gt; <code>Git</code>, copy/paste your repo’s clone URL, choose your local project directory path of choice, and click on <code>Create Project</code>.</p>
<p>RStudio IDE’s <em>New Project Wizard</em> will launch in a popup window after selecting <code>New Project</code> from the file menu. You should expect the following:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/images/clone_git_repo.gif" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:65.0%"></p>
</figure>
</div>
<p>This process will create a local clone of all files and folders found on your remote GitHub repo. Once all files are downloaded for this project, the files and folder structure will look like this:</p>
<p>├── Dockerfile <br>├── LICENSE <br>├── README.md <br>├── bslib_demo_shiny.Rproj <br>├── global.R <br>├── helpers <br>│ ├── custom_theme.R <br>│ ├── footer.R <br>│ └── navbar.R <br>├── modules <br>│ ├── mod_About.R <br>│ ├── mod_Mapping.R <br>│ ├── mod_Overview.R <br>│ └── mod_Reports.R <br>├── renv <br>│ ├── activate.R <br>│ ├── library <br>│ │ └── R-4.2 <br>│ ├── sandbox <br>│ │ └── R-4.2 <br>│ ├── settings.dcf <br>│ └── staging <br>├── renv.lock <br>├── server.R <br>├── ui.R <br>├── www <br>│ ├── Shiny_Demo_Preview.png <br>│ ├── blr_logo-primary.png <br>│ └── styles.scss</p>
</section>
<section id="install-packages" class="level2">
<h2 class="anchored" data-anchor-id="install-packages">2: Install Packages</h2>
<p>It’s important to make note of the <code>renv</code> folder. This Shiny app relies on the <a href="https://rstudio.github.io/renv/articles/renv.html"><code>renv</code></a> package to manage project-package dependencies. If the Shiny app requires different package versions than those installed on your system’s main R library path, this approach will install the necessary packages in the <code>renv</code> directory.</p>
<p>To install the required R packages for this app, follow the instructions presented in your RStudio IDE Console immediately after cloning the GitHub repo or execute the command <code>renv::restore()</code>. This process will restore the package state as recorded in the <code>renv.lock</code> file - This “lockfile” records all of the R package names and versions required to run this specific Shiny app.</p>
<p>Once the project-package dependencies are installed, test launch the Shiny app by running <code>shiny::runApp()</code> in the Console or by clicking on the <code>Run App</code> button in the RStudio IDE. The <code>Run App</code> button only appears if you’re inside the <code>global.R</code>, <code>ui.R</code>, and <code>server.R</code> files - These three filenames are unique to Shiny and the RStudio IDE recognizes them as such.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/images/run_app.gif" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
</section>
<section id="app-customization" class="level2">
<h2 class="anchored" data-anchor-id="app-customization">3: App Customization</h2>
<p>While there are many ways to configure a Shiny app, the global aesthetics of this app have been configured using the <a href="https://rstudio.github.io/bslib/"><code>bslib</code></a> and <a href="https://rstudio.github.io/thematic/"><code>thematic</code></a> packages. These packages provide tools for customizing Bootstrap themes and they introduce Sass variables to Shiny (in my opinion, making the customization process much more unified).</p>
<p>The default Bootstrap version for <code>bslib</code> is version 5, however, this app was designed using Bootstrap 4. The <code>thematic</code> package provides a centralized approach to styling certain R visualizations such as those generated from <code>ggplot2</code>.</p>
<p>To fully customize the color scheme and overall theme of the app to your liking, make revisions to the following files that are sourced at runtime:</p>
<section id="helperscustom_theme.r" class="level3">
<h3 class="anchored" data-anchor-id="helperscustom_theme.r"><code>helpers/custom_theme.R</code></h3>
<p>This helper file creates two functions (<code>fn_custom_theme()</code> and <code>fn_thematic_theme()</code>) that are loaded into your global environment at runtime. Here you can specify base fonts, background (<code>bg</code>) and foreground (<code>fg</code>) colors, and other accent colors used throughout the Shiny app. The <code>fn_custom_theme()</code> function is called in line 3 of <code>ui.R</code> (in the app’s root directory) and the <code>fn_thematic_theme()</code> function is called in line 56 of <code>modules/mod_Overview.R</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Overarching bslib theme</span></span>
<span id="cb1-2">fn_custom_theme <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb1-3">  bslib<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;">bs_theme</span>(</span>
<span id="cb1-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">version =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4"</span>,</span>
<span id="cb1-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_font =</span> sass<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;">font_google</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open Sans"</span>),</span>
<span id="cb1-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffffff"</span>,</span>
<span id="cb1-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1d2d42"</span>,</span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">primary =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f3d436"</span>, </span>
<span id="cb1-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">secondary =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1d2d42"</span>,</span>
<span id="cb1-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">success =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1d2d42"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bs_add_variables</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"border-bottom-width"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6px"</span>,</span>
<span id="cb1-12">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"border-color"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"$primary"</span>,</span>
<span id="cb1-13">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.where =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"declarations"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb1-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bs_add_rules</span>(sass<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;">sass_file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"www/styles.scss"</span>))</span>
<span id="cb1-15">}</span>
<span id="cb1-16"></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Thematic theme for ggplot2 outputs</span></span>
<span id="cb1-18">fn_thematic_theme <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {</span>
<span id="cb1-19">  thematic<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;">thematic_theme</span>(</span>
<span id="cb1-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffffff"</span>,</span>
<span id="cb1-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1d2d42"</span>,</span>
<span id="cb1-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accent =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f3d436"</span>,</span>
<span id="cb1-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">font =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">font_spec</span>(sass<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;">font_google</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open Sans"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scale =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.75</span>)</span>
<span id="cb1-24">  )</span>
<span id="cb1-25">}</span></code></pre></div></div>
</div>
</section>
<section id="helpersfooter.r" class="level3">
<h3 class="anchored" data-anchor-id="helpersfooter.r"><code>helpers/footer.R</code></h3>
<p>This file creates the <code>fn_footer()</code> helper function that powers the Shiny app’s footer. The <code>fn_footer()</code> function uses Shiny <code>tags</code> in conjunction with <code>htmltools::HTML()</code> (exported to the <code>shiny</code> package) to create an HTML footer tag that can process raw HTML. I’ve used it to include my name and website in the footer and the <code>fn_footer()</code> function is called in line 7 of <code>ui.R</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb2-1">fn_footer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb2-2">  tags<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">footer</span>(</span>
<span id="cb2-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HTML</span>(</span>
<span id="cb2-4">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This web app demo was developed by &lt;a href='https://www.javierorracadeatcu.com'&gt;Javier Orraca-Deatcu&lt;/a&gt; with R + Shiny"</span></span>
<span id="cb2-5">    )</span>
<span id="cb2-6">  )</span>
<span id="cb2-7">}</span></code></pre></div></div>
</div>
</section>
<section id="helpersnavbar.r" class="level3">
<h3 class="anchored" data-anchor-id="helpersnavbar.r"><code>helpers/navbar.R</code></h3>
<p>This script sets the navigation bar’s branding (logo and hyperlink) and wraps it into the helper function <code>fn_navbar()</code>. This function is passed to the <code>title</code> argument on line 5 of <code>ui.R</code> and it creates an HTML <code>div</code> with a custom <code>class</code> attribute (<code>bloomreach_logo</code>) that can be further styled with CSS in the <code>www/styles.scss</code> “Sassy CSS” file.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>About Shiny’s www folder
</div>
</div>
<div class="callout-body-container callout-body">
<p>The image path in line 5, below, does not specify a full path despite this image existing in the <code>www</code> folder. In Shiny projects, the <code>www</code> directory is used for storing elements that will be rendered in the web browser and not from script outputs. We do not need to include the full path on line 5 of <code>helpers/navbar.R</code> as Shiny knows to look for <code>blr_logo-primary.png</code> in the <code>www</code> folder.</p>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb3-1">fn_navbar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(){</span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">div</span>(</span>
<span id="cb3-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bloomreach_logo"</span>,</span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">a</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">href =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.bloomreach.com/"</span>,</span>
<span id="cb3-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">img</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blr_logo-primary.png"</span>,</span>
<span id="cb3-6">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bloomreach"</span>)</span>
<span id="cb3-7">    )</span>
<span id="cb3-8">  )</span>
<span id="cb3-9">}</span></code></pre></div></div>
</div>
</section>
<section id="modulesabout.r" class="level3">
<h3 class="anchored" data-anchor-id="modulesabout.r"><code>modules/About.R</code></h3>
<p>This script defines the UI and Server functions used to render the <em>About</em> page on the Shiny app. Technically, the <em>Overview</em>, <em>Mapping</em>, <em>Reports</em>, and <em>About</em> pages of the Shiny app are tabs toggled with <code>shiny::tabPanel()</code>. Now that you’ve had some exposure to <code>htmltools::HTML()</code>, you should be able to revise the raw HTML in this script to build your own <em>About</em> page. It’s fairly straightforward so I won’t show this module in a code chunk - Please reach out to me directly or in the comments section if you have any questions.</p>
</section>
</section>
<section id="example-tweaks" class="level2">
<h2 class="anchored" data-anchor-id="example-tweaks">4: Example Tweaks</h2>
<p>Now that we’ve covered the files in your Shiny project that need to be updated if you want to deploy your own custom-styled demo Shiny app, let’s pivot to a real world example. Let’s say our name is Steve Wozniak and we want to design a Shiny app that invokes a retro Apple feel… After finding a decent logo to use and saving it to <code>www/retro_apple_logo.png</code>, below are the steps that we would need to follow to update our app:</p>
<ul>
<li><code>helpers/custom_theme.R</code>
<ul>
<li>Line 5: <code>base_font = sass::font_google("Press Start 2P", local = FALSE, display = "swap"),</code></li>
<li>Line 8: <code>primary = "#01c700",</code></li>
<li>Line 22: <code>accent = "#01c700",</code></li>
</ul></li>
<li><code>helpers/footer.R</code>
<ul>
<li>Line 4: <code>"This web app demo was developed by &lt;a href='http://www.woz.org/'&gt;Steve Wozniak&lt;/a&gt; with R + Shiny"</code></li>
</ul></li>
<li><code>helpers/navbar.R</code>
<ul>
<li>Line 3: <code>class = "retro_apple",</code></li>
<li>Line 4: <code>a(href = "http://www.apple.com/",</code></li>
<li>Line 5: <code>img(src = "retro_apple_logo.png",</code></li>
<li>Line 6: <code>title = "Apple")</code></li>
</ul></li>
</ul>
<p>Let’s give this app a test run and see how it changed the appearance:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/images/run_revised_app.gif" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
<p>So far, so good! We’ve got a retro logo (re-sized to fit in the top-left of the navbar), a retro font (pulled from Google Fonts), and the primary and accent colors have been updated from yellow to neon green. There are several additional edits we <em>could</em> make but for now, I like it.</p>
</section>
<section id="app-deployment" class="level2">
<h2 class="anchored" data-anchor-id="app-deployment">5: App Deployment</h2>
<p>Now we’re ready to publish your newly revised Shiny app. We’ll use the <a href="https://www.shinyapps.io/">shinyapps.io</a> platform by Posit to push-button deploy our app from the RStudio IDE. For our needs, a free account will suffice as the free tier allows up to 5 Shiny app deployments with a combined 25 monthly active hours of usage. It’s important to note that if someone launches your app and keeps it running in a tab, this is considered “active” runtime, so encourage your shinyapps.io users to close the browser window when they’re done using your app.</p>
<p>If you do not have a shinyapps.io account or you have not synced your RStudio IDE to shinyapps.io via security token, please review the shinyapps.io <a href="https://docs.posit.co/shinyapps.io/getting-started.html#working-with-shiny-for-r">Getting Started</a> documentation. Once you’re set up, it will be a breeze to publish your app. If you’re stuck, again feel free to reach out to me directly.</p>
<p>Similar to the way that the <code>Run App</code> button will present itself if you’re inside the <code>global.R</code>, <code>ui.R</code>, or <code>server.R</code> files, you’ll also see a blue sync-looking button that gives you the power of push-button deployment from within the RStudio IDE:</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/images/publish_button.png" class="img-fluid"> <br>Clicking that button will open a new window to help you publish your app to the shinyapps.io servers. The title you select will be the name of the application on your shinyapps.io dashboard and it will also be included in the URL of your app. For example, I named my app “Bloomreach_Shiny_App” so the URL to visit my app is <a href="https://javierorraca.shinyapps.io/Bloomreach_Shiny_App/">https://javierorraca.shinyapps.io/Bloomreach_Shiny_App/</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/images/publish_to_server.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:75.0%"></p>
</figure>
</div>
<p>If you use a title that already exists in your shinyapps.io dashboard, you will force overwrite the prior app with the same name, so take caution when publishing. More often than not, it’s desired to overwrite an app so that you can regularly push updates to production to the same URL, however, there are instances where this behavior would not be desired. Regardless of your publishing intention, RStudio IDE will provide a warning to confirm that you want to write over a shinyapps.io application with the same name.</p>
<p>Since the <code>Dockerfile</code> is not relevant to this project deployment, I recommend un-checking <code>Dockerfile</code> from the list of file from which to publish. Once you’re ready, click on <code>Publish</code> and a new tab, <em>Deploy</em>, will be visible in the bottom-left of your RStudio IDE. This process takes several minutes since shinyapps.io will 1) install an R image with all the project-package dependencies needed to run your Shiny app, and 2) build and deploy the Shiny app itself. Once publishing is complete, you should see something like this:</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/images/deploy_pane.png" class="img-fluid quarto-figure quarto-figure-center" style="width:85.0%"> <br></p>
</section>
<section id="git-commit-push" class="level2">
<h2 class="anchored" data-anchor-id="git-commit-push">6: Git Commit &amp; Push</h2>
<p>Now that you’ve successfully published your application from the RStudio IDE, wrap up your changes and commit / push them back to your GitHub repo. There are several ways to do this (GitHub Desktop app, Terminal, Windows Powershell, etc.) but I’ve found the easiest to be within the RStudio IDE. First, you’ll want to commit (or save) a local snapshot of the edits you’ve made from the remote GitHub repo. Since this commit is saved locally, you’ll also need to push this commit back to your GitHub repo.</p>
<p>Under the <em>Git</em> pane, click on <code>Commit</code> to open a window that allows you to inspect the files that were modified, added, deleted, etc. The quickest way to stage all files for commit is to click on any of the filenames you edited in the top-left pane of the commit popup window, select all files using the <em>Select All</em> keystroke (Ctrl+A on Windows or Cmd+A on Mac), then click on the <code>Stage</code> button. Write a message in the blank top-right message box for this commit - Keep it succinct about the file(s) changes that you’ve made. Click on the <code>Commit</code> button under the message box and this saves a local snapshot of the changes you’ve made.</p>
<p>To push the changes back to GitHub, click on the green <code>Push</code> button in the top-right of the commit window and this will push your locally committed changes to your remote GitHub repo. Now, in addition to the live app that you’ve deployed, you’ll also be able to direct end users to your GitHub repo with your app’s source code.</p>
</section>
</section>
<section id="recap-next-steps" class="level1">
<h1>Recap &amp; Next Steps</h1>
<p>It didn’t feel like it but if you’re newer to Shiny, we did a lot! Let’s recap everything we ran through in this tutorial:</p>
<ol type="1">
<li>From GitHub, you forked my original project repo to your account</li>
<li>You cloned your remote GitHub repo to your local machine using the RStudio IDE</li>
<li>You used the <code>renv</code> package to install specific package versions as recorded on the <code>renv.lock</code> lockfile</li>
<li>You customized the Shiny app by tweaking files in the <code>helpers/</code> and <code>modules/</code> folders</li>
<li>You deployed your app to shinyapps.io from the RStudio IDE</li>
<li>You pushed your changes back to your public GitHub repo so others can easily review your source code</li>
</ol>
<p>You’ve done a lot! If you’re stuck on any of the above, please do reach out either on LinkedIn, or Twitter, or by using the Comments section below this post. I won’t go into details of Docker on this blog post but in the future, I’d like to make a follow-up post to show you how to build your own Docker image from the <code>Dockerfile</code> and push it to a public Docker Hub repo… With this approach, you have many more options for deploying your Shiny apps on your server or virtual machines of choice.</p>
</section>
<section id="shiny-session-info" class="level1">
<h1>Shiny Session Info</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">R version <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022-10-31</span>)</span>
<span id="cb4-2">Platform<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> aarch64<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>apple<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;">darwin20</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>bit)</span>
<span id="cb4-3">Running under<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> macOS Ventura <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span></span>
<span id="cb4-4"></span>
<span id="cb4-5">Matrix products<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> default</span>
<span id="cb4-6">LAPACK<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/</span>Library<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Frameworks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>R.framework<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Versions<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>arm64<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Resources<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lib<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>libRlapack.dylib</span>
<span id="cb4-7"></span>
<span id="cb4-8">locale<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb4-9">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>C<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span></span>
<span id="cb4-10"></span>
<span id="cb4-11">attached base packages<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb4-12">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] stats     graphics  grDevices datasets  utils     methods   base     </span>
<span id="cb4-13"></span>
<span id="cb4-14">other attached packages<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb4-15"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] rsconnect_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.28</span>   leaflet_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>      DT_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.26</span>            thematic_0.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>  </span>
<span id="cb4-16"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] bslib_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.1</span>        shinyWidgets_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.4</span> scales_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>       showtext_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.9</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-5</span>    </span>
<span id="cb4-17"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>] showtextdb_3<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>     sysfonts_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.8</span>     ggplot2_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.6</span>      purrr_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>       </span>
<span id="cb4-18">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>] dplyr_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>       rlang_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>        shiny_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.3</span>       </span>
<span id="cb4-19"></span>
<span id="cb4-20">loaded via a <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">namespace</span> (and not attached)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb4-21"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] tidyselect_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>        splines_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>           lattice_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.20</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-45</span>        </span>
<span id="cb4-22"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] leaflet.providers_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.0</span> colorspace_2<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-3</span>        vctrs_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>            </span>
<span id="cb4-23"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] generics_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.3</span>          htmltools_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.3</span>         yaml_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.6</span>             </span>
<span id="cb4-24">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>] mgcv_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.8</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-41</span>             utf8_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>              later_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>            </span>
<span id="cb4-25">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>] pillar_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.1</span>            jquerylib_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>         glue_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.2</span>             </span>
<span id="cb4-26">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>] withr_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>             lifecycle_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>         munsell_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>          </span>
<span id="cb4-27">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>] gtable_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.1</span>            fontawesome_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>       htmlwidgets_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.4</span>      </span>
<span id="cb4-28">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>] memoise_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>           labeling_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>          fastmap_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>          </span>
<span id="cb4-29">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>] httpuv_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.6</span>            crosstalk_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>         fansi_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>            </span>
<span id="cb4-30">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>] Rcpp_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>              xtable_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.8</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-4</span>            renv_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16.0</span>            </span>
<span id="cb4-31">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>] promises_1.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>        cachem_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>            jsonlite_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.3</span>         </span>
<span id="cb4-32">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">34</span>] mime_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.12</span>               farver_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>            fs_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.2</span>               </span>
<span id="cb4-33">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>] digest_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.30</span>           grid_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>              cli_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.1</span>              </span>
<span id="cb4-34">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>] tools_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>             magrittr_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>          sass_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>             </span>
<span id="cb4-35">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">43</span>] tibble_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8</span>            pkgconfig_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>         Matrix_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>           </span>
<span id="cb4-36">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">46</span>] ellipsis_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>          rstudioapi_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.14</span>         R6_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.1</span>               </span>
<span id="cb4-37">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">49</span>] nlme_3<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-160</span>            compiler_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>              </span></code></pre></div></div>
</div>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>shiny</category>
  <category>github</category>
  <category>bslib</category>
  <category>docker</category>
  <guid>https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/2022-11-15-shiny-bslib-demo.html</guid>
  <pubDate>Tue, 15 Nov 2022 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2022-11-15-shiny-bslib-demo/images/Shiny_Demo_Preview.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>Torch for R + luz</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2021-10-21-torch-for-r/2021-10-21-torch-for-r.html</link>
  <description><![CDATA[ 





<p>Torch for R… wow! 🔥🔥🔥 I was recently having a discussion with a coworker about the benefits of Torch, especially the power of training one global model capable of hierarchical projections (awesome for time series) and predicting multiple group-specific regressions. I went down a Googling rabbit hole last weekend and came across some amazing articles by Sigrid Keydana (see links below) introducing <code>torch</code> to the R community and also recently releasing <code>luz</code>, a high-level R interface to Torch (“<code>luz</code> is to <code>torch</code> what <code>Keras</code> is to <code>TensorFlow</code>”).</p>
<p>RStudio’s MLVerse team is doing really exciting things for the R machine learning and AI community. With <code>torch</code>, I no longer need to launch a conda environment for complex NNs (although having Python on your system is always handy 😅). And even better, “<code>torch</code> for R is built directly on top of <code>libtorch</code>, a C++ library that provides the tensor-computation and automatic-differentiation capabilities essential to building neural networks.” If you’re looking for fast NNs and deep learning solutions within the <strong>#rstats</strong> framework, give these packages a try. Happy Friday and happy learning! 🤓📚</p>
<section id="sources" class="level2">
<h2 class="anchored" data-anchor-id="sources">Sources</h2>
<ul>
<li>Torch for R: <a href="https://torch.mlverse.org/">https://torch.mlverse.org/</a></li>
<li>Intro to Torch: <a href="https://blogs.rstudio.com/ai/posts/2020-09-29-introducing-torch-for-r/">Torch for R</a></li>
<li>Intro to Luz: <a href="https://blogs.rstudio.com/ai/posts/2021-06-17-luz/">Que haja luz: More light for torch!</a></li>
<li>Intro to TabNet: <a href="https://blogs.rstudio.com/ai/posts/2021-02-11-tabnet/">TabNet for Tabular Data with Torch</a></li>
<li>Intro to Time Series with Torch: <a href="https://blogs.rstudio.com/ai/posts/2021-03-10-forecasting-time-series-with-torch_1/">Introductory Time-Series Forecasting with Torch</a></li>
<li>Sigrid Keydana: <a href="https://www.linkedin.com/in/sigrid-keydana-9a16b410/?originalSubdomain=de">Sigrid on LinkedIn</a></li>
</ul>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>machine learning</category>
  <guid>https://www.javierorracadeatcu.com/posts/2021-10-21-torch-for-r/2021-10-21-torch-for-r.html</guid>
  <pubDate>Fri, 22 Oct 2021 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2021-10-21-torch-for-r/images/torch_and_luz.png" medium="image" type="image/png" height="91" width="144"/>
</item>
<item>
  <title>Beautiful Maps with Rayshader</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/2021-09-07-rayshader.html</link>
  <description><![CDATA[ 





<section id="overview" class="level1">
<h1>Overview</h1>
<p>R, <code>rayshader</code>, OpenStreetMap… Lots of amazing learning to be had with these mapping tools. I’m following Tyler Morgan-Wall PhD’s <a href="https://www.tylermw.com/adding-open-street-map-data-to-rayshader-maps-in-r/">Adding Open Street Map Data to Rayshader Maps in R</a> tutorial and learning a lot about the development of beautiful maps with R. Laguna Beach is one of my favorite cities in the USA (<em>and the world!</em>) so I applied the concepts from this tutorial to building 2D and 3D maps of Laguna Beach. My work-in-progress and screen captures are below.</p>
<p>Tyler Morgan-Wall PhD (TMW) authored the <code>rayshader</code> package and his step-by-step tutorial is both easy to follow and incredibly informative. If you get stuck (<em>more like, “when you get stuck”</em>), you can almost always find an answer on the <code>rayshader</code> website or GitHub repo (excellent technical documentation and code examples).</p>
<p>One last caveat before I dive into my code - Finding high-quality LIDAR or Digital Elevation Models (DEMs) for United States cities is <em>not easy</em>. To apply the steps below to your location of choice, you’ll have to get creative about cropping LIDAR/DEMs to zoom into the geography that you want to model (more info on cropping in the sections below).</p>
</section>
<section id="setup" class="level1">
<h1>Setup</h1>
<p>Where do you start? Find yourself a high-resolution DEM or LIDAR capture of a location that you love. The team at <a href="https://shadedrelief.com/">Shaded Relief</a> has some high-res DEMs for public consumption: <a href="http://shadedrelief.com/SampleElevationModels/">http://shadedrelief.com/SampleElevationModels/</a></p>
<p>My starting point was a LIDAR capture of Laguna Canyon in California. This geo-data, made available by the United States Geological Survey’s <a href="https://ngmdb.usgs.gov/topoView/">National Geologic Map Database</a>, was captured in 2018 as part of a study on Southern California wildfires. The LIDAR image (captured as a TIF file) includes three variables per observation: latitude, longitude, and elevation. This Laguna Canyon LIDAR image captures these data points in one meter increments (hence the 257 MB file size). You can download this TIF from my Google Drive here: <a href="https://drive.google.com/file/d/1IfdYa0nYYk7_n1McV5IRhgk-rXJtLA9P/view?usp=sharing">https://drive.google.com/Javier/USGS_TIF</a></p>
<p>Start by installing the development versions of the <code>rayshader</code> and <code>rayimage</code> packages, and loading several others packages used in this project. I run Ubuntu and experienced some dependency issues, at least initially. If you can Google Search, you can figure out the additional Linux libraries that you might need (my apologies for not documenting this better! - I worked on this code in March but just now getting around to documenting my workflow… <em>in September!</em>).</p>
<section id="libraries" class="level2">
<h2 class="anchored" data-anchor-id="libraries">Libraries</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install dev packages</span></span>
<span id="cb1-2">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tylermorganwall/rayshader"</span>)</span>
<span id="cb1-3">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tylermorganwall/rayimage"</span>)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load libraries</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rayshader)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(osmdata)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Source elevation data</span></span>
<span id="cb1-14">laguna <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> raster<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;">raster</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./USGS_one_meter_x42y372_CA_SoCal_Wildfires_B1_2018.tif"</span>)</span></code></pre></div></div>
</div>
</section>
<section id="eda" class="level2">
<h2 class="anchored" data-anchor-id="eda">EDA</h2>
<p>To better explore the data, lets begin by visualizing the elevation data with <code>rayshader</code> defaults to understand the topography that we are working with. Before we can render this visual, we need to transform the <strong>laguna</strong> data object (assigned above) to a matrix. For faster rendering and prototyping, you can also resize the matrix using <code>rayshader::resize_matrix()</code>. This resizing will downscale your full dataset using bilinear interpolation.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transform the spatial data structure into a regular R matrix</span></span>
<span id="cb2-2">laguna_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rayshader<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;">raster_to_matrix</span>(laguna)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create small version of matrix for quick visual prototyping</span></span>
<span id="cb2-5">laguna_small <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rayshader<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;">resize_matrix</span>(laguna_mat, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot base map using basic color defaults</span></span>
<span id="cb2-8">laguna_small <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-9">  rayshader<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;">height_shade</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-10">  rayshader<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;">plot_map</span>()</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/images/laguna_lidar.png" class="img-fluid figure-img"></p>
<figcaption>USGS Laguna Canyon LIDAR</figcaption>
</figure>
</div>
</section>
<section id="cropping" class="level2">
<h2 class="anchored" data-anchor-id="cropping">Cropping</h2>
<p>I wanted to focus on downtown Laguna Beach but this only represents a small fraction of the raw LIDAR. To get the desired lat-long range of downtown Laguna Beach, I used Google Maps to get the endpoints of my desired box. To accomplish this cropping, below, we relied on a custom cropping function that TMW shared on the <code>rayshader</code> site.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Crop to fit desired box around downtown Laguna Beach</span></span>
<span id="cb3-2">lat_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">33.541370925562376</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">33.55945552155357</span>)</span>
<span id="cb3-3">long_range <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">117.79622401045448</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">117.77493476466955</span>)</span>
<span id="cb3-4"></span>
<span id="cb3-5">convert_coords <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(lat, long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CRS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"+init=epsg:4326"</span>), to) {</span>
<span id="cb3-6">  data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">long =</span> long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> lat)</span>
<span id="cb3-7">  sp<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;">coordinates</span>(data) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">~</span> long <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> lat</span>
<span id="cb3-8">  sp<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;">proj4string</span>(data) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> from</span>
<span id="cb3-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to coordinate system specified by EPSG code</span></span>
<span id="cb3-10">  xy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(sp<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;">spTransform</span>(data, to))</span>
<span id="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(xy) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>)</span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(xy))</span>
<span id="cb3-13">}</span>
<span id="cb3-14"></span>
<span id="cb3-15">raster<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;">crs</span>(laguna)</span>
<span id="cb3-16"></span>
<span id="cb3-17">utm_bbox <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">convert_coords</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> lat_range,</span>
<span id="cb3-18">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">long =</span> long_range,</span>
<span id="cb3-19">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crs</span>(laguna))</span>
<span id="cb3-20"></span>
<span id="cb3-21">raster<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;">extent</span>(laguna)</span>
<span id="cb3-22"></span>
<span id="cb3-23">extent_zoomed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> raster<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;">extent</span>(utm_bbox[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], utm_bbox[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], </span>
<span id="cb3-24">                                utm_bbox[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], utm_bbox[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])h</span>
<span id="cb3-25"></span>
<span id="cb3-26">laguna_zoom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> raster<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;">crop</span>(laguna, extent_zoomed)</span>
<span id="cb3-27"></span>
<span id="cb3-28">laguna_zoom_mat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rayshader<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;">raster_to_matrix</span>(laguna_zoom)</span></code></pre></div></div>
</div>
</section>
</section>
<section id="d-map" class="level1">
<h1>2D Map</h1>
<section id="base-map" class="level2">
<h2 class="anchored" data-anchor-id="base-map">Base Map</h2>
<p>I was inspired by the relief shading work of Eduard Imhof, Swiss cartographer and mapping visionary. In particular, I loved the blueish-pink palette that Imhof popularized. When you get to this step, try adjusting the settings below to your liking. We will continue to build on top of this initial base map layer.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">base_map <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laguna_zoom_mat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-2">  rayshader<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;">height_shade</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-3">  rayshader<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;">add_overlay</span>(</span>
<span id="cb4-4">    rayshader<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;">sphere_shade</span>(</span>
<span id="cb4-5">      laguna_zoom_mat, </span>
<span id="cb4-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">texture =</span> rayshader<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;">create_texture</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f5dfca"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#63372c"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#dfa283"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#195f67"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#c2d1cf"</span>,</span>
<span id="cb4-7">                                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cornercolors =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffc500"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#387642"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d27441"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#296176"</span>)),</span>
<span id="cb4-8">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sunangle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, </span>
<span id="cb4-9">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colorintensity =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-10">  rayshader<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;">add_shadow</span>(rayshader<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;">lamb_shade</span>(laguna_zoom_mat), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-11">  rayshader<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;">add_overlay</span>(</span>
<span id="cb4-12">    rayshader<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;">generate_altitude_overlay</span>(</span>
<span id="cb4-13">      rayshader<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;">height_shade</span>(laguna_zoom_mat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">texture =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#91aaba"</span>),</span>
<span id="cb4-14">      laguna_zoom_mat,</span>
<span id="cb4-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_transition =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(laguna_zoom_mat)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>,</span>
<span id="cb4-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end_transition =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(laguna_zoom_mat)))</span>
<span id="cb4-17"></span>
<span id="cb4-18">rayshader<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;">plot_map</span>(base_map)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/images/base_map.png" class="img-fluid figure-img"></p>
<figcaption>Laguna Beach base map a la Eduard Imhof</figcaption>
</figure>
</div>
</section>
<section id="openstreetmap" class="level2">
<h2 class="anchored" data-anchor-id="openstreetmap">OpenStreetMap</h2>
<p>OpenStreetMap (OSM) offers an open-source, editable geographic database of the world and the underlying data includes details for roads, buildings, hiking paths, rivers, etc. We’ll leverage the <code>osmdata</code> package for R to import OSM data (and utilizing the <code>overpass</code> API). The <code>osmdata</code> package processes routines in C++ for fast construction and loading into R. We’ll first load several layers of OSM details. Subsequently, we’ll convert those <code>sf</code> objects to the coordinate system we’re working with for this <code>rayshader</code> project.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">osm_bbox <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(long_range[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],lat_range[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], long_range[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],lat_range[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb5-2"></span>
<span id="cb5-3">laguna_highway <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> osmdata<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;">opq</span>(osm_bbox) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-4">  osmdata<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;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"highway"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-5">  osmdata<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;">osmdata_sf</span>() </span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transform coordinates to new projection</span></span>
<span id="cb5-8">laguna_lines <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sf<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;">st_transform</span>(laguna_highway<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_lines,</span>
<span id="cb5-9">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> raster<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;">crs</span>(laguna))</span>
<span id="cb5-10"></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View streets as a ggplot2 render </span></span>
<span id="cb5-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(laguna_lines, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> osm_id)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Open Street Map `highway` attribute in Laguna Beach"</span>)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/images/OSM_ggplot2_render.png" class="img-fluid figure-img"></p>
<figcaption>OSM render of Laguna Beach roads and highways using ggplot2</figcaption>
</figure>
</div>
</section>
<section id="layering" class="level2">
<h2 class="anchored" data-anchor-id="layering">Layering</h2>
<p>To begin, lets simply layer all the “highway” OSM attributes on top of our base map using white lines to represent the lines.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transform sf LINESTRING geometry and create semi-transparent overlay</span></span>
<span id="cb6-2">base_map <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">  rayshader<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;">add_overlay</span>(</span>
<span id="cb6-4">    rayshader<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;">generate_line_overlay</span>(</span>
<span id="cb6-5">      laguna_lines, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extent =</span> extent_zoomed,</span>
<span id="cb6-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb6-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heightmap =</span> laguna_zoom_mat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-8">  rayshader<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;">plot_map</span>()</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/images/base_map_layer1.png" class="img-fluid figure-img"></p>
<figcaption>Base Map Layer 1</figcaption>
</figure>
</div>
<p>I want different styles for different OSM lines so let’s continue by creating several groups of OSM lines.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Subset layers of laguna_lines</span></span>
<span id="cb7-2">laguna_trails <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laguna_lines <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-3">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(highway <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bridleway"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steps"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"track"</span>))</span>
<span id="cb7-4"></span>
<span id="cb7-5">laguna_footpaths <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laguna_lines <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-6">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(highway <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"footway"</span>))</span>
<span id="cb7-7"></span>
<span id="cb7-8">laguna_roads <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> laguna_lines <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-9">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(highway <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unclassified"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primary"</span>, </span>
<span id="cb7-10">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primary_link"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"secondary"</span>, </span>
<span id="cb7-11">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tertiary"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"residential"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"service"</span>))</span>
<span id="cb7-12"></span>
<span id="cb7-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create one encompassing layer with independent element styling</span></span>
<span id="cb7-14">trails_layer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb7-15">  rayshader<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;">generate_line_overlay</span>(</span>
<span id="cb7-16">    </span>
<span id="cb7-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pink footpaths</span></span>
<span id="cb7-18">    laguna_footpaths, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extent =</span> extent_zoomed,</span>
<span id="cb7-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>,</span>
<span id="cb7-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heightmap =</span> laguna_zoom_mat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-21">  </span>
<span id="cb7-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note: While barely visible in the final outputs, the following</span></span>
<span id="cb7-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># code adds an offset black shadow to the white dashed lines to</span></span>
<span id="cb7-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># represent the hiking trails around Laguna Beach </span></span>
<span id="cb7-25">  rayshader<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;">add_overlay</span>(</span>
<span id="cb7-26">    rayshader<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;">generate_line_overlay</span>(</span>
<span id="cb7-27">      laguna_trails, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extent =</span> extent_zoomed,</span>
<span id="cb7-28">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">offset =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb7-29">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heightmap =</span> laguna_zoom_mat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-30">  rayshader<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;">add_overlay</span>(</span>
<span id="cb7-31">    rayshader<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;">generate_line_overlay</span>(</span>
<span id="cb7-32">      laguna_trails, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extent =</span> extent_zoomed,</span>
<span id="cb7-33">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb7-34">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heightmap =</span> laguna_zoom_mat)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-35">  </span>
<span id="cb7-36">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># White roads</span></span>
<span id="cb7-37">  rayshader<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;">add_overlay</span>(</span>
<span id="cb7-38">    rayshader<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;">generate_line_overlay</span>(</span>
<span id="cb7-39">      laguna_roads, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extent =</span> extent_zoomed,</span>
<span id="cb7-40">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb7-41">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heightmap =</span> laguna_zoom_mat))</span></code></pre></div></div>
</div>
<p>We’ll create some additional projections of OSM layers.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">laguna_parking <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> osmdata<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;">opq</span>(osm_bbox) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-2">  osmdata<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;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"parking"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-3">  osmdata<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;">osmdata_sf</span>() </span>
<span id="cb8-4"></span>
<span id="cb8-5">laguna_building <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> osmdata<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;">opq</span>(osm_bbox) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-6">  osmdata<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;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"building"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-7">  osmdata<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;">osmdata_sf</span>()</span>
<span id="cb8-8"></span>
<span id="cb8-9">laguna_parking_poly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(laguna_parking<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_polygons, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crs</span>(laguna))</span>
<span id="cb8-10">laguna_building_poly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_transform</span>(laguna_building<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_polygons, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">crs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crs</span>(laguna))</span></code></pre></div></div>
</div>
</section>
<section id="final-touches" class="level2">
<h2 class="anchored" data-anchor-id="final-touches">Final Touches</h2>
<p>Now we’re ready to build upon our base map and see the 2D visual in action. Let’s create a polygon layer of the parking and building features, and adding the styled <strong>trails_layer</strong> on top of that.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">polygon_layer <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb9-2">  rayshader<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;">generate_polygon_overlay</span>(laguna_parking_poly, </span>
<span id="cb9-3">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extent =</span> extent_zoomed,</span>
<span id="cb9-4">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heightmap =</span> laguna_zoom_mat, </span>
<span id="cb9-5">                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey30"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-6">  rayshader<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;">add_overlay</span>(</span>
<span id="cb9-7">    rayshader<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;">generate_polygon_overlay</span>(laguna_building_poly, </span>
<span id="cb9-8">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extent =</span> extent_zoomed,</span>
<span id="cb9-9">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heightmap =</span> laguna_zoom_mat, </span>
<span id="cb9-10">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>))</span>
<span id="cb9-11"></span>
<span id="cb9-12">base_map <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-13">  rayshader<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;">add_overlay</span>(polygon_layer) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-14">  rayshader<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;">add_overlay</span>(trails_layer) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-15">  rayshader<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;">plot_map</span>()</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/images/2D_render.png" class="img-fluid figure-img"></p>
<figcaption>2D render of Laguna Beach… Success!</figcaption>
</figure>
</div>
</section>
</section>
<section id="d-map-1" class="level1">
<h1>3D Map</h1>
<section id="features" class="level2">
<h2 class="anchored" data-anchor-id="features">Features</h2>
<p>To render a 3D map, we follow a very similar pipeline to the 2D map but utilizing <code>rayshader::plot_3d()</code>. This function introduces new arguments and I spent much more time than I had anticipated in adjusting the zscale, zoom, rotation, pitch, and water specifications.</p>
<p>To exaggerate the elevation details in this area, I decided on a <code>zscale</code> &lt; 1 that would still resemble the reality and beauty of the Laguna Beach hills. Too low of a <code>zscale</code>, e.g., <code>zscale = 0.25</code>, was too exaggerated for my liking. Once the 3D viewer renders your visual, enter <code>rayshader::render_snapshot()</code> into your console to take a flat snapshot of your 3D rendering, saving your snapshot if desired.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">base_map <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-2">  rayshader<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;">add_overlay</span>(polygon_layer) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-3">  rayshader<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;">add_overlay</span>(trails_layer) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-4">  rayshader<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;">plot_3d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heightmap =</span> laguna_zoom_mat, </span>
<span id="cb10-5">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zscale =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fov =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, </span>
<span id="cb10-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zoom =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">phi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>,</span>
<span id="cb10-7">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">water =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">waterdepth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, </span>
<span id="cb10-8">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wateralpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">watercolor =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>,</span>
<span id="cb10-9">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">waterlinecolor =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">waterlinealpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb10-10">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">background =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>)</span>
<span id="cb10-11"></span>
<span id="cb10-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create 2D snapshot of 3D rendering</span></span>
<span id="cb10-13">rayshader<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;">render_snapshot</span>()</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/images/3D_render.png" class="img-fluid figure-img"></p>
<figcaption>3D render of Laguna Beach</figcaption>
</figure>
</div>
</section>
<section id="next-steps" class="level2">
<h2 class="anchored" data-anchor-id="next-steps">Next Steps</h2>
<p>I’ve seen some neat examples where developers add 3D polygons to their maps to represent residential housing, commercial properties, and public facilities. I recently read about methods for rendering much higher-resolution maps (this would be great for printing). Creating 3D flyovers with <code>rayshader</code> also looks surprisingly high-end for open-source software. These are all things I hope to revisit at some point in the near future. TMW knocked it out of the park with his package and tutorials, so I hope you find his content as useful as I did.</p>
</section>
<section id="session-info" class="level2">
<h2 class="anchored" data-anchor-id="session-info">Session Info</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">R version <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2021-08-10</span>)</span>
<span id="cb11-2">Platform<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> x86_64<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>linux<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;">gnu</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>bit)</span>
<span id="cb11-3">Running under<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Ubuntu <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">04.3</span> LTS</span>
<span id="cb11-4"></span>
<span id="cb11-5">Matrix products<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> default</span>
<span id="cb11-6">BLAS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>   <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/</span>usr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lib<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>x86_64<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>linux<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>gnu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>blas<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>libblas.so.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.0</span></span>
<span id="cb11-7">LAPACK<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/</span>usr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lib<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>x86_64<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>linux<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>gnu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>lapack<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>liblapack.so.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.0</span></span>
<span id="cb11-8"></span>
<span id="cb11-9">locale<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb11-10"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] LC_CTYPE<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span>       LC_NUMERIC<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>C              </span>
<span id="cb11-11"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] LC_TIME<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span>        LC_COLLATE<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span>    </span>
<span id="cb11-12"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] LC_MONETARY<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span>    LC_MESSAGES<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span>   </span>
<span id="cb11-13"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>] LC_PAPER<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span>       LC_NAME<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>C                 </span>
<span id="cb11-14"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>] LC_ADDRESS<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>C               LC_TELEPHONE<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>C            </span>
<span id="cb11-15">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>] LC_MEASUREMENT<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>en_US.UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span> LC_IDENTIFICATION<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span>C       </span>
<span id="cb11-16"></span>
<span id="cb11-17">attached base packages<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb11-18">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] stats     graphics  grDevices utils     datasets  methods   base     </span>
<span id="cb11-19"></span>
<span id="cb11-20">other attached packages<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb11-21">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] ggplot2_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>    dplyr_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>      sf_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-2</span>         osmdata_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span>   </span>
<span id="cb11-22">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] raster_3<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-13</span>    sp_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-5</span>         rayshader_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.1</span></span>
<span id="cb11-23"></span>
<span id="cb11-24">loaded via a <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">namespace</span> (and not attached)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb11-25"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] rgl_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">107.14</span>       Rcpp_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>         lubridate_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.10</span>   lattice_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.20</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-44</span>   </span>
<span id="cb11-26"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>] png_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-7</span>          prettyunits_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>  class_7<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-19</span>       ps_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.0</span>          </span>
<span id="cb11-27"> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>] assertthat_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>   rprojroot_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>    digest_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.27</span>      foreach_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.1</span>     </span>
<span id="cb11-28">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>] utf8_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>         R6_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.1</span>           e1071_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.7</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span>        httr_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>        </span>
<span id="cb11-29">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>] pillar_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.2</span>       rlang_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.11</span>       progress_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2</span>     curl_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>        </span>
<span id="cb11-30">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>] callr_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.0</span>        magick_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.3</span>       pkgdown_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.1</span>      desc_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>        </span>
<span id="cb11-31">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>] devtools_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>     rgdal_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-23</span>       htmlwidgets_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.3</span>  munsell_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>     </span>
<span id="cb11-32">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">29</span>] proxy_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-26</span>       compiler_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>     xfun_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.25</span>          pkgconfig_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>   </span>
<span id="cb11-33">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">33</span>] pkgbuild_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>     htmltools_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.2</span>    tidyselect_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>   tibble_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>      </span>
<span id="cb11-34">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>] codetools_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-18</span>   fansi_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>        crayon_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.1</span>       withr_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>       </span>
<span id="cb11-35">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">41</span>] grid_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>         gtable_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>       jsonlite_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.2</span>     lifecycle_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>   </span>
<span id="cb11-36">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>] DBI_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>          magrittr_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>     scales_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>       units_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.7</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-2</span>       </span>
<span id="cb11-37">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">49</span>] KernSmooth_2<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.23</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-20</span> cli_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>          cachem_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>       farver_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>      </span>
<span id="cb11-38">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">53</span>] fs_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>           remotes_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>      doParallel_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.16</span>  testthat_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>    </span>
<span id="cb11-39">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">57</span>] xml2_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>         ellipsis_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>     generics_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>     vctrs_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.8</span>       </span>
<span id="cb11-40">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</span>] iterators_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.13</span>   tools_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>        rayimage_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.2</span>     glue_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.2</span>        </span>
<span id="cb11-41">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>] purrr_0.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.4</span>        hms_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>          processx_3.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.2</span>     pkgload_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>     </span>
<span id="cb11-42">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">69</span>] parallel_4.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>     fastmap_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>      colorspace_2<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-2</span>   sessioninfo_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span> </span>
<span id="cb11-43">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">73</span>] classInt_0<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-3</span>     rvest_1.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>        memoise_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>      knitr_1<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.33</span>        </span>
<span id="cb11-44">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>] usethis_2.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>     </span></code></pre></div></div>
</div>
</section>
<section id="resources" class="level2">
<h2 class="anchored" data-anchor-id="resources">Resources</h2>
<ul>
<li>rayshader: <a href="https://www.rayshader.com/">https://www.rayshader.com/</a></li>
<li>rayrender: <a href="http://www.rayrender.net/">http://www.rayrender.net/</a></li>
<li>Tyler Morgan-Wall, PhD: <a href="https://www.tylermw.com/">https://www.tylermw.com/</a></li>
</ul>


</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>rayshader</category>
  <category>ggplot2</category>
  <category>visualizations</category>
  <guid>https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/2021-09-07-rayshader.html</guid>
  <pubDate>Tue, 07 Sep 2021 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2021-09-07-rayshader/images/smallhobart.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>A Brief History of the Dataframe</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2021-05-24-dataframe/2021-05-24-dataframe.html</link>
  <description><![CDATA[ 





<p>This is quite the insightful read on the history of the dataframe. From its origins in the S programming language, to R, to the <code>pandas</code> library for Python (to the <code>tibble</code> for R!)… I couldn’t imagine doing my work without this fundamental data structure, but will it really be defined out of existence? See below link for an excellent overview by <a href="https://devin-petersohn.medium.com/">Devin Petersohn</a>:</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2021-05-24-dataframe/images/dataframe.jpeg" class="img-fluid"></p>
<p>Source:</p>
<ul>
<li><p><a href="https://towardsdatascience.com/preventing-the-death-of-the-dataframe-8bca1c0f83c8">Towards Data Science: Preventing the Death of the Dataframe</a></p></li>
<li><p><a href="https://pandas.pydata.org/">pandas for Python</a></p></li>
<li><p><a href="https://tibble.tidyverse.org/">tibble for R</a></p></li>
</ul>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>tibble</category>
  <guid>https://www.javierorracadeatcu.com/posts/2021-05-24-dataframe/2021-05-24-dataframe.html</guid>
  <pubDate>Mon, 24 May 2021 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2021-05-24-dataframe/images/dataframe.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>DT: An R Interface to the JavaScript library DataTables</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2021-05-03-DT/2021-05-03-DT.html</link>
  <description><![CDATA[ 





<p>The <a href="https://rstudio.github.io/DT/">DT package</a> for R is a powerful integration of the JavaScript library <em>DataTables</em>. One of the things that I enjoy with <code>DT</code> is enhancing my Shiny apps by rendering interactive tables with <code>DT</code>. The <code>DT</code> documentation, written by <a href="https://www.rstudio.com/">RStudio PBC</a> and the <code>DT</code> devs, is incredibly well-written and you’ll learn a lot about how you can use these tables in the development of robust R web apps. And the available Options…. Endless in a good way!</p>
<p>If you’re a Shiny dev, check out the live demos sourced below.</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2021-05-03-DT/images/DT_Shiny.png" class="img-fluid"></p>
<p>Source:</p>
<ul>
<li><p><a href="https://github.com/rstudio/DT">DT on GitHub</a></p></li>
<li><p><a href="https://rstudio.github.io/DT/">DT site</a></p></li>
<li><p><a href="https://yihui.shinyapps.io/DT-selection/">Live Shiny demo: DT Selections Ex. 1</a></p></li>
<li><p><a href="https://yihui.shinyapps.io/DT-rows/">Live Shiny demo: DT Selection Ex. 2</a></p></li>
</ul>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>dt</category>
  <guid>https://www.javierorracadeatcu.com/posts/2021-05-03-DT/2021-05-03-DT.html</guid>
  <pubDate>Mon, 03 May 2021 07:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2021-05-03-DT/images/DT_Shiny.png" medium="image" type="image/png" height="84" width="144"/>
</item>
<item>
  <title>rstudio::global tips, tricks, and more</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/2021-01-24-rstudio-global.html</link>
  <description><![CDATA[ 





<p>I attended several sessions throughout rstudio::global and below are my favorite reminders, takeaways, and new concepts that I learned. You’ll gain the most from this article if you are comfortable with predictive analytics with R and have been exposed to the <code>tidyverse</code> and <code>tidymodels</code> collections of packages. If you missed the conference, all of the presentations and slides will be made available on RStudio’s website soon. Some philosophical, some technical.<br><br> <img src="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/global-logo-dark.jpg" class="img-fluid" alt="rstudio::global(2021) conference"></p>
<section id="fostering-positive-thinking" class="level1">
<h1>Fostering Positive Thinking</h1>
<p>The opening keynote presentation with RStudio’s Chief Data Scientist, Hadley Wickham, was super refreshing and very encouraging to hear. In short, Wickham encouraged analysts and data scientists to change the way you approach challenges when programming or maintaining code.</p>
<p>Below are several reminders that Wickham discussed… If you’re your harshest critic, revisit these. I know that I will:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Automatic Thoughts</th>
<th>Balanced Alternatives</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>I must be a moron if I can’t perform this “simple” task.</td>
<td>This isn’t something I do very often so it’s unreasonable to expect that I’d automatically be an expert at it.</td>
</tr>
<tr class="even">
<td>The documentation is useless and doesn’t help me at all.</td>
<td>It’s not possible to document every possible existing situation, but I can still read the docs and learn something.</td>
</tr>
<tr class="odd">
<td>This was a total waste of time. I’ll never get those four hours back again.</td>
<td>Maybe I didn’t succeed in my original goal, but I made some progress, and I gained valuable insights for the next time that I try.</td>
</tr>
</tbody>
</table>
</section>
<section id="tidymodels" class="level1">
<h1>Tidymodels</h1>
<section id="what-is-it" class="level3">
<h3 class="anchored" data-anchor-id="what-is-it">What is it?</h3>
<p>Coming from a converted <code>caret</code>-ista, it wasn’t an easy transition to the full <code>tidymodels</code> framework. I liked the way <code>caret</code> <em>felt</em>, and I did almost everything with it from resampling, modeling, tuning, to model evaluation. Enter <code>tidymodels</code>, a framework that feels like a deconstructed <code>caret</code> - Each major <code>caret</code> feature has been enhanced and wrapped into its own package. I’ve been using this framework for roughly one year and the rate at which this ecosystem has evolved has been impressive.</p>
<p><code>tidymodels</code> is a collection of tidy-friendly packages written with a consistent vocabulary for reproducible machine learning workflows. Its core packages include the following:</p>
<ul>
<li><p><a href="https://rsample.tidymodels.org/">rsample</a>: provides infrastructure for efficient data splitting and resampling</p></li>
<li><p><a href="https://parsnip.tidymodels.org/">parsnip</a>: a tidy, unified interface to models that can be used to try a range of models without getting bogged down in the syntactical minutiae of the underlying packages</p></li>
<li><p><a href="https://recipes.tidymodels.org/">recipes</a>: a tidy interface to data pre-processing tools for feature engineering</p></li>
<li><p><a href="https://workflows.tidymodels.org/">workflows</a>: expands the traditional model-only recipe to a much more holistic blueprint for pre-processing, modeling, post-processing, and evaluation</p></li>
<li><p><a href="https://tune.tidymodels.org/">tune</a>: helps you optimize the hyperparameters of your model and pre-processing steps</p></li>
<li><p><a href="https://yardstick.tidymodels.org/">yardstick</a>: measures the effectiveness of models using performance metrics</p></li>
<li><p><a href="https://broom.tidymodels.org/">broom</a>: converts the information in common statistical R objects into user-friendly, predictable formats</p></li>
<li><p><a href="https://dials.tidymodels.org/">dials</a>: creates and manages tuning parameters and parameter grids</p></li>
<li><p><a href="https://usemodels.tidymodels.org/">usemodels</a>: a helpful way to quickly create code snippets to fit models using the tidymodels framework</p>
<ul>
<li>At the time of writing this, <code>usemodels</code> is still in very early development (version 0.1.0.). This package follows the <code>usethis</code> philosophy of automating repetitive project development tasks but with a focus on modeling. <code>usemodels</code> helped me fully transition from the <code>caret</code> meta-engine to <code>parsnip</code> + <code>tune</code> + <code>dials</code> + more of the <code>tidymodels</code> framework.</li>
</ul></li>
<li><p><a href="https://stacks.tidymodels.org/">stacks</a>: similar to <code>usemodels</code>, this is an early-stage package for model stacking, an ensembling method that takes the outputs of many models and combines them to generate a new model that generates predictions informed by each of its members</p></li>
</ul>
</section>
<section id="irregular-grid-searches" class="level2">
<h2 class="anchored" data-anchor-id="irregular-grid-searches">Irregular Grid Searches</h2>
<p>Irregular grid searches help you tune your models by cycling through randomized hyperparameters in an effort to yield the best (or at least better) performance. I’ve been using the <code>dials</code> package to create Latin hypercubes which is explained below, highlighting the benefits of this space-filling design vs a purely randomized irregular grid search. Max Kuhn’s <em>“New in tidymodels”</em> rstudio::global event provided me with a fresh reminder about <em>why</em> these methods are beneficial and <em>how</em> they actually optimize performance.</p>
<p>The documentation below was copied almost entirely from Chapter 13 of Max Kuhn and Julia Silge’s <a href="https://www.tmwr.org/"><em>Tidy Modeling with R</em></a> (“TMWR”). I’ve deleted certain bits and added minimal helper comments in brackets. If you find yourself statistically programming, bootstrapping / resampling, improving model performance through grid searches, maybe even ensemble stacking, I’d highly recommend that you bookmark the TMWR book.</p>
<section id="random-grid-search" class="level3">
<h3 class="anchored" data-anchor-id="random-grid-search">Random Grid Search</h3>
<p>There a several options for creating non-regular grids. The first is to use random sampling across the range of parameters. The <code>grid_random()</code> function [of the <code>dials</code> package] generates independent uniform random numbers across the parameter ranges. If the parameter object has an associated transformation (such as we have for <code>penalty</code>), the random numbers are generated on the transformed scale. For example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-2">mlp_param <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb1-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_random</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 'size' is the number of combinations</span></span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>()</span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   hidden_units      penalty           epochs    </span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Min.   : 1.00   Min.   :0.0000   Min.   :  10  </span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  1st Qu.: 3.00   1st Qu.:0.0000   1st Qu.: 259  </span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Median : 6.00   Median :0.0000   Median : 480  </span></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Mean   : 5.58   Mean   :0.0432   Mean   : 496  </span></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  3rd Qu.: 8.00   3rd Qu.:0.0050   3rd Qu.: 738  </span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  Max.   :10.00   Max.   :0.9932   Max.   :1000</span></span></code></pre></div></div>
</div>
<p>For <code>penalty</code>, the random numbers are uniform on the log (base 10) scale but the values in the grid are in the natural units.</p>
<p>The issue with random grids is that, with small-to-medium grids, random values can result in overlapping parameter combinations. Also, the random grid needs to cover the whole parameter space but the likelihood of good coverage increases with the number of grid values. Even for a sample of 15 candidate points, this plot shows some overlap between points for our example multilayer perceptron:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggforce)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb2-3">mlp_param <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The 'original = FALSE' option keeps penalty in log10 units</span></span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_random</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">original =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> .panel_x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> .panel_y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_blank</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(hidden_units, penalty, epochs), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layer.diag =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Random design with 15 candidates"</span>)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/grid-random-matrix-1.svg" class="img-fluid figure-img"></p>
<figcaption>Random grid search using dials::grid_random()</figcaption>
</figure>
</div>
</section>
<section id="space-filling-designs" class="level3">
<h3 class="anchored" data-anchor-id="space-filling-designs">Space-Filling Designs</h3>
<p>A much better approach is to use a set of experimental designs called space-filling designs. While different design methods have slightly different goals, they generally find a configuration of points that cover the parameter space with the smallest chance of overlapping or redundant values. See <a href="https://www.asc.ohio-state.edu/santner.1/TJS-BJW-WIN/master-driver.pdf">Santner et al.&nbsp;(2003)</a> for an overview of space-filling designs.</p>
<p>The <code>dials</code> package contains functions for Latin hypercube and maximum entropy designs. As with <code>dials::grid_random()</code>, the primary inputs are the number of parameter combinations and a parameter object. Let’s compare the above random design with a Latin hypercube design for 15 candidate parameter values.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb3-2">mlp_param <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_latin_hypercube</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">original =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> .panel_x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> .panel_y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_blank</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(hidden_units, penalty, epochs), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layer.diag =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Latin Hypercube design with 15 candidates"</span>)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/grid-sfd-compare-1.svg" class="img-fluid figure-img"></p>
<figcaption>Latin hypercube designed using the dials::grid_latin_hypercube() helper</figcaption>
</figure>
</div>
<p>While not perfect, this design spaces the points further away from one another.</p>
<p>Space-filling designs can be very effective at representing the parameter space. The default design used by the <code>tune</code> package is the maximum entropy design. These tend to produce grids that cover the candidate space well and drastically increase the chances of finding good results.</p>
<p>To learn more about advanced <em>iterative search</em> methods such as Bayesian optimization and simulated annealing, please visit <a href="https://www.tmwr.org/iterative-search.html">Chapter 14 of TMWR</a>. Max Kuhn covered these advanced techniques during his <code>tidymodels</code> rstudio::global event and I haven’t had a chance to try these iterative search methods.</p>
</section>
</section>
<section id="usemodels" class="level2">
<h2 class="anchored" data-anchor-id="usemodels">usemodels</h2>
<p>In the spirit of sharing useful tips, tricks, and packages, I’d like to introduce you to Max Kuhn’s <code>usemodels</code> package. If you’re on the fence about adopting the <code>tidymodels</code> framework, this package is definitely for you. Even for experienced users, <code>usemodels</code> is an excellent way to quickly generate boilerplate code snippets that are algorithm-specific. This package is not yet on CRAN, so install it using the following:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Installing usemodels from its GitHub repo</span></span>
<span id="cb4-2">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidymodels/usemodels"</span>)</span></code></pre></div></div>
</div>
<p>Given a simple formula and a data set, the <code>use_*</code> functions can create code that is appropriate for the data (given the model). [Note: The model formula will be in the form of <code>y ~ a + b + c</code> or <code>y ~ .</code> if you plan on including all available variables in your model.]</p>
<p>For example, using the <code>palmerpenguins</code> data with a <code>glmnet</code> model:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(usemodels)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(penguins)</span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_glmnet</span>(body_mass_g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins)</span>
<span id="cb5-5"></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">NOTE</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: The below will be printed in your console with your model recipe and tailored</span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># with the required pre-processing steps given your algorithm of choice (in this case,</span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># glmnet). The `usemodels` output also provides the code structure for a reproducible </span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># workflow, made possible with the `workflows` package. Should you choose to tune</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># your model, `usemodels` also provides code snippets for producing a grid of parameter</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># combinations to use with your hyperparameter tuning grid search.</span></span>
<span id="cb5-12"></span>
<span id="cb5-13">glmnet_recipe <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recipe</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> body_mass_g <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_novel</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_nominal</span>(), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_outcomes</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_dummy</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_nominal</span>(), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_outcomes</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_zv</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_predictors</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_normalize</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_predictors</span>(), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_nominal</span>()) </span>
<span id="cb5-19"></span>
<span id="cb5-20">glmnet_spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb5-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">linear_reg</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">penalty =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tune</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mixture =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tune</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regression"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glmnet"</span>) </span>
<span id="cb5-24"></span>
<span id="cb5-25">glmnet_workflow <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb5-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">workflow</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_recipe</span>(glmnet_recipe) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(glmnet_spec) </span>
<span id="cb5-29"></span>
<span id="cb5-30">glmnet_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tidyr<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;">crossing</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">penalty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mixture =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, </span>
<span id="cb5-31">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) </span>
<span id="cb5-32"></span>
<span id="cb5-33">glmnet_tune <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb5-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tune_grid</span>(glmnet_workflow, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resamples =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"add your rsample object"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grid =</span> glmnet_grid)</span></code></pre></div></div>
</div>
<p>As of today’s date, this package includes templates available with the following code: <code>use_cubist</code>, <code>use_earth</code>, <code>use_glmnet</code>, <code>use_kknn</code>, <code>use_ranger</code>, and <code>use_xgboost</code></p>
</section>
<section id="stacks" class="level2">
<h2 class="anchored" data-anchor-id="stacks">stacks</h2>
<section id="ensemble-models" class="level3">
<h3 class="anchored" data-anchor-id="ensemble-models">Ensemble Models</h3>
<p>Similar to <code>usemodels</code>, this is a very early-stage package that you should start following. There are already several AI tools for producing “model stacks” or “ensemble models” such as <a href="http://h2o-release.s3.amazonaws.com/h2o/master/1735/docs-website/Ruser/Rinstall.html">H2O.ai</a>, <a href="https://community.datarobot.com/t5/resources/welcome-to-using-r-with-datarobot/ta-p/1770">DataRobot</a>, and a few others, however, <code>stacks</code> is the first purpose-built for use with <code>tidymodels</code>. The following narrative is copied almost directly from the <a href="https://stacks.tidymodels.org/">stacks website</a>.</p>
<p><code>stacks</code> is an R package for model stacking that aligns with the <code>tidymodels</code>. Model stacking is an ensembling method that takes the outputs of many models and combines them to generate a new model - <em>referred to as an ensemble in this package</em> - that generates predictions informed by each of its members.</p>
<p>The process goes something like this:</p>
<ol type="1">
<li><p>Define candidate ensemble members using functionality from rsample, parsnip, workflows, recipes, and tune</p></li>
<li><p>Initialize a data_stack object with stacks()</p></li>
<li><p>Iteratively add candidate ensemble members to the data_stack with add_candidates()</p></li>
<li><p>Evaluate how to combine their predictions with blend_predictions()</p></li>
<li><p>Fit candidate ensemble members with non-zero stacking coefficients with fit_members()</p></li>
<li><p>Predict on new data with predict()</p></li>
</ol>
</section>
<section id="stacks-grammar" class="level3">
<h3 class="anchored" data-anchor-id="stacks-grammar">stacks Grammar</h3>
<p>At the highest level, ensembles are formed from model definitions. In this package, model definitions are an instance of a minimal workflow, containing a model specification (as defined in the parsnip package) and, optionally, a preprocessor (as defined in the recipes package). Model definitions specify the form of candidate ensemble members.</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/model_defs.png" class="img-fluid"></p>
<p>To be used in the same ensemble, each of these model definitions must share the same resample. This rsample rset object, when paired with the model definitions, can be used to generate the tuning/fitting results objects for the candidate ensemble members with tune.</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/candidates.png" class="img-fluid"></p>
<p>Candidate members first come together in a data_stack object through the add_candidates() function. Principally, these objects are just tibbles, where the first column gives the true outcome in the assessment set (the portion of the training set used for model validation), and the remaining columns give the predictions from each candidate ensemble member. (When the outcome is numeric, there’s only one column per candidate ensemble member. Classification requires as many columns per candidate as there are levels in the outcome variable.) They also bring along a few extra attributes to keep track of model definitions.</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/data_stack.png" class="img-fluid"></p>
<p>Then, the data stack can be evaluated using blend_predictions() to determine to how best to combine the outputs from each of the candidate members. In the stacking literature, this process is commonly called meta-learning.</p>
<p>The outputs of each member are likely highly correlated. Thus, depending on the degree of regularization you choose, the coefficients for the inputs of (possibly) many of the members will zero out—their predictions will have no influence on the final output, and those terms will thus be thrown out.</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/coefs.png" class="img-fluid"></p>
<p>These stacking coefficients determine which candidate ensemble members will become ensemble members. Candidates with non-zero stacking coefficients are then fitted on the whole training set, altogether making up a <code>model_stack</code> object.</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/class_model_stack.png" class="img-fluid"></p>
<p>This model stack object, outputted from <code>fit_members()</code>, is ready to predict on new data! The trained ensemble members are often referred to as base models in the stacking literature. To learn more about how to use <code>stacks</code>, check out the following excellent vignettes from the <code>tidymodels</code> team:</p>
<ul>
<li><p><a href="https://stacks.tidymodels.org/articles/basics.html">Getting Started with stacks</a></p></li>
<li><p><a href="https://stacks.tidymodels.org/articles/classification.html">Classification Models with stacks</a></p></li>
</ul>
<p>Sources:</p>
<ul>
<li><p><a href="https://rstudio.com/conference/">rstudio::global(2021)</a></p></li>
<li><p><a href="https://www.tmwr.org/">Tidy Modeling with R</a></p></li>
<li><p><a href="https://www.tidymodels.org/">tidymodels</a></p></li>
<li><p><a href="https://usemodels.tidymodels.org/">usemodels</a></p></li>
<li><p><a href="https://stacks.tidymodels.org/">stacks</a></p></li>
</ul>


</section>
</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>tidymodels</category>
  <guid>https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/2021-01-24-rstudio-global.html</guid>
  <pubDate>Sun, 24 Jan 2021 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2021-01-24-rstudio-global/images/rstudio-global-2021.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>rsthemes: Customizing your RStudio IDE</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2021-01-19-rsthemes/2021-01-19-rsthemes.html</link>
  <description><![CDATA[ 





<p>Feel like freshening up your RStudio IDE? Check out the <code>rsthemes</code> package by <a href="https://www.linkedin.com/in/garrick-aden-buie/">Garrick Aden-Buie</a> to explore and apply different themes. For the last year or so, I’ve been enjoying the “base16 Ashes” theme at work and home. I prefer the “One Dark” theme on Atom / Juno for my Julia setup, But I’m starting to feel like I should use “One Dark” across the board. 🤓</p>
<p>This package is not yet on CRAN, so you’ll have to install via <code>devtools::install_github()</code>. See installation and usage code below, pulled from the <code>rsthemes</code> GitHub:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## INSTALLATION</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Instructions assume devtools is installed)</span></span>
<span id="cb1-3">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gadenbuie/rsthemes"</span>)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install custom themes + additional set of base16-based themes</span></span>
<span id="cb1-6">rsthemes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_rsthemes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_base16 =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## USAGE</span></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># list installed themes</span></span>
<span id="cb1-10">rsthemes<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;">list_rsthemes</span>()</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Try all themes</span></span>
<span id="cb1-13">rsthemes<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;">try_rsthemes</span>()</span>
<span id="cb1-14"></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Try just the light, dark, or base16 themes, e.g.</span></span>
<span id="cb1-16">rsthemes<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;">try_rsthemes</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"light"</span>)</span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use a theme</span></span>
<span id="cb1-19">rstudioapi<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;">applyTheme</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"One Dark {rsthemes}"</span>)</span></code></pre></div></div>
</div>
<p>Source:</p>
<ul>
<li><p><a href="https://github.com/gadenbuie/rsthemes">rsthemes on GitHub</a></p></li>
<li><p><a href="http://chriskempson.com/projects/base16/">base16 themes</a></p></li>
</ul>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>quick tips</category>
  <guid>https://www.javierorracadeatcu.com/posts/2021-01-19-rsthemes/2021-01-19-rsthemes.html</guid>
  <pubDate>Tue, 19 Jan 2021 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2021-01-19-rsthemes/images/rsthemes.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Preparing for 2021 Goals with a Raspberry Pi</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2020-12-31-Raspberry-Pi/2020-12-31-Raspberry-Pi.html</link>
  <description><![CDATA[ 





<p>Killing a few birds with one stone and getting ready for 2021… <br><br>1) Getting better at Linux / shell commands, <br> 2) Learning Julia (see my Resources tab for some great Julia starter links!), <br> 3) Doing it all on a Raspberry Pi 😜 Happy holidays and Happy New Years to all!</p>
<p>Below are some pics of my current progress.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2020-12-31-Raspberry-Pi/images/Pi1.jpg" class="img-fluid figure-img"></p>
<figcaption>Raspberry Pi 4, Model B, 8GB RAM, &amp; Super Cute Tiny Heat Sinks</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2020-12-31-Raspberry-Pi/images/Pi2.jpg" class="img-fluid figure-img"></p>
<figcaption>Enclosing the Pi 4 in a CanaKit case + fan</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2020-12-31-Raspberry-Pi/images/Pi3.jpg" class="img-fluid figure-img"></p>
<figcaption>Raspberry Pi OS, a Debian distro for the Pi</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.javierorracadeatcu.com/posts/2020-12-31-Raspberry-Pi/images/Pi4.jpeg" class="img-fluid figure-img"></p>
<figcaption>Jupyter Lab server on the Pi set up w/ Python, R, and Julia</figcaption>
</figure>
</div>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>linux</category>
  <guid>https://www.javierorracadeatcu.com/posts/2020-12-31-Raspberry-Pi/2020-12-31-Raspberry-Pi.html</guid>
  <pubDate>Thu, 31 Dec 2020 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2020-12-31-Raspberry-Pi/images/Pi-banner.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>One-Hour R Package Development Tutorial by Shannon Pileggi, PhD</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2020-12-19-R-Package-One-Hour-Tutorial/2020-12-19-R-Package-One-Hour-Tutorial.html</link>
  <description><![CDATA[ 





<p>Package development should be on every R programmer’s radar in 2021, especially with all the free information available online! Here’s an excellent, concise, ultra-bookmark-worthy tutorial by Shannon Pileggi, PhD, titled “Your first R package in 1 hour”. Folks, she wasn’t lying either. I followed her tutorial and built an R package in roughly 45 minutes (note: I assume you have experience with the R language and understand the basics of <code>devtools</code>, <code>usethis</code>, <code>roxygen2</code>, and <code>rmarkdown</code>).</p>
<p>Using functions from just the <code>devtools</code> and <code>usethis</code> packages, the whole process is much more streamlined than what I would have thought. Check out the rest of Shannon’s <em>Piping Hot Data</em> website as well… It’s a great resource for R programmers and researchers, and she built it with the newly reintroduced <code>distill</code> package, just like this site. :-)</p>
<p>Source: <br>* Shannon’s <em>Piping Hot Data</em> <a href="https://www.pipinghotdata.com/posts/2020-10-25-your-first-r-package-in-1-hour/">“Your first R package in 1 hour” post</a> <br>* Shannon’s <a href="https://www.linkedin.com/in/shannon-m-pileggi/">LinkedIn</a></p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>packages</category>
  <guid>https://www.javierorracadeatcu.com/posts/2020-12-19-R-Package-One-Hour-Tutorial/2020-12-19-R-Package-One-Hour-Tutorial.html</guid>
  <pubDate>Sat, 19 Dec 2020 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2020-12-19-R-Package-One-Hour-Tutorial/images/package_workshop.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>What’s New in dbplyr 2.0.0</title>
  <dc:creator>Javier Orraca-Deatcu</dc:creator>
  <link>https://www.javierorracadeatcu.com/posts/2020-11-04-dbplyr-2.0.0/2020-11-04-dbplyr-2.0.0.html</link>
  <description><![CDATA[ 





<p><code>dbplyr</code> 2.0.0 comes loaded with improvements including the translation of <code>dplyr</code>’s new <code>across()</code> function. If you aren’t familiar with this library, <code>dbplyr</code> translates your <code>dplyr</code> syntax to SQL. I regularly connect to enterprise databases using R and have almost completely transitioned from SQL. Yes, SQL is awesome. Yes, using <code>dplyr</code> is a lot more fun!</p>
<p><img src="https://www.javierorracadeatcu.com/posts/2020-11-04-dbplyr-2.0.0/images/dbplyr.jpeg" class="img-fluid"></p>
<p>Source:<br> * tidyverse blog’s <a href="https://www.tidyverse.org/blog/2020/11/dbplyr-2-0-0/">dbplyr 2.0.0 Official Announcement</a></p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section></div> ]]></description>
  <category>tidyverse</category>
  <category>dplyr</category>
  <guid>https://www.javierorracadeatcu.com/posts/2020-11-04-dbplyr-2.0.0/2020-11-04-dbplyr-2.0.0.html</guid>
  <pubDate>Wed, 04 Nov 2020 08:00:00 GMT</pubDate>
  <media:content url="https://www.javierorracadeatcu.com/posts/2020-11-04-dbplyr-2.0.0/images/dbplyr_what_is_new.jpg_large" medium="image"/>
</item>
</channel>
</rss>
