I have a large R program that I am constantly running over and over again. At the beginning of this program, I create a hige matrix and a huge dataframe but these are constant. What I mean by constant is that, if I run the program over later, I really should just use the old matrix and dataframe ( if they exist ) that were created in a previous run so that the program doesn't have to spend time creating them. Unfortunately, I don't know how to do this so things take foreeer because every time i do a new run, i recreate these objects and the boss is getting a little annoyed. I know/think that I should use attach and detach commands but 1) i can't find an example somewhere of just saving two objects rather than the whole session. i've looked and looked and i can't find it. 2) if i am able to save these two objects, i was hoping that it would be possible to write code inside my R program that basically says, "if these objects already exist in such and such database, then skip over this section of the code that creates them". if someone has some code or a page number or reference, this is fine because i used to do pretty much this in Splus so think i can figure it out if i can just find an example. thanks a lot.
There is an example of saving just two objects in the example section of ?save To check whether an object of a given name exists see ?exists On 7/7/06, markleeds at verizon.net <markleeds at verizon.net> wrote:> > I have a large R program that I am constantly running over and over again. At the beginning of this program, I create a hige matrix and a huge dataframe but these are constant. What I mean by constant is that, if I run the program over later, I really should just use the old matrix and dataframe ( if they exist ) that were created in a previous run so that the program doesn't have to spend time creating them. Unfortunately, I don't know how to do this so things take foreeer because every time i do a new run, i recreate these objects and the boss is getting a little annoyed. I know/think that I should use attach and detach commands but > > 1) i can't find an example somewhere of > just saving two objects rather than the whole session. > i've looked and looked and i can't find it. > > 2) if i am able to save these two objects, i was hoping that it > would be possible to write code inside my R program > that basically says, "if these objects already exist in > such and such database, then skip over this section of the code > that creates them". > > if someone has some code or a page number or reference, this is fine because i used to do pretty much this in Splus so think i can figure it out if i can just find an example. thanks a lot. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
markleeds at verizon.net wrote:> I have a large R program that I am constantly running over and over again. At the beginning of this program, I create a hige matrix and a huge dataframe but these are constant. What I mean by constant is that, if I run the program over later, I really should just use the old matrix and dataframe ( if they exist ) that were created in a previous run so that the program doesn't have to spend time creating them. Unfortunately, I don't know how to do this so things take foreeer because every time i do a new run, i recreate these objects and the boss is getting a little annoyed. I know/think that I should use attach and detach commands but > > 1) i can't find an example somewhere of > just saving two objects rather than the whole session. > i've looked and looked and i can't find it. > > 2) if i am able to save these two objects, i was hoping that it > would be possible to write code inside my R program > that basically says, "if these objects already exist in > such and such database, then skip over this section of the code > that creates them".You probably want save() and load() rather than attach() and detach(). # Save only a couple of objects save(huge.mat, huge.df, file="myfile.Rdata") # Load objects only if they do not exist if(!exists("huge.mat") & !exists ("huge.df")) load("myfile.Rdata") else {cat("\n", "Object(s) Already Exist", "\n")} ?save ?load ?exists> if someone has some code or a page number or reference, this is fine because i used to do pretty much this in Splus so think i can figure it out if i can just find an example. thanks a lot. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
On Fri, 2006-07-07 at 12:57 -0500, markleeds at verizon.net wrote: <snipped />> > 1) i can't find an example somewhere of > just saving two objects rather than the whole session. > i've looked and looked and i can't find it.?save> 2) if i am able to save these two objects, i was hoping that it > would be possible to write code inside my R program > that basically says, "if these objects already exist in > such and such database, then skip over this section of the code > that creates them". >Is this the kind of thing you mean? I used the FileExists function from package RandomFields as this does the file checking - you could do something similar with ?file.list and match the name but why reinvent the wheel. I flipped your situation 2 around, if you have some really big objects then it would make sense to generate the objects, save them out to files. From a new session, you are likely not to have objects, so perhaps better to check the file exists, if so load it, then check the right objects are there. Alter to what suits you best. ## create two objects to be saved obj1 <- matrix(rnorm(1000), ncol = 10) obj2 <- matrix(rnorm(1000), ncol = 10) ## save them save(obj1, obj2, file = "tmp.file.RData") ## clean up rm(list = ls()) ls() ## load the required package require(RandomFields) ## load the saved objects if(FileExists("tmp.file.RData")) { load("tmp.file.RData") if(exists("obj1") & exists("obj2")) { ## more code here summary(obj1) summary(obj2) } else stop("Required objects don't exist!") } else stop("Big objects not found, check file exists!") ## clean up rm(list = ls()) ## if file exists but not correct objects if(FileExists("tmp.file.RData")) { load("tmp.file.RData") ## simulate different object names obj3 <- obj1 obj4 <- obj2 rm(obj1, obj2) if(exists("obj1") & exists("obj2")) { ## more code here summary(obj1) summary(obj2) } else stop("Required objects don't exist!") } else stop("Big objects not found, check file exists!") ## clean up rm(list = ls()) ## if file doesn't exists unlink("tmp.file.RData") if(FileExists("tmp.file.RData")) { load("tmp.file.RData") ## remove the objects obj3 <- obj1 obj4 <- obj2 rm(obj1, obj2) if(exists("obj1") & exists("obj2")) { ## more code here summary(obj1) summary(obj2) } else stop("Required objects don't exist!") } else stop("Big objects not found, check file exists!") HTH, G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC & ENSIS, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/cv/ London, UK. WC1E 6BT. [w] http://www.ucl.ac.uk/~ucfagls/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%