Compactly Display the Structure of an Arbitrary R Object

Compactly display the internal structure of an Robject, a diagnostic function and an alternative to summary (and to some extent, dput). Ideally, only one line for each basic structure is displayed. It is especially well suited to compactly display the (abbreviated) contents of (possibly nested) lists. The idea is to give reasonable output for any Robject. It calls args for (non-primitive) function objects.

strOptions() is a convenience function for setting options(str = .), see the examples.

utilities, print, documentation
str(object, ...)

## S3 method for class 'data.frame': str(object, \dots)

## S3 method for class 'default': str(object, max.level = NA, vec.len = strO$vec.len, digits.d = strO$digits.d, nchar.max = 128, give.attr = TRUE, give.head = TRUE, give.length = give.head, width = getOption("width"), nest.lev = 0, indent.str = paste(" ", max(0, nest.lev + 1)), collapse = ".."), comp.str = "$ ", no.list = FALSE, envir = baseenv(), strict.width = strO$strict.width, formatNum = strO$formatNum, list.len = 99, ...)

strOptions(strict.width = "no", digits.d = 3, vec.len = 4, formatNum = function(x, ...) format(x, trim = TRUE, drop0trailing = TRUE, ...))

any Robject about which you want to have some information.
maximal level of nesting which is applied for displaying nested structures, e.g., a list containing sub lists. Default NA: Display all nesting levels.
numeric (>= 0) indicating how many first few elements are displayed of each vector. The number is multiplied by different factors (from .5 to 3) depending on the kind of vector. Defaults to the vec.len component of option "str" (see options) which defaults to 4.
number of digits for numerical components (as for print). Defaults to the digits.d component of option "str" which defaults to 3.
maximal number of characters to show for character strings. Longer strings are truncated, see longch example below.
logical; if TRUE (default), show attributes as sub structures.
logical; if TRUE (default), indicate length (as [1:...]).
logical; if TRUE (default), give (possibly abbreviated) mode/class and length (as [1:...]).
the page width to be used. The default is the currently active options("width"); note that this has only a weak effect, unless strict.width is not "no".
current nesting level in the recursive calls to str.
the indentation string to use.
string to be used for separating list components.
logical; if true, no list of ... nor the class are printed.
the environment to be used for promise (see delayedAssign) objects only.
string indicating if the width argument's specification should be followed strictly, one of the values c("no", "cut", "wrap"), which can be abbreviated. Defaults to the strict.width component of option "str" (see options) which defaults to "no" for back compatibility reasons; "wrap" uses strwrap(*, width = width) whereas "cut" cuts directly to width. Note that a small vec.length may be better than setting strict.width = "wrap".
a function such as format for formatting numeric vectors. It defaults to the formatNum component of option "str", see Usage of strOptions() above, which is almost back compatible to R<= 2.7.x,="" however,="" using="" formatC may be slightly better.
numeric; maximum number of list elements to display within a level.
potential further arguments (required for Method/Generic reasons).

  • str does not return anything, for efficiency reasons. The obvious side effect is output to the terminal.

See Also

ls.str for listing objects with their structure; summary, args.

  • str
  • str.default
  • strOptions
library(utils) require(stats); require(grDevices); require(graphics) ## The following examples show some of 'str' capabilities str(1:12) str(ls) str(args) #- more useful than args(args) ! str(freeny) str(str) str(.Machine, digits.d = 20) # extra digits for identification of binary numbers str( lsfit(1:9, 1:9)) str( lsfit(1:9, 1:9), max.level = 1) str( lsfit(1:9, 1:9), width = 60, strict.width = "cut") str( lsfit(1:9, 1:9), width = 60, strict.width = "wrap") op <- options(); str(op) # save first; # otherwise internal options() is used. <- !exists(".Device") || is.null(.Device) || .Device == "null device" { if( postscript() str(par()) if( } ch <- letters[1:12]; <- 3:5 str(ch) # character NA's str(list(a = "A", L = as.list(1:100)), list.len = 9) nchar(longch <- paste(rep(letters,100), collapse = "")) str(longch) str(longch, nchar.max = 52) str(longch, strict.width = "wrap") ## Settings for narrow transcript : op <- options(width = 60, str = strOptions(strict.width = "wrap")) str(lsfit(1:9,1:9)) str(options()) ## reset to previous: options(op) ##-- Some "crazy" objects str(array(1:5, dim = 20)) str(factor(character(0))) str( str(quote( { A+B; list(C, D) } ))had.stats4 <- "package:stats4" %in% search() if(!had.stats4) rs <- ## S4 classes : require(stats4) x <- 0:10; y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8) ll <- function(ymax = 15, xh = 6) -sum(dpois(y, lambda=ymax/(1+x/xh), log=TRUE)) fit <- mle(ll) str(fit) if(!had.stats4 && rs) detach("package:stats4")
Documentation reproduced from package utils, version 3.3, License: Part of R 3.3

Community examples

Looks like there are no examples yet.