mercoledì 2 maggio 2007

ls() improved!

This marvelous little function shows all objects in the current workspace
by mode, class and 'size'! Thanks to Bendix Carstensen!

lls <- function (pos = 1, pat = "")

{

dimx <- function(dd) if (is.null(dim(dd)))

length(dd)

else dim(dd)

lll <- ls(pos = pos, pat = pat)

cat(formatC("mode", 1, 15), formatC("class", 1, 18),

formatC("name",1, max(nchar(lll)) + 1), "size\n-----------------------------------------------------------------\n")

if (length(lll) > 0)

{

for (i in 1:length(lll))

{

cat(formatC(eval(parse(t = paste("mode(", lll[i],

")"))), 1, 15), formatC(paste(eval(parse(t = paste("class(",

lll[i], ")"))), collapse = " "), 1, 18), formatC(lll[i],

1, max(nchar(lll)) + 1), " ", eval(parse(t = paste("dimx(", lll[i], ")"))), "\n")

}

}

}

3 commenti:

  1. A very handy little function but I did a slight modification to order output by class (see below). Thanks for the very helpful blog!!

    #ls() improved!
    #from One R Tip A Day by Paolo
    #
    #This marvelous little function shows all objects in the current workspace
    #by mode, class and 'size'! Thanks to Bendix Carstensen!

    lls <- function (pos = 1, pat = "")

    {

    dimx <- function(dd) if (is.null(dim(dd)))

    length(dd)

    else dim(dd)

    lll <- ls(pos = pos, pat = pat)
    #jack's mod
    #######################################
    class.get <- function(x){class(get(x))}
    lll2 <- lapply(lll, class.get)
    a <- order(unlist(lll2))
    lll <- lll[a]
    #######################################

    cat(formatC("mode", 1, 15), formatC("class", 1, 18),

    formatC("name",1, max(nchar(lll)) + 1), "size\n-----------------------------------------------------------------\n")

    if (length(lll) > 0)

    {

    for (i in 1:length(lll))

    {

    cat(formatC(eval(parse(t = paste("mode(", lll[i],

    ")"))), 1, 15), formatC(paste(eval(parse(t = paste("class(",

    lll[i], ")"))), collapse = " "), 1, 18), formatC(lll[i],

    1, max(nchar(lll)) + 1), " ", eval(parse(t = paste("dimx(", lll[i], ")"))), "\n")

    }

    }

    }

    RispondiElimina
  2. You're welcome! Thanks for your useful contribution, I really appreciate it!

    RispondiElimina
  3. This is really cool!!! :)) love it!!

    RispondiElimina