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-h...@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.
>
> 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)))
> objclasses
a 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)
[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
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
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
[[alternative HTML version deleted]]
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...@imail.org
801.408.8111
But i liked the idea of using attach(). I had overlooked this point in
the attach() description.
Thanks.
Paulo Gustavo Grahl, CFA
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))