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
You can use the following command to clear out ALL variables. Be careful because it you cannot get your variables back.
Here's a one-liner that removes all objects except for functions:
It uses
setdiff
to find the subset of objects in the global environment (as returned byls()
) that don't have modefunction
(as returned bylsf.str()
)Here's a pretty convenient function I picked up somewhere and adjusted a little. Might be nice to keep in the directory.
The posted
setdiff
answer is nice. I just thought I'd post this related function I wrote a while back. Its usefulness is up to the reader :-).