Samstag, 24. Oktober 2009

A primer to Sweave with LyX

This is a step-by-step guide to 'Sweave' a document in R with Lyx on Ubuntu (9.04).

System: Ubuntu 9.04, LyX 1.6.2, R 2.11.0 (Development version, r50178)

  1. Install LyX with Synaptic if it is not installed yet. (Current version 1.6.2)
  2. Start LyX, go to menu Help -> About LyX, in the prompting version window note the user directory, by default it is ~/.lyx (following written as $LYXUSRDIR). Note that by using this directory the setting is only applied to the current user. If you want to make the settings applied system-wide, use the LyX system directory, by default /usr/share/lyx, as your $LYXUSRDIR in the following steps.
  3. Go to LyX on CRAN, download the 'preferences' and all files starting with 'literate', that is, 'literate-*.*'.
  4. Check if a 'preferences' file is existing under $LYXUSRDIR (again, by default ~/.lyx). If yes, append the content of downloaded 'preferences' to the existing one. Otherwise copy the 'preferences' file there.
  5. Copy 'literate-*.*' to $LYXUSRDIR/layouts.
  6. Close and open LyX, go to menu tools-> reconfigure, to reconfigure LyX and make it recognize our changes.
  7. Make sure that R is available on your search path (type 'R' in the terminal to see whether the familiar command line appears). If you installed R manually (for example by svn-checkout) and have a name other than by default (for example R-devel), maybe you want to set 'R' as an alias or by soft linking (sudo ln -s $MYRHOME/bin/R /usr/bin/R)
  8. Make sure that the Sweave.sty file is on the searching hash of your latex. The Sweave.sty file can be found in $RSOURCE/share/texmf/, where the $RSOURCE refers to the path of R source codes (svn checkout). If you are using TexLive distribution of LaTeX, the default path to put style files is /usr/share/texmf-texlive/tex/latex (short as $TEXDIR). To install the style file, you have to create a directory in the $TEXDIR, maybe named 'RSweave', and copy the Sweave.sty there under $TEXDIR/RSweave/. Next you have to run the program mktexlsr as root (sudo mktexlsr), to refresh the hash of TeX. In case of question or another distribution like TeTex, see the reference (3) below.
  9. Download the Sweave-test-1.lyx file from CRAN to test whether it works.
Sweave supporting in LyX is interesting for R users performing reproducible research and documentation, especially for those using the beamer package to prepare LaTex-style representations. Although it is also possible to use other text editors like Emacs to prepare such presentations, the WYSIWYG principle of LyX makes it a more intuitive way to make professional presentations.

  1. LyX extra on CRAN
  2. How to use LyX together with R, by Paul E. Johnson and Gregor Gorjanc
  3. Installing latex style files on ubuntu