Site icon R-bloggers

A primer to Sweave with LyX

[This article was first published on B.I.S. dato, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 < path>~/.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 < path>/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 < path>~/.lyx). If yes, append the content of downloaded ‘preferences’ to the existing one. Otherwise copy the ‘preferences’ file there.
  5. Copy ‘literate-*.*’ to < path>$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 < path>$RSOURCE/share/texmf/, where the < variables>$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 < path>/usr/share/texmf-texlive/tex/latex (short as < variables>$TEXDIR). To install the style file, you have to create a directory in the < variables>$TEXDIR, maybe named ‘RSweave’, and copy the Sweave.sty there under < path>$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.

References:
  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
To leave a comment for the author, please follow the link and comment on their blog: B.I.S. dato.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.