[R] using assign on save?
Thomas W Blackwell
tblackw at umich.edu
Thu Sep 25 01:48:49 CEST 2003
"Value: This function is invoked for its side effect, which is
assigning `value' to the variable `x'. ..."
Gosh. The help page isn't very specific about what the return
value of assign() IS, but it's not a named object. Your basic
strategy of assigning, writing, then removing sounds like a good
one, but perhaps these have to be three separate lines inside the
loop. If that's so, then it's worth making a temporary character
string variable with the result of paste(), so you can use it
three times. Think also about where the assignment is going to
Warning: I haven't tried out any of the suggestions I am making.
HTH - tom blackwell - u michigan medical school - ann arbor -
On Wed, 24 Sep 2003, Andy Bunn wrote:
> I have an object which I want to save into an external file, using save, on a time step in loop. In the loop I have an if statment that will save when the appropraite time, t, is reached.
> I want to change the object and the name of the saved object to reflect which time step is being written. I can do the file name in save using paste but cannot rename the object.
> For example, I want to save object foo at time t = 50 in file "Run.50.Rdata." Furthermore, I want the object in that file to be "foo50."
> This is what I'm doing now:
> save(foo, file = paste("Run", t, "Rdata",sep="."))
> which creates the right file name but obviously doesn't rename the object.
> So, I tried assigning the object in the save function.
> save(assign(paste("foo", t, sep = ""), foo),
> file = paste("Run", t, "Rdata",sep="."))
> This crashes. It would also leave a copy of foo50 in the workspace which is messy.
> The logic of how to rename the object at a time step, reference it in the save statement and then remove it is eluding me.
> Any thoughts on how to proceed?
More information about the R-help