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.
Install from CRAN with:
Or you can install the development version from GitHub with:
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.
You’re more likely to want to do this within a function:
More complex inputs like lists and data frames are summarised to avoid cluttering the terminal.
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.
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.
The following options can be used to control behaviour:
This is printed at the beginning of every line. Defaults to
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.
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
These two options control how the result of evaluation of an expression is printed.
icecream.peeking.function indicates the function that summarizes the object. Default value is
ic_autopeek, which works like
utils::str for most of the time, but gives more informative output for
data.frames and their subclasses in a more compact way.
icecream.max.lines determines maximum number of lines that the peek of an object occupies; defaults to 1.
For more complex data you may want to use e.g.
head function and 5 lines.
data(iris) ic(iris) # we would like to see header of the data #> ℹ ic| `iris`: data.frame [150 x 5]: $'Sepal.Length': dbl , ... options(icecream.peeking.function = head, icecream.max.lines = 5) ic(iris) #> ℹ ic| `iris`: #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa
Note that if
icecream.max.lines is greater than 1 and summary of an object is longer than 1, the alert occupies one line more due to the header.
Not implemented yet. See the configuration section of the original project docs for details of what they will do.