Skip to content
Prev 173258 / 398506 Next

R equivalent to MATLAB's "whos" Command?

Something similar in case anyone is interested, my own 'lls' command:


################################################################################
# This function performs a similar operation to the Unix command 'ls -l' 
# It works like ls() in R except that it will also display object class,
# dimension/length, and object size.  
# It can also take a class filter to view only objects of a certain type.
#
# Usage:
#   lls() # list all objects
#   lls(environmentName) # list all the objects in the specified environment
#   lls(pattern="foo") # list all objects whose name matches pattern specified
#   lls(classFilter="data.frame") # find all data frame objects
#
lls <- function(name, pos=-1, envir=as.environment(pos), all.names=FALSE, 
  pattern, classFilter)
{ 
  if( !missing(name) )
  {
    envir = name
  } else
  {
    envir = parent.frame()
  }
  
  lsList = ls(name, pos, envir, all.names, pattern)   

  cat("\nName: Class, Length, Size\n")
  cat("-------------------------\n")
  for( item in lsList )
  {
    realItem = eval(parse(text = item), envir)
    itemClass = class(realItem)
    itemSize = object.size(realItem)
    itemDimension = paste(dim(realItem), collapse="x" )
    if( itemDimension == "" )
    {
      itemDimension = length(realItem)
    }
    
    classFilterMatches = !missing(classFilter) && itemClass == classFilter
    if( classFilterMatches || missing(classFilter) )
    {
      format(cat(item, ": ", itemClass, ", ", itemDimension, ", ", itemSize, "\n", 
        sep=""), justify="centre")
    } 
  } 
  cat("\n") 
  
  invisible(lsList)
}


################################################################################
# Overload the internal object.size function to return the size in B/KB/MB
#
object.size <-function(x)
{
    size = .Internal(object.size(x)) 
    
    if (size<1024)
    {
        size=paste(size, "B")
    }
    else if (size>=1024 & size < 1024*1024)
    {
        size=paste( round(size/1024, 1), "KB")
    }
    else
    {
        size=paste( round( size/1024/1024, 1 ),"MB")
    }
    
    return(size)
}


Creates output like:

Name: Class, Length, Size
-------------------------
calc: function, 1, 4.7 KB
data: data.frame, 175656x76, 100.9 MB
dollar: function, 1, 7.1 KB
file: character, 1, 120 B
fileList: character, 17, 1.9 KB
lls: function, 1, 17.6 KB
object.size: function, 1, 7.8 KB
changes: data.frame, 175656x70, 94.5 MB
columns: integer, 70, 304 B
value: numeric, 1, 32 B

Feel free to clean up & use as appropriate.

HTH,
John

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of andrew
Sent: Tuesday, March 10, 2009 5:46 PM
To: r-help at r-project.org
Subject: Re: [R] R equivalent to MATLAB's "whos" Command?

I saw this in
http://groups.google.com.au/group/r-help-archive/browse_thread/thread/7dca300a7fde5286/718dc5f1405618c9?lnk=gst&q=sort(+sapply(ls()%2Cfunction(x){object.size(get(x))}))#718dc5f1405618c9

The command is something like

sort( sapply(ls(),function(x){object.size(get(x))}))

very useful, so I have added it to my .First function
On Mar 11, 8:29?am, Jason Rupert <jasonkrup... at yahoo.com> wrote:
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.