Dear list members, Does anyone know how to use rm() to remove only variables but not declared functions from the environment ? I understand I could name all the functions with, let's say "f_something", make sure that all variables do not start with "f_" and then remove all BUT objects starting with "f_". However, I have already defined all the functions and it would be troublesome to change all of them to a new name. Any hint ? Thanks Paulo Gustavo Grahl, CFA
Paulo Grahl <pgrahl <at> gmail.com> writes:> > Dear list members, > > Does anyone know how to use rm() to remove only variables but not > declared functions from the environment ? > I understand I could name all the functions with, let's say > "f_something", make sure that all variables do not start with "f_" and > then remove all BUT objects starting with "f_". > However, I have already defined all the functions and it would be > troublesome to change all of them to a new name. >> a <- 1 > b <- 2 > d <- function(x) { x^2 } > objs <- ls() > objclasses <- sapply(objs,function(x) class(get(x))) > objclassesa b d "numeric" "numeric" "function"> rm(list=objs[objclasses!="function"]) > ls()[1] "d" "objclasses" "objs">(objclasses and objs are left over, but a and b have been deleted)
On 02/02/2009 8:16 AM, Paulo Grahl wrote:> Dear list members, > > Does anyone know how to use rm() to remove only variables but not > declared functions from the environment ? > I understand I could name all the functions with, let's say > "f_something", make sure that all variables do not start with "f_" and > then remove all BUT objects starting with "f_". > However, I have already defined all the functions and it would be > troublesome to change all of them to a new name. > > Any hint ?Here's a list of functions: lsf.str() And here's a list of everything: ls() So here are non-functions: setdiff(ls(), lsf.str()) And here they go: rm(list = setdiff(ls(), lsf.str()) ) Duncan Murdoch
On Mon, Feb 2, 2009 at 2:16 PM, Paulo Grahl <pgrahl at gmail.com> wrote:> Dear list members, > > Does anyone know how to use rm() to remove only variables but not > declared functions from the environment ? > I understand I could name all the functions with, let's say > "f_something", make sure that all variables do not start with "f_" and > then remove all BUT objects starting with "f_". > However, I have already defined all the functions and it would be > troublesome to change all of them to a new name. > > Any hint ? > Thanks > > Paulo Gustavo Grahl, CFA >[Note to Paulo:I changed the code slightly: defining Nonfunctions separately messed things up.] Hi Paulo, The following should do it. test<-function(x)x^2 test2<-5 test3<-77 ls() rm(list=ls()[ sapply(ls(), function(x){ class(get(x))!="function" }) ]) ls() Regards, Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
You can get a list of all functions in your workspace with ls()[sapply(ls(), function(x) is.function(get(x)))] # or ls()[sapply(sapply(ls(), get), is.function)] Removing everything else is rm(list=ls()[sapply(ls(), function(x) !is.function(get(x)))]) # or rm(list=ls()[!sapply(sapply(ls(), get), is.function)]) That suffices if you don't have any names starting with .period; if you do, you'll need ls(all=TRUE) KK On Mon, Feb 2, 2009 at 3:16 PM, Paulo Grahl <pgrahl@gmail.com> wrote:> Dear list members, > > Does anyone know how to use rm() to remove only variables but not > declared functions from the environment ? > I understand I could name all the functions with, let's say > "f_something", make sure that all variables do not start with "f_" and > then remove all BUT objects starting with "f_". > However, I have already defined all the functions and it would be > troublesome to change all of them to a new name. > > Any hint ? > Thanks > > Paulo Gustavo Grahl, CFA > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
If you want to keep the functions, why not move them to a different environment so that they don't get deleted when you delete everything else (this will also work better if you want to use these same functions in other R sessions). The most comprehensive way to do this is to create a package with the functions (package.skeleton will get you started). One of the simplest ways to do this (if the package idea is overkill, though if you expand this, the package solution may not be overkill in the long run) is to use the 'save' command to save your functions into a file, delete everything including the functions, then use 'attach' to attach the file you saved the functions in. Now you can still use the functions (just be careful if you try to edit them), but they are not in the main environment where the data is stored and when you delete 'everything' the next time, the attached functions will not be affected. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Paulo Grahl > Sent: Monday, February 02, 2009 6:17 AM > To: r-help at r-project.org > Subject: [R] Selectively Removing objects > > Dear list members, > > Does anyone know how to use rm() to remove only variables but not > declared functions from the environment ? > I understand I could name all the functions with, let's say > "f_something", make sure that all variables do not start with "f_" and > then remove all BUT objects starting with "f_". > However, I have already defined all the functions and it would be > troublesome to change all of them to a new name. > > Any hint ? > Thanks > > Paulo Gustavo Grahl, CFA > > ______________________________________________ > 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.
Slightly related to this (I think Mr. Rydevik's code solved the question), is a silly thing I wrote up one weekend. It started out as a clone of the unix "rm -i" command, and kept on going out of control :-) I'm not claiming this is clean, or the best way to do this, but it does let you apply a wide variety of functions to your collection of 'items' Carl ------------- askrm<-function(items,fn="rm",ask=TRUE){ killed<-NA thecall<-vector('list') j<-1 for (thenam in c(items)){ if(ask==TRUE){ prmpt<-paste("Do ",fn," on ",thenam,"? ") readline(prompt=prmpt)->theans } else theans="y" if(theans=="y"){ #have to get to parent envir. to find the object of interest #as.name() gets rid of quotes... # paste() dumps all output into a single element of list # Note that,e.g., str() returns nothing, just cats to screen. eval(call(fn,as.name(thenam)),envir=parent.frame(1))->evout paste(evout,collapse=" ")->thecall[j] cat("the result is ", as.character(thecall[j]),'\n') killed[j]<-thenam j<-j+1 } } #keeping track of what happened outs<-list(killed=killed, calls=thecall) return(invisible(outs)) }