[R] stupid R tricks

David Winsemius dwinsemius at comcast.net
Sun Nov 7 18:25:06 CET 2010


On Nov 7, 2010, at 11:40 AM, Carl Witthoft wrote:

> Hi all,
> Just thought I'd post this (maybe) helpful tool I wrote.  For people  
> like me who are bad at keeping a clean environment, it's a time-saver.
>
> #simple command to get only one type of object in current environment
> lstype<-function(type='closure'){
> 	inlist<-ls(.GlobalEnv)
> 	if (type=='function') type <-'closure'
> 	typelist<-sapply(sapply(inlist,get),typeof)
> 	return(names(typelist[typelist==type]))
> }

As a fellow messy-enviromnetalist that was useful. Here's a similar  
function that returns a vector of object names belonging to a  
particular (single) class:

 > getclass <- function(cls) ls(envir=.GlobalEnv)[
             sapply(ls(envir=.GlobalEnv), function(x)  
class(eval(parse(text=x))) ) == cls
                                                 ]

 > getclass("data.frame")
[1] "d"       "df"      "set1HLI"
 > getclass("function")
  [1] "capture.ps"  "getclass"    "getsizes"    "getweek"      
"is.defined"
  [6] "maintainer"  "myfunction"  "rd.txt"      "rhelpSearch"  
"write.clip"

It is not designed to identify multi-class objects, so it would miss a  
class of "lm" if there were 2 members in a class vector.
>
> Carl
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list