remove all variables except functions

RockScience picture RockScience · Nov 29, 2011 · Viewed 119.1k times · Source

I have loaded in a R console different type of objects. I can remove them all using

rm(list=ls())

or remove only the functions (but not the variables) using

rm(list=lsf.str())

My question is: is there a way to remove all variables except the functions

Answer

Josh O'Brien picture Josh O'Brien · Nov 29, 2011

Here's a one-liner that removes all objects except for functions:

rm(list = setdiff(ls(), lsf.str()))

It uses setdiff to find the subset of objects in the global environment (as returned by ls()) that don't have mode function (as returned by lsf.str())