> 4) One problem with saving an R session and then restoring it is that > the packages in use are not reloaded. Quitting an R session and > saving could write .Rpackages in the current directory (with the > library recorded if it were not the default). Then restarting a > session in that directory would restore the loaded packages > automatically.I've been using another approach for this. For the R + Zope tools that I've developed here (Yes, they will be released once I finally get through all the red tape!) I needed a way to store all of the information about a session, including loaded packages and the search path. At the end of this message, I list two functions, save.session() and load.session() which I wrote to accomplish this. In addition, I have an .Rd documentation file for them, and can provide that if anyone is interested. The functions save.session() records the list of loaded packages into a variable named ".save.session.packages" which is then saved as part of save.image(). The load.session() function then uses load() to reload the saved image and uses the ".save.session.packages" function to reload the packages. A similar thing happens for the search path. This works well and seems less hazard-prone than overwriting the users '.Rpackages' file for two reasons: 1) the loaded package list and search path are preserved with the data, and 2) we aren't messing with the user's files... One enhancement that would be desirable is to allow the user to request that the search path or the packages not be restored. Of course, both can be avoided by just using load() instead of load.session() on the data file. -Greg save.session <- function(file=".RSession",...) { if(!is.null(dev.list())) warning("Open graphics devices will not be saved or restored.") cat("Saving search path..\n") .save.session.search <<- search() cat("Saving list of loaded packages..\n") .save.session.packages <<- .packages() cat("Saving all data...\n") save(list=ls(envir = .GlobalEnv, all.names = TRUE), file=file, ...) cat("Done.\n") } restore.session <- function(file=".RSession",...) { cat("Loading all data...\n") load(file,envir = .GlobalEnv,...) cat("Loading packages...\n") sapply( rev(.save.session.packages), library, character.only=T ) cat("Restoring search path...\n") pad <- function(x,n) c( rep(NA,n-length(x)), x ) current.search <- search()[-1] saved.search <- .save.session.search[-1] identical <- pad(current.search, length(saved.search)) == saved.search for( i in saved.search[!identical] ) { if( charmatch( "file:", i, nomatch=FALSE) ) attach(sub( "file:", "", "file:test" ) ) else if (charmatch( "package:", i, nomatch=FALSE) ) stop(paste("Somehow we missed loading package",i)) else { do.call("attach",list(as.name(i))) } } rm(list=c(".save.session.packages", ".save.session.search"), envir = .GlobalEnv ) cat("Done.\n") } LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Oh wow, just looking over this, I already see a bug. This section of load.session():> for( i in saved.search[!identical] ) > { > if( charmatch( "file:", i, nomatch=FALSE) ) > attach(sub( "file:", "", "file:test" ) ) > else if (charmatch( "package:", i, nomatch=FALSE) ) > stop(paste("Somehow we missed loading package",i)) > else > { > do.call("attach",list(as.name(i))) > } >should be> for( i in saved.search[!identical] ) > { > if( charmatch( "file:", i, nomatch=FALSE) ) > attach(sub( "file:", "", i ) ) > else if (charmatch( "package:", i, nomatch=FALSE) ) > stop(paste("Somehow we missed loading package",i)) > else > { > do.call("attach",list(as.name(i))) > } >-Greg LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
[Sorry, just noticed this thread and don't know what's been said already, but I will comment nonetheless.] I've also thought about this problem and I think we should be careful to make sure its solved in a very general manner. Objects bind methods to data, but this is broken when saving and reloading R sessions (only the data ends up in .RData and not the bindings). What happens if someone has multiple saved R sessions in a single directory? A single .Rpackages file would either leave out packages or have to include all packages across all sessions. I would far rather have a mechanism that recognizes an object as an object and lets that object load the appropriate bindings to methods at (re)instantiation. I once proposed that there be function hooks in the save/load process that could be overloaded by object type to make sure that any initializations (including package loading) could be completed before attaching the data. This would also allow for example database objects to reestablish their connections when a session is reloaded from disk. Tim On Tue, 2002-10-22 at 06:46, Warnes, Gregory R wrote:> > > > 4) One problem with saving an R session and then restoring it is that > > the packages in use are not reloaded. Quitting an R session and > > saving could write .Rpackages in the current directory (with the > > library recorded if it were not the default). Then restarting a > > session in that directory would restore the loaded packages > > automatically. > > > I've been using another approach for this. For the R + Zope tools that > I've developed here (Yes, they will be released once I finally get through > all the red tape!) I needed a way to store all of the information about a > session, including loaded packages and the search path. > > At the end of this message, I list two functions, save.session() and > load.session() which I wrote to accomplish this. In addition, I have an .Rd > documentation file for them, and can provide that if anyone is interested. > > The functions save.session() records the list of loaded packages into a > variable named ".save.session.packages" which is then saved as part of > save.image(). The load.session() function then uses load() to reload the > saved image and uses the ".save.session.packages" function to reload the > packages. A similar thing happens for the search path. This works well > and seems less hazard-prone than overwriting the users '.Rpackages' file for > two reasons: 1) the loaded package list and search path are preserved with > the data, and 2) we aren't messing with the user's files... > > One enhancement that would be desirable is to allow the user to request that > the search path or the packages not be restored. Of course, both can be > avoided by just using load() instead of load.session() on the data file. > > -Greg > > save.session <- function(file=".RSession",...) > { > if(!is.null(dev.list())) > warning("Open graphics devices will not be saved or restored.") > > cat("Saving search path..\n") > .save.session.search <<- search() > > cat("Saving list of loaded packages..\n") > .save.session.packages <<- .packages() > > cat("Saving all data...\n") > save(list=ls(envir = .GlobalEnv, all.names = TRUE), file=file, ...) > cat("Done.\n") > } > > restore.session <- function(file=".RSession",...) > { > cat("Loading all data...\n") > load(file,envir = .GlobalEnv,...) > > cat("Loading packages...\n") > sapply( rev(.save.session.packages), library, character.only=T ) > > cat("Restoring search path...\n") > > pad <- function(x,n) c( rep(NA,n-length(x)), x ) > > current.search <- search()[-1] > saved.search <- .save.session.search[-1] > > identical <- pad(current.search, length(saved.search)) == saved.search > > for( i in saved.search[!identical] ) > { > if( charmatch( "file:", i, nomatch=FALSE) ) > attach(sub( "file:", "", "file:test" ) ) > else if (charmatch( "package:", i, nomatch=FALSE) ) > stop(paste("Somehow we missed loading package",i)) > else > { > do.call("attach",list(as.name(i))) > } > > } > > rm(list=c(".save.session.packages", > ".save.session.search"), > envir = .GlobalEnv ) > > > cat("Done.\n") > } > > > > LEGAL NOTICE > Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Timothy H. Keitt The University of Texas at Austin Section of Integrative Biology 1 University Station C0930 Austin, Texas 78712-0253 USA -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I hardly know what to say, other than this seems such a mild but useful suggestion I find any opposition to it hard to imagine. I say go for it. Bill Venables. -----Original Message----- From: Prof Brian D Ripley [mailto:ripley@stats.ox.ac.uk] Sent: Tuesday, October 22, 2002 6:25 AM To: R-devel@stat.math.ethz.ch Subject: RFC: Loading packages at startup I've been kicking the following idea around for a while, and am now proposing to put some version into 1.7.0. I'd be interested in comments on the desirability and the design, before I start writing any code. S4 introduced a file .S.chapters which can contain a list of S chapters (equivalent to R packages) to be loaded on start-up. This was the germ of this proposal. Proposal: Extend the initialization as described in Startup.Rd by having optionally files named, say, R_HOME/etc/Rpackages.site and .Rpackages, the latter in the starting directory or failing that the user's home directory. Each would contain a list of packages, one per line, to be loaded when R is started, in the order in the files. Some details: 1) I think the packages should be loaded before .Rprofile and .RData are processed, and R_HOME/etc/Rpackages.site before .Rpackages. This can be argued, and the S4 parallel would seem to be to load packages after S.init (the nearest it has to Rprofile). But we would load library/base/Rprofile first of all so the analogy is not close. 2) The present kludge of loading ctest in .First could be replaced by making ctest the default content of R_HOME/etc/Rpackages.site (in the light of point 5). 3) It would be useful to allow the library tree to be specified, as a second field on the line. 4) One problem with saving an R session and then restoring it is that the packages in use are not reloaded. Quitting an R session and saving could write .Rpackages in the current directory (with the library recorded if it were not the default). Then restarting a session in that directory would restore the loaded packages automatically. 5) We might want to allow a .Rpackages file to override Rpackages.site (or we might not). One idea is to allow a minus sign in front of a package name, and to merge the Rpackages.site and .Rpackages files before loading any packages. If we did this we probably need to be able to save the list of packages to be loaded (and can't easily save those not to be loaded), so perhaps -- as the first list of .Rpackages should empty the list. 6) One could argue for R_HOME/etc/Rpackages as the `system' file as well, and this might be useful if we break base up into smaller components. 7) I would allow comment lines in the files, starting with #. 8) The file names or names could be set by environment variables. It's strange that we allow the site file names for Rprofile and Renviron and the user file name for command histories to be set in that way. -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._