[R] Re: Re RGui and object browser

Dan Putler dputler at scu.edu
Fri Jul 19 10:27:42 CEST 2002

Hi Petr and any other interested parties,

Objects with mode "list" that have more than a single class attribute do cause 
my original function to die in a less than pleasant way.  Unfortunately, when 
the class attribute is longer than one, there are a huge number of potential 
special cases.  The two that have been brought to my attention so far are 
related to the Orthodont data frame (part of the nlme package) which has four 
class attributes (last in the list being "data.frame") and objects created by 
the aov() function (which can have two or four class descriptors).  I have 
revised the code to make it a bit more robust, and correct the two known 
problems (an object created by aov is given the Object.Type of "aov").  
However, the function should be viewed as being in an "early beta" state, so 
there will be other (hopefully minor) bugs that will come up.  In addition, 
there are other things missing from the function as it now stands.  
Specifically, it does not provide Object.Type information for certain types 
of objects such as "call" and "name".  Although this information gets 
captured by Object.Mode.

I've attached my revised version of the code below.  Any other suggestions or 
improvements would be welcomed.  Although, you may want to do this in a 
private e-mail to me.

Dan Putler


ls.objects <- function (pos = 1, pattern, mode = "any", type = "any"){
    Object.Name <- ls(pos = pos, envir = as.environment(pos), 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]]))
			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
					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"
	 	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.Type[[i]] <- c("vector")
		Variables[[i]] <- c("1")
		Observations[[i]] <- as.character(length(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 <- 
    if(mode != "any") objList <- objList[objList[["Object.Mode"]] == mode,]
    if(type != "any") objList <- objList[objList[["Object.Type"]] == type,]

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