An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081231/9918c208/attachment.pl>
Paste in a FOR loop
5 messages · Michael Pearmain, Carlos J. Gil Bellosta, Dieter Menne +2 more
?eval Carlos J. Gil Bellosta http://www.datanalytics.com
Michael Pearmain <mpearmain <at> google.com> writes:
Hi All, I've been having a little trouble using R2HTML and a loop, but can't figure out where the problem lies, any hints gratefully received.
..
library(R2HTML)
HTMLStart(outdir =
file.path("C://Example_work","R_projects","Dynamic_creative"),filename =
"RMDC_mockup",Title="Mock up for RMDC")
for(group in 1:3){
paste("summary(z.out.", group, sep = "")
paste("summary(s.out.", group, sep = "")
paste("s.out.",group,"$qi$ev", sep = "")
HTMLplot()
}
HTMLStop()
Check documentation on HTMLInsertGraph, and don't forget to change the filename in the loop, otherwise you always see the last file. Dieter
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081231/133eb908/attachment.pl>
On Wed, Dec 31, 2008 at 6:12 AM, Michael Pearmain <mpearmain at google.com> wrote:
summary(z.out.1) summary(s.out.1) hist(s.out.1$qi$ev)... This seemed a rather long winded way of doing things to me and a simple for loop should handle this, as later i want it to be dynamic for a number of groups so my new code is(not working):
...
for(group in 1:3){
paste("summary(z.out.", group, sep = "")
paste("summary(s.out.", group, sep = "")
paste("s.out.",group,"$qi$ev", sep = "")
This just constructs strings. You actually want to call "summary" on
the value of the variable z.out.1 etc., so constructing the string
"z.out.1" or "summary(z.out.1" [sic] doesn't help. To call summary on
the value of the variables, you can use:
summary( get(paste("z.out",group,sep="")) ) ...
But in general, if you're not doing real meta-programming, the
presence of a "get" or even worse an "eval" in your code generally
indicates that you're not organizing your data appropriately. Why not
have z.out be a *list* of values, so you can just write
for (group in 1:3) { summary(z.out[[group]]); ...}
Hope this helps,
-s