Skip to content

Commit 6eee4f2

Browse files
updates
1 parent e30b664 commit 6eee4f2

File tree

6 files changed

+168
-8
lines changed

6 files changed

+168
-8
lines changed

Diff for: demo/error_inspector.R

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ f(factor("a,b"))
1313
# IDE Error Inspector yes triggered
1414
g <- function(x) f(x)
1515
g(factor("a,b"))
16+
17+

Diff for: demo/trace-strsplit.R

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
f <- function(x) { strsplit(x, ",") }
3+
g <- function(x) { f(x) }
4+
5+
6+
x <- as.list(body(g))
7+
g(factor("a,b"))
8+
9+
10+
#trace(g, browser, at = list(c(2)))
11+
#g(factor("a,b"))
12+
#untrace(g)
13+
#
14+
#
15+
#trace(g, recover)
16+
#g("a")
17+
#untrace(g)

Diff for: demo/trace.R

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# IDE Error Inspector not triggered
2+
f <- function(x) {x + 1}
3+
4+
g <- function(x) {f(x)}
5+
6+
7+
#x <- as.list(body(g))
8+
#
9+
#
10+
#trace(g, browser, at = list(c(2)))
11+
#g("a")
12+
#untrace(g)
13+
#
14+
#
15+
#trace(g, recover)
16+
#g("a")
17+
#untrace(g)

Diff for: demo/warning.R

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
options(warn = 0)

Diff for: index.html

+59-6
Original file line numberDiff line numberDiff line change
@@ -948,14 +948,67 @@ <h2>debugonce()</h2>
948948
<li><p>one time only!</p></li>
949949
<li><p>interactive debugger initiated a <strong>single</strong> time when <code>g()</code> is executed</p></li>
950950
</ul>
951-
</section></section>
952-
<section>
953-
<section id="advanced-debugging" class="title-slide slide level1 center">
954-
<h1>Advanced debugging</h1>
951+
</section>
952+
<section id="trace-overview" class="slide level2">
953+
<h2><code>trace()</code> overview</h2>
954+
<p><br></p>
955+
<ul>
956+
<li><p><code>trace()</code> allows you to insert debugging code <br>(e.g., a call to <code>browser</code> or <code>recover</code>)<br> at chosen places in any function.</p></li>
957+
<li><p>a call to <code>untrace</code> cancels the tracing.</p></li>
958+
</ul>
959+
</section>
960+
<section id="trace-where-to-insert" class="slide level2">
961+
<h2><code>trace()</code> where to insert</h2>
962+
<p><br></p>
963+
<div class="cell">
964+
<div class="sourceCode cell-code" id="cb17" data-code-line-numbers="3"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1"></a>f <span class="ot">&lt;-</span> <span class="cf">function</span>(x) { x <span class="sc">+</span> <span class="dv">1</span> }</span>
965+
<span id="cb17-2"><a href="#cb17-2"></a>g <span class="ot">&lt;-</span> <span class="cf">function</span>(x) { <span class="fu">f</span>(x) }</span>
966+
<span id="cb17-3"><a href="#cb17-3"></a>x <span class="ot">&lt;-</span> <span class="fu">as.list</span>(<span class="fu">body</span>(g))</span>
967+
<span id="cb17-4"><a href="#cb17-4"></a>x</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
968+
<div class="cell-output cell-output-stdout">
969+
<pre><code>[[1]]
970+
`{`
955971

972+
[[2]]
973+
f(x)</code></pre>
974+
</div>
975+
</div>
976+
<p><br></p>
977+
<p>use <code>as.list</code> to investigate 🧐 the function</p>
978+
<div class="fragment">
979+
<p><br></p>
980+
<p>⚠️ <code>trace()</code> requires <code>{</code> in function definition</p>
981+
</div>
982+
</section>
983+
<section id="traceintiate-browser" class="slide level2">
984+
<h2><code>trace()</code>intiate browser</h2>
985+
<p><br></p>
986+
<div class="cell">
987+
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1"></a><span class="co"># specify browser to initiate at second step in function</span></span>
988+
<span id="cb19-2"><a href="#cb19-2"></a><span class="fu">trace</span>(g, browser, <span class="at">at =</span> <span class="fu">list</span>(<span class="fu">c</span>(<span class="dv">2</span>)))</span>
989+
<span id="cb19-3"><a href="#cb19-3"></a></span>
990+
<span id="cb19-4"><a href="#cb19-4"></a><span class="co"># call function to initiate browser</span></span>
991+
<span id="cb19-5"><a href="#cb19-5"></a><span class="fu">g</span>(<span class="st">"a"</span>)</span>
992+
<span id="cb19-6"><a href="#cb19-6"></a></span>
993+
<span id="cb19-7"><a href="#cb19-7"></a><span class="co"># cancel tracing</span></span>
994+
<span id="cb19-8"><a href="#cb19-8"></a><span class="fu">untrace</span>(g)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
995+
</div>
956996
</section>
957-
<section id="trace" class="slide level2">
958-
<h2>Trace</h2>
997+
<section id="trace-intiate-recover" class="slide level2">
998+
<h2><code>trace()</code> intiate recover</h2>
999+
<p><br></p>
1000+
<p>general idea, buggy on this example</p>
1001+
<p><br></p>
1002+
<div class="cell">
1003+
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1"></a><span class="co"># specify recover to initiate at second step in function</span></span>
1004+
<span id="cb20-2"><a href="#cb20-2"></a><span class="fu">trace</span>(g, recover, <span class="at">at =</span> <span class="fu">list</span>(<span class="fu">c</span>(<span class="dv">2</span>)))</span>
1005+
<span id="cb20-3"><a href="#cb20-3"></a></span>
1006+
<span id="cb20-4"><a href="#cb20-4"></a><span class="co"># call function to initiate browser</span></span>
1007+
<span id="cb20-5"><a href="#cb20-5"></a><span class="fu">g</span>(<span class="st">"a"</span>)</span>
1008+
<span id="cb20-6"><a href="#cb20-6"></a></span>
1009+
<span id="cb20-7"><a href="#cb20-7"></a><span class="co"># cancel tracing</span></span>
1010+
<span id="cb20-8"><a href="#cb20-8"></a><span class="fu">untrace</span>(g)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
1011+
</div>
9591012
</section></section>
9601013
<section>
9611014
<section id="special-cases" class="title-slide slide level1 center">

Diff for: index.qmd

+72-2
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,84 @@ g("a")
544544

545545
- interactive debugger initiated a **single** time when `g()` is executed
546546

547-
# Advanced debugging
548547

549-
## Trace
548+
## `trace()` overview
549+
550+
<br>
551+
552+
* `trace()` allows you to insert debugging code <br>(e.g., a call to `browser` or `recover`)<br> at chosen places in any function.
553+
554+
* a call to `untrace` cancels the tracing.
555+
556+
## `trace()` where to insert
557+
558+
<br>
559+
560+
```{r, eval=TRUE, echo=TRUE,`code-line-numbers`="3"}
561+
f <- function(x) { x + 1 }
562+
g <- function(x) { f(x) }
563+
x <- as.list(body(g))
564+
x
565+
```
566+
567+
<br>
568+
569+
use `as.list` to investigate 🧐 the function
570+
571+
<br>
572+
573+
⚠️ `trace()` requires `{` in function definition
574+
575+
## `trace()`intiate browser
576+
577+
<br>
578+
579+
```{r, eval=FALSE, echo=TRUE}
580+
# specify browser to initiate at second step in function
581+
trace(g, browser, at = list(c(2)))
582+
583+
# call function to initiate browser
584+
g("a")
585+
586+
# cancel tracing
587+
untrace(g)
588+
```
589+
590+
591+
## `trace()` intiate recover
592+
593+
<br>
594+
595+
general idea, buggy on this example
596+
597+
<br>
598+
599+
```{r, eval=FALSE, echo=TRUE}
600+
# specify recover to initiate at second step in function
601+
trace(g, recover, at = list(c(2)))
602+
603+
# call function to initiate browser
604+
g("a")
605+
606+
# cancel tracing
607+
untrace(g)
608+
```
609+
550610

551611
# Special cases
552612

553613
## Warnings
554614

615+
If you want to dig deeper into a warning, you can convert them to errors.
616+
617+
```{r}
618+
#options(warn = 0) # default, warnings deferred to end
619+
options(warn = 1) # print warning when it happens
620+
options(warn = 2) # converts all warnings to errors, allows you to use regular debugging tools
621+
```
622+
623+
624+
555625
## Piped expressions
556626

557627
## Rmarkdown

0 commit comments

Comments
 (0)