[R] Re: check variables: a Q from a beginner

David Hinds David_Hinds at perlegen.com
Fri Jan 17 21:58:02 CET 2003


I played around a bit with the ls.objects() code posted by Petr Pikal
yesterday and came up with the following that is a bit faster, shorter,
and adds memory usage information.  It combines an object's mode and 
class information into a "Type" column that seems more parsimonious,
and adds an argument for specifying a sort column.

-- Dave Hinds


ls.obj <- function (pos = 1, pattern, order.by)
{
    napply <- function(names, fn)
        sapply(names, function(x) fn(get(x, pos = pos)))
    names <- ls(pos = pos, pattern = pattern)
    obj.class <- napply(names, function(x) as.character(class(x))[1])
    obj.mode <- napply(names, mode)
    obj.type <- ifelse(is.na(obj.class),obj.mode,obj.class)
    obj.size <- napply(names, object.size)

    obj.dim <- t(napply(names, function(x) as.numeric(dim(x))[1:2]))
    vec <- is.na(obj.dim)[,1] & (obj.type != 'function')
    obj.dim[vec,1] <- napply(names, length)[vec]

    out <- data.frame(obj.type,obj.size,obj.dim)
    names(out) <- c("Type","Size","Rows","Columns")
    if (!missing(order.by)) out <- out[order(out[[order.by]]),]
    out
}

> ls.obj(o=2)
                   Type    Size Rows Columns
db              numeric      36    1      NA
A             character     100    6      NA
U             character     100    6      NA
fetch          function    2924   NA      NA
ls.obj         function    9848   NA      NA
get.snp.data   function   15692   NA      NA
hap.fit        function   16124   NA      NA
get.hap.info   function   22476   NA      NA
bb           data.frame   32676  534       7
snp.data     data.frame  621936 3873      20
hap.data           list 1426080  571      NA
s                  list 1901952  566      NA




More information about the R-help mailing list