4

When compiling with sweave/pgfsweave, every time a figure is created in R it is shown in a graphics windows (during the sweave compilation process). This is helpful in many cases as I can see what the figures look like as the document is being compiled.

But when I compile through ssh a large document this can be very slow. Is there a way to tell sweave/pgfsweave to avoid displaying the figure during the compilation (I still want the figure in the final pdf document though).

4 Answers4

4

For interactive sessions, the figs.only Sweave option controls this behavior. To plot figures only to the target graphics files (and not to a console graphical window) set figs.only=TRUE.

As explained in the RweaveLatex help file:

figs.only: logical (‘FALSE’). By default each figure chunk is run once, then re-run for each selected type of graphics. That will open a default graphics device for the first figure chunk and use that device for the first evaluation of all subsequent chunks. If this option is true, the figure chunk is run only for each selected type of graphics, for which a new graphics device is opened and then closed.

As with other Sweave options, you can set this option: (1) for the current compilation (e.g. Sweave("example.Rnw", figs.only=TRUE); (2) within the .Rnw file, using \SweaveOpts{figs.only=TRUE}; or (3) as a global default, by putting SWEAVE_OPTIONS="figs.only=TRUE" in, e.g., $R_HOME/etc/Renviron.site

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 2
    I recommend (2) because it is self-contained (so always reproducible); (1) is bad and (3) is worse. – Yihui Xie Nov 28 '11 at 16:25
  • @Yihui -- Thanks for adding that. Also (though it's slightly off-topic) many thanks for your work on the formatR package. – Josh O'Brien Nov 28 '11 at 16:59
2

figs.only is the correct way to go, and I also want to mention the default graphical device in R here:

For now you may look at this: http://yihui.name/en/2010/12/a-special-graphics-device-in-r-the-null-device/

After R 2.14.1 (not released yet) you will be able to set the default device to a null PDF device, which is both safe and fast: https://github.com/yihui/knitr/issues/9

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
1

If you sweave from the command line instead of in an interactive session, graphics aren't produced in an interactive graphic window.

You can run R from the command line by just typing R CMD Sweave mydoc.Rnw or via a batch file, or a makefile for larger projects. I've started to use makefiles for many of my sweave documents as it handles dependencies, can clear up after itself and much more.

PaulHurleyuk
  • 8,009
  • 15
  • 54
  • 78
0

One option could be

<<label=myplotlabel, fig=TRUE, include=FALSE>>=
graph code
@

then

\begin{figure}[h]
   \includegraphics[width=6cm, height=6cm]{myplotlabel}
   \caption{My Plot}
   \label{fig:label}
\end{figure}
MYaseen208
  • 22,666
  • 37
  • 165
  • 309