Skip to content

sxp(x) is similar to .Internal(inspect(x)), recursing into the C data structures underlying any R object. The main difference is the output is a little more compact, it recurses fully, and avoids getting stuck in infinite loops by using a depth-first search. It also returns a list that you can compute with, and carefully uses colour to highlight the most important details.

Usage

sxp(x, expand = character(), max_depth = 5L)

Arguments

x

Object to inspect

expand

Optionally, expand components of the true that are usually suppressed. Use:

  • "character" to show underlying entries in the global string pool.

  • "environment" to show binding components without any side effects (e.g. promises or active bindings).

  • "altrep" to show the underlying data.

  • "call" to show the full AST (but ast() is usually superior)

  • "bytecode" to show generated bytecode.

max_depth

Maximum depth to recurse. Use max_depth = Inf (with care!) to recurse as deeply as possible. Skipped elements will be shown as ....`

Details

The name sxp comes from SEXP, the name of the C data structure that underlies all R objects.

See also

Other object inspectors: ast(), ref(), src()

Examples

x <- list(
  TRUE,
  1L,
  runif(100),
  "3"
)
sxp(x)
#> [1:0x55ef5bf830d8] <VECSXP[4]> (refs:2+)
#>   [2:0x55ef5c2f3060] <LGLSXP[1]> (refs:2+)
#>   [3:0x55ef5c2f2ea0] <INTSXP[1]> (refs:2+)
#>   [4:0x55ef5c491f70] <REALSXP[100]> (refs:1)
#>   [5:0x55ef5c2f2810] <STRSXP[1]> (refs:2+)

# Expand "character" to see underlying CHARSXP entries in the global
# string pool
x <- c("banana", "banana", "apple", "banana")
sxp(x)
#> [1:0x55ef5a4b72d8] <STRSXP[4]> (refs:2+)
sxp(x, expand = "character")
#> [1:0x55ef5a4b72d8] <STRSXP[4]> (refs:2+)
#>   [2:0x55ef5bf8a310] <CHARSXP> (refs:2+)
#>   [2:0x55ef5bf8a310]
#>   [3:0x55ef59124e50] <CHARSXP> (refs:2+)
#>   [2:0x55ef5bf8a310]

# Expand altrep to see underlying data
x <- 1:10
sxp(x)
#> [1:0x55ef5911fd78] <INTSXP[10]> (altrep refs:2+)
sxp(x, expand = "altrep")
#> [1:0x55ef5911fd78] <INTSXP[10]> (altrep refs:2+)
#>   _class [2:0x55ef53d24860] <RAWSXP[144]> (refs:2+)
#>     _attrib [3:0x55ef59052238] <LISTSXP> (refs:1)
#>       [4:0x55ef53d5a928] <SYMSXP: compact_intseq> (refs:2+)
#>       [5:0x55ef53d26380] <SYMSXP: base> (refs:2+)
#>       [6:0x55ef53d591e8] <INTSXP[1]> (refs:2+)
#>   _data1 [7:0x55ef59750d68] <REALSXP[3]> (refs:1)
#>   _data2 <NILSXP>

# Expand environments to see promise expressions without forcing
e <- new.env(parent = emptyenv())
delayedAssign("x", 1 + 1, assign.env = e)

sxp(e)
#> [1:0x55ef58a459b0] <ENVSXP> (refs:2+)
#>   x <PROMSXP>
#>   _enclos [2:0x55ef53d27688] <ENVSXP: empty> (refs:2+)
sxp(e, expand = "environment")
#> [1:0x55ef58a459b0] <ENVSXP> (refs:2+)
#>   x <PROMSXP>
#>   _code [2:0x55ef5c8aa2c8] <LANGSXP> (refs:2+)
#>     ...
#>   _env [3:0x55ef5c8bbf10] <ENVSXP> (refs:2+)
#>     x [4:0x55ef5911fd78] <INTSXP[10]> (altrep refs:2+)
#>     e [1:0x55ef58a459b0]
#>     _enclos [5:0x55ef5c5e3bb0] <ENVSXP> (refs:2+)
#>       DONTSHOW [6:0x55ef5c5e3718] <CLOSXP> (refs:1)
#>         _formals [7:0x55ef57d358b0] <LISTSXP> (refs:2+)
#>           ...
#>         _body [8:0x55ef57d35878] <BCODESXP> (refs:2+)
#>           ...
#>         _env [9:0x55ef5c585a08] <ENVSXP> (refs:2+)
#>           ...
#>       TESTONLY [10:0x55ef5c5e36a8] <CLOSXP> (refs:1)
#>         _formals [11:0x55ef57d350d0] <LISTSXP> (refs:2+)
#>           ...
#>         _body [12:0x55ef57d35098] <BCODESXP> (refs:2+)
#>           ...
#>         _env [9:0x55ef5c585a08]
#>       _enclos [13:0x55ef5c585c70] <ENVSXP> (refs:2+)
#>         _enclos [14:0x55ef55a5e140] <ENVSXP> (refs:2+)
#>           ...
#>   _enclos [15:0x55ef53d27688] <ENVSXP: empty> (refs:2+)