About half a year ago, I wrote a post on the configuration of (pgf)Sweave and LyX, which was intended to save us some efforts in going through all the details during the configuration. Now many things have changed: LyX 2.0 has internal support for Sweave, and fortunately I have been in touch with the developers on this feature (thanks to Gregor); meanwhile, there have also been many changes in the pgfSweave package. In all, we have a number of new features which we should definitely make use of.
New Features
A list of new features as far as I can remember:
- support for Sweave in LyX 2.0 is internal, so there is no need to modify the preferences file manually (the converters have been defined internally)
- most importantly, Sweave becomes an independent module now in LyX, which means you can use it with arbitrary layouts!
- we can see the messages during compilation in LyX 2.0 (View–>View Messages), which is really really helpful and I would strongly recommend you to turn on this option when compiling Sweave documents, because you will know which code chunk goes wrong in case of any errors (in the past, you only got an annoying error dialog box which told you almost nothing about the error)
- pgfSweave is faster: it uses the GNU make utility to compile graphics, and you can use multi cores if you like; the compilation becomes 3 steps (pdflatex, make, then pdflatex); other nice features include: the R code is put in an environment Hinput now so you can customize it in LaTeX preamble; there will be no longer a huge gap between the R code and the output (fixed by Liang Qi)…
- tikzDevice has better support for multi-byte characters (using UTF8)
I have been working on improving the Sweave module and adding a new pgfSweave module to LyX, and now I have basically finished what I planned to do. See the ticket #7555 for details. To sum up,
- LaTeX will not complain about not being able to find Sweave.sty; I used several tricks to guarantee this — even in the worst case, LaTeX can still use the hard-coded Sweave style;
- Spaces and dots in path names or filenames will no longer be a problem;
- the pgfSweave module is also working now;
- you can export the reformatted R code in a LyX document with the pgfSweave module;
I have also tried to document all the cool bells and whistles in two examples, sweave.lyx and pgfsweave.lyx. Or you can directly read the PDF documents, sweave.pdf.tar.xz and pgfsweave.pdf.
Try the (pgf)Sweave Module
It seems several people are interested in testing the two modules, and it is actually very easy under Linux. So far I have had no luck with Windows to build LyX from source (I tried once; it took me days to compile and ended up with errors).
- check out the source code of LyX:
svn co svn://svn.lyx.org/lyx/lyx-devel/trunk lyx-devel - (
cd lyx-devel) apply my patch sweave-patch.diff to the svn source you checked out just now:patch -p0 -i /path/to/sweave-patch.diff - build LyX
./autogen.sh ./configure make sudo make install
Done.
Currently the patch is still waiting on LyX Trac. If you run into any problems before the developers begin to look at the patch, please let me know and we will try to make the two modules more stable and useful. But first of all, please remember to keep all your software packages up-to-date: R 2.13.0, pgfSweave 1.2.1 (run update.packages() in R as frequently as you can) and LaTeX package pgf 2.10 (very important for externalization).
It is well-known that R has several graphics devices — either the screen devices (X11(), windows(), …) or the off-screen devices (pdf(), png(), …). We can query the default graphics device in options():
getOption('device')
In a non-interactive session, the default device is pdf(). This is why Sweave has to create a file named Rplots.pdf no matter if you want it or not when you run Sweave on an Rnw file which has code chunks creating plots. Such a behaviour is annoying to me — the PDF file is not only unnecessary, but also time-consuming (creating this PDF file is completely a waste of time). Is there a way to set a “null” device? (like the /dev/null for *nix users) The answer is yes, but not so obvious. I have not found the device below documented anywhere:
options(device = function(...) {
.Call("R_GD_nullDevice", PACKAGE = "grDevices")
})
This device can speed up Sweave a lot when there are many plots to draw. Here is a comparison:
x = rnorm(1000)
system.time({
.Call("R_GD_nullDevice", PACKAGE = "grDevices")
replicate(500, plot(x, pch = 1:21))
dev.off()
})
# user system elapsed
# 1.51 0.02 1.53
system.time({
pdf(file.path(tempdir(), "Rplots.pdf"))
replicate(500, plot(x, pch = 1:21))
dev.off()
})
# user system elapsed
# 47.81 0.20 48.10
One thing I don’t understand in Sweave is that it evaluates the code chunk twice if its Sweave options contain fig=TRUE. I think this might be a waste of time as well, and this is why I like pgfSweave, which has both the mechanism of caching R objects (using cacheSweave) and a smart way to cache graphics (using pgf).
WARNING: this null device may not work with plots that contain (math) expressions! (take a look at demo(plotmath) in case you do not know what are expressions in R graphics)
0. Summary
Take a look at the video in this entry if you don’t understand the title. To put it short,
- install LyX and R as well as a working LaTeX toolkit such as MikTeX or TeXLive or MacTeX;
- run
source('http://gitorious.org/yihui/lyx-sweave/blobs/raw/master/lyx-sweave-config.R')in R under Windows or Ubuntu or Mac; I tried my best to automatically configure LaTeX, R and LyX; - restart LyX as instructed, and you can enjoy
pgfSweavein LyX now — either play with my demo (demo 1; demo 2 with bibliography; a beamer demo; an animation demo with PDF output), or DIY: create a new document, change the document class to article (Sweave noweb) from Document –> Settings, switch the environment to Scrap from the top-left drop list, start your Sweave code chunks like
<<test>>=
rnorm(10)
@
and click the PDF button to compile this document. Done. Take a look at this video if you feel confused.
This works for MikTeX under Windows (Server 2003 / Win7), and TeXLive 2009 under Ubuntu 10.10, MacTeX 2010 under Mac OS; R 2.12.0 or 2.11.1; LyX 1.6.x.
Motivated by the excellent R package pgfSweave, I begin to notice the font families in my graphs when writing Sweave documents. The default font family for PDF graphs is Helvetica, which is, in most cases (I think), inconsistent with the LaTeX font styles. Some common font families are listed in ?postscript, and we can take a look at them by:
for (f in c("AvantGarde", "Bookman", "Courier", "Helvetica",
"Helvetica-Narrow", "NewCenturySchoolbook", "Palatino", "Times")) {
pdf.options(family = f)
pdf(paste(f, ".pdf", sep = ""))
set.seed(123)
plot(rnorm(25), pch = 1:25, xlab = "xlab family", ylab = "ylab font",
main = paste("Font Families in R (PDF):", f))
text(13, 0, "Text in the Middle")
mtext(sprintf("pdf.options(family = \"%s\")", f), side = 4)
dev.off()
}
Here is a merged PDF containing the above single PDF files:
R-PDF-font-families.pdf (29K)It seems that "Bookman", "NewCenturySchoolbook", "Palatino" and "Times" can be better choices when using Sweave because they are serif fonts, which are usually more consistent with LaTeX PDF.
For a long time I’ve been wondering why we are not able to use Enter in the LyX Scrap environment which was set up by Gregor Gorjanc for Sweave. Two weeks ago, I (finally!) could not help asking Gregor about this issue, as I’m using “LyX + Sweave” more and more in my daily work. He explained it here: LyX-Sweave: mandatory use of control+enter in code chunks
After digging into the LyX customization manual for a while, I found a solution which allows us to press the Enter key just as we normally do when typing in a LyX document. The key is to use Environment instead of paragraph as LatexType for the style definition of Scrap. Besides, I used the LatexName as wrapsweave, as a LatexName is required by LyX. The definition for wrapsweave is simple: just two empty lines by \par. (If you define it as \newenvironment{wrapsweave}{}{}, you will run into troubles sometimes; especially when you use indent for paragraphs.)
As we know, LaTeX environment cannot be centered in LyX (only paragraphs can), so I defined a special environment ScrapCenter when I want to insert graphics via Sweave and make them center-aligned.
Recent Comments