[R] using eval-parse-paste in a loop

Bert Gunter gunter.berton at gene.com
Tue Feb 26 18:51:08 CET 2008


As Thomas Lumley has frequently noted, eval(parse...)) should generally be
avoided.

You probably want do.call(), as in

## warning: UNTESTED

lapply(numbs,
function(n)do.call(read.xls,list(file=paste("Book",n,".xls",sep=""),sheet=1,
...)))   ## ... contains the other explicitly names arguments


Note that this would automatically create your list for you.

Cheers,

Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Erik Iverson
Sent: Tuesday, February 26, 2008 9:29 AM
To: Michael Anyadike-Danes
Cc: r-help at r-project.org
Subject: Re: [R] using eval-parse-paste in a loop


> ############### stripping off eval, looking for clues
> 
>  
> 
> parse(paste('read.xls("Book',nam,'.xls",sheet=1,from=4,colClasses="numer
> ic")',sep=''))
> 
> Error in file(file, "r") : unable to open connection
> 
> In addition: Warning message:
> 
> In file(file, "r") :
> 
>   cannot open file
> 'read.xls("Book1.xls",sheet=1,from=4,colClasses="numeric")', reason
> 'Invalid argument'
> 

Your clue is that it looks like parseis unsuccessfully trying to open a 
file.  Look at ?parse and note the first argument, which you are using 
because of positional matching.

You want eval(parse(text = paste(...)))

Best,
Erik Iverson

______________________________________________
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.



More information about the R-help mailing list