Hello, I'm having some issues getting a ggplot figure to show up in the knitr output, when placed in a loop. Specifically, I have a loop inside a knitr chunk : ```{r fitting, warning=FALSE, fig.width=10, fig.height=10, fig.keep='high'} for (t in 1:T) { # do a regression of tgt.vals ~ predictors and compute coeffs and fitted values (fit.vals / fit.adj.vals) plot( x=tgt.vals, y=fit.vals ); plot( x=tgt.vals, y = fit.adj.vals ); qplot( x = pred.names, y = coeffs); } ``` The html output file shows the output of first 2 plot commands but not that of the qplot command. Even if i remove the 2 plots and keep only the qplot in the loop, it does not display in the html output. The qplot just by itself, not in a loop, displays fine. Also, there are no issues when I run in regular R. Any suggestions of what is going on ? [[alternative HTML version deleted]]
You need to explicitly print() the graphs to make them show -- or in recent ggplot2 versions I think you can also use plot() as an alias. Michael On Aug 2, 2012, at 2:37 PM, "firdaus.janoos" <fjanoos at bwh.harvard.edu> wrote:> Hello, > > I'm having some issues getting a ggplot figure to show up in the knitr > output, when placed in a loop. > > Specifically, I have a loop inside a knitr chunk : > > ```{r fitting, warning=FALSE, fig.width=10, fig.height=10, fig.keep='high'} > for (t in 1:T) > { > # do a regression of tgt.vals ~ predictors and compute coeffs and > fitted values (fit.vals / fit.adj.vals) > > plot( x=tgt.vals, y=fit.vals ); > plot( x=tgt.vals, y = fit.adj.vals ); > > qplot( x = pred.names, y = coeffs); > > } > > ``` > > The html output file shows the output of first 2 plot commands but not that > of the qplot command. Even if i remove the 2 plots and keep only the qplot > in the loop, it does not display in the html output. > The qplot just by itself, not in a loop, displays fine. Also, there are no > issues when I run in regular R. > > Any suggestions of what is going on ? > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hi Michael, Thanks a lot - enclosing the qplot in a print() worked perfectly ! Regards, -fj On Thu, Aug 2, 2012 at 7:37 PM, R. Michael Weylandt < michael.weylandt@gmail.com> wrote:> On Thu, Aug 2, 2012 at 6:30 PM, Dennis Murphy <djmuser@gmail.com> wrote: > > On Thu, Aug 2, 2012 at 1:38 PM, Michael Weylandt > > <michael.weylandt@gmail.com> wrote: > >> You need to explicitly print() the graphs to make them show -- or in > recent ggplot2 versions I think you can also use plot() as an alias. > > > > @ Michael: > > You don't need the print() wrapper for grid-based graphics in knitr, > > although you do need it in Sweave. See the bottom of p. 1 in the knitr > > manual for an example: > > http://cloud.github.com/downloads/yihui/knitr/knitr-manual.pdf > > But see my follow-up with firdaus and section 3.2 of the same manual. > > Cheers, > Michael >[[alternative HTML version deleted]]