R/Startup.R

Defines functions lsos source.pro source.lib ln hh ht last

# ------------------------------------------------------------------- #
# accessory functions
last <- function(x) { tail(x, n = 1) }

ht <- function(d) rbind(head(d,10),tail(d,10))

hh <- function(d) d[1:5,1:5]

ln <- function(x)length(unique(x))

source.lib = function(x){source(file.path(lib.path, x))}
source.pro = function(x){source(file.path(pro.path, x))}


# ------------------------------------------------------------------- #
# calculates the memory usage of objects
.ls.objects <- function (pos = 1, pattern, order.by,
                        decreasing=FALSE, head=FALSE, n=5) {
    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]], decreasing=decreasing), ]
    if (head)
        out <- head(out, n)
    out
}
# shorthand
lsos <- function(..., n=10) {
    .ls.objects(..., order.by="Size", decreasing=TRUE, head=TRUE, n=n)
}
frenkiboy/MyLib documentation built on Oct. 24, 2020, 11:01 a.m.