forked from DominikRafacz/icecream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
231 lines (163 loc) · 5.22 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
demo_file <- file.path(tempdir(TRUE), "demo.R")
demo_fns <- "# demo.R
f1 <- function(x) {
ic()
if (x > 0) {
f2()
}
}
f2 <- function() {
ic()
}
f3 <- function(x) {
ic(x)
}
"
cat(demo_fns, file = demo_file)
source(demo_file)
```
# icecream <img src="man/figures/logo.svg" align="right" width="120" />
<!-- badges: start -->
[![](https://cranlogs.r-pkg.org/badges/icecream)](https://cran.r-project.org/package=icecream)
[![R-CMD-check](https://github.com/lewinfox/icecream/actions/workflows/check-standard.yaml/badge.svg)](https://github.com/lewinfox/icecream/actions)
<!-- badges: end -->
icecream is designed to make print debugging easier. It allows you to print out an expression, its
value and (optionally) which function and file the call originated in.
This is an R port of [gruns/icecream](https://github.com/gruns/icecream). All credit for the idea
belongs to [Ansgar Grunseid](https://github.com/gruns).
## Installation
Install from CRAN with:
``` r
install.packages("icecream")
```
Or you can install the development version from GitHub with:
``` r
devtools::install_github("lewinfox/icecream")
```
## Inspect variables
The `ic()` function prints its argument and its value. It also returns the value of the evaluated
argument, meaning that it is effectively transparent in code - just wrap an expression in `ic()` to
get debugging output.
```{r example}
library(icecream)
is_negative <- function(x) x < 0
ic(is_negative(1))
ic(is_negative(-1))
```
You're more likely to want to do this within a function:
```{r}
some_function <- function(x) {
intermediate_value <- x * 10
answer <- ic(intermediate_value / 2)
return(answer)
}
some_function(1)
some_function(10)
```
More complex inputs like lists and data frames are summarised to avoid cluttering the terminal.
```{r}
df <- ic(iris)
my_list <- ic(list(a = 1, b = 3, c = 1:100))
```
## Inspect execution
Calling `ic()` with no arguments causes it to print out the file, line and parent function it was
called from.
In this example we have a file `demo.R` that contains two functions. We've inserted `ic()` calls at
strategic points so we can track what's being executed.
```{r code=readLines(demo_file)}
```
``` r
source("demo.R")
f1(-1)
#> ℹ ic| `global::f1()` in demo.R:3:2
f1(1)
#> ℹ ic| `global::f1()` in demo.R:3:2
#> ℹ ic| `global::f2()` in demo.R:10:2
```
In the case of functions that haven't been `source()`d or loaded from a package there is no source
code to refer to. In these cases the function's environment will be displayed.
``` r
orphan_func <- function() {
ic()
TRUE
}
orphan_func()
#> ℹ ic| `global::orphan_func()` in <env: global>
#> [1] TRUE
e <- new.env()
attr(e, "name") <- "icecream_van"
environment(orphan_func) <- e
orphan_func()
#> ℹ ic| `orphan_func()` in <env: icecream_van>
#> [1] TRUE
```
## Enable / disable
The `ic_enable()` and `ic_disable()` functions enable or disable the `ic()` function. If disabled,
`ic()` will return the result of evaluating its input but will not print anything.
```{r}
ic_enable() # This is TRUE by default
ic(mean(1:100))
ic_disable()
ic(mean(1:100))
```
```{r, include=FALSE}
ic_enable()
```
## Options
The following options can be used to control behaviour:
### `icecream.enabled`
Boolean. If `FALSE`, calls to `ic(foo)` simply evaluate and return `foo`. No output is printed. This
option can be set directly or with the `ic_enable()` and `ic_disable()` functions.
### `icecream.prefix`
This is printed at the beginning of every line. Defaults to `"ic|"`.
```{r, include=FALSE}
old.prefix <- getOption("icecream.prefix")
```
```{r}
ic(mean(1:5))
options(icecream.prefix = "DEBUG:")
ic(mean(1:5))
options(icecream.prefix = "\U1F366")
ic(mean(1:5))
```
```{r, include=FALSE}
options(icecream.prefix = old.prefix)
```
### `icecream.always.include.context`
Boolean. If `TRUE`, when calling `ic(foo)` the source file and line will be printed along with the
expression and value. If no `srcref()` is available the function's environment will be displayed
instead. This can be useful for more complicated debugging but produces a lot of output so is
disabled by default.
```{r, include=FALSE}
old.ctx <- getOption("icecream.always.include.context")
```
``` r
f3(1)
#> ℹ ic| `x`: num 1
options(icecream.always.include.context = TRUE)
f3(1)
#> ℹ ic| `global::f3()` in demo.R:14:2 | `x`: num 1
```
When `ic()` is called with no arguments, the context is always printed because showing the location
of the call is the only reason to call `ic()` on its own.
```{r, include=FALSE}
options(icecream.always.include.context = old.ctx)
```
### `icecream.output.function`, `icecream.arg.to.string.function`
Not implemented yet. See the [configuration](https://github.com/gruns/icecream#configuration)
section of the original project docs for details of what they will do.
## TODO:
* Implement `ic.format()` (see [here](https://github.com/gruns/icecream#miscellaneous)).
* Implement `ic.output.function`. At the moment it uses `cli::cli_alert_info()`
* Implement `ic.arg.to.string.function`