0

Starting from this nice response, in turn based on code from this question, I wrote the following function to get the size of objects in memory:

# improved list 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) class(x)[1])
    obj.mode <- napply(names, mode)
    obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
    obj.prettysize <- napply(names, function(x) {
                           format(utils::object.size(x), units = "auto") })
    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.prettysize, obj.dim)
    names(out) <- c("Type", "Size", "PrettySize", "Length/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)
}

However, when I try to call lsos with arguments, in order to display all objects in memory and not just the first n, I get the following error:

> lsos(head = FALSE)
Error in .ls.objects(..., order.by = "Size", decreasing = TRUE, head = TRUE,  : 
  formal argument "head" matched by multiple actual arguments

What's happening?

DeltaIV
  • 4,773
  • 12
  • 39
  • 86

1 Answers1

1

The ... in your lsos function call are basically calling

.ls.objects(head = FALSE, order.by="Size", decreasing=TRUE, head=TRUE, n=n)

Note you are trying to set head= twice (once in the ... and one hard-coded in the function call). The elipsis (...) doesn't try to work out what named parameters are already present or decide which value "wins". If you want to be able to override those values, then you'll need to make them explicit parameters.

lsos <- function(..., order.by="Size", decreasing=TRUE, head=TRUE, n=10) {
    .ls.objects(..., order.by=order.by, decreasing=decreasing, head=head, n=n)
}
MrFlick
  • 195,160
  • 17
  • 277
  • 295