/
index.en.Rmd
326 lines (267 loc) · 7.55 KB
/
index.en.Rmd
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
---
title: Print Debugging (Now with Icecream!)
author: Jonathan Carroll
date: '2023-11-07'
slug: print-debugging-now-with-icecream
categories:
- rstats
- rust
tags:
- rstats
- rust
type: ''
subtitle: ''
image: ''
editor_options:
chunk_output_type: console
---
```{r, setup, include = FALSE}
knitr::opts_chunk$set(
class.output = "bg-success",
class.message = "bg-info text-info",
class.warning = "bg-warning text-warning",
class.error = "bg-danger text-danger"
)
```
Print debugging has its place. Sure, it's not always the *best* way to debug something,
but it can often be the fastest. In this post I describe a useful way to do this in Rust
and how we can get similar behaviour in R.
<!--more-->
Print debugging has its place. Sure, it's not always the *best* way to debug something,
but it can often be the fastest. In this post I describe a useful way to do this in Rust
and how we can get similar behaviour in R.
I love the Rust [`dbg!`](https://dev-doc.rust-lang.org/beta/std/macro.dbg.html)
macro - it wraps a value or expression and prints the result to help debug
what's happening in the middle of some function. If we had some complicated
function that combined some values, e.g.
```rust
fn f(val1: i32, val2: i32) -> i32 {
// do some things
let otherval: i32 = 10;
// final result
val1 + val2 + otherval
}
fn main() {
println!("final result = {}", f(5, 6))
}
```
Running this gives just the final result, as expected
```
final result = 21
```
We might want to check on what the intermediate combinations of `otherval` and `val1`
or `val2` (terrible names, I know). One option is to just print them
```rust
fn f(val1: i32, val2: i32) -> i32 {
// do some things
let otherval: i32 = 10;
println!("{}", otherval + val1);
println!("{}", otherval + val2);
// final result
val1 + val2 + otherval
}
fn main() {
println!("final result = {}", f(5, 6))
}
```
Running this shows the values we printed, but with no context
```
15
16
final result = 21
```
We could add some context manually
```rust
fn f(val1: i32, val2: i32) -> i32 {
// do some things
let otherval: i32 = 10;
println!("first temp val = {}", otherval + val1);
println!("second temp val = {}", otherval + val2);
// final result
val1 + val2 + otherval
}
fn main() {
println!("final result = {}", f(5, 6))
}
```
producing
```
first temp val = 15
second temp val = 16
final result = 21
```
but across an entire codebase, this is going to get messy, fast.
A better option is the `dbg!` macro, which takes an _expression_ (a value, or
something that produces a value) and prints both the expression itself and the resulting
value
```rust
fn f(val1: i32, val2: i32) -> i32 {
// do some things
let otherval: i32 = 10;
dbg!(otherval + val1);
dbg!(otherval + val2);
// final result
val1 + val2 + otherval
}
fn main() {
println!("final result = {}", f(5, 6))
}
```
Running this produces
```
[src/main.rs:15] otherval + val1 = 15
[src/main.rs:16] otherval + val2 = 16
final result = 21
```
and we see we get the file/linenumber context, the expression we wrapped, and the value.
This is extremely useful, and helps me to figure out what's going on in the middle of
some implementation.
One of the downsides is that even if I make a release build, these statements remain,
so I need to go through and remove them all once I'm finished debugging.
A "better" solution is to use a full logging solution like the
[`log`](https://docs.rs/log/latest/log/) crate which enables using different log
levels, turning off logging outside of some threshold, etc... but that seems
more suited to intentional logging, not debugging something that _isn't_
working.
Having played with this in Rust, of course I wanted the same thing in R. I built a
minimal viable proof-of-concept which leverages {rlang} to capture the expression
```{r}
dbg <- function(x) {
ex <- rlang::f_text(rlang::enquos(x)[[1]])
ret <- rlang::eval_bare(x)
message(glue::glue("DEBUG: {ex} = {ret}"))
ret
}
```
This works rather well - it postpones evaluation of the expression and prints the result
without affecting any variables
```{r}
a <- 1
b <- 3
x <- dbg(a + b)
y <- dbg(2*x + 3)
z <- 10 + dbg(y*2)
z
```
It lacks one beautiful part of the Rust solution, though - if I include this in some
functions sourced from a file, I won't be able to tell which file the statement came from. Plus,
it doesn't deal so well with large structures
```{r}
x <- dbg(head(mtcars))
```
At some point I saw [a blog
post](https://turtletopia.github.io/2022/07/28/ice-cream-for-r-programmers/)
about a debug logging package [{icecream}](https://github.com/lewinfox/icecream)
which had this ability of tracing the `srcref` of a file containing the debug
statement, so I wanted to see if I could extract that to suit my needs. Running
the `ic()` statement as a print debugger works nicely
```{r, include=FALSE}
options(
icecream.enabled = TRUE,
icecream.prefix = "ic|",
icecream.output.function = NULL,
icecream.peeking.function = icecream:::ic_autopeek,
icecream.max.lines = 1,
icecream.arg.to.string.function = NULL,
icecream.always.include.context = FALSE
)
```
```{r}
f <- function(val1, val2) {
otherval <- 10
icecream::ic(otherval + val1)
icecream::ic(otherval + val2)
val1 + val2 + otherval
}
f(5, 6)
```
it indeed wraps the expression and shows the result. After poking around at the
internals, I realised it actually does everything that I wanted, I just needed to change
some of the defaults
```{r}
options(icecream.peeking.function = utils::head,
icecream.max.lines = 5,
icecream.prefix = "dbg:",
icecream.always.include.context = TRUE)
```
Now it prints like the `dbg!` macro
```{r}
f <- function(val1, val2) {
otherval <- 10
icecream::ic(otherval + val1)
icecream::ic(otherval + val2)
val1 + val2 + otherval
}
f(5, 6)
```
To make it even more like the Rust macro, I made a similar binding of `.dbg` (so
that it doesn't appear in my workspace by default) and added the following to my
.Rprofile
```
# install.packages("icecream")
if (requireNamespace("icecream", quietly = TRUE)) {
.dbg <- icecream::ic
options(icecream.peeking.function = utils::head,
icecream.max.lines = 5,
icecream.prefix = "dbg:",
icecream.always.include.context = TRUE)
}
```
so now I can add debug statements
```{r}
f <- function(val1, val2) {
otherval <- 10
.dbg(otherval + val1)
.dbg(otherval + val2)
val1 + val2 + otherval
}
f(5, 6)
```
Better yet, I can turn them off if I don't need them
```{r}
icecream::ic_disable()
f(5, 6)
```
This works as I had hoped; I can even debug partway through an expression
```{r}
icecream::ic_enable()
3 + .dbg(4 + 6)
```
and if I source a file, I get the context
```
## test_dbg.R:
f <- function() {
x <- 7
.dbg(x + 3)
7
}
```
```{r, eval = FALSE}
source("test_dbg.R")
f()
```
```{r, eval = FALSE, class.source = "bg-info"}
ℹ dbg: `f()` in test_dbg.R:3:2 | `x + 3`: [1] 10
```
```{r, eval = FALSE, class.source = "bg-success"}
[1] 7
```
It even deals with printing larger objects, given the "peeking_function" and "max lines"
options above
```{r}
.dbg(mtcars)
```
That seems to be feature-equivalent to the Rust `dbg!` macro, plus the ability to
turn off the messages, so I'm very happy with this result.
Do you have a better solution? I can be found on
[Mastodon](https://fosstodon.org/@jonocarroll) or use the comments below.
<br />
<details>
<summary>
<tt>devtools::session_info()</tt>
</summary>
```{r sessionInfo, echo = FALSE}
devtools::session_info()
```
</details>
<br />