rm(ls()=='function')

12 views
Skip to first unread message

Jonathan Rosenblatt

unread,
Oct 22, 2014, 4:14:07 PM10/22/14
to israel-r-user-group
Don't run the code in the subject.

My question is how can one remove from the workspace all objects of a given class? Say, function. 

--
Jonathan Rosenblatt
www.john-ros.com

Jonathan Rosenblatt

unread,
Oct 22, 2014, 4:30:54 PM10/22/14
to israel-r-user-group
OK... possibly solved it...

function.ind <- unlist(
  lapply(
    lapply(
      mget(ls()),
      class)
    , function(x) x[[1]]=='function')) 
rm(list=ls()[function.ind])

--
Jonathan Rosenblatt
www.john-ros.com

amit gal

unread,
Oct 22, 2014, 5:01:22 PM10/22/14
to israel-r-...@googlegroups.com
you can use
as.list(.GlobalEnv) to get a list of all objects in your environment (instead of the list of names you get by ls() ). you can then apply the class() function to that list, and so on. I'm not sure if the code gets less messy than yours, but I think it is logically cleaner.


--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages