<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>debug | B101nfo</title>
    <link>https://llrs.dev/tags/debug/</link>
      <atom:link href="https://llrs.dev/tags/debug/index.xml" rel="self" type="application/rss+xml" />
    <description>debug</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>If it is code you can copy and reuse (MIT) if it is text, please cite and reuse CC-BY 2024.</copyright><lastBuildDate>Sat, 25 Jan 2020 00:00:00 +0000</lastBuildDate>
    <image>
      <url>img/map[gravatar:%!s(bool=false) shape:circle]</url>
      <title>debug</title>
      <link>https://llrs.dev/tags/debug/</link>
    </image>
    
    <item>
      <title>Debugging only when needed</title>
      <link>https://llrs.dev/post/2020/01/25/debugging-conditional/</link>
      <pubDate>Sat, 25 Jan 2020 00:00:00 +0000</pubDate>
      <guid>https://llrs.dev/post/2020/01/25/debugging-conditional/</guid>
      <description>
&lt;script src=&#34;https://llrs.dev/post/2020/01/25/debugging-conditional/index_files/header-attrs/header-attrs.js&#34;&gt;&lt;/script&gt;


&lt;p&gt;One of the worst thing I experienced is having a bug in a nested call of functions.
If it is not simple the problems comes to isolate when and why does it occur.
Here I explore something that I found once on twitter but haven’t find explained in Advanced R or other blogs.&lt;/p&gt;
&lt;div id=&#34;function-stack&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Function stack&lt;/h1&gt;
&lt;p&gt;A simple function to debug is this one:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;myfunction1 &amp;lt;- function(x, y) {
  if (sum(x &amp;gt; 0) &amp;gt;= 5) {
    x + y
  } else {
    x - y
  }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can use it:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;myfunction1(runif(10), 5)
##  [1] 5.703426 5.516683 5.217670 5.943402 5.173702 5.137270 5.768233 5.132304
##  [9] 5.513719 5.404423
myfunction1(runif(4), 5)
## [1] -4.706239 -4.447211 -4.078848 -4.997719&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However if our input has a NA we’ll get an error:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;x &amp;lt;- runif(10)
x[5] &amp;lt;- NA
myfunction1(x, 5)
## Error in if (sum(x &amp;gt; 0) &amp;gt;= 5) {: missing value where TRUE/FALSE needed&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem is easy to find but lets assume it is not.
The missing value where TRUE/FALSE needed indicates that we get a NA.
So to find where we can use &lt;code&gt;browser&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; the expression argument as this:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;myfunction2 &amp;lt;-  function(x, y) {
  browser(expr = any(is.na(x)))
  if (sum(x &amp;gt; 0) &amp;gt;= 5) {
    x + y
  } else {
    x - y
  }
}
myfunction2(x, 5)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which lead us to this:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;https://llrs.dev/post/2020-01-debugging-only-when-needed_files/Screenshot%20from%202020-01-25%2013-21-03.png&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;debug with expr&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;If we use other input we can use the function as usual without triggering the debug:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;myfunction2(runif(6), 5)
## [1] 5.231687 5.008704 5.614821 5.561843 5.745173 5.026838&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So if you are developing a package or functions and find an error on some calculation and you don’t want to press next or skip a loop just use the &lt;code&gt;expr&lt;/code&gt; argument of browser to just jump into the problematic call.&lt;/p&gt;
&lt;div id=&#34;reproducibility&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Reproducibility&lt;/h3&gt;
&lt;details&gt;
&lt;pre&gt;&lt;code&gt;## ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 4.0.1 (2020-06-06)
##  os       Ubuntu 20.04.1 LTS          
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  ctype    en_US.UTF-8                 
##  tz       Europe/Madrid               
##  date     2021-01-08                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
##  package     * version date       lib source                           
##  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.1)                   
##  blogdown      0.21.84 2021-01-07 [1] Github (rstudio/blogdown@c4fbb58)
##  bookdown      0.21    2020-10-13 [1] CRAN (R 4.0.1)                   
##  cli           2.2.0   2020-11-20 [1] CRAN (R 4.0.1)                   
##  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.1)                   
##  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.1)                   
##  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.1)                   
##  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.1)                   
##  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.1)                   
##  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.1)                   
##  knitr         1.30    2020-09-22 [1] CRAN (R 4.0.1)                   
##  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.1)                   
##  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.1)                   
##  rmarkdown     2.6     2020-12-14 [1] CRAN (R 4.0.1)                   
##  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.1)                   
##  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.1)                   
##  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.1)                   
##  withr         2.3.0   2020-09-22 [1] CRAN (R 4.0.1)                   
##  xfun          0.20    2021-01-06 [1] CRAN (R 4.0.1)                   
##  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.1)                   
## 
## [1] /home/lluis/bin/R/4.0.1/lib/R/library&lt;/code&gt;&lt;/pre&gt;
&lt;/details&gt;
&lt;/div&gt;
&lt;/div&gt;
</description>
    </item>
    
  </channel>
</rss>
