avoid displayed figures during sweave/pgfsweave compilation

大憨熊 提交于 2019-12-06 05:50:23

问题


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


回答1:


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




回答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




回答3:


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.




回答4:


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}


来源:https://stackoverflow.com/questions/8276069/avoid-displayed-figures-during-sweave-pgfsweave-compilation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!