Is there a similar command as str
in R
in Mathematica?
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
For ggplot
plot in R
, str
also return a reasonable small output
$ layers :List of 1
..$ :Classes 'proto', 'environment' <environment: 0x10b6168e0>
$ scales :Reference class 'Scales' [package "ggplot2"] with 1 field
..$ scales: list()
..and 21 methods, of which 9 are possibly relevant:
.. add, clone, find, get_scales, has_scale, initialize, input, n,
.. non_position_scales
$ mapping :List of 3
..$ x : symbol x
..$ y : symbol price
..$ colour: symbol color
$ theme : list()
$ coordinates:List of 1
..$ limits:List of 2
.. ..$ x: NULL
.. ..$ y: NULL
..- attr(*, "class")= chr [1:2] "cartesian" "coord"
$ facet :List of 1
..$ shrink: logi TRUE
..- attr(*, "class")= chr [1:2] "null" "facet"
$ plot_env :<environment: R_GlobalEnv>
$ labels :List of 3
..$ x : chr "x"
..$ y : chr "price"
..$ colour: chr "color"
- attr(*, "class")= chr [1:2] "gg" "ggplot"
I find it is pretty painful to open large figure output in FullForm
or TreeForm
. It is very useful to have a function that let you know the structure of the expression and the size of each element without knowing what is actually in the expression.
Update
Here is a rough example of what is looking for:
p = ContourPlot[x^2 + y^2 == 4, {x, -3, 3}, {y, -3, 3}];
p[[1]]
Output is something like following:
GraphicsComplex[{{-0.6991341991341999,
-1.872294372294372}, {-0.6428571428571431,
-1.8921568627450978}, {-0.6159420289855072,
-1.901656314699793}, {-0.5190476190476178,
-1.9285714285714288}, {-0.44824016563146996,
...
-1.872294372294372}},
{{{}, {}, Tooltip[{Directive[RGBColor[0.368417,
0.506779, 0.709798], AbsoluteThickness[1.6]],
Line[{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,
...
189, 190, 191, 192, 193}]}, x^2 + y^2 == 4]}}]
What I am asking is a function which can shrink the output down to a reasonable size. For large objects, only a few elements is shown as representatives, but the shape of the object is printed for reference.
SomeFunction[p]
GraphicsComplex[
List: Shape 192*2 {{-0.6991341991341999, -1.872294372294372}, {-0.6428571428571431, -1.8921568627450978}, ...
{{{}, {}, Tooltip[{Directive[RGBColor[0.368417,
0.506779, 0.709798], AbsoluteThickness[1.6]],
Line[ List: Shape 192*1 {1, 2, 3...}]
}, x^2 + y^2 == 4]}}]
]
here is the document of str funtion: http://www.inside-r.org/r-doc/utils/str . It claims
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 R object.
The document online keeping the examples unevaluated, I attach the output of the examples as below. It is clearly, str
output plenty of information in a very compact form.
str> ## The following examples show some of 'str' capabilities
str> str(1:12)
int [1:12] 1 2 3 4 5 6 7 8 9 10 ...
str> str(ls)
function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE,
pattern, sorted = TRUE)
str> str(args) #- more useful than args(args) !
function (name)
str> str(freeny)
'data.frame': 39 obs. of 5 variables:
$ y : Time-Series from 1962 to 1972: 8.79 8.79 8.81 8.81 8.91 ...
$ lag.quarterly.revenue: num 8.8 8.79 8.79 8.81 8.81 ...
$ price.index : num 4.71 4.7 4.69 4.69 4.64 ...
$ income.level : num 5.82 5.83 5.83 5.84 5.85 ...
$ market.potential : num 13 13 13 13 13 ...
str> str(str)
function (object, ...)
str> str(.Machine, digits.d = 20) # extra digits for identification of binary numbers
List of 18
$ double.eps : num 2.2204460492503130808e-16
$ double.neg.eps : num 1.1102230246251565404e-16
$ double.xmin : num 2.2250738585072013831e-308
$ double.xmax : num 1.7976931348623157081e+308
$ double.base : int 2
$ double.digits : int 53
$ double.rounding : int 5
$ double.guard : int 0
$ double.ulp.digits : int -52
$ double.neg.ulp.digits: int -53
$ double.exponent : int 11
$ double.min.exp : int -1022
$ double.max.exp : int 1024
$ integer.max : int 2147483647
$ sizeof.long : int 8
$ sizeof.longlong : int 8
$ sizeof.longdouble : int 16
$ sizeof.pointer : int 8
str> str( lsfit(1:9, 1:9))
List of 4
$ coefficients: Named num [1:2] 1.18e-15 1.00
..- attr(*, "names")= chr [1:2] "Intercept" "X"
$ residuals : num [1:9] -9.01e-16 1.72e-15 -2.47e-16 -2.25e-16 -2.03e-16 ...
$ intercept : logi TRUE
$ qr :List of 6
..$ qt : num [1:9] -1.50e+01 7.75 -2.22e-16 0.00 2.22e-16 ...
..$ qr : num [1:9, 1:2] -3 0.333 0.333 0.333 0.333 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "Intercept" "X"
..$ qraux: num [1:2] 1.33 1.26
..$ rank : int 2
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..- attr(*, "class")= chr "qr"
str> str( lsfit(1:9, 1:9), max.level = 1)
List of 4
$ coefficients: Named num [1:2] 1.18e-15 1.00
..- attr(*, "names")= chr [1:2] "Intercept" "X"
$ residuals : num [1:9] -9.01e-16 1.72e-15 -2.47e-16 -2.25e-16 -2.03e-16 ...
$ intercept : logi TRUE
$ qr :List of 6
..- attr(*, "class")= chr "qr"
str> str( lsfit(1:9, 1:9), width = 60, strict.width = "cut")
List of 4
$ coefficients: Named num [1:2] 1.18e-15 1.00
..- attr(*, "names")= chr [1:2] "Intercept" "X"
$ residuals : num [1:9] -9.01e-16 1.72e-15 -2.47e-16 -2..
$ intercept : logi TRUE
$ qr :List of 6
..$ qt : num [1:9] -1.50e+01 7.75 -2.22e-16 0.00 2.22e..
..$ qr : num [1:9, 1:2] -3 0.333 0.333 0.333 0.333 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "Intercept" "X"
..$ qraux: num [1:2] 1.33 1.26
..$ rank : int 2
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..- attr(*, "class")= chr "qr"
str> str( lsfit(1:9, 1:9), width = 60, strict.width = "wrap")
List of 4
$ coefficients: Named num [1:2] 1.18e-15 1.00
..- attr(*, "names")= chr [1:2] "Intercept" "X"
$ residuals : num [1:9] -9.01e-16 1.72e-15 -2.47e-16
-2.25e-16 -2.03e-16 ...
$ intercept : logi TRUE
$ qr :List of 6
..$ qt : num [1:9] -1.50e+01 7.75 -2.22e-16 0.00 2.22e-16
...
..$ qr : num [1:9, 1:2] -3 0.333 0.333 0.333 0.333 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "Intercept" "X"
..$ qraux: num [1:2] 1.33 1.26
..$ rank : int 2
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..- attr(*, "class")= chr "qr"
str> op <- options(); str(op) # save first;
List of 71
$ CBoundsCheck : logi FALSE
$ HTTPUserAgent : chr "R (3.2.0 x86_64-apple-darwin13.4.0 x86_64 darwin13.4.0)"
$ OutDec : chr "."
$ add.smooth : logi TRUE
$ bitmapType : chr "quartz"
$ browser :function (url)
$ browserNLdisabled : logi FALSE
$ buildtools.check :function (action)
$ buildtools.with :function (code)
$ check.bounds : logi FALSE
$ citation.bibtex.max : int 1
$ continue : chr "+ "
$ contrasts : Named chr [1:2] "contr.treatment" "contr.poly"
..- attr(*, "names")= chr [1:2] "unordered" "ordered"
$ defaultPackages : chr [1:6] "datasets" "utils" "grDevices" "graphics" ...
$ demo.ask : chr "default"
$ deparse.cutoff : int 60
$ device : chr "RStudioGD"
$ device.ask.default : logi TRUE
$ digits : int 7
$ dplyr.print_max : int 20
$ dplyr.print_min : int 10
$ dplyr.strict_sql : logi FALSE
$ dvipscmd : chr "dvips"
$ echo : logi TRUE
$ editor : chr "vi"
$ encoding : chr "native.enc"
$ error : language (structure(function () { ...
$ example.ask : chr "default"
$ expressions : int 5000
$ ggvis.renderer : chr "svg"
$ help.search.types : chr [1:3] "vignette" "demo" "help"
$ help.try.all.packages : logi FALSE
$ help_type : chr "html"
$ install.packages.compile.from.source: chr "interactive"
$ internet.info : int 2
$ keep.source : logi TRUE
$ keep.source.pkgs : logi FALSE
$ locatorBell : logi TRUE
$ mailer : chr "mailto"
$ max.print : int 10000
$ menu.graphics : logi FALSE
$ na.action : chr "na.omit"
$ nwarnings : int 50
$ pager :function (files, header, title, delete.file)
$ papersize : chr "a4"
$ pdfviewer : chr "/usr/bin/open"
$ pkgType : chr "both"
$ printcmd : chr "lpr"
$ prompt : chr "> "
$ repos : atomic [1:1] http://cran.stat.nus.edu.sg/
..- attr(*, "RStudio")= logi TRUE
$ restart :function (afterRestartCommand = "")
$ rl_word_breaks : chr " \t\n\"\\'`><=%;,|&{()}"
$ scipen : num 0
$ shiny.launch.browser :function (url)
..- attr(*, "shinyViewerType")= num 3
$ show.coef.Pvalues : logi TRUE
$ show.error.messages : logi TRUE
$ show.signif.stars : logi TRUE
$ str :List of 3
..$ strict.width: chr "no"
..$ digits.d : int 3
..$ vec.len : int 4
$ str.dendrogram.last : chr "`"
$ stringsAsFactors : logi TRUE
$ texi2dvi : chr "/usr/local/bin/texi2dvi"
$ timeout : num 60
$ ts.S.compat : logi FALSE
$ ts.eps : num 1e-05
$ unzip : chr "/usr/bin/unzip"
$ useFancyQuotes : logi TRUE
$ verbose : logi FALSE
$ viewer :function (url, height = NULL)
$ warn : num 0
$ warning.length : int 1000
$ width : int 75
str> # otherwise internal options() is used.
str> need.dev <-
str+ !exists(".Device") || is.null(.Device) || .Device == "null device"
str> { if(need.dev) postscript()
str+ str(par())
str+ if(need.dev) graphics.off()
str+ }
List of 72
$ xlog : logi FALSE
$ ylog : logi FALSE
$ adj : num 0.5
$ ann : logi TRUE
$ ask : logi TRUE
$ bg : chr "white"
$ bty : chr "o"
$ cex : num 1
$ cex.axis : num 1
$ cex.lab : num 1
$ cex.main : num 1.2
$ cex.sub : num 1
$ cin : num [1:2] 0.2 0.267
$ col : chr "black"
$ col.axis : chr "black"
$ col.lab : chr "black"
$ col.main : chr "black"
$ col.sub : chr "black"
$ cra : num [1:2] 14.4 19.2
$ crt : num 0
$ csi : num 0.267
$ cxy : num [1:2] 0.029 0.0879
$ din : num [1:2] 8.56 5.49
$ err : int 0
$ family : chr ""
$ fg : chr "black"
$ fig : num [1:4] 0 1 0 1
$ fin : num [1:2] 8.56 5.49
$ font : int 1
$ font.axis: int 1
$ font.lab : int 1
$ font.main: int 2
$ font.sub : int 1
$ lab : int [1:3] 5 5 7
$ las : int 0
$ lend : chr "round"
$ lheight : num 1
$ ljoin : chr "round"
$ lmitre : num 10
$ lty : chr "solid"
$ lwd : num 1
$ mai : num [1:4] 1.36 1.09 1.09 0.56
$ mar : num [1:4] 5.1 4.1 4.1 2.1
$ mex : num 1
$ mfcol : int [1:2] 1 1
$ mfg : int [1:4] 1 1 1 1
$ mfrow : int [1:2] 1 1
$ mgp : num [1:3] 3 1 0
$ mkh : num 0.001
$ new : logi FALSE
$ oma : num [1:4] 0 0 0 0
$ omd : num [1:4] 0 1 0 1
$ omi : num [1:4] 0 0 0 0
$ page : logi TRUE
$ pch : int 1
$ pin : num [1:2] 6.9 3.03
$ plt : num [1:4] 0.128 0.935 0.248 0.801
$ ps : int 16
$ pty : chr "m"
$ smo : num 1
$ srt : num 0
$ tck : num NA
$ tcl : num -0.5
$ usr : num [1:4] 0 1 0 1
$ xaxp : num [1:3] 0 1 5
$ xaxs : chr "r"
$ xaxt : chr "s"
$ xpd : logi FALSE
$ yaxp : num [1:3] 0 1 5
$ yaxs : chr "r"
$ yaxt : chr "s"
$ ylbias : num 0.2
str> ch <- letters[1:12]; is.na(ch) <- 3:5
str> str(ch) # character NA's
chr [1:12] "a" "b" NA NA NA "f" "g" "h" "i" "j" ...
str> str(list(a = "A", L = as.list(1:100)), list.len = 9)
List of 2
$ a: chr "A"
$ L:List of 100
..$ : int 1
..$ : int 2
..$ : int 3
..$ : int 4
..$ : int 5
..$ : int 6
..$ : int 7
..$ : int 8
..$ : int 9
.. [list output truncated]
str> nchar(longch <- paste(rep(letters,100), collapse = ""))
[1] 2600
str> str(longch)
chr "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw"| __truncated__
str> str(longch, nchar.max = 52)
chr "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy"| __truncated__
str> str(longch, strict.width = "wrap")
chr
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop"..
__truncated__
str> ## Settings for narrow transcript :
str> op <- options(width = 60,
str+ str = strOptions(strict.width = "wrap"))
str> str(lsfit(1:9,1:9))
List of 4
$ coefficients: Named num [1:2] 1.18e-15 1.00
..- attr(*, "names")= chr [1:2] "Intercept" "X"
$ residuals : num [1:9] -9.01e-16 1.72e-15 -2.47e-16
-2.25e-16 -2.03e-16 ...
$ intercept : logi TRUE
$ qr :List of 6
..$ qt : num [1:9] -1.50e+01 7.75 -2.22e-16 0.00 2.22e-16
...
..$ qr : num [1:9, 1:2] -3 0.333 0.333 0.333 0.333 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "Intercept" "X"
..$ qraux: num [1:2] 1.33 1.26
..$ rank : int 2
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..- attr(*, "class")= chr "qr"
str> str(options())
List of 71
$ CBoundsCheck : logi FALSE
$ HTTPUserAgent : chr "R (3.2.0 x86_64-apple-darwin13.4.0
x86_64 darwin13.4.0)"
$ OutDec : chr "."
$ add.smooth : logi TRUE
$ bitmapType : chr "quartz"
$ browser :function (url)
$ browserNLdisabled : logi FALSE
$ buildtools.check :function (action)
$ buildtools.with :function (code)
$ check.bounds : logi FALSE
$ citation.bibtex.max : int 1
$ continue : chr "+ "
$ contrasts : Named chr [1:2] "contr.treatment"
"contr.poly"
..- attr(*, "names")= chr [1:2] "unordered" "ordered"
$ defaultPackages : chr [1:6] "datasets" "utils"
"grDevices" "graphics" ...
$ demo.ask : chr "default"
$ deparse.cutoff : int 60
$ device : chr "RStudioGD"
$ device.ask.default : logi TRUE
$ digits : int 7
$ dplyr.print_max : int 20
$ dplyr.print_min : int 10
$ dplyr.strict_sql : logi FALSE
$ dvipscmd : chr "dvips"
$ echo : logi TRUE
$ editor : chr "vi"
$ encoding : chr "native.enc"
$ error : language (structure(function () { ...
$ example.ask : chr "default"
$ expressions : int 5000
$ ggvis.renderer : chr "svg"
$ help.search.types : chr [1:3] "vignette" "demo" "help"
$ help.try.all.packages : logi FALSE
$ help_type : chr "html"
$ install.packages.compile.from.source: chr "interactive"
$ internet.info : int 2
$ keep.source : logi TRUE
$ keep.source.pkgs : logi FALSE
$ locatorBell : logi TRUE
$ mailer : chr "mailto"
$ max.print : int 10000
$ menu.graphics : logi FALSE
$ na.action : chr "na.omit"
$ nwarnings : int 50
$ pager :function (files, header, title, delete.file)
$ papersize : chr "a4"
$ pdfviewer : chr "/usr/bin/open"
$ pkgType : chr "both"
$ printcmd : chr "lpr"
$ prompt : chr "> "
$ repos : atomic [1:1] http://cran.stat.nus.edu.sg/
..- attr(*, "RStudio")= logi TRUE
$ restart :function (afterRestartCommand = "")
$ rl_word_breaks : chr " \t\n\"\\'`><=%;,|&{()}"
$ scipen : num 0
$ shiny.launch.browser :function (url)
..- attr(*, "shinyViewerType")= num 3
$ show.coef.Pvalues : logi TRUE
$ show.error.messages : logi TRUE
$ show.signif.stars : logi TRUE
$ str :List of 4
..$ strict.width: chr "wrap"
..$ digits.d : num 3
..$ vec.len : num 4
..$ formatNum :function (x, ...)
$ str.dendrogram.last : chr "`"
$ stringsAsFactors : logi TRUE
$ texi2dvi : chr "/usr/local/bin/texi2dvi"
$ timeout : num 60
$ ts.S.compat : logi FALSE
$ ts.eps : num 1e-05
$ unzip : chr "/usr/bin/unzip"
$ useFancyQuotes : logi TRUE
$ verbose : logi FALSE
$ viewer :function (url, height = NULL)
$ warn : num 0
$ warning.length : int 1000
$ width : int 60
str> ## reset to previous:
str> options(op)
str> ## Don't show:
str> ##-- Some "crazy" objects
str> str(array(1:5, dim = 20))
int [1:20(1d)] 1 2 3 4 5 1 2 3 4 5 ...
str> str(factor(character(0)))
Factor w/ 0 levels:
str> str(as.data.frame(NULL))
'data.frame': 0 obs. of 0 variables
str> ## End(Don't show)
str> str(quote( { A+B; list(C, D) } ))
length 3 { A + B; list(C, D) }
- attr(*, "srcref")=List of 3
..$ :Class 'srcref' atomic [1:8] 53 12 53 12 12 12 53 53
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x10aa7f8d8>
..$ :Class 'srcref' atomic [1:8] 53 14 53 16 14 16 53 53
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x10aa7f8d8>
..$ :Class 'srcref' atomic [1:8] 53 19 53 28 19 28 53 53
.. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x10aa7f8d8>
- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x10aa7f8d8>
- attr(*, "wholeSrcref")=Class 'srcref' atomic [1:8] 1 0 53 30 0 30 1 53
.. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x10aa7f8d8>
str> ## Don't show:
str> had.stats4 <- "package:stats4" %in% search()
str> if(!had.stats4)
str+ rs <-
str+ ## End(Don't show)
str+ ## S4 classes :
str+ require(stats4)
Loading required package: stats4
str> x <- 0:10; y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8)
str> ll <- function(ymax = 15, xh = 6)
str+ -sum(dpois(y, lambda=ymax/(1+x/xh), log=TRUE))
str> fit <- mle(ll)
str> str(fit)
Formal class 'mle' [package "stats4"] with 9 slots
..@ call : language mle(minuslogl = ll)
..@ coef : Named num [1:2] 24.99 3.06
.. ..- attr(*, "names")= chr [1:2] "ymax" "xh"
..@ fullcoef : Named num [1:2] 24.99 3.06
.. ..- attr(*, "names")= chr [1:2] "ymax" "xh"
..@ vcov : num [1:2, 1:2] 17.85 -3.72 -3.72 1.07
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "ymax" "xh"
.. .. ..$ : chr [1:2] "ymax" "xh"
..@ min : num 28.6
..@ details :List of 6
.. ..$ par : Named num [1:2] 24.99 3.06
.. .. ..- attr(*, "names")= chr [1:2] "ymax" "xh"
.. ..$ value : num 28.6
.. ..$ counts : Named int [1:2] 25 18
.. .. ..- attr(*, "names")= chr [1:2] "function" "gradient"
.. ..$ convergence: int 0
.. ..$ message : NULL
.. ..$ hessian : num [1:2, 1:2] 0.203 0.706 0.706 3.388
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "ymax" "xh"
.. .. .. ..$ : chr [1:2] "ymax" "xh"
..@ minuslogl:function (ymax = 15, xh = 6)
.. ..- attr(*, "srcref")=Class 'srcref' atomic [1:8] 63 7 64 52 7 52 63 64
.. .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x10aa7f8d8>
..@ nobs : int NA
..@ method : chr "BFGS"
Short[]
andShallow[]
? $\endgroup$ContourPlot
does not give many information about the expression. Unlike thestr
example, you can see the first element is layer, and there is one layer in the object and so on. $\endgroup$Short
andShallow
with additional parameters. Have you explored that yet? $\endgroup$