lobstr 1.0.0

  r-lib, lobstr

  Mara Averick

We’re so happy to announce the release of lobstr on CRAN. lobstr provides tools that allow you to dig into the internal representation and structure of R objects, in a similar vein to str().

You can install it by running:

install.packages("lobstr")

Visualising R data structures with trees

Abstract syntax trees

ast() draws the abstract syntax tree (AST) of R expressions. You can learn about these in greater detail in the Abstract syntax trees section of Advanced R.

ast(a + b + c)
#> █─`+` 
#> ├─█─`+` 
#> │ ├─a 
#> │ └─b 
#> └─c

Call stack trees

cst() shows how the frames of a call stack are connected:

f <- function(x) g(x)
g <- function(x) h(x)
h <- function(x) x
f(cst())
#>     █
#>  1. ├─global::f(cst())
#>  2. │ └─global::g(x)
#>  3. │   └─global::h(x)
#>  4. └─lobstr::cst()

Learn more about this in The call stack.

References

ref() shows the connection between shared references using a locally unique id by printing the memory address of each object (further discussed here).

x <- 1:100
ref(x)
#> [1:0x7fb19c66bbc8] <int>

y <- list(x, x, x)
ref(y)
#> █ [1:0x7fb19d0d5a28] <list> 
#> ├─[2:0x7fb19c66bbc8] <int> 
#> ├─[2:0x7fb19c66bbc8] 
#> └─[2:0x7fb19c66bbc8]

Object inspection

The obj_addr() function gives the address of the value that an object, x, points to. In Binding basics, this is used to illustrate the “lazy copying” used by R: when multiple names reference the same value, they point to the same identifier. The object itself is not duplicated.

x <- 1:10
y <- x

obj_addr(x)
#> [1] "0x7fb19c7aeeb0"
obj_addr(y)
#> [1] "0x7fb19c7aeeb0"

obj_size() computes the size of an object or set of objects. It is different to object.size() in three ways. It:

  • Accounts for all types of shared values, not just strings in the global string pool,
  • Includes the size of environments (up to env), and
  • Accurately measures the size of ALTREP objects.

obj_size() attempts to take into account the size of the environments associated with an object. By default, it never counts the size of the global environment, the base environment, the empty environment, or any namespace. However, the optional env argument allows you to specify another environment at which to stop.

x <- runif(1e4)
obj_size(x)
#> 80,048 B

z <- list(a = x, b = x, c = x)
obj_size(z)
#> 80,488 B

You can use obj_sizes() to see the unique contribution of each component:

obj_sizes(x, z)
#> * 80,048 B
#> *    440 B

For more detail, see the Object size section of Advanced R.

Memory usage

mem_used() wraps around the base-R garbage collection function, gc(), and returns the exact number of bytes currently used by R. See Unbinding and the garbage collector for details.