Skip to content

vignettes: .png graphics or pre-compiled .pdf

8 messages · Achim Zeileis, Yihui Xie, Michael Friendly

#
In a package I'm working on there is a vignette with a number of graphs 
that result in huge .pdf files, so
the .pdf for the vignette is around 17 Mb.  If these graphs are 
converted to .png, and the .tex file
is compiled with pdflatex, the resulting .pdf is ~1 Mb.

I'm reluctant to put the .Rnw file into the package as is, generating 
the huge .pdf for the vignette.
I first tried installing the smaller .pdf file in the package by itself 
(no .Rnw)
together with a file inst/doc/index.html as recommended
in 'Writing R Extensions.'  However, when the package is installed, 
vignette() can't find it

 > vignette(package="Guerry")
no vignettes found
 > vignette("MultiSpat")
Warning message:
vignette 'MultiSpat' *not* found

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}.

-Michael
#
On Fri, 13 Nov 2009, Michael Friendly wrote:

            
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
#
Hi Michael,

I have a dirty solution as attached to use png() for Sweave.

HTH.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA
On Fri, Nov 13, 2009 at 10:02 AM, Michael Friendly <friendly at yorku.ca> wrote:
#
I was reminded that the attachments were blocked by the list, so I
send these links again:

http://yihui.name/en/wp-content/uploads/2009/11/Sweave2.Rnw
http://yihui.name/en/wp-content/uploads/2009/11/Sweave2.r
http://yihui.name/en/wp-content/uploads/2009/11/Sweave2.pdf

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA
On Fri, Nov 13, 2009 at 8:31 PM, Yihui Xie <xieyihui at gmail.com> wrote:
#
Thanks, Yihui
Your solution, for png(), only looks dirty because you had to hack the 
Sweave code.
It would be nice to have png() support included directly.
Yihui Xie wrote:

  
    
#
Yes, I also wish Sweave could give us more flexible options, e.g. it
should not be difficult to free the graphics device specification as
an R function (pdf, png, CairoPDF, ...) instead of just letting us set
pdf=T/F and eps=T/F.

If we don't want to hack the Sweave code, we may also rewrite it as a
package. This has been in my mind for a long time.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA



2009/11/14 Michael Friendly <friendly at yorku.ca>:
3 days later
#
Achim Zeileis wrote:
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()
@
#
On Wed, 18 Nov 2009, Michael Friendly wrote:

            
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