mcnda839 at mncn.csic.es
2009-May-16 15:21 UTC
[R] How to save R "clean" sessions in BATCH mode?
Thanks a lot for all of you that have reply me about opening and ending R workspaces in BATCH mode. However replies were a king general and I?m afraid I could not take the entire message from them. Therefore I chose to expose here a representative fraction of my work. I have 50 Rdata files (F1,F2,F3,F4, ,F50) with objects inside. I need to: open F1: - perform some simple operations with the objects - export the solution with write.table - end F1 session open F2 repeat procedures as F1 open F50 repeat procedures as F1 My difficulty here is to end a workspace and open one from the scratch to avoid mixing files from consecutive worksessions, and thus using R memory unnecessarily. I could use rm() to delete objects from the previous sessions but it seems not an efficient task. Any suggestions on how to perform this in Batch Mode? An examplified help would be nice! Diogo Andr? Alagador http://www.biochange-lab.eu/people/diogo-alagador Biodiversity and Global Change Lab, Museo Nacional de Ciencias Naturales, CSIC, Madrid, Espa?a Forest Research Centre, Instituto Superior de Agronomia, UTL, Lisboa, Portugal
Would rm(list=ls()) not be an easier solution, if I understand your question correctly? Sarah On Sat, May 16, 2009 at 11:21 AM, <mcnda839 at mncn.csic.es> wrote:> Thanks a lot for all of you that have reply me about opening and ending R > workspaces in BATCH mode. However replies were a king general and I?m afraid > I could not take the entire message from them. Therefore I chose to expose > here a representative fraction of my work. > > I have 50 Rdata files (F1,F2,F3,F4,?,F50) with objects inside. > I need to: > > open F1: > ? - perform some simple operations with the objects > ? - export the solution with write.table > ? - end F1 session > open F2 > ? repeat procedures as F1 > ? > open F50 > ? repeat procedures as F1 > ? > > My difficulty here is to end a workspace and open one from the scratch to > avoid mixing files from consecutive worksessions, and thus using R memory > unnecessarily. I could use rm() to delete objects from the previous sessions > but it seems not an efficient task. > Any suggestions on how to perform this in Batch Mode? An examplified help > would be nice! > > Diogo Andr? Alagador > http://www.biochange-lab.eu/people/diogo-alagador > Biodiversity and Global Change Lab, Museo Nacional de Ciencias Naturales, > CSIC, Madrid, Espa?a > Forest Research Centre, Instituto Superior de Agronomia, UTL, Lisboa, > Portugal-- Sarah Goslee http://www.functionaldiversity.org
On Sat, May 16, 2009 at 10:21 AM, <mcnda839 at mncn.csic.es> wrote:> Thanks a lot for all of you that have reply me about opening and ending R > workspaces in BATCH mode. However replies were a king general and I?m afraid > I could not take the entire message from them. Therefore I chose to expose > here a representative fraction of my work. > > I have 50 Rdata files (F1,F2,F3,F4,?,F50) with objects inside. > I need to: > > open F1: > ? - perform some simple operations with the objects > ? - export the solution with write.table > ? - end F1 session > open F2 > ? repeat procedures as F1 > ? > open F50 > ? repeat procedures as F1 > ? > > My difficulty here is to end a workspace and open one from the scratch to > avoid mixing files from consecutive worksessions, and thus using R memory > unnecessarily. I could use rm() to delete objects from the previous sessions > but it seems not an efficient task. > Any suggestions on how to perform this in Batch Mode? An examplified help > would be nice!First try either Rcmd BATCH --help # for Windows or R CMD BATCH --help # for Mac OS X or for Linux and note that there are optional arguments --no-save and --no-restore. Use them.> Diogo Andr? Alagador > http://www.biochange-lab.eu/people/diogo-alagador > Biodiversity and Global Change Lab, Museo Nacional de Ciencias Naturales, > CSIC, Madrid, Espa?a > Forest Research Centre, Instituto Superior de Agronomia, UTL, Lisboa, > Portugal > > ______________________________________________ > 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. >
Emmanuel Charpentier
2009-May-18 06:09 UTC
[R] How to save R "clean" sessions in BATCH mode?
Le samedi 16 mai 2009 ? 17:21 +0200, mcnda839 at mncn.csic.es a ?crit :> Thanks a lot for all of you that have reply me about opening and > ending R workspaces in BATCH mode. However replies were a king general > and I?m afraid I could not take the entire message from them. > Therefore I chose to expose here a representative fraction of my work. > > I have 50 Rdata files (F1,F2,F3,F4,,F50) with objects inside.> I need to: > > open F1: > - perform some simple operations with the objects > - export the solution with write.table > - end F1 session > open F2 > repeat procedures as F1 >> open F50 > repeat procedures as F1 >> > My difficulty here is to end a workspace and open one from the scratch > to avoid mixing files from consecutive worksessions, and thus using R > memory unnecessarily. I could use rm() to delete objects from the > previous sessions but it seems not an efficient task.And re-loading R, rebuilding a whole process context, re-allocating memory is an efficient one ? Hah !> Any suggestions on how to perform this in Batch Mode? An examplified > help would be nice!Why not encapsulate your procedures in a function taking the filename as its argument and loop?ng on the filenames list ? Anything created in the function, being local to the function, will be (efficiently) cleaned up at the function exit. Magic... Exemple :> ls()character(0)> Foo<-runif(10,0,1) > ls()[1] "Foo"> ?save.image > save.image("Foo1.RData") > ls()[1] "Foo"> rm(list=ls()) > Foo<-letters[round(runif(10,min=1,max=26))] > Foo[1] "v" "m" "b" "y" "g" "u" "r" "f" "y" "q"> save.image("Foo2.RData") > rm(list=ls()) > bar<-edit()bar<-edit() Waiting for Emacs...> barfunction(filename) { load(file=filename) print(ls()) print(Foo) invisible(NULL) }> ls()[1] "bar"> bar("Foo1.RData")[1] "filename" "Foo" # Note : by default, ls() list the function's # environment, not the global one... **> no "bar" here... [1] 0.8030422 0.6326055 0.8188481 0.6161665 0.5917206 0.6631358 0.7290200 [8] 0.2970315 0.2016259 0.4473244> ls()[1] "bar" # Bar is still in the global environment...> bar("Foo2.RData")[1] "filename" "Foo" [1] "v" "m" "b" "y" "g" "u" "r" "f" "y" "q"> ls()[1] "bar">Good enough for you ? HTH, Emmanuel Charpentier