I would like to write a script main.r
that returns the workspace to the state it was in before being run (i.e., at the end of the script, remove all and only the objects that had been added to the workspace). Running the following:
#main.r
initial.objects <- objects()
tmp1 <- 1
remove(list = setdiff(objects(), initial.objects)
via source('main.r')
from the R console works as desired. HOWEVER, this does NOT work in Splus with tmp1
being left in the working directory (it does work when I run each line individually rather than sourcing the entire file). Investigating a little further, I found that in R objects()
keeps track of the objects entering the workspace even in the MIDDLE of a call to source()
. In Splus, objects()
d开发者_JAVA技巧oesn't seem to "know" about the objects that have been added to the workspace until the END of a source()
call.
Q: What's going on? What can I do to get something similar to main.r
working in Splus?
I'm not sure what you're trying to do here, but the best way to reload an environment is to save it and reload it.
save("pre-environ.Rdata")
## Your script goes here
rm(list=ls()) ## clean the environment
## Reload the original environ at end of your script
load("pre-environ.Rdata")
精彩评论