[R] R equivalent to MATLAB's "whos" Command?

Scillieri, John John.Scillieri at constellation.com
Tue Mar 10 23:03:43 CET 2009


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:
> Wow.  Thank you for your quick response.  However, I'm looking for an R command that lists what is currently in the R workspace.
>
> Here is a link to a description of the MATLAB "whos" command:http://www.math.carleton.ca/old/help/matlab/MathWorks_R13Doc/techdoc/...
> Essentially, it does the following:
> "who lists the variables currently in the workspace.
>
> whos lists the current variables and their sizes and types. It also reports the totals for sizes.
>
> who('global') and whos('global') list the variables in the global workspace
> "
>
> --- On Tue, 3/10/09, Jorge Ivan Velez <jorgeivanve... at gmail.com> wrote:
>
>
>
> > From: Jorge Ivan Velez <jorgeivanve... at gmail.com>
> > Subject: Re: [R] R equivalent to MATLAB's "whos" Command?
> > To: jasonkrup... at yahoo.com
> > Cc: R-h... at r-project.org
> > Date: Tuesday, March 10, 2009, 4:14 PM
> > Dear Jason,
> > How about this?
>
> > ?ls()
> > ls()
>
> > HTH,
>
> > Jorge
>
> > On Tue, Mar 10, 2009 at 5:05 PM, Jason Rupert
> > <jasonkrup... at yahoo.com>wrote:
>
> By any chance is there an R equivalent to MATLAB' "whos" command? I tried searching R and R-seek, but didn't really come up with anything. There are several items I would like to make sure are stored in the workspace and check their values.  Thank you again for your help and any feedback.
>
>
>
> > > > ?workspace
> > > No documentation for 'workspace' in specified
> > packages and libraries:
> > > you could try '??workspace'
> > > > ??workspace
> > > No help files found with alias or concept or title
> > matching using fuzzy matching.
> > > > ??whos
> > > No help files found with alias or concept or title
> > matching using regular expression matching.
> > > > ??whos
> > > No help files found with alias or concept or title
> > matching using regular expression matching.
> > > > ?whos
> > > No documentation for 'whos' in specified
> > packages and libraries:
> > > you could try '??whos'
>
> > >        [[alternative HTML version deleted]]
>
> > > ______________________________________________
> > > R-h... 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.
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
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.
>>> This e-mail and any attachments are confidential, may contain legal, professional or other privileged information, and are intended solely for the addressee.  If you are not the intended recipient, do not use the information in this e-mail in any way, delete this e-mail and notify the sender. CEG-IP1




More information about the R-help mailing list