tags:

views:

81

answers:

3

I have a list of objects that I've created manually, like this:

rand1 <- rnorm(1e3)
rand2 <- rnorm(1e6)

myObjects <- NULL
myObjects[[1]] <-rand1
myObjects[[2]] <-rand2
names(myObjects) <- c("rand1","rand2")

I'm working on some code that bundles up objects and puts them up in S3. Then I have code in EC2 that I want to grab the myObjects list and 'unbundle' it automatically. In this example the list only has two objects and the names are known, but how do I code this to handle lists of any length and any names?

#pseudo code
for each thing in myObjects
  thing <- myObjects[[thing]]

I can't quite figure out how to take names(myObjects)[1] and turn it into the name of an object for which I will assign the contents of myObjects[[1]]. I can handle the looping but creating each object kinda has me hung. I'm sure this is quite simple, but I can't quite grok it.

+4  A: 

You can use assign:

for(i in 1:length(myObjects)) assign(names(myObjects)[i], myObjects[[i]])
Shane
assign()... that's the piece I was missing! thanks to both you and Richie for pointing me in the right direction.
JD Long
`seq_along(myObjects)` is a little safer if `length(myObjects) == 0`
hadley
@hadley: Thanks! seq_along() is a great find. As in the following examples: bad = `for(i in 1:length(NULL)) print(i)`, good = `for(i in seq_along(NULL)) print(i)`
Shane
+2  A: 

To expand Shane's answer:

mapply(assign, names(myObjects), myObjects, MoreArgs=list(envir = globalenv())

(You may wish to change globalenv() to another environment.)

Richie Cotton
very good idea about changing environments. I should use that. That's a bonus idea!
JD Long
In this example I see the objects rand1 and rand2 are created, but it also seems to output the myObjects object as well. If I assign the results of this mapply() to an object I end up with a copy of myObjects PLUS rand1 and rand2. Kinda noisy and seems inefficient. Should this be a different flavor of apply?
JD Long
+6  A: 

attach(myObjects)

hadley
I had basically taken attach() out of my vocabulary because I found that the way most folks use it results in kinda crappy code that's hard to track. So I didn't even think of using it in this case. But now that you mention it, I think it's a really good solution. Thanks for helping me think outside of my small box.
JD Long
+1 Hadn't considered that usage either.
Shane
I don't think it's ever a good idea to use attach. But if you're going to use it, use it, don't write your own version of it.
hadley
See also: `with(myObjects, { ... })` for a faster and safer alternative to `attach`.
Richie Cotton