[R] How to print the objects in the memory

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 31 10:43:40 CET 2007


On Wed, 31 Jan 2007, Vladimir Eremeev wrote:

[replying to something else, without any indication or context.  That 
'something' was about how to print all objects.]

>
> 1. ? ls.str()
>
> 2. My favourite "eval(parse(text= ... ))" :)
>
> sapply(ls(),FUN=function(x)eval(parse(text=x)))

Note

> library(fortune)
> fortune("parse")

If the answer is parse() you should usually rethink the question.
    -- Thomas Lumley
       R-help (February 2005)

Here the answer is 'get', as ls.str uses via utils:::print.ls_str. You 
need to be careful to get the correct object of the given name, and using 
your favourite will not in general do so.

When you have a character string containing the name of a variable, 
eval(as.name(x)) is more direct than parsing (but less direct that get(x) 
).  And since there is no reason that the results are all suitable for 
simplifying to a vector, lapply is safer.  E.g.

> xx <- pi
> y <- "test it"
> sapply(ls(), function(x) eval(parse(text=x)))
                 xx                  y
"3.14159265358979"          "test it"

is not what is wanted.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list