[R] Saving objects inside a list
William Dunlap
wdunlap at tibco.com
Mon Jan 3 22:44:44 CET 2011
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Eduardo de
> Oliveira Horta
> Sent: Monday, January 03, 2011 12:58 PM
> To: r-help
> Subject: Re: [R] Saving objects inside a list
>
> sapply(ls(),get) works fine. Thanks.
>
> ps: the as.list and the eapply suggestions didn't work.
What version of R are you using? In 2.12.0 I get
> x <- 1:7
> y <- function(...)list(...)
> z <- runif(3) # creates .Random.seed also
> str(as.list(.GlobalEnv)) # add all.names=TRUE to get .Random.seed
List of 3
$ z: num [1:3] 0.698 0.475 0.354
$ y:function (...)
..- attr(*, "source")= chr "function(...)list(...)"
$ x: int [1:7] 1 2 3 4 5 6 7
> str(eapply(.GlobalEnv, function(x)x)) # all.names=TRUE ok here also
List of 3
$ z: num [1:3] 0.698 0.475 0.354
$ y:function (...)
..- attr(*, "source")= chr "function(...)list(...)"
h$ x: int [1:7] 1 2 3 4 5 6 7
Use environment() instead of .GlobalEnv if you want to
process the current environment.
In any version of R sapply() can give incorrect results,
as in (in a fresh vanilla R session):
> x <- 1:4
> y <- 11:14
> z <- 21:24
> str(sapply(ls(), get))
int [1:4, 1:3] 1 2 3 4 11 12 13 14 21 22 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "x" "y" "z"
or, in another fresh session
> FUN <- "a string named FUN"
> X <- 17
> sapply(ls(), get)
$FUN
function (x, pos = -1, envir = as.environment(pos), mode = "any",
inherits = TRUE)
.Internal(get(x, envir, mode, inherits))
$X
[1] "FUN" "X"
as.list(envir) and eapply(envir, function(x)x) do
not have problems in those cases.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
>
> On Mon, Jan 3, 2011 at 3:56 PM, Jorge Ivan Velez
> <jorgeivanvelez at gmail.com>wrote:
>
> > Hi Eduardo,
> >
> > Try
> >
> > r <- ls()
> > result <- sapply(r, get)
> > result
> >
> > HTH,
> > Jorge
> >
> >
> > On Mon, Jan 3, 2011 at 12:25 PM, Eduardo de Oliveira Horta <> wrote:
> >
> >> Hello there,
> >>
> >> any ideas on how to save all the objects on my workspace
> inside a list
> >> object?
> >>
> >> For example, say my workspace is as follows
> >> ls()
> >> [1] "x" "y" "z"
> >>
> >> and suppose I want to put these objects inside a list object, say
> >>
> >> object.list <- list()
> >>
> >> without having to explicitly write down their names as in
> >>
> >> object.list$x = x
> >> object.list$y = y
> >> object.list$z = z
> >>
> >> Is this possible?
> >>
> >> Thanks in advance,
> >>
> >> Eduardo Horta
> >>
> >> [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> 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.
> >>
> >
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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