<?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>Jour Fixe R Sessions</title>
<link>https://jourfixe.github.io/</link>
<atom:link href="https://jourfixe.github.io/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.5.56</generator>
<lastBuildDate>Wed, 09 Jul 2025 23:00:00 GMT</lastBuildDate>
<item>
  <title>R tips July 2025</title>
  <dc:creator>Nikita M.</dc:creator>
  <link>https://jourfixe.github.io/posts/July_2025/July_2025.html</link>
  <description><![CDATA[ 




<section id="debugging-in-r-practical-tools-and-techniques" class="level1">
<h1>Debugging in R: Practical Tools and Techniques</h1>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Debugging is essential in programming for identifying and resolving errors effectively. Good debugging skills enhance productivity and ensure code reliability.</p>
<p><strong>Topics Covered:</strong></p>
<ul>
<li><code>browser()</code></li>
<li><code>debugonce()</code></li>
<li><code>traceback()</code></li>
<li><code>recover()</code></li>
<li>Debugging Shiny applications</li>
</ul>
<hr>
</section>
<section id="the-browser-function" class="level2">
<h2 class="anchored" data-anchor-id="the-browser-function">The <code>browser()</code> Function</h2>
<p><strong>Purpose:</strong> Pauses execution, allowing interactive inspection.</p>
<p><strong>Example:</strong></p>
<div class="cell">
<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">my_function <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="cb1-2">    y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb1-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">browser</span>()</span>
<span id="cb1-4">    z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y <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="cb1-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(z)</span>
<span id="cb1-6">}</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_function</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div>
</div>
<p><strong>How to use:</strong></p>
<ul>
<li>Inspect variables directly.</li>
<li>Step through code (<code>n</code> for next, <code>c</code> to continue).</li>
</ul>
<hr>
</section>
<section id="the-debugonce-function" class="level2">
<h2 class="anchored" data-anchor-id="the-debugonce-function">The <code>debugonce()</code> Function</h2>
<p><strong>Purpose:</strong> Debugs a function exactly once.</p>
<p><strong>Example:</strong></p>
<div class="cell">
<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">add_numbers <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>(a, b) {</span>
<span id="cb2-2">    sum <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b</span>
<span id="cb2-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(sum)</span>
<span id="cb2-4">}</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">debugonce</span>(add_numbers)</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_numbers</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>)</span></code></pre></div>
</div>
<p><strong>How to use:</strong></p>
<ul>
<li>Automatically enters debug mode at next call.</li>
<li>Useful for inspecting unexpected errors inside functions.</li>
</ul>
<hr>
</section>
<section id="the-traceback-function" class="level2">
<h2 class="anchored" data-anchor-id="the-traceback-function">The <code>traceback()</code> Function</h2>
<p><strong>Purpose:</strong> Identifies the call sequence leading to an error.</p>
<p><strong>Example:</strong></p>
<div class="cell">
<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">func_a <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">func_b</span>() }</span>
<span id="cb3-2">func_b <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 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;">"An error occurred!"</span>) }</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">func_a</span>()</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">traceback</span>()</span></code></pre></div>
</div>
<p><strong>How to use:</strong></p>
<ul>
<li>Quickly identifies problematic calls.</li>
<li>Ideal for debugging nested functions.</li>
</ul>
<hr>
</section>
<section id="the-recover-function" class="level2">
<h2 class="anchored" data-anchor-id="the-recover-function">The <code>recover()</code> Function</h2>
<p><strong>Purpose:</strong> Interactive debugging at the moment an error occurs.</p>
<p>Set this first:</p>
<div class="cell">
<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;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> recover)</span>
<span id="cb4-2"></span>
<span id="cb4-3">f1 <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f2</span>(x) }</span>
<span id="cb4-4">f2 <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f3</span>(x) }</span>
<span id="cb4-5">f3 <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 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;">"Unexpected error!"</span>) }</span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f1</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</div>
<p><strong>How to use:</strong></p>
<ul>
<li>Provides menu to enter error environments.</li>
<li>Inspect variables interactively.</li>
</ul>
<hr>
</section>
<section id="debugging-shiny-applications" class="level2">
<h2 class="anchored" data-anchor-id="debugging-shiny-applications">Debugging Shiny Applications</h2>
<p>Debugging Shiny can be challenging. Key approaches include:</p>
<ul>
<li>Use <code>browser()</code> in reactive contexts:</li>
</ul>
<div class="cell">
<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>(shiny)</span>
<span id="cb5-2"></span>
<span id="cb5-3">ui <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;">fluidPage</span>(</span>
<span id="cb5-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numericInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"num"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number:"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb5-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">textOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"result"</span>)</span>
<span id="cb5-6">)</span>
<span id="cb5-7"></span>
<span id="cb5-8">server <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(input, output) {</span>
<span id="cb5-9">    output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>result <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;">renderText</span>({</span>
<span id="cb5-10">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">browser</span>()</span>
<span id="cb5-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Double:"</span>, input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>num <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="cb5-12">    })</span>
<span id="cb5-13">}</span>
<span id="cb5-14"></span>
<span id="cb5-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shinyApp</span>(ui, server)</span></code></pre></div>
</div>
<ul>
<li>Monitor logs and console outputs.</li>
</ul>
<hr>
</section>
<section id="summary-key-takeaways" class="level2">
<h2 class="anchored" data-anchor-id="summary-key-takeaways">Summary: Key Takeaways</h2>
<ul>
<li><code>browser()</code> - Interactive inspection.</li>
<li><code>debugonce()</code> - Single-use debugging.</li>
<li><code>traceback()</code> - Error call sequence.</li>
<li><code>recover()</code> - Interactive error handling.</li>
<li>Shiny apps - Combine <code>browser()</code>, logging, and output checks.</li>
</ul>
<p>Effective debugging greatly enhances coding efficiency and reliability.</p>
<hr>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li><a href="https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Debugging">R Debugging manual – CRAN</a></li>
<li><a href="https://adv-r.hadley.nz/debugging.html">Advanced R: Debugging R code</a></li>
<li><a href="https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio">RStudio: Debugging tools</a></li>
<li><a href="https://shiny.rstudio.com/articles/debugging.html">Shiny – Debugging guide</a></li>
</ul>


</section>
</section>

 ]]></description>
  <category>R-tips</category>
  <guid>https://jourfixe.github.io/posts/July_2025/July_2025.html</guid>
  <pubDate>Wed, 09 Jul 2025 23:00:00 GMT</pubDate>
</item>
<item>
  <title>R tips March 2025</title>
  <dc:creator>Nikita M.</dc:creator>
  <link>https://jourfixe.github.io/posts/March_2025/March2025.html</link>
  <description><![CDATA[ 




<section id="floating-point-precision-in-r" class="level2">
<h2 class="anchored" data-anchor-id="floating-point-precision-in-r">Floating-Point Precision in R</h2>
<p>Floating-point numbers in R are subject to precision limitations due to <a href="https://en.wikipedia.org/wiki/IEEE_754">IEEE 754</a> representation. This section explores how seemingly simple operations can lead to unexpected results.</p>
<section id="example-1-floating-point-comparison-fails" class="level3">
<h3 class="anchored" data-anchor-id="example-1-floating-point-comparison-fails"><strong>Example 1: Floating-Point Comparison Fails</strong></h3>
<div class="cell">
<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">3</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
<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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="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;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div>
<p><strong>Why does this return <code>FALSE</code>?</strong></p>
<ul>
<li><p>Due to how floating-point arithmetic works in computers, numbers like <code>0.1</code>, <code>0.3</code>, and <code>0.3 / 3</code> are stored in binary.</p></li>
<li><p>Some decimal fractions <strong>cannot be represented exactly in binary</strong>, leading to tiny rounding errors. E.g.:</p></li>
</ul>
<div class="cell">
<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;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%a"</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "0x1.999999999999ap-4"</code></pre>
</div>
</div>
<p><strong>Explanation:</strong> - 0x indicates a hexadecimal number. - 1.999999999999a represents the normalised significand (mantissa) in base-16 (hexadecimal).</p>
<ul>
<li><p>p-4 is the exponent: p represents the power of 2 (exponent in base-2); 4 means the value is multiplied by 2⁻⁴.</p></li>
<li><p>Even though mathematically <code>.1</code> and <code>.3 / 3</code> should be the same, their internal representations are not exactly equal.</p></li>
</ul>
</section>
<section id="example-2-inspecting-the-actual-value-stored-in-memory" class="level3">
<h3 class="anchored" data-anchor-id="example-2-inspecting-the-actual-value-stored-in-memory"><strong>Example 2: Inspecting the Actual Value Stored in Memory</strong></h3>
<div class="cell">
<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.1</code></pre>
</div>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.09999999999999999</code></pre>
</div>
</div>
<p><strong>Explanation:</strong></p>
<ul>
<li><p>The first output shows what R normally prints, which is rounded for display.</p></li>
<li><p>The second output uses <code>digits=16</code> to show more of the actual stored precision.</p></li>
<li><p>This reveals that <code>0.3 / 3</code> is actually stored as <code>0.09999999999999999</code>, not exactly <code>0.1</code>.</p></li>
</ul>
</section>
<section id="example-3-using-all.equal-instead-of" class="level3">
<h3 class="anchored" data-anchor-id="example-3-using-all.equal-instead-of"><strong>Example 3: Using <code>all.equal()</code> Instead of <code>==</code></strong></h3>
<div class="cell">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all.equal</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
<p><strong>Why does this return <code>TRUE</code>?</strong></p>
<ul>
<li><p><code>all.equal()</code> does not check for <strong>exact</strong> equality but allows a <strong>small tolerance</strong> to account for floating-point errors.</p></li>
<li><p>It is the <strong>preferred way</strong> to compare floating-point numbers in R.</p></li>
<li><p>Do not use all.equal directly in <code>if</code> expressions—either use <code>isTRUE(all.equal(...))</code> or identical if appropriate. If x and y are not equal, it returns a character string like: “Mean relative difference: 5.551115e-17”. The if statement expects <code>TRUE</code> or <code>FALSE</code>, but it receives a string, causing an error.</p></li>
</ul>
</section>
<section id="example-4-machine-epsilon-adjusting-tolerance" class="level3">
<h3 class="anchored" data-anchor-id="example-4-machine-epsilon-adjusting-tolerance"><strong>Example 4: Machine Epsilon &amp; Adjusting Tolerance</strong></h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">.Machine<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>double.eps</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.220446e-16</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all.equal</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</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;">tolerance =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.2e-17</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Mean relative difference: 1.387779e-16"</code></pre>
</div>
</div>
<p><strong>Explanation:</strong></p>
<ul>
<li><p><code>.Machine$double.eps</code> is the smallest possible difference that R can distinguish between two floating-point numbers.</p></li>
<li><p>The default tolerance of <code>all.equal()</code> is <code>sqrt(.Machine$double.eps)</code>, but we can adjust it manually if needed.</p></li>
</ul>
</section>
<section id="example-5-using-round" class="level3">
<h3 class="anchored" data-anchor-id="example-5-using-round"><strong>Example 5: Using <code>round()</code></strong></h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>round(..., digits = 16)</code> forces R to round the computed value to 16 decimal places, reducing precision issues.</li>
<li>The comparison <code>0.1 == round(0.3 / 3, digits = 16)</code> now returns <code>TRUE</code></li>
</ul>
<p>📖 <strong>More details:</strong> <a href="https://www.burns-stat.com/documents/books/the-r-inferno/">The R Inferno</a></p>
<hr>
</section>
</section>
<section id="memory-allocation-in-r" class="level2">
<h2 class="anchored" data-anchor-id="memory-allocation-in-r">Memory Allocation in R</h2>
<p>Memory management in R determines how objects are stored and how much space they occupy. <strong>Understanding how R allocates memory</strong> can help optimize performance, especially with large datasets.</p>
<section id="example-6-comparing-memory-usage-of-integer-and-double" class="level3">
<h3 class="anchored" data-anchor-id="example-6-comparing-memory-usage-of-integer-and-double"><strong>Example 6: Comparing Memory Usage of Integer and Double</strong></h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lobstr)</span>
<span id="cb19-2"></span>
<span id="cb19-3">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stored as double</span></span>
<span id="cb19-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">obj_size</span>(x)  </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>56 B</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>L   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stored as integer</span></span>
<span id="cb21-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">obj_size</span>(y) </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>56 B</code></pre>
</div>
</div>
<p><strong>Why are both <code>x</code> and <code>y</code> 56 bytes?</strong></p>
<ul>
<li><p>Even though <code>y</code> is an integer (4 bytes) and <code>x</code> is a double (8 bytes), R adds metadata overhead.</p></li>
<li><p>Every object in R carries around 48 bytes of metadata, including type and reference count.</p></li>
<li><p>Memory is aligned in multiples of 8 bytes, so the total allocation rounds up to 56 bytes.</p></li>
</ul>
</section>
<section id="example-7-memory-usage-for-large-vectors" class="level3">
<h3 class="anchored" data-anchor-id="example-7-memory-usage-for-large-vectors"><strong>Example 7: Memory Usage for Large Vectors</strong></h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">x_vec <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;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000000</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Double vector</span></span>
<span id="cb23-2">y_vec <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;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000000</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Integer vector</span></span>
<span id="cb23-3"></span>
<span id="cb23-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">obj_size</span>(x_vec) </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>8.00 MB</code></pre>
</div>
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">obj_size</span>(y_vec)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>4.00 MB</code></pre>
</div>
</div>
<p><strong>Why does the integer vector use half the memory?</strong></p>
<ul>
<li><p>Each integer (<code>5L</code>) takes 4 bytes per element.</p></li>
<li><p>Each double (<code>5</code>) takes 8 bytes per element.</p></li>
<li><p>For large datasets, using integers instead of doubles can save significant memory.</p></li>
</ul>
</section>
<section id="example-8-type-conversion-affects-memory-usage" class="level3">
<h3 class="anchored" data-anchor-id="example-8-type-conversion-affects-memory-usage"><strong>Example 8: Type Conversion Affects Memory Usage</strong></h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">y_vec[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span></span>
<span id="cb27-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">obj_size</span>(y_vec)  </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>8.00 MB</code></pre>
</div>
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">new_vec <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;">lapply</span>(y_vec, as.integer)</span>
<span id="cb29-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">obj_size</span>(new_vec) </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>64.00 MB</code></pre>
</div>
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">new_vec[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span></span>
<span id="cb31-2">new_vec[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] 5

[[2]]
[1] 5.5

[[3]]
[1] 5</code></pre>
</div>
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">obj_size</span>(new_vec) </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>64.00 MB</code></pre>
</div>
</div>
<p><strong>Explanation:</strong></p>
<ul>
<li><p>Assigning <code>5.0</code> to <code>y_vec</code> forces R to convert the entire vector to double, increasing memory usage.</p></li>
<li><p><code>lapply(y_vec, as.integer)</code> converts the values back to integers, but not reducing its size.</p></li>
<li><p>Assigning <code>5.5</code> back forces conversion to double again but only for the selected element because <code>new_vec</code> is now a <code>list</code>.</p></li>
</ul>
<p>📖 <strong>More details:</strong> <a href="https://rdpeng.github.io/RProgDA/the-role-of-physical-memory.html">Mastering Software Development in R</a></p>
</section>
</section>
<section id="partial-matching-in-r" class="level2">
<h2 class="anchored" data-anchor-id="partial-matching-in-r">Partial Matching in R</h2>
<p>R allows <strong>partial matching</strong> of list elements, which can lead to unintended behaviour and difficult-to-debug errors.</p>
<section id="example-9-partial-matching-works" class="level3">
<h3 class="anchored" data-anchor-id="example-9-partial-matching-works"><strong>Example 9: Partial Matching Works</strong></h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">test_function <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) {</span>
<span id="cb35-2">  user_defined <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha=</span>alpha, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power=</span>power, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p=</span>p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0=</span>p0)</span>
<span id="cb35-3">  user_defined <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;">Filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Negate</span>(is.null), user_defined)</span>
<span id="cb35-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(user_defined)</span>
<span id="cb35-5">}</span>
<span id="cb35-6"></span>
<span id="cb35-7">results <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;">test_function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span>
<span id="cb35-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Partial matching</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.8</code></pre>
</div>
<div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(results)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$alpha
[1] 0.05

$power
[1] 0.8</code></pre>
</div>
</div>
<ul>
<li><p><code>test_function</code> - the function takes four optional arguments, all defaulting to NULL, making them optional.</p></li>
<li><p>A named list is created to store all input arguments. If an argument is not provided, it remains NULL.</p></li>
<li><p>Filter(Negate(is.null), user_defined) removes all NULL elements, ensuring only explicitly provided parameters are retained.</p></li>
</ul>
<p><strong>Why is <code>results$p</code> returning <code>0.8</code>?</strong></p>
<ul>
<li>Because <code>p</code> was not provided in <code>test_function()</code> call, but R still allows partial matching.</li>
</ul>
</section>
<section id="example-10-unexpected-partial-matching-in-lists" class="level3">
<h3 class="anchored" data-anchor-id="example-10-unexpected-partial-matching-in-lists"><strong>Example 10: Unexpected Partial Matching in Lists</strong></h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1">x <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;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb39-2">x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p  </span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10</code></pre>
</div>
<div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Avoiding partial matching</span></span>
<span id="cb41-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warnPartialMatchDollar =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb41-3">x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in x$p: partial match of 'p' to 'power'</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10</code></pre>
</div>
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1">x[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p"</span>]]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>NULL</code></pre>
</div>
</div>
<p><strong>Best Practices:</strong></p>
<ul>
<li><p>Use <code>options(warnPartialMatchDollar = TRUE)</code> to warn about unintended partial matches.</p></li>
<li><p>Always use <code>x[["p"]]</code> instead of <code>x$p</code> to ensure explicit matching.</p></li>
</ul>
<p>📖 <strong>More details:</strong> <a href="https://adv-r.hadley.nz/subsetting.html?q=partial#section-1">Advanced R - Subsetting</a></p>
<hr>
</section>
</section>
<section id="extending-subsetting-with-operator" class="level2">
<h2 class="anchored" data-anchor-id="extending-subsetting-with-operator"><strong>Extending Subsetting with <code>[[</code> Operator</strong></h2>
<p>Now that we’ve seen how to extract specific elements from function outputs, we can extend this idea to other cases where structured outputs require efficient subsetting.</p>
<p>Extracting values from structured objects often requires a combination of <code>sapply()</code>, <code>lapply()</code>, and <code>[[</code> to efficiently access named list elements.</p>
<hr>
</section>
<section id="example-11-running-multiple-t.test-and-extracting-p-values" class="level2">
<h2 class="anchored" data-anchor-id="example-11-running-multiple-t.test-and-extracting-p-values"><strong>Example 11: Running Multiple <code>t.test()</code> and Extracting P-values</strong></h2>
<p>When running multiple tests, we can store results in a list and apply subsetting with <code>[[</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-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;">123</span>)</span>
<span id="cb46-2"></span>
<span id="cb46-3">data_list <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="cb46-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)),</span>
<span id="cb46-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)),</span>
<span id="cb46-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test3 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>))</span>
<span id="cb46-7">)</span>
<span id="cb46-8"></span>
<span id="cb46-9">p_values <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;">sapply</span>(data_list, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">[[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p.value"</span>)</span>
<span id="cb46-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(p_values)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>    test1     test2     test3 
0.7672027 0.7040755 0.4752422 </code></pre>
</div>
</div>
<ul>
<li>data_list stores multiple t.test() results in a list.</li>
<li><code>sapply(data_list, "[[", "p.value")</code> extracts p.value from each result.</li>
<li>Similarly, <code>[[</code> can extract “conf.int”, “statistic”, or “parameter”, making it a powerful way to subset structured outputs.</li>
</ul>
<p>📖 <strong>More details:</strong> <a href="https://adv-r.hadley.nz/subsetting.html?q=partial#section-1">Advanced R - Subsetting</a></p>


</section>

 ]]></description>
  <category>R-tips</category>
  <guid>https://jourfixe.github.io/posts/March_2025/March2025.html</guid>
  <pubDate>Thu, 20 Mar 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Running R Computational Tasks</title>
  <dc:creator>Nikita M.</dc:creator>
  <link>https://jourfixe.github.io/posts/August_2024/August2024.html</link>
  <description><![CDATA[ 




<section id="motivation" class="level2">
<h2 class="anchored" data-anchor-id="motivation">Motivation</h2>
<p>Running computationally expensive tasks in R can be challenging, especially when working with <strong>large datasets</strong> and <strong>intensive calculations</strong>. This post provides <strong>practical tips</strong> for efficiently handling background jobs, optimising performance, debugging, and parallelisation.</p>
</section>
<section id="addressing" class="level2">
<h2 class="anchored" data-anchor-id="addressing">Addressing</h2>
<p>Efficient execution of computational tasks ensures that R processes run without blocking user interaction, effectively utilising system resources. The pyramid below illustrates the layers of computational efficiency in R programming.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jourfixe.github.io/posts/August_2024/media/pyramid.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
<p><a href="https://github.com/selinaZitrone/tools_and_tips/">Source: Selina Baldauf</a></p>
<hr>
</section>
<section id="running-background-jobs-in-r" class="level2">
<h2 class="anchored" data-anchor-id="running-background-jobs-in-r">Running Background Jobs in R</h2>
<section id="example-running-an-iterative-task-in-the-background" class="level3">
<h3 class="anchored" data-anchor-id="example-running-an-iterative-task-in-the-background"><strong>Example: Running an Iterative Task in the Background</strong></h3>
<p>Background jobs allow long-running computations** to execute independently, freeing the R console for other tasks.</p>
</section>
<section id="steps-to-run-a-background-job-in-rstudio" class="level3">
<h3 class="anchored" data-anchor-id="steps-to-run-a-background-job-in-rstudio"><strong>Steps to Run a Background Job in RStudio</strong></h3>
<ol type="1">
<li>Open RStudio.</li>
<li>Click on “Jobs” → “Start Job” from the RStudio Environment Pane.</li>
<li>Choose “Run Script in Background” and select <code>script_name.R</code>.</li>
<li>Monitor progress in the Jobs Pane.</li>
</ol>
<div class="columns">
<div class="column" style="width:55%;">
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate a large dataset</span></span>
<span id="cb1-4"><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-5">large_data <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;">data.frame</span>(</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>,</span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>)</span>
<span id="cb1-8">)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Perform some computationally intensive operations</span></span>
<span id="cb1-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) {</span>
<span id="cb1-12">  result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> large_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(id <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;">10</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;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(value))</span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate long-running process</span></span>
<span id="cb1-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Running"</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iteration.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-17">}</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Background job completed.</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
</div><div class="column" style="width:5%;">

</div><div class="column" style="width:40%;">
<section id="section" class="level4">
<h4 class="anchored" data-anchor-id="section"></h4>
<p><a href="media/bg_job_settings.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://jourfixe.github.io/posts/August_2024/media/bg_job_settings.png" class="img-fluid"></a></p>
</section>
</div>
</div>
</section>
<section id="explanation" class="level3">
<h3 class="anchored" data-anchor-id="explanation"><strong>Explanation:</strong></h3>
<ul>
<li><code>Sys.sleep(2)</code> simulates a time-consuming task.</li>
<li>Each iteration performs a grouped summary operation.</li>
<li>Running this in the background allows users to interact with R without interruption.</li>
</ul>
<hr>
</section>
</section>
<section id="responsible-use-of-resources" class="level2">
<h2 class="anchored" data-anchor-id="responsible-use-of-resources">Responsible Use of Resources</h2>
<p>It’s crucial to balance computational demand to prevent overloading shared servers.</p>
<p><strong>Example:</strong> Most non-HPC servers allow to run small to medium-sized tasks, so large-scale simulations should be managed carefully.</p>
<p><a href="media/overloaded.jpg" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://jourfixe.github.io/posts/August_2024/media/overloaded.jpg" class="img-fluid" width="1100"></a></p>
<p><strong>Best Practice:</strong></p>
<ul>
<li><p>Use resource monitoring tools (<code>top</code>, <code>htop</code>) to check CPU usage before launching intensive computations.</p></li>
<li><p>Run long processes inside terminal multiplexers (tmux, screen) to keep jobs running even if the connection is lost.</p></li>
</ul>
<p><strong>🔗 Useful Resources</strong></p>
<p>📖 <a href="https://www.gnu.org/software/screen/manual/screen.html">GNU Screen Documentation</a><br>
📖 <a href="https://github.com/tmux/tmux/wiki">tmux: Terminal Multiplexer</a><br>
📖 Monitoring System Usage in Linux: <a href="https://man7.org/linux/man-pages/man1/top.1.html"><code>top</code> command</a>, <a href="https://htop.dev/"><code>htop</code> command</a></p>
<hr>
</section>
<section id="code-optimisation-in-r" class="level1">
<h1>Code Optimisation in R</h1>
<section id="profiling-computational-performance" class="level2">
<h2 class="anchored" data-anchor-id="profiling-computational-performance">Profiling Computational Performance</h2>
<p>Optimising R code begins with understanding where time and resources are spent. Profiling helps identify bottlenecks - functions or operations that consume excessive computation time. By analysing execution time, memory usage, and function calls, we can make targeted improvements to enhance efficiency.</p>
<section id="why-profiling-matters" class="level3">
<h3 class="anchored" data-anchor-id="why-profiling-matters"><strong>Why Profiling Matters?</strong></h3>
<ul>
<li>Detects slow functions that need optimisation.<br>
</li>
<li>Highlights redundant computations or excessive loops.<br>
</li>
<li>Helps determine whether vectorisation or parallelisation could improve performance.<br>
</li>
<li>Identifies memory-intensive operations that could cause inefficiencies.</li>
</ul>
</section>
<section id="example-using-profvis-to-analyse-execution-time" class="level3">
<h3 class="anchored" data-anchor-id="example-using-profvis-to-analyse-execution-time"><strong>Example: Using <code>profvis()</code> to Analyse Execution Time</strong></h3>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(profvis)</span>
<span id="cb2-2"></span>
<span id="cb2-3">function_1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(size) {</span>
<span id="cb2-4">  large_list <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;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>size, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>))</span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(large_list)</span>
<span id="cb2-6">}</span>
<span id="cb2-7"></span>
<span id="cb2-8">function_2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(iterations) {</span>
<span id="cb2-9">  result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-10">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>iterations) {</span>
<span id="cb2-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) </span>
<span id="cb2-12">    result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> result <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i</span>
<span id="cb2-13">  }</span>
<span id="cb2-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(result)</span>
<span id="cb2-15">}</span>
<span id="cb2-16"></span>
<span id="cb2-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">profvis</span>({</span>
<span id="cb2-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">function_1</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span>
<span id="cb2-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">function_2</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb2-20">})</span></code></pre></div>
</section>
<section id="explanation-1" class="level3">
<h3 class="anchored" data-anchor-id="explanation-1"><strong>Explanation:</strong></h3>
<ul>
<li><code>profvis()</code> visualises where time is spent in an R script.</li>
<li>Helps identify slow functions and optimise code for better efficiency.</li>
</ul>
<p><a href="media/profvis.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://jourfixe.github.io/posts/August_2024/media/profvis.png" class="img-fluid"></a></p>
<hr>
</section>
</section>
<section id="debugging-r-code" class="level2">
<h2 class="anchored" data-anchor-id="debugging-r-code">Debugging R Code</h2>
<p>Writing R code often involves unexpected errors, warnings, or incorrect outputs. Effective debugging strategies help identify issues quickly, saving time and improving code reliability. Debugging tools allow users to step through execution, inspect variable values, and trace errors at runtime.</p>
<section id="why-debugging-matters" class="level3">
<h3 class="anchored" data-anchor-id="why-debugging-matters"><strong>Why Debugging Matters?</strong></h3>
<ul>
<li>Helps locate logic errors and unexpected behavior.<br>
</li>
<li>Allows interactive inspection of variables during execution.<br>
</li>
<li>Enables tracing of function calls to find where an error originates.<br>
</li>
<li>Reduces reliance on excessive <code>print()</code> statements for debugging.</li>
</ul>
</section>
<section id="common-debugging-techniques-in-r" class="level3">
<h3 class="anchored" data-anchor-id="common-debugging-techniques-in-r"><strong>Common Debugging Techniques in R</strong></h3>
<table class="caption-top table">
<colgroup>
<col style="width: 52%">
<col style="width: 47%">
</colgroup>
<thead>
<tr class="header">
<th><strong>Method</strong></th>
<th><strong>Use Case</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>browser()</code></td>
<td>Interactive debugging by pausing function execution.</td>
</tr>
<tr class="even">
<td><code>traceback()</code></td>
<td>Identifies where an error occurred after execution fails.</td>
</tr>
<tr class="odd">
<td><code>debug()</code></td>
<td>Step-through debugging for specific functions.</td>
</tr>
<tr class="even">
<td><code>options(error = recover)</code></td>
<td>Allows selecting the function frame where an error occurred.</td>
</tr>
<tr class="odd">
<td><code>tryCatch()</code></td>
<td>Handles errors gracefully without stopping execution.</td>
</tr>
</tbody>
</table>
</section>
<section id="example-using-browser-for-step-by-step-debugging" class="level3">
<h3 class="anchored" data-anchor-id="example-using-browser-for-step-by-step-debugging"><strong>Example: Using <code>browser()</code> for Step-by-Step Debugging</strong></h3>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">foo <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="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">browser</span>()</span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-4">}</span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foo</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</section>
<section id="explanation-2" class="level3">
<h3 class="anchored" data-anchor-id="explanation-2"><strong>Explanation:</strong></h3>
<ul>
<li><code>browser()</code> allows stepwise execution, helping debug unexpected errors.</li>
<li>The function pauses at <code>browser()</code>, enabling users to inspect variables interactively.</li>
</ul>
<p><video src="media/browser.mp4" class="img-fluid" controls=""><a href="media/browser.mp4">Video</a></video></p>
<hr>
</section>
</section>
<section id="parallelisation-in-r" class="level2">
<h2 class="anchored" data-anchor-id="parallelisation-in-r">Parallelisation in R</h2>
<p>Parallel computing significantly reduces execution time by distributing workloads across multiple CPU cores or machines. R provides several parallel computing strategies, each suited for different use cases, from simple parallel loops to high-performance distributed computing.</p>
<section id="why-parallelisation-matters" class="level3">
<h3 class="anchored" data-anchor-id="why-parallelisation-matters"><strong>Why Parallelisation Matters?</strong></h3>
<ul>
<li>Reduces execution time for large datasets and simulations.<br>
</li>
<li>Utilises multiple CPU cores to improve efficiency.<br>
</li>
<li>Allows for distributed computing across clusters or cloud services.</li>
</ul>
</section>
<section id="common-parallelisation-strategies-in-r" class="level3">
<h3 class="anchored" data-anchor-id="common-parallelisation-strategies-in-r"><strong>Common Parallelisation Strategies in R</strong></h3>
<table class="caption-top table">
<colgroup>
<col style="width: 62%">
<col style="width: 37%">
</colgroup>
<thead>
<tr class="header">
<th><strong>Method</strong></th>
<th><strong>Use Case</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>foreach</code> + <code>doParallel</code></td>
<td>Loops that can be parallelised across CPU cores.</td>
</tr>
<tr class="even">
<td><code>parallel::mclapply()</code></td>
<td>Multi-core version of <code>lapply()</code> (Linux/macOS only).</td>
</tr>
<tr class="odd">
<td><code>future</code> + <code>furrr</code></td>
<td>Asynchronous parallel computing with flexible backends.</td>
</tr>
<tr class="even">
<td><code>mirai</code></td>
<td>High-performance, low-overhead async computing.</td>
</tr>
</tbody>
</table>
<p>Using the right parallelisation approach depends on the type of computation, hardware resources, and scalability requirements.</p>
<hr>
</section>
<section id="example-parallelising-loops-with-foreach" class="level3">
<h3 class="anchored" data-anchor-id="example-parallelising-loops-with-foreach"><strong>Example: Parallelising Loops with <code>foreach</code></strong></h3>
<p>Using parallel computing significantly reduces execution time.</p>
<div class="columns">
<div class="column" style="width:45%;">
<section id="regular-for-loop" class="level4">
<h4 class="anchored" data-anchor-id="regular-for-loop"><strong>Regular <code>for()</code> Loop</strong></h4>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">result <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="cb4-2"></span>
<span id="cb4-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>) {</span>
<span id="cb4-4">  result[i] <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;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> </span>
<span id="cb4-5">}</span></code></pre></div>
</section>
</div><div class="column" style="width:5%;">

</div><div class="column" style="width:50%;">
<section id="parallelised-foreach-loop" class="level4">
<h4 class="anchored" data-anchor-id="parallelised-foreach-loop"><strong>Parallelised <code>foreach()</code> Loop</strong></h4>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(foreach)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(doParallel)</span>
<span id="cb5-3"></span>
<span id="cb5-4">cl <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;">makeCluster</span>(parallel<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">detectCores</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>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoParallel</span>(cl)</span>
<span id="cb5-6"></span>
<span id="cb5-7">result <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;">foreach</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">i =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combine =</span> <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;">%dopar%</span> {</span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> </span>
<span id="cb5-9">}</span>
<span id="cb5-10"></span>
<span id="cb5-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopCluster</span>(cl)</span></code></pre></div>
</section>
</div>
</div>
</section>
<section id="explanation-3" class="level3">
<h3 class="anchored" data-anchor-id="explanation-3"><strong>Explanation:</strong></h3>
<ul>
<li>Standard <code>for()</code> loops execute sequentially, which is slow for large-scale tasks.<br>
</li>
<li><code>foreach()</code> with <code>doParallel</code> allows parallel execution, using multiple CPU cores.<br>
</li>
<li><code>makeCluster(parallel::detectCores() - 1)</code> ensures all but one core are used, preventing system overload.<br>
</li>
</ul>
<p><strong>Best Practice:</strong></p>
<ul>
<li>Always <strong>test code sequentially first</strong> before enabling parallel execution to debug potential issues.<br>
</li>
<li>Ensure efficient memory usage, as parallel processing may increase RAM consumption.<br>
</li>
<li>Use <code>stopCluster(cl)</code> after parallel execution to release system resources.</li>
<li>Using <code>makeCluster(parallel::detectCores() - 1)</code> may overload the system, especially on shared servers or personal machines, leading to performance degradation for other tasks. Best practice suggests limiting the number of cores to 2 in CRAN package examples and vignettes to ensure compatibility across different systems and prevent excessive resource consumption.</li>
</ul>
<p><strong>🔗 Useful Resources</strong><br>
📖 <a href="https://cran.r-project.org/web/packages/foreach/index.html">foreach Package Documentation</a><br>
📖 <a href="https://cran.r-project.org/web/packages/future/vignettes/future-1-overview.html">future Package Documentation</a><br>
📖 <a href="https://cran.r-project.org/web/packages/mirai/index.html">mirai: Lightweight Async Computing</a><br>
📖 <a href="https://cran.r-project.org/web/views/HighPerformanceComputing.html">Parallel Computing in R Guide</a><br>
————————————————————————</p>
</section>
<section id="conclusion" class="level3">
<h3 class="anchored" data-anchor-id="conclusion"><strong>Conclusion</strong></h3>
<p>By implementing these techniques, you can run computational tasks in R more efficiently, reducing runtime and improving workflow performance.</p>


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

 ]]></description>
  <category>R-tips</category>
  <guid>https://jourfixe.github.io/posts/August_2024/August2024.html</guid>
  <pubDate>Wed, 21 Aug 2024 23:00:00 GMT</pubDate>
</item>
</channel>
</rss>
