[R] R GUI and object browser

Dan Putler dputler at scu.edu
Mon Jul 15 10:13:14 CEST 2002


As part of the obveRsive GUI project, we've written a function with much of 
the functionality that Patrick Connolly was suggesting (which is a bit more 
compact than the output from ls.str()).  Alas, no date capabilities.  The 
function is call ls.objects(), and example output appears below:

   Object.Name Object.Mode Object.Type Observations Variables
1    ccsest.df        list  data.frame          800        21
2    ccsval.df        list  data.frame          800        21
3   junkfactor     numeric      factor          100         1
4     junklist        list     unknown            -         -
5      junkmat     numeric      matrix          100         2
6    junkmodel        list          lm            -         -
7   ls.objects    function    function            -         -
8      orderdf    function    function            -         -
9          pkg   character      vector            1         1
10       stuff        list  data.frame           12         2
11         try     numeric      vector          100         1
12        try1     numeric      vector          100         1
13        try2     numeric      vector          100         1
14      trydat        list  data.frame          100         2
15     tryname   character      vector            1         1

The function has "mode" and "type" arguments (type is typically, but not 
always an object's class attribute).  If the user only wants to list 
data.frames, then the command ls.objects(type="data.frame") will list only 
information for data.frames.

The code itself follows below:
__________________________________________________________
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]]))
			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))
			}
		}
	}
	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)
}

Hopefully this is what some of you were looking for.

Dan Putler

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list