Skip to content
Prev 302182 / 398503 Next

ggplot does not show in knitr

See R FAQ 7.22 for the gory details -- It needs to go around the
qplot() statement.

In interactive use, most objects are "printed" after evaluation:
sometimes this doesn't happen because the returned value is marked
invisible. For grid graphics objects (of which ggplot objects are a
type), this "printing" is the actual display mechanism. This makes
lots of sense in a interactive context because after the object is
assembled, it is auto-printed back to you, and hence displayed.

However, inside a for loop (and a few other cases) the auto-printing
is turned off. For example

2 + 2 # will display 4

for(i in 1:10) 2 + 2 # Will not print 4 ten times

To get printing behavior in a for loop, you need to print explicitly:

for(i in 1:10) print(2+2) # Will print 4 ten times.

AFAICT, knitr turns on the regular auto-printing, but it doesn't get
the printing inside the for loop (perhaps because there's no mechanism
to make it always happen, but that's just idle speculation)

Hope this helps,
Michael
On Thu, Aug 2, 2012 at 3:52 PM, firdaus.janoos <fjanoos at bwh.harvard.edu> wrote: