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

Phil Saunders phil at lacertacapital.com
Sun Jan 19 13:03:02 CET 2003


I like your ls.obj function - how might I turn it into a standard function for me whenever I launch R?

-----Original Message-----
From: David Hinds [mailto:David_Hinds at perlegen.com] 
Sent: 17 January 2003 20:58
To: r-help at stat.math.ethz.ch
Subject: [R] Re: check variables: a Q from a beginner


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

______________________________________________
R-help at stat.math.ethz.ch mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help

________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com




More information about the R-help mailing list