[R] how to keep functions while remove all other commands

Simon Fear Simon.Fear at synequanon.com
Mon Feb 2 10:42:23 CET 2004

As an alternative to Petr's approach of using separate
directories, one can also write a function (which I find quite
useful in its own right) to separate ls() output by mode, e.g.

"lsd" <- function(splitby=mode, pos=1, ...) {
    lsout <- ls(pos=pos, ...)
      sapply(lsout, function(x) {splitby(get(x))}),

Given that, you can use expressions such as

rm(list=lsd(splitby=class, all.names=T)$integer)

or to remove functions,

rm(list=lsd()$"function") # nb reserved word function needs quotes

this latter also removes `lsd` itself, so do also follow the advice
re keeping a copy of all your functions in a text file somewhere!

> -----Original Message-----
> On 24 Jan 2004 at 21:47, Yong Wang wrote:
> > Dear all:
> > a quick question:
> > I am used to apply  rm(list=()) regularly to remove all old codes in
> > preventing them creeping in current analysis.however, with that
> > application, functions I wrote are also removed. please let me know
> > how to keep the thing you want while remove those you don't.
Simon Fear 
Senior Statistician 
Syne qua non Ltd 
Tel: +44 (0) 1379 644449 
Fax: +44 (0) 1379 644445 
email: Simon.Fear at synequanon.com 
web: http://www.synequanon.com 
Number of attachments included with this message: 0 
This message (and any associated files) is confidential and\...{{dropped}}

More information about the R-help mailing list