Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Last active December 17, 2015 14:29
Show Gist options
  • Save benmarwick/5625246 to your computer and use it in GitHub Desktop.
Save benmarwick/5625246 to your computer and use it in GitHub Desktop.
List data object in memory with size (in kB) and mode. From http://r.789695.n4.nabble.com/Size-of-an-object-in-workspace-tp823649p823653.html
# List data object in memory with size (in kB) and mode. From http://r.789695.n4.nabble.com/Size-of-an-object-in-workspace-tp823649p823653.html
ls.kb <- function(pos=1, sorted=F){
.result <- sapply(ls(pos=pos, all.names=TRUE),
function(..x)object.size(eval(as.symbol(..x))))
if (sorted){
.result <- rev(sort(.result))
}
.ls <-
as.data.frame(rbind(as.matrix(.result),"**Total"=sum(.result)))
names(.ls) <- "Size (kB)"
.ls$Size <- formatC(.ls$Size, big.mark=',', digits=0, format='f')
.ls$Mode <- c(unlist(lapply(rownames(.ls)[-nrow(.ls)],
function(x)mode(eval(as.symbol(x))))), '-------')
.ls
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment