[R] Possible to pretty-printing using str()?

Duncan Murdoch murdoch.duncan at gmail.com
Tue Nov 23 20:42:49 CET 2010


On 23/11/2010 2:35 PM, Janko Thyson wrote:
> A short annotation:
> I already considered deparsing the object via deparse(x), but that's not
> exactly the info I'd like. I'm only interested in a short summary like str()
> would give me, not in the deparsed object.

I think you need to give more details.  What's wrong with the first line 
of str(x)?  For example,

str(LETTERS)
  chr [1:26] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" ...

str(mtcars)
'data.frame':   32 obs. of  11 variables:
[ more lines deleted ]

You can get the first line by capture.output(str(x))[1].  You can put 
the whole thing into one big ugly line by
paste( capture.output(str(x)), collapse=" ")

Duncan Murdoch

> Thx,
> Janko
>
> ########## SYSTEM INFO ##########
> Windows XP SP3
> R 2.12.0 (patched as of 2010-11-22)
> Eclipse 3.6.1 (Helios)
> StatET 0.9.x
> ###############################
>
>
> >  -----Ursprüngliche Nachricht-----
> >  Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> >  Im Auftrag von Janko Thyson
> >  Gesendet: Dienstag, 23. November 2010 20:32
> >  An: r-help at r-project. org
> >  Betreff: [R] Possible to pretty-printing using str()?
> >
> >  Dear list,
> >
> >  I'm looking for a suitable way to sort of "one-line-pretty-print" an
> >  arbitrary R object in some of my log outputs.
> >
> >  Consider this:
> >
> >  cat(paste("The object/value is: ", x, ".", sep=""), sep="\n")
> >
> >  No problem if x is of class:
> >    - character (length=1)
> >    - numeric   (length=1)
> >    - logical   (length=1)
> >
> >  For lengths>  1 I can get around by paste(x, collapse=my.delimiter).
> >  So:
> >
> >  cat(paste("The object/value is: ", paste(x, collapse=my.delimiter),
> >  ".",
> >  sep=""), sep="\n")
> >
> >  "Problem" if x is of class
> >    - data.frame
> >    - matrix
> >    - list
> >    - other complex objects
> >
> >  For those objects something like the output of str() arranged in one
> >  line of
> >  class 'character' would be great.
> >
> >  Is that possible somehow?
> >
> >  Thanks for any comments,
> >  Janko
> >
> >  ########## SYSTEM INFO ##########
> >  Windows XP SP3
> >  R 2.12.0 (patched as of 2010-11-22)
> >  Eclipse 3.6.1 (Helios)
> >  StatET 0.9.x
> >  ###############################
> >
> >  ______________________________________________
> >  R-help at r-project.org mailing list
> >  https://stat.ethz.ch/mailman/listinfo/r-help
> >  PLEASE do read the posting guide http://www.R-project.org/posting-
> >  guide.html
> >  and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list