[R] R: to view the memory
Matthew Keller
mckellercran at gmail.com
Wed Sep 12 19:11:10 CEST 2007
Hi Sumit,
Here are a couple of functions I've picked up along the way and
modified. The first lists all objects, their class, and their
dimensions (I grabbed this from the web and modified - sorry for not
acknowledging the person who first wrote it). The second is much the
same but gives their size. Hope this helps:
##LS
LS<-function (pattern = "")
{
if (length(ls(1,pat=pattern)) <1){stop("No objects are in memory")}
obs <- ls(1, pat = pattern)
cat(
formatC("DIM/LEN"),
formatC("NAME", width=max(nchar(obs)+5)),
formatC("CLASS",width=16),
"\n")
if (length(ls(1,pat="tmp")) >0){
for (i in 2:length(obs)) {
widim <- 0
ww <- eval(parse(t = paste("length(dimx(", obs[i], "))")))
for (k in 1:ww){
widim <- eval(parse(t =
paste("length(dimx(",obs[i],"))+widim-2+nchar(dimx(", obs[i],
")[k])")))}
cat(
eval(parse(t = paste("dimx(", obs[i], ")"))),
formatC(obs[i], width=max(nchar(obs))-widim+10),
formatC(eval(parse(t = paste("class(", obs[i], ")"))),1, 16),
"\n") }}
else{
for (i in 1:length(obs)) {
widim <- 0
ww <- eval(parse(t = paste("length(dimx(", obs[i], "))")))
for (k in 1:ww){
widim <- eval(parse(t =
paste("length(dimx(",obs[i],"))+widim-2+nchar(dimx(", obs[i],
")[k])")))}
cat(
eval(parse(t = paste("dimx(", obs[i], ")"))),
formatC(obs[i], width=max(nchar(obs))-widim+10),
formatC(eval(parse(t = paste("class(", obs[i], ")"))),1, 16),
"\n") }}}
##LSIZE
lsize <- function (pattern = "",sort=TRUE)
{
if (length(ls(1,pat=pattern)) <1){stop("No objects are in memory")}
obs <- ls(1, pat = pattern)
sizes <- vector(length=length(obs))
for (i in 1:length(obs)) { sizes[i] <-
eval(parse(t=paste("object.size(", obs[i], ")")))}
sizes <- round(sizes/1048600,2)
if (sort){obs <- obs[order(sizes)]}
cat(formatC("SIZE"),
formatC("NAME", width=max(nchar(obs)+5)),
formatC("CLASS",width=16),
"\n")
for (i in 1:length(obs)) {
widim <- 0
ww <- eval(parse(t = paste("length(object.size(", obs[i], "))")))
for (k in 1:ww){
widim <- eval(parse(t =
paste("length(object.size(",obs[i],"))+widim-2+nchar(00.00)")))}
cat(eval(parse(t = paste("round(object.size(", obs[i], ")/1048600,2)"))),
formatC(obs[i], width=max(nchar(obs))-widim+10),
formatC(eval(parse(t = paste("class(", obs[i], ")"))),1, 16),
"\n") }
cat(sum(sizes),formatC("***TOTAL***",
width=nchar(sum(sizes))-widim+12),"\n")}
On 9/12/07, Doran, Harold <HDoran at air.org> wrote:
> Is this what you want when you say "values". It seems this could be very
> expensive if some of your objects are large matrices, for example. I
> thought the poster meant "size" when he said values since he later
> mentioned memory. If that is the case, you want object.size().
>
>
>
> > -----Original Message-----
> > From: r-help-bounces at r-project.org
> > [mailto:r-help-bounces at r-project.org] On Behalf Of R Help
> > Sent: Wednesday, September 12, 2007 8:28 AM
> > To: Sumit.Gupta at ubs.com
> > Cc: r-help at r-project.org
> > Subject: Re: [R] R: to view the memory
> >
> > the function ls() will list all the variables currently in the memory.
> > To get their values, you'll also need to use the parse and
> > eval functions. Try the following:
> >
> > x = ls()
> > for(i in 1:length(x)){
> > print(x[i])
> > print(eval(parse(text=x[i])))
> > }
> >
> > It's a little crude, but it will do the job.
> >
> > Sam
> > On 9/11/07, Sumit.Gupta at ubs.com <Sumit.Gupta at ubs.com> wrote:
> > > Hello,
> > >
> > > I am wondering if it is possible to view what variables and
> > vairable
> > > values are stored in the R memory. This to enable debugging of
> > > R-scripts I write.
> > >
> > > Sumit
> > >
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > 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.
> > >
> >
> > ______________________________________________
> > 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.
> >
>
> ______________________________________________
> 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.
>
--
Matthew C Keller
Postdoctoral Fellow
Virginia Institute for Psychiatric and Behavioral Genetics
More information about the R-help
mailing list