[R] vignettes: .png graphics or pre-compiled .pdf

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Wed Nov 18 16:15:03 CET 2009


On Wed, 18 Nov 2009, Michael Friendly wrote:

> Achim Zeileis wrote:
>> On Fri, 13 Nov 2009, Michael Friendly wrote:
>> 
>>> Alternatively, is there a way to generate .png graphs from the .Rnw file 
>>> so that those are used in building
>>> the .pdf for the package?  AFAICS, \SweaveOpts{} offers only the choices 
>>> of eps/pdf = {TRUE/FALSE}.
>> 
>> Yes, but you can call the png() device yourself within the code chunk
>> 
>> <<>>=
>> png(file = "fig1.png", ...)
>> ...
>> dev.off()
>> @
>> 
>> and then put
>> 
>> \includegraphics{fig1}
>> 
>> manually into the LaTeX. It's certainly not as nice as Sweave's automatic 
>> handling of figures but will work and still keep the file self-contained.
>> Z
>> 
> Thanks, Achim
>
> This works, but has the infelicitous effect that both the png() call and 
> dev.off(), with its output appear in the vignette (with echo=TRUE)
>
> % do .png manually ...
> <<fig1cpng, fig=TRUE, pdf=FALSE, eval=TRUE, echo=TRUE, debug=TRUE, 
> results=verbatim, include=FALSE>>=
> png(file="figs/fig-fig1c.png", width=4, height=4, units="in", res=72)
> # plot stuff ...
> dev.off()
> @
>
> I suppose I could break this up into 3 chunks as follows, but is there some 
> other way, either with R or LaTeX constructs?
>
> <<echo=FALSE>>=
> png(file="figs/fig-fig1c.png", width=4, height=4, units="in", res=72)
> @
> <<fig1cpng, fig=TRUE, pdf=FALSE, eval=TRUE, echo=TRUE, debug=TRUE, 
> results=verbatim, include=FALSE>>=
> # plot stuff  ...
> @
> <<echo=FALSE>>=
> dev.off()
> @

I typically re-use the code chunks, e.g.:

<<myplot, echo=TRUE, eval=FALSE>>=
plot(...)
@

<<myplot-png, echo=FALSE, eval=TRUE>>=
png(...)
<<myplot>>
dev.off()
@

which is also convenient if you want to add par() settings (e.g., 
mfrow/mfcol) and have the code somewhere in the text but the resulting 
figure in a floating environment.
Z

>
> -- 
> Michael Friendly     Email: friendly AT yorku DOT ca Professor, Psychology 
> Dept.
> York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
> 4700 Keele Street    http://www.math.yorku.ca/SCS/friendly.html
> Toronto, ONT  M3J 1P3 CANADA
>
>
>
>




More information about the R-help mailing list