[R] Sweave in png

Thibaut Jombart jombart at biomserv.univ-lyon1.fr
Wed Mar 22 16:29:22 CET 2006


Hello list,

Here is a problem I had. I write this to the list in case the solution I 
found would be useful to anyone else.

I had to Sweave document including pixmap pictures, which are very heavy 
when converted into pdf pictures. In practice, this eventually led to 
very heavy pdf, sometimes impossible to read. So the first solution I 
found was to save pictures in png, when too heavy in pdf :

### in a .rnw document ###

% here is an invisible chunck to create a picture
<<fig =FALSE,echo=FALSE>>=
png(filename='figs/myPic.png')
@

% next, R code to generate picture
<<fig=FALSE,echo=TRUE>>=
...
@

% then, close the device. Hidden, again
<<fig =FALSE,echo=FALSE>>=
dev.off()
@

% and then, include it as a picture
\includegraphics{figs/myPic.png}

### end of the example ###

I found that quite heavy, though, when more than one such figure were 
needed in my document.
So I adapted the Sweave driver 'RweaveLatex' in order to allow to 
generate png pictures instead of ps or pdf, when using a pdf-oriented 
compiler (such as pdflatex). I just have to source the new driver 
(RweaveInPng), then call it when Sweaving.

Then for example, I simply use:

### rnw document ###
% a single chunck containing R code to generate the picture
<<fig=TRUE,pdf=FALSE,png=TRUE>>=
...
@

The driver, which is only a slight modification of RweaveLatex, can 
generate ps, pdf or png figures; it was tested on Ubuntu64, Debian, 
several Windows systems and macOS X partforms with no detected problem.

Does someone find this useful, or were there better solutions I missed?

Regards,

Thibaut Jombart .

-- 
######################################
Thibaut JOMBART
CNRS UMR 5558 - Laboratoire de Biométrie et Biologie Evolutive
Universite Lyon 1
43 bd du 11 novembre 1918
69622 Villeurbanne Cedex
Tél. : 04.72.43.29.35
Fax : 04.72.43.13.88
jombart at biomserv.univ-lyon1.fr




More information about the R-help mailing list