[R] A way to list only variables or functions?

Shin, Daehyok sdhyok at email.unc.edu
Sun Jun 20 17:55:43 CEST 2004


Interesting, but it seems too complex.
In my opinion, just listing functions or non-functions meets my need.
Anyway, thanks.

Daehyok Shin (Peter)

> -----Original Message-----
> From: S?en Merser [mailto:merser at image.dk]
> Sent: Sunday, June 20, 2004 AM 10:51
> To: Shin
> Subject: Re: [R] A way to list only variables or functions?
> 
> 
> hi
> forgot who actualy wrote this code but it works nicely
> use:
>     ls.objects(type='function')
> 
> regards soren
> 
>  ls.objects<-function (pos = 1, pattern, mode = "any", type = "any"){
>     Object.Name <- ls(pos = pos, envir = as.environment(pos), pattern =
> pattern)
>     Object.Mode <- rep("",length(Object.Name))
>     Object.Type <- rep("",length(Object.Name))
>     Variables <- rep("-",length(Object.Name))
>     Observations <- rep("-",length(Object.Name))
>     for (i in 1:length(Object.Name)){
> Object.Mode[[i]] <- mode(get(Object.Name[[i]]))
> if(is.list(get(Object.Name[[i]]))){
> if(is.null(class(get(Object.Name[[i]]))))
> Object.Type[[i]] <- c("unknown")
> else {
> Object.Attrib <- attributes(get(Object.Name[[i]]))
> if(length(Object.Attrib$class) == 1) {
> Object.Type[[i]] <- Object.Attrib$class
> if(Object.Type[[i]]=="data.frame"){
> Variables[[i]] <- as.character(length(Object.Attrib$names))
> Observations[[i]] <- as.character(length(Object.Attrib$row.names))
> }
> }
> else {
> if("data.frame" %in% Object.Attrib$class){
> Object.Type[[i]] <- "data.frame"
> Variables[[i]] <- as.character(length(Object.Attrib$names))
> Observations[[i]] <- as.character(length(Object.Attrib$row.names))
> }
> else {
> if("aov" %in% Object.Attrib$class) Object.Type[[i]] <- "aov"
> }
> }
> }
> }
> if(is.matrix(get(Object.Name[[i]]))){
> Object.Attrib <- dim(get(Object.Name[[i]]))
> Object.Type[[i]] <- c("matrix")
> Variables[[i]] <- as.character(Object.Attrib[2])
> Observations[[i]] <- as.character(Object.Attrib[1])
> }
> if(is.vector(get(Object.Name[[i]])) && (Object.Mode[[i]]=="character" ||
> Object.Mode[[i]]=="numeric")){
> Object.Type[[i]] <- c("vector")
> Variables[[i]] <- c("1")
> Observations[[i]] <- as.character(length(get(Object.Name[[i]])))
> }
> if(is.factor(get(Object.Name[[i]]))){
> Object.Type[[i]] <- c("factor")
> Variables[[i]] <- c("1")
> Observations[[i]] <- as.character(length(get(Object.Name[[i]])))
> }
> if(is.function(get(Object.Name[[i]]))) Object.Type[[i]] <- c("function")
>     }
>     objList <-
> data.frame(Object.Name,Object.Mode,Object.Type,Observations,Variables)
>     if(mode != "any") objList <- objList[objList[["Object.Mode"]] 
> == mode,]
>     if(type != "any") objList <- objList[objList[["Object.Type"]] 
> == type,]
>     return(objList)
> }
> ----- Original Message ----- 
> From: "Shin" <sdhyok at email.unc.edu>
> To: "R Help" <r-help at stat.math.ethz.ch>
> Sent: Sunday, June 20, 2004 4:59 AM
> Subject: [R] A way to list only variables or functions?
> 
> 
> > I am curious if there is any way to list only variables or functions in
> > current environment, rather than listing all objects? Thanks.
> >
> > -- 
> > Daehyok Shin (Peter)
> > Geography Department
> > Univ. of North Carolina-Chapel Hill
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
> 
> 
>




More information about the R-help mailing list