s-news
[Top] [All Lists]

Re: the use of eval and parse

To: "'Colin Maurice'" <colinsplus@hotmail.com>, <s-news@lists.biostat.wustl.edu>
Subject: Re: the use of eval and parse
From: <Rich@Mango-Solutions.com>
Date: Mon, 27 Feb 2006 16:23:49 -0000
In-reply-to: <BAY17-F18C049D3ADA73DC89D846DC1F60@phx.gbl>
Thread-index: AcY7uLQ0mbkwHCLhRn6t5KTdF0jpUAAAPXvQ
In my experience, there are many workarounds to problems solved with
eval/parse.  These can involve the use of applys, loops or calls to
"do.call".  One quick example where I've used eval/parse is as follows:-

> myDf <- data.frame(X=rnorm(100), Y=rnorm(100), Z=runif(100))

> # Can't get a bubble plot directly because cex won't use vector of values
> plot(myDf$X, myDf$Y, type="n")
> points(myDf$X, myDf$Y, cex=myDf$Z)
Too many values for parameter cex; some ignored

> # Eval and Parse is one possible solution
> plot(myDf$X, myDf$Y, type="n")
> eval(parse(text=paste("points(", myDf$X, ",", myDf$Y, ", cex=", myDf$Z,
")")))

Sometimes, I think "because you can" is also an answer :o)

Cheers,
Rich.

mangosolutions
S & R Training & Consulting

-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Colin Maurice
Sent: 27 February 2006 16:12
To: s-news@lists.biostat.wustl.edu
Subject: [S] the use of eval and parse


Dear members,

I was wondering why so many people use the "eval" and "parse" commands 
together along with the "paste" command, even when there doesn't appear to 
be any obvious reason why. For instance, I found the following code in an 
Splus Library:

table.to.data.frame <- function(TheTable, DataName, TheList)
{
#
#  convert a table to a data.frame containing the same information
#
#  TheTable contains the table
#  DataName is a string to head the data column
#  TheList is a list of strings being the headings for the dimensions
#     of the table
#
   TheData <- as.vector(TheTable)
   N <- length(TheData)
   eval(parse(text = paste("TheDataFrame <- data.frame(", DataName,
      " = TheData, row.names = 1:N)", sep = "")))
   DN <- dimnames(TheTable)
   K <- length(DN)
   if(K != length(TheList))
   {
      print("List is wrong length")
      return(NA)
   }
   r1 <- 1
   r2 <- N
   for(k in 1:K)
   {
      len <- length(DN[[k]])
      r2 <- r2/len
      eval(parse(text = paste("TheDataFrame$", TheList[k],
         " <- rep(rep(DN[[k]], rep(r1, len)), r2)", sep = "")))
      r1 <- r1 * len
   }
   TheDataFrame
}

But I don't know why

eval(parse(text = paste("TheDataFrame <- data.frame(", DataName,
      " = TheData, row.names = 1:N)", sep = "")))

couldn't have been replaced with

TheDataFrame <- data.frame(DataName= TheData, row.names = 1:N)

Can someone explain why it is sometimes better to use "eval" and "parse" 
rather than the standard why of assigning objects.

Any help is much appreciated.

Colin

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news


<Prev in Thread] Current Thread [Next in Thread>