Skip to content

Selectively Removing objects

7 messages · Paulo Grahl, Ben Bolker, Duncan Murdoch +2 more

#
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:
a          b          d 
 "numeric"  "numeric" "function"
[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:
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
#
Thank you all for the prompt answer !!
It did solve my problem perfectly !
Rgds,
Paulo
On Mon, Feb 2, 2009 at 11:51 AM, Kenn Konstabel <lebatsnok at gmail.com> wrote:
#
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,
#
Thanks Greg.
I did in the past looked into the details of how to create a package.
I did not seem too complicated, but it would be time consuming -- an
then when one thinks in creating a package (even for his own use)  it
always a good idea to check for wrong inputs, to have all functions
well documented, etc. The functions I've developed so far are not yet
in this stage.

But i liked the idea of using attach(). I had overlooked this point in
the attach() description.
Thanks.


Paulo Gustavo Grahl, CFA
On Mon, Feb 2, 2009 at 4:41 PM, Greg Snow <Greg.Snow at imail.org> wrote: