<< | Page list | >>
How to use LyX together with R through Sweave
LyX can work together with the statistical program R to produce documents which incorporate graphs and statistical results that are created "on the fly" when the LyX-document is compiled. The basic idea is to think of the LyX-document as a piece of literate programming, and then it gets processed into LaTeX, incorporating code, figures and statistical results.
Documentation files are available:
From LyX 2.0 onwards, a new module Sweave has been introduced. One hence can simply load it with Document -> Settings -> Modules, chosing the Sweave module (see also the Rnw/knitr module). This done, one can then insert Sweave modules in the Lyx file as one does for any environment: it is the Sweave ->Chunk environment.
Here are the example LyX-files Normal-01.lyx and Gamma-02.lyx and the corresponding PDF-files that LyX produces: Normal-01.pdf and Gamma-02.pdf. Note that you can also use Beamer with LyX and Sweave, see Gregor Gorjanc's instructions on how to make a literate-beamer layout file for LyX.
Windows
Lyx version 2>=
Lyx comes now automatically with the Sweave module. On Windows, you can get an error message such as:
Packages not Available: Missing Pre-requisite: Sweave -> latex (or knitr-> latex for the most recent module).
This means that the path to Rscript is not recognised, and the user needs to add it manually.
Go to:
Tools-> Preferences-> Paths-> PATH
and add the path to R, that can be found in R with the command:
R.home('bin')
Do Tools-> Reconfigure so that LyX reads the required files.
More informations (with alternative procedures) are available on: https://github.com/downloads/yihui/lyx/sweave.pdf
Old versions of Lyx ( <2)
Step-by-step instructions for Windows users are provided in a couple of places, but you will probably need multiple sources to get it all working (unless you are lucky or blessed). One source is this blog from Murat Yildizoglu. You should definitely read the paper from R News by Gregor, which includes somewhat dated information about configuration. More details related to this paper are here. Recent attempts by Jeff Laake and others to get this working (March 2010) ran into some hurdles, and this How-to document explains what worked for them.
Note for Tinn-R users (and potentially others): The normal Rprofile.site
configuration that ships with Tinn-R will automatically load some packages that clash with certain things needed when Sweaving through LyX (e.g., the R2HTML
package). The simplest solution is to modify the Rweave.bat
file mentioned here to include the following option after the first path specification (near the --no-save
option):
--no-site-file
Thus, your whole line in that batch file might look like this:
"C:\Program Files\R\R-2.12.0\bin\i386\Rterm.exe" --no-site-file --no-save -f "C:/Program Files/LyX 1.6.8/bin/MakeSweave.R" --args %1
Setting this option ignores the Rprofile.site
when loading R as called by LyX. This is a great option because the load time for R "in the background" is much faster (some of the Tinn-R loaded packages are time-consuming to load, and they aren't needed) and thus the PDF conversion is much faster. This option may very well be preferred by many users, even those that do not use Tinn-R but like to load a lot of packages in their Rprofile.site
. Of course, the packages then need to be loaded in code chunks in LyX.
Mac
Mac users need to copy the literate-*.*
files provided here to their ~/Library/Application Support/LyX-x.xx/layouts/
folder. In addition, copy the lines from the provided preferences
file to the appropriate sections in your own ~/Library/Application Support/LyX-x.xx/preferences
file. Next, start Terminal and type "kpsewhich Sweave.sty"
to check if TeX can find Sweave.sty
. If it returns the path alright, reconfigure LyX and you're done. If it doesn't, locate Sweave.sty
on your system and copy it to ~/Library/texmf/tex/latex/R/
(for your convenience, an R folder is provided here), then run "sudo texhash"
in the Terminal, reconfigure LyX and you're good to go. -- More detailed instructions are available in the PDF mentioned above.
Linux
Lyx version 2>=
Lyx comes now automatically with the Sweave module, so most, if not all of the efforts below (see old versions) are not needed anymore. Unlike Windows users, path to R CMD is found immediately, so no special configuration should be needed.
As for the Rnw (knitr) module, it is necessary to install the knitr package and dependencies in R, with the following command:
$ sudo -s
# R
> install.packages('knitr', dependencies = TRUE)
You will be requested for a CRAN mirror, and loading and compilation of the modules will proceed.
If you get errors due to config files missing, you will have to install the dev packages of the corresponding libraries, e.g. for XML and RCurl:
sudo apt-get install libxml2-dev libcurl4-gnutls-dev
Then reinstall the failed dependencies, e.g.:
> install.packages("XML")
Old versions of Lyx ( <2)
There are 3 steps to make LyX work with Sweave in Linux.
- Put the "batch" script for processing Rnw-files into an executable file and save it in your path. That file is in the Sweave-documentation, but here it is again for reference. I named it Rweave. The version recommended by the Sweave author is this:
#!/bin/sh
echo "library(\"utils\"); Sweave(\"$1\")" | R --no-save --no-restore
- Fool LyX into thinking you have the whole of Noweb installed. Noweb is a package you can download and install, but you don't need it all. All you need is the Noweb.sty file, the LaTeX-style file. Install that in your LaTeX, I put it under /usr/local/share/texmf/tex/latex/noweb because that's a standard place to store user-added LaTeX-files. If you want, I could package up my /usr/local/share/texmf directory and you could untar it into your system.
After dropping the noweb.sty file in, run
texhash
to make sure your LaTeX system knows about the file.
- Configure LyX: First, open LyX and use the menu Edit→Reconfigure. Now you need to configure LyX. LyX needs to know that your file is a Noweb file and that it has to be "post processed". Ordinarly, LyX goes directly from *.lyx format to *.tex format, but now it will go from *.lyx to *.nw format. You have to tell it how to handle the nw-file.
In LyX, you will see the document classes now include "Noweb" variants. Choose article(Noweb). THat tells LyX that you want to process your file through another program. Now you have to tell LyX what that other program is. In LyX's Edit→Preferences (or LyX→Reconfigure) dialog, choose the Converters option. This menu interface is confusing. Here's the way it should go:
- In the "From" pulldown, choose Noweb
- In the "To" pulldown, choose LaTeX
- Hit the "new" button toward the bottom.
- Make sure the Converter Noweb→LaTeX is chosen, and then in the box called "Converter" type the name of the batch script you saved above. If it is e.g. called Rweave, put Rweave in there. The syntax should be: Rweave $$i
If TeX Live doesn't recognize ‘Sweave.sty’, search for the LATEX style sheet called ‘Sweave.sty’. it is located in the R installation directory. Typically in the folder ‘/usr/lib/R/share/texmf/tex/latex’. Copy this folder to the LATEX libary latex folder, tipically ‘/usr/lib/texmf/tex/latex’ (sudo cp -R /usr/lib/R/share/texmf/tex/latex /usr/lib/texmf/tex/latex
) and rename it ‘Sweave’ (sudo mv /usr/lib/texmf/tex/latex/latex /usr/lib/texmf/tex/latex/Sweave
). Then run texhash
.
Links
Sweave
R
Noweb