Using RedHat Linux 7.0, R-1.2.2, R-hdf5-1.2 library, I want to load a dataset, do some stuff with it, then erase its objects, get an other, repeat. My friend wrote a function which tried to clear away all the objects. At the end, it uses rm() to remove objects. This is the same way we do it interactively, from the R prompt: testLoadSeveralHDF <- function(numFiles) { for (i in 0:(numFiles-1)) { filename <- paste("trial",i,".hdf",sep="") print(filename) hdf5load(filename) rm(list = ls(pat="g*")) } } However, after that program runs, the g* objects are not erased, but typing "rm(list=ls(pat="g*")) from the command line does erase them. I realized there's something about the environment that I need to tell the function, I just don't know what? pj ps. Now that Frank Harrell is using R, I'm so enthusiastic! I'll do some R in my advanced methods class in Fall, 2001 and continue my updating of the tipsheet: http://lark.cc.ukans.edu/~pauljohn/R/statsRus.html -- Paul E. Johnson email: pauljohn at ukans.edu Dept. of Political Science http://lark.cc.ukans.edu/~pauljohn University of Kansas Office: (785) 864-9086 Lawrence, Kansas 66045 FAX: (785) 864-5700 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help 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-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Paul E Johnson <pauljohn at ukans.edu> writes:> Using RedHat Linux 7.0, R-1.2.2, R-hdf5-1.2 library, > > I want to load a dataset, do some stuff with it, then erase its objects, > get an other, repeat. My friend wrote a function which tried to clear > away all the objects. At the end, it uses rm() to remove objects. This > is the same way we do it interactively, from the R prompt: > > testLoadSeveralHDF <- function(numFiles) { > for (i in 0:(numFiles-1)) { > filename <- paste("trial",i,".hdf",sep="") > print(filename) > hdf5load(filename) > rm(list = ls(pat="g*")) > } > } > > However, after that program runs, the g* objects are not erased, but > typing "rm(list=ls(pat="g*")) from the command line does erase them. I > realized there's something about the environment that I need to tell the > function, I just don't know what?Add envir = .GlobalEnv to both the ls and the rm calls. That is rm(list = ls(pat = "g*", envir = .GlobalEnv), envir = .GlobalEnv) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help 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-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
In case this is not what you intended, may I point out that rm(list=ls(pat="g*")) will remove _all_ objects and its effect appears to be the same as rm(list=ls()) as far as I can tell. To remove objects whose names have "g" anywhere in the string, use rm(list=ls(pat="g")), or names starting with "g" one can use rm(list=ls(pat="^g")). The "^" and "$" can be used to delimit the beginning and ending of a string, respectively. Rashid Nassar On Tue, 10 Apr 2001, Paul E Johnson wrote:> Date: Tue, 10 Apr 2001 09:08:18 -0500 > From: Paul E Johnson <pauljohn at ukans.edu> > To: "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch> > Subject: [R] clear R-objects inside a function? > > Using RedHat Linux 7.0, R-1.2.2, R-hdf5-1.2 library, > > I want to load a dataset, do some stuff with it, then erase its objects, > get an other, repeat. My friend wrote a function which tried to clear > away all the objects. At the end, it uses rm() to remove objects. This > is the same way we do it interactively, from the R prompt: > > testLoadSeveralHDF <- function(numFiles) { > for (i in 0:(numFiles-1)) { > filename <- paste("trial",i,".hdf",sep="") > print(filename) > hdf5load(filename) > rm(list = ls(pat="g*")) > } > } > > However, after that program runs, the g* objects are not erased, but > typing "rm(list=ls(pat="g*")) from the command line does erase them. I > realized there's something about the environment that I need to tell the > function, I just don't know what? > > pj > > ps. Now that Frank Harrell is using R, I'm so enthusiastic! I'll do some > R in my advanced methods class in Fall, 2001 and continue my updating of > the tipsheet: http://lark.cc.ukans.edu/~pauljohn/R/statsRus.html > > -- > Paul E. Johnson email: pauljohn at ukans.edu > Dept. of Political Science http://lark.cc.ukans.edu/~pauljohn > University of Kansas Office: (785) 864-9086 > Lawrence, Kansas 66045 FAX: (785) 864-5700 > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help 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-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help 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-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._