-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathDebugging.Rmd
More file actions
67 lines (48 loc) · 1.77 KB
/
Copy pathDebugging.Rmd
File metadata and controls
67 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
---
title: "Debugging"
---
```{r echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```
Debugging methods in R6 classes is somewhat different from debugging normal R functions.
RStudio breakpoints don't work in R6 class methods. The simplest way to debug code is to insert a `browser()` line where you want to open a debugging console, reload the classes, and then step through your code. But this involves modifying your code, reloading it, and re-instantiating any objects you want to test.
## Enabling debugging for all future instances of a class
R6 generator objects have a method called `debug()` which will enable debugging for a method. This will affect all instances of the class that are created after the `debug()` is called.
```{r eval=FALSE}
# An example class
Simple <- R6Class("Simple",
public = list(
x = 10,
getx = function() self$x
)
)
# This will enable debugging the getx() method for objects of the 'Simple'
# class that are instantiated in the future.
Simple$debug("getx")
s <- Simple$new()
s$getx()
# [Debugging prompt]
```
To disable debugging for future instances, use the generator's `undebug()` method:
```{r eval=FALSE}
# Disable debugging for future instances:
Simple$undebug("getx")
s <- Simple$new()
s$getx()
#> [1] 10
```
## Debugging methods in individual objects
To enable debugging for a method in a single instance of an object, use the `debug()` function (not the `debug()` method in the generator object).
```{r eval=FALSE}
s <- Simple$new()
debug(s$getx)
s$getx()
# [Debugging prompt]
```
Use `undebug()` to disable debugging on an object's method.
```{r eval=FALSE}
undebug(s$getx)
s$getx()
#> [1] 10
```
You can also use the `trace()` function to specify where in a method you want to drop into the debugging console.