r - TeachingDemos: using etxtPlot with numerical results in loops -
when running code:
library(teachingdemos) etxtstart(dir=getwd(), file="nofunciona.txt") etxtcomment('just test') for(i in 1:10){ cat("###",i,":\n") my.sample = sample(100) print(summary(my.sample)) qqnorm(my.sample) etxtplot(width=7.5) } etxtstop()
i file named "nofunciona.txt" text line "just test" , commands include graphs, nothing results of cat() or print(summary()), although can see results on console.
if change loop using these 2 loops:
for(i in 1:10){ cat("###",i,":\n") my.sample = sample(100) print(summary(my.sample)) } for(i in 1:10){ qqnorm(my.sample) if(archivo) etxtplot(width=7.5) } etxtstop()
then can obtain file text results of cat(), , summary() , commands include graphs @ end. know last loop obtain ten times same graph, not relevant.
it seems cannot obtain graphical results , text results @ same time inside loop. why not first code work?
any idea?
thanks.
the reason happening because assumed not want etxtplot
command show in transcript or command history, when function called sets variable tells workhorse internal function (that called task manager) skip outputting commands , results temporarily. works correctly outside of loop because suppression of output lasts call etxtplot
, else output. problem comes when in loop, done in loop processed in single step (see ?addtaskcallback
details on how things handled), suppressing of command , output etxtplot
ends suppressing commands , output else in loop.
a possible work around run command:
trace(etxtplot, edit=true)
then change true
false
in second last line of code. see commands , output (including calls etxtplot
), plots come before output (because commands include plots inserted @ each iteration, other output inserted after loop has completed).
you might consider using knitr package alternative, stitch
or spin
functions if don't want create full template file, have code processed. don't realtime transcript, deal better automatic plot insertion.
Comments
Post a Comment