Go to page:

Search:   Help

Navigation

Groups

LyX documentation

Edit

Shared groups

Links

LyX /

LyX with R through Sweave

Categories: Sweave, R, Noweb
<< Using Memoir in LyX | Page list | Fonts >>

How to use LyX together with R

by Paul E. Johnson and Gregor Gorjanc

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 figures and statistical results.

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.

For information on the statistical program R, consult its website.

For more info on Sweave, the R-component that does the processing of the Rnw-file, consult the Sweave webpage.

Step-by-step instructions for Windows XP users are provided in this blog. You may also want to read the paper "Using Sweave with LyX" for the configuration. There is also a companion webpage at CRAN, and a PDF of the iPaper document.

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.

Old notes about the configuration

There are 3 steps to make LyX work with Sweave in Linux. I hope somebody who uses Windows will explain to us the bare-minimum needed for windows users.

  1. 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
  2. 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.
  3. Configure LyX: First, open LyX and use the menu Edit→Reconfigure (or, on OSX at least, use the menu LyX→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:

    1. In the "From" pulldown, choose Noweb
    2. In the "To" pulldown, choose LaTeX
    3. Hit the "new" button toward the bottom.
    4. 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

Links

Sweave R Noweb

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2009-03-02 14:50 CET