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

From: Bert Gunter <gunter.berton_at_gene.com>
Date: Tue, 26 Feb 2008 09:51:08 -0800

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.

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. Received on Tue 26 Feb 2008 - 17:55:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 26 Feb 2008 - 18:30:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive