<?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>%&gt;% dreams</title>
<link>https://ivelasq.rbind.io/til.html</link>
<atom:link href="https://ivelasq.rbind.io/til.xml" rel="self" type="application/rss+xml"/>
<description>Isabella Velásquez Personal Website</description>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Tue, 18 Feb 2025 00:00:00 GMT</lastBuildDate>
<item>
  <title>How to open a Quarto project in GitHub Codespaces</title>
  <link>https://ivelasq.rbind.io/til-other/quarto-github-codespaces/</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This blog post has been superseded by a newer post written by Mickaël Canouil on the <a href="https://quarto.org/docs/blog/posts/2025-05-19-quarto-codespaces/">Quarto Blog</a>.</p>
</div>
</div>
<p>I recently received a question on <a href="https://www.youtube.com/watch?v=uLGe9zuuNl0">YouTube</a> about running a Quarto project in GitHub Codespaces. This was new to me, so I did some research (thanks to <a href="https://github.com/mcanouil">Mickaël Canouil</a> and others for their help!). The key takeaway is that everything needs to be containerized: Quarto, Jupyter, the Quarto extension, R, Python, etc. Installing the VS Code Quarto extension is not enough. The easiest way to achieve this is by using the <a href="https://github.com/mcanouil/quarto-codespaces">quarto-codespaces</a> repository.</p>
<ol type="1">
<li>Fork Mickaël’s <code>quarto-codespaces</code> repo: <a href="https://github.com/mcanouil/quarto-codespaces" class="uri">https://github.com/mcanouil/quarto-codespaces</a></li>
<li>Click the green “Code” button, select “Codespaces,” and then “Create codespace on main.”</li>
</ol>
<p><img src="https://ivelasq.rbind.io/til-other/quarto-github-codespaces/images/1.png" class="img-fluid"></p>
<ol start="3" type="1">
<li>Click “Authorize” and continue. Creating the codespace may take a few minutes.</li>
</ol>
<p><img src="https://ivelasq.rbind.io/til-other/quarto-github-codespaces/images/2.png" class="img-fluid"></p>
<ol start="4" type="1">
<li>Once the codespace is ready, clone your Quarto project into it. For example, to clone the <code>water-insecurity-dashboard</code> project, run the following in the terminal:</li>
</ol>
<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 https://github.com/ivelasq/water-insecurity-dashboard.git</span></code></pre></div></div>
<ol start="5" type="1">
<li><p>Navigate to the project directory: <code>cd water-insecurity-dashboard</code></p></li>
<li><p>Create a new Python environment. In the Command Palette (<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> or <kbd>Cmd</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>), select “Python: Create Environment…”. Choose “Venv” and the appropriate Python interpreter. <strong>Very importantly,</strong> make sure you check the box to install dependencies from <code>requirements.txt</code>.</p></li>
</ol>
<p><img src="https://ivelasq.rbind.io/til-other/quarto-github-codespaces/images/3.png" class="img-fluid"></p>
<ol start="7" type="1">
<li>Open your <code>.qmd</code> file (e.g., <code>final.qmd</code>) and click the “Preview” button in the top right.</li>
</ol>
<p><img src="https://ivelasq.rbind.io/til-other/quarto-github-codespaces/images/4.png" class="img-fluid"></p>
<p>That’s it! Your Quarto project should now be running in GitHub Codespaces.</p>
<p><img src="https://ivelasq.rbind.io/til-other/quarto-github-codespaces/images/5.png" class="img-fluid"></p>



 ]]></description>
  <guid>https://ivelasq.rbind.io/til-other/quarto-github-codespaces/</guid>
  <pubDate>Tue, 18 Feb 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to find out how much of Core R is R</title>
  <link>https://ivelasq.rbind.io/til-r/r-composition/</link>
  <description><![CDATA[ 




<p>Cleaning out my computer as I get ready to switch to a new one has me running into old gems. So, when I say “Today I learned,” I really mean “I learned this back in December 2021.” 😅</p>
<p>Back then, I gave a talk at Why R? called <a href="https://www.youtube.com/watch?v=vyA2EiIz4pI&amp;feature=youtu.be">Packages for Using R With Python, Tableau, and Other Tools</a>. One part of the talk was about how R itself isn’t just made up of R.</p>
<p>I adapted <a href="https://librestats.wordpress.com/2011/08/27/how-much-of-r-is-written-in-r/">this classic blog post</a> by wrathematics to explore the composition of the <a href="https://cran.r-project.org/src/base/R-4/">R 4.1.2 source package</a>. The post features a script that scans the <code>.R</code>, <code>.c</code>, and <code>.f</code> files in the source, then records the language (R, C, or Fortran) and the number of lines of code in each language to a CSV file. Keep in mind, I have almost no knowledge of Shell (and this was pre-ChatGPT days!), so it took me a bit to adapt the original script from 2011.</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>shell.sh</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" data-filename="shell.sh" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">outdir</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./"</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rdir</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./R-4.1.2"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#eg, ~/R-2.13.1/</span></span>
<span id="cb1-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$rdir</span>/src</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> rfile <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find</span> . <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-type</span> f <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-name</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.R<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="cb1-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb1-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">loc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-l</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$rfile</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sed</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/ ./,/'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\//\//g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\//\//g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\///g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\///'</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="cb1-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R,</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$loc</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$outdir</span>/r_source_loc.csv</span>
<span id="cb1-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> cfile <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find</span> . <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-type</span> f <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-name</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.c<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="cb1-13"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb1-14"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">loc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-l</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$cfile</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sed</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/ ./,/'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\//\//g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\//\//g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\///g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\///'</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="cb1-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C,</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$loc</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$outdir</span>/r_source_loc.csv</span>
<span id="cb1-16"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ffile <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find</span> . <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-type</span> f <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-name</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.f<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="cb1-19"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb1-20"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">loc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-l</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$ffile</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sed</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/ ./,/'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\//\//g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\//\//g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\/[^/]*\///g'</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-e</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s/\///'</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="cb1-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fortran,</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$loc</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$outdir</span>/r_source_loc.csv</span>
<span id="cb1-22"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span></code></pre></div></div>
</div>
<p>The script creates a file called <code>r_source_loc.csv</code>. It shows the number of lines by programming language by script in R 4.1.2. We can read it into R:</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>(dplyr)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span>
<span id="cb2-3"></span>
<span id="cb2-4">r_loc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-5">  readr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_table</span>(here<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"til-r"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r-composition"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r_source_loc.csv"</span>),</span>
<span id="cb2-6">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_names =</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;">"language"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"script"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-7">  <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;">language =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(language, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R,,"</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"</span>,</span>
<span id="cb2-8">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(language, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C,,"</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;">"C"</span>,</span>
<span id="cb2-9">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(language, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fortran,,"</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;">"Fortran"</span>),</span>
<span id="cb2-10">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lines =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(lines)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>()</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(r_loc)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 3
  language lines script        
  &lt;chr&gt;    &lt;dbl&gt; &lt;chr&gt;         
1 R           20 .snow2.RR     
2 R            9 .multicore3.RR
3 R           15 .multicore2.RR
4 R           10 .multicore1.RR
5 R           25 .RSeed.R      
6 R           36 .Master.R     </code></pre>
</div>
</div>
<p>Now, we can visualize the percentage of Core R sourcecode files by language using ggplot2:</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(forcats)</span>
<span id="cb4-3"></span>
<span id="cb4-4">r_loc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(language)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(language) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-8">  <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;">rel.freq =</span>  n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb4-9">  <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_reorder</span>(language, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(rel.freq)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> rel.freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> language)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb4-12">    <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;">label =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent</span>(rel.freq)),</span>
<span id="cb4-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>),</span>
<span id="cb4-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</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;">0.25</span>,</span>
<span id="cb4-15">    <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;">4</span></span>
<span id="cb4-16">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-17">  <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="cb4-18">  <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;">"Percentage of Core R Sourcecode Files by Language"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-19">  <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;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</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;">14</span>),</span>
<span id="cb4-20">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb4-21">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb4-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</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;">12</span>),</span>
<span id="cb4-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</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;">"#332288"</span>, </span>
<span id="cb4-25">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</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;">"#882255"</span>, </span>
<span id="cb4-26">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fortran"</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;">"#44AA99"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://ivelasq.rbind.io/til-r/r-composition/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Or, we can visualize the percentage of Core R lines of code by language:</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">r_loc <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(language)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(language) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sum_lines =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(lines, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-6">  <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;">percent =</span> sum_lines<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(sum_lines)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb5-7">  <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fct_reorder</span>(language, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(percent)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> percent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> language)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb5-10">    <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;">label =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">percent</span>(percent)),</span>
<span id="cb5-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>),</span>
<span id="cb5-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</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;">0.25</span>,</span>
<span id="cb5-13">    <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;">4</span></span>
<span id="cb5-14">  )<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;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-16">  <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;">"Percentage of Core R Lines of Code by Language"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-17">  <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;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</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;">14</span>),</span>
<span id="cb5-18">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb5-19">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb5-20">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</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;">12</span>),</span>
<span id="cb5-21">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb5-22">        <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-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</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;">"#332288"</span>, </span>
<span id="cb5-24">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</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;">"#882255"</span>, </span>
<span id="cb5-25">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fortran"</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;">"#44AA99"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://ivelasq.rbind.io/til-r/r-composition/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>It’s interesting to see how much goes into making R what it is: an ecosystem built on collaboration across languages and tools (which was the takeaway from the talk!). If you’re curious about R’s source code, give the script a shot!</p>



 ]]></description>
  <guid>https://ivelasq.rbind.io/til-r/r-composition/</guid>
  <pubDate>Wed, 25 Dec 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to add a custom font in plotnine</title>
  <link>https://ivelasq.rbind.io/til-python/plotnine-fonts/</link>
  <description><![CDATA[ 




<p><a href="https://plotnine.org/">Plotnine</a> is a package that brings the grammar of graphics, similar to <a href="https://ggplot2.tidyverse.org/">{ggplot2}</a> in R, to Python.</p>
<p>Like {ggplot2}, <code>plotnine</code> supports the use of custom fonts. To add a custom font:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="annotated-cell-1" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><a class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-1-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.font_manager <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> fm</span>
<span id="annotated-cell-1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="annotated-cell-1-3"></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-1-4" class="code-annotation-target">project_dir <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> os.getcwd()</span>
<span id="annotated-cell-1-5">font_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> os.path.join(project_dir, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Raleway-VariableFont_wght.ttf"</span>)</span>
<span id="annotated-cell-1-6">prop <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fm.FontProperties(fname<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>font_path)</span>
<span id="annotated-cell-1-7"></span>
<span id="annotated-cell-1-8">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="annotated-cell-1-9">    ggplot(rates_long, aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rate"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>))</span>
<span id="annotated-cell-1-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_line()</span>
<span id="annotated-cell-1-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme(</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-1-12" class="code-annotation-target">        title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(hjust<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, fontproperties<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prop),</span>
<span id="annotated-cell-1-13">        text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(fontproperties<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prop),</span>
<span id="annotated-cell-1-14">        plot_background<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_rect(fill<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>)</span>
<span id="annotated-cell-1-15">    )</span>
<span id="annotated-cell-1-16">)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-1" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="1" data-code-annotation="1">Install <code>matplotlib</code>’s font_manager.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="4,5,6" data-code-annotation="2">Install the custom font on your system, and use the <code>os</code> package to point to the directory with your custom font.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="12,13" data-code-annotation="3">Set the font in your <code>plotnine</code> plot using the <code>theme()</code> function.</span>
</dd>
</dl>
<p>I posted a <code>plotnine</code> plot on Reddit using the Raleway font (<a href="https://github.com/ivelasq/plotnine-visualizations/blob/main/historical-mortgage-rates/historical-mortgages-plot.py">code here</a>):</p>
<center>
<blockquote class="reddit-embed-bq blockquote" style="height:500px" data-embed-height="546">
<a href="https://www.reddit.com/r/dataisbeautiful/comments/1dcnrmb/15_and_30year_fixed_rate_mortgage_average_in_the/">15- and 30-Year Fixed Rate Mortgage Average in the United States [OC]</a><br> by<a href="https://www.reddit.com/user/ionychal/">u/ionychal</a> in<a href="https://www.reddit.com/r/dataisbeautiful/">dataisbeautiful</a>
</blockquote>
<script async="" src="https://embed.reddit.com/widgets.js" charset="UTF-8"></script>
</center>



 ]]></description>
  <guid>https://ivelasq.rbind.io/til-python/plotnine-fonts/</guid>
  <pubDate>Thu, 29 Aug 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to set and access environment variables in Python</title>
  <link>https://ivelasq.rbind.io/til-python/python-env/</link>
  <description><![CDATA[ 




<p>I recently presented a <a href="https://www.youtube.com/watch?v=xnJuXOw7iu8">webinar</a> on creating a Quarto dashboard using BLS data and deploying it to Posit Connect. The process required setting three environment variables containing sensitive information, which are needed for the script but should not be publicly shared<sup>1</sup>. <a href="https://github.com/posit-marketing/inflation-explorer/blob/main/data-pull.qmd">See the script using the environment variables</a>.</p>
<p>In Python, you can edit and access environment variables with the <code>os</code> and <code>dotenv</code> packages. Install the <code>dotenv</code> package (you don’t need to install <code>os</code> separately as it is part of the initial Python installation):</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>Terminal</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" data-filename="Terminal" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install python-dotenv</span></code></pre></div></div>
</div>
<p>In your working directory, create a <code>.env</code> file:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>Terminal</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" data-filename="Terminal" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> .env</span></code></pre></div></div>
</div>
<p>Open the <code>.env</code> file and add your environment variables using the syntax <code>VAR_NAME="value"</code>:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>.env</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" data-filename=".env" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">BLS_KEY</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12345"</span></span></code></pre></div></div>
</div>
<p>Save the file. Then, import the two packages in your Python script:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>script.py</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" data-filename="script.py" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dotenv <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_dotenv</span>
<span id="cb4-3">load_dotenv()</span></code></pre></div></div>
</div>
<p>Refer to the environment variable in your Python script using <code>os.environ.get()</code>:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>script.py</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" data-filename="script.py" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">bls_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> os.environ.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BLS_KEY"</span>)</span></code></pre></div></div>
</div>
<p>You can test whether it was successfully accessed:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>script.py</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" data-filename="script.py" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(bls_key)</span></code></pre></div></div>
</div>
<p>Here’s an example of how I used it in my script:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>script.py</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" data-filename="script.py" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> table_id <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> table_ids:</span>
<span id="cb7-2">    bls_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> os.environ.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BLS_KEY"</span>)</span>
<span id="cb7-3">    parameters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> json.dumps(</span>
<span id="cb7-4">        {</span>
<span id="cb7-5">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"registrationkey"</span>: bls_key,</span>
<span id="cb7-6">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seriesid"</span>: [table_id],</span>
<span id="cb7-7">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"startyear"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2018"</span>,</span>
<span id="cb7-8">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"endyear"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024"</span>,</span>
<span id="cb7-9">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"calculations"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"true"</span>,</span>
<span id="cb7-10">        }</span>
<span id="cb7-11">    )</span></code></pre></div></div>
</div>
<p>Be sure to add <code>.env</code> to your <code>.gitignore</code> file if using Git, so that you don’t accidentally upload your secret keys:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>.gitignore</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" data-filename=".gitignore" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.env</span></span></code></pre></div></div>
</div>




<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>R users: we’re fortunate to have <code>Sys.setenv()</code> and <code>usethis::edit_r_environ()</code>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <guid>https://ivelasq.rbind.io/til-python/python-env/</guid>
  <pubDate>Tue, 18 Jun 2024 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to add .gitkeep to a bunch of folders</title>
  <link>https://ivelasq.rbind.io/til-other/gitkeep/</link>
  <description><![CDATA[ 




<p>I needed to add a bunch of folders to a GitHub repository. I knew their names but didn’t have any documents for them yet.</p>
<p>GitHub only allows you to push folders that contain something. However, you can use <code>.gitkeep</code> files to make “empty” folders.</p>
<p>First, I created all the folders that I needed.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">letters <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-2">  <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;">"a"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>)</span>
<span id="cb1-3"></span>
<span id="cb1-4">foldernames <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-5">  <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;">"2022/"</span>, letters)</span>
<span id="cb1-6">  </span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(foldernames, dir.create, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">recursive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<p>Next, I navigated to the new “2022” folder and created the <code>.gitkeep</code> files.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> 2022</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">find</span> . <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-type</span> d <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-empty</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-not</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-path</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./.git/*"</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-exec</span> touch {}/.gitkeep</span></code></pre></div></div>



 ]]></description>
  <guid>https://ivelasq.rbind.io/til-other/gitkeep/</guid>
  <pubDate>Fri, 12 Aug 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to count the number of characters (or bytes or width)</title>
  <link>https://ivelasq.rbind.io/til-r/nzchar/</link>
  <description><![CDATA[ 




<p>From the <a href="https://rdrr.io/r/base/nchar.html">documentation</a>, <code>nzchar()</code> is a fast way to find out if elements of a character vector are non-empty strings. It returns <code>TRUE</code> for non-empty strings and <code>FALSE</code> for empty strings.</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="">Not Empty Vector</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="">Empty Vector</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;"># This is not empty</span></span>
<span id="cb1-2"><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;">"R_LIBS_USER"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "/Users/ivelasq/Library/R/x86_64/4.3/library"</code></pre>
</div>
</div>
<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;"># This returns TRUE</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nzchar</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;">"R_LIBS_USER"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</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="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is empty</span></span>
<span id="cb5-2"><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;">"test"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] ""</code></pre>
</div>
</div>
<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;"># This returns FALSE</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nzchar</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;">"test"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div>
</div>
</div>
</div>
<center>
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
TIL: nzchar(). Super useful when working with environment variables in R.<br><br>also, <a href="https://twitter.com/hashtag/asciicast?src=hash&amp;ref_src=twsrc%5Etfw">#asciicast</a> is amazing! install the GIF converter with remotes::install_github('r-lib/asciicast', ref = remotes::github_pull(24)) <a href="https://twitter.com/hashtag/rstats?src=hash&amp;ref_src=twsrc%5Etfw">#rstats</a> h/t <a href="https://twitter.com/GaborCsardi?ref_src=twsrc%5Etfw"><span class="citation" data-cites="GaborCsardi">@GaborCsardi</span></a> <a href="https://t.co/pCZQLCNaDl">pic.twitter.com/pCZQLCNaDl</a>
</p>
— Isabella Velásquez (<span class="citation" data-cites="ivelasq3">@ivelasq3</span>) <a href="https://twitter.com/ivelasq3/status/1524193394037342211?ref_src=twsrc%5Etfw">May 11, 2022</a>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</center>



 ]]></description>
  <guid>https://ivelasq.rbind.io/til-r/nzchar/</guid>
  <pubDate>Tue, 10 May 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to create a GIF of code and its output</title>
  <link>https://ivelasq.rbind.io/til-r/asciicast/</link>
  <description><![CDATA[ 




<p>We can create a GIF of code and its output using <a href="https://github.com/r-lib/asciicast">asciicast</a>.</p>
<p>Install the development version:</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">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r-lib/asciicast'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ref =</span> remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">github_pull</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>)) </span></code></pre></div></div>
</div>
<p>Create a file called <code>nzchar.R</code> with specifications for the GIF and the code to run.</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;">#' Title: Using nzchar</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Columns: 60</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Rows: 18</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is not empty</span></span>
<span id="cb2-6"><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;">"R_LIBS_USER"</span>)</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is empty</span></span>
<span id="cb2-9"><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;">"test"</span>)</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This returns TRUE</span></span>
<span id="cb2-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nzchar</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;">"R_LIBS_USER"</span>))</span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This returns FALSE</span></span>
<span id="cb2-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nzchar</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;">"test"</span>))</span></code></pre></div></div>
</div>
<p>Create another file that creates the GIF:</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;">#' Title: Using nzchar</span></span>
<span id="cb3-2"></span>
<span id="cb3-3">src <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nzchar.R"</span></span>
<span id="cb3-4">cast <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> asciicast<span 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</span>(src)</span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;&lt;</span></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># `cast` is an `asciicast` object, which has some metadata and the</span></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># recording itself:</span></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;&lt;</span></span>
<span id="cb3-10"></span>
<span id="cb3-11">cast</span>
<span id="cb3-12"></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;&lt;</span></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># You can write `cast` to a GIF file with the version installed above.</span></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;&lt;</span></span>
<span id="cb3-16"></span>
<span id="cb3-17">svg <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;">"gif"</span>)</span>
<span id="cb3-18">asciicast<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_gif</span>(cast, svg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theme =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"monokai"</span>)</span></code></pre></div></div>
</div>
<p>Really fun for adding to tweets!</p>
<p><img src="https://ivelasq.rbind.io/til-r/asciicast/example.gif" class="img-fluid" alt="An animation created with asciicast that shows that nzchar() returns TRUE when a string is nonempty and FALSE when a string is empty, using Sys.getenv() to query existing 'R_LIBS_USER' and nonexisting 'test' environment variables."></p>



 ]]></description>
  <guid>https://ivelasq.rbind.io/til-r/asciicast/</guid>
  <pubDate>Tue, 10 May 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>How to filter on conditions for more than one variable at the time</title>
  <link>https://ivelasq.rbind.io/til-r/filter-if-any/</link>
  <description><![CDATA[ 




<p>TIL I learned that you can filter on conditions for more than one variable at a time using <code>if_any()</code> or <code>if_all()</code>.</p>
<center>
<blockquote class="twitter-tweet blockquote">
<p lang="en" dir="ltr">
Just to add to the confusion (😅) I think you still do use it for mutate but not for filter?<br><br>⚠️ Using <code>across()</code> in <code>filter()</code> is deprecated, use <code>if_any()</code> or <code>if_all()</code>.
</p>
— Lucy D’Agostino McGowan (<span class="citation" data-cites="LucyStats">@LucyStats</span>) <a href="https://twitter.com/LucyStats/status/1506026504618221579?ref_src=twsrc%5Etfw">March 21, 2022</a>
</blockquote>
<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</center>
<p>Turns out that <code>across()</code> is only for selecting functions (like <code>summarize()</code> and <code>mutate()</code>). This was announced in <a href="https://www.tidyverse.org/blog/2021/02/dplyr-1-0-4-if-any/">dplyr 1.0.4</a>.</p>
<p>You use <code>if_any()</code> vs.&nbsp;<code>if_all()</code> depending if you need to match some vs.&nbsp;all columns.</p>
<p><code>if_any()</code>:</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">mtcars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>() <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;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>(), as.integer)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_any</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span>), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> . <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 19 × 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
 1    21     6   258   110     3     3    19     1     0     3     1
 2    18     8   360   175     3     3    17     0     0     3     2
 3    18     6   225   105     2     3    20     1     0     3     1
 4    14     8   360   245     3     3    15     0     0     3     4
 5    24     4   146    62     3     3    20     1     0     4     2
 6    22     4   140    95     3     3    22     1     0     4     2
 7    19     6   167   123     3     3    18     1     0     4     4
 8    17     6   167   123     3     3    18     1     0     4     4
 9    16     8   275   180     3     4    17     0     0     3     3
10    17     8   275   180     3     3    17     0     0     3     3
11    15     8   275   180     3     3    18     0     0     3     3
12    10     8   472   205     2     5    17     0     0     3     4
13    10     8   460   215     3     5    17     0     0     3     4
14    14     8   440   230     3     5    17     0     0     3     4
15    21     4   120    97     3     2    20     1     0     3     1
16    15     8   318   150     2     3    16     0     0     3     2
17    15     8   304   150     3     3    17     0     0     3     2
18    13     8   350   245     3     3    15     0     0     3     4
19    19     8   400   175     3     3    17     0     0     3     2</code></pre>
</div>
</div>
<p><code>if_all()</code>:</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">mtcars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tibble</span>() <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;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>(), as.integer)) <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;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"am"</span>), <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> . <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 19 × 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
 1    21     6   258   110     3     3    19     1     0     3     1
 2    18     8   360   175     3     3    17     0     0     3     2
 3    18     6   225   105     2     3    20     1     0     3     1
 4    14     8   360   245     3     3    15     0     0     3     4
 5    24     4   146    62     3     3    20     1     0     4     2
 6    22     4   140    95     3     3    22     1     0     4     2
 7    19     6   167   123     3     3    18     1     0     4     4
 8    17     6   167   123     3     3    18     1     0     4     4
 9    16     8   275   180     3     4    17     0     0     3     3
10    17     8   275   180     3     3    17     0     0     3     3
11    15     8   275   180     3     3    18     0     0     3     3
12    10     8   472   205     2     5    17     0     0     3     4
13    10     8   460   215     3     5    17     0     0     3     4
14    14     8   440   230     3     5    17     0     0     3     4
15    21     4   120    97     3     2    20     1     0     3     1
16    15     8   318   150     2     3    16     0     0     3     2
17    15     8   304   150     3     3    17     0     0     3     2
18    13     8   350   245     3     3    15     0     0     3     4
19    19     8   400   175     3     3    17     0     0     3     2</code></pre>
</div>
</div>
<p>Any tidyselect usage is allowable inside <code>if_*()</code> just like inside <code>across()</code>, so they work very similarly.</p>
<p><strong>Thanks to <a href="https://github.com/gvelasq"><span class="citation" data-cites="gvelasq">@gvelasq</span></a> for his explanation.</strong></p>



 ]]></description>
  <guid>https://ivelasq.rbind.io/til-r/filter-if-any/</guid>
  <pubDate>Mon, 21 Mar 2022 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
