Sunday, February 15, 2009

Lyx and Sweave under Windows XP

These instructions propose a solution for making Sweave work under Windows XP, using Lyx 1.6.1 and R-project 2.8.x.

Adapted from the instructions provided by Paul Johnson and Cheng-shan (Frank) Liu:
(see http://n2.nabble.com/Converter-failure-with-Sweave-td479669.html)

These instructions correct some small problems that were impeding the original instructions from working under the recent version of R that I use. I also take into account the fact that the default installation folders of R and Lyx are under Program Files, a path that contains a space, and can cause problems.

We will suppose that Lyx 1.6.1 is installed in C:\Program Files\LyX16 and R in C:\Program Files\R\R-2.8.1 (their default folders under Windows).
  1. Place noweb.sty and sweave.sty (part of the R installation - see the share\texmf subfolder of R, see the next instruction) in a folder that can be find by your Latex installation (under texmf-local for example).
  2. Copy the content of the C:\Program Files\R\R-2.8.1\share\texmf folder in the previous folder or in another folder under your texmf tree.
  3. Refresh the file catalogue of Latex (execute mktexlsr for TexLive in a Dos command box, for example). You can now check the placement of these files by executing kpsewhich noweb.sty in a Dos command box.
  4. Reconfigure Lyx (go to Edit-> Reconfigure). Check if you have document class "article(noweb)" or "article(Sweave noweb)" (in Document-> Settings->Document class). If not, you will need to reinstall Lyx.
  5. Create a batch script called Rweave.bat and put it in the folder C:\Program Files\Lyx16\bin\. You can create this file using notepad or any other text editor (PSPad is a very nice and free one). The file should contain a one line instruction:
    "C:\Program Files\R\R-2.8.1\bin\Rterm" --no-save --args "%1" < "C:/Program Files/LyX16/bin/MakeSweave.R" > "%1.log"
  6. Create a R file MakeSweave.R with the following lines and put it in C:\Program Files\Lyx16\bin\:


    library(tools)

    args <- commandArgs() filename <- args[length(args)] Sweave(filename) basename <- sub("\\.(Rnw|Rtex|nw)$", "",filename) texi2dvi(paste(basename, ".tex",sep=""), pdf=TRUE)


  7. You must now configure in Lyx the converter for noweb files. Go to Edit->Preferences->File Handlers->Converters. In the "From" pulldown, choose Noweb. In the "To" pulldown, choose PDF (pdflatex).In the box called "Converter" type "Rweave $$i" without the quotation marks. If necessary, click the “Modify” button and save the new command.
  8. You can now test your installation by opening an example file that contains R scraps. For example Paul Johnson's Gamma distribution lyx document.
    You can typeset this document using the pdf icon and the resulting file should open in the acrobat viewer you have configured in Lyx.
    You should be able to read the results of computations and see the plots.
Links:

21 comments:

Anonymous said...

Hi Murat

I've tried your script but got stuck.

noweb and sweave.sty are visible OK in LyX. And the converters run, but fail with a message unexpected symbol in "args <- commandArgs() filename
.. error occurred whilst running RWeave "Gamma-02.nw"

My LyX installation is in
c:\Program Files\Lyx 1.6.2 but I changed RWeave.bat to:

"C:\Program Files\R\R-2.8.1\bin\Rterm" --no-save --args "%1" < "C:/Program Files/LyX 1.6.2/bin/MakeSweave.R" > "%1.log"

Are quotes round variable %1 OK?

Is extension on file gamma-02 OK? I thought to use .Rnw.

Any idea welcome.

regards

Ian

Anonymous said...

I got it going in the end.

Two trivial changes I made:
in the Rweave.bat command line I removed spaces between the angle brackets and the path to MakeSweave.R

I edited MakeSweave.R so the R code was line-by-line. In the blog it appears in my browser as a single line.

regards

Ian

yildi said...

You are right Ian, R commands have become garbled on the blog. I do not know why. I have introduced them in separate lines. I am sorry for the inconvenience. I will correct it immediately.

yildi said...

I have now corrected the contents of MakeSweave.R and they should show now on separate lines in the blog.
But I do not understand why you have problems with spaces around the > operators because they do not pose any problem in my case.
Anyway, I am happy that it worked for you. I find this tool very useful and fun to use.
Regards,
Murat

Dan and the Bups said...

I was able to get the example document partially working, I can't remember how though, the graphs showed up, but they were not centered or anything. When I try to get a PDF out of Lyx I get the error message "An error occurred whilst running Rweave "Normal-02.nw".

Anyone have any ideas where I might have gone wrong?

Thanks,
Stephen

yildi said...

Hi Stephen,
I am getting the same message with the last version of Lyx (1.6.5) but I have observed that the PDF file is nevertheless created in the temporary folder used by Lyx. I can open it from there. I am trying to find a solution to this problem.

Murat

Jeff Laake said...

Thank you for posting this on your blog. I'm using Lyx 1.6.5 and R 2.10.0 on a Vista 64 machine. As long as you handle the typical administrator issues (eg run notepad as admin) then your instructions work with the appropriate changes to directories etc. Several small typos of mine in Rweave.bat and changing extension to .rnw instead of .nw did result in the error message "An error occurred whilst running Rweave" but once those were corrected it worked fine on the gamma example. One aspect that confused me was the point that "article(noweb)" or "article(Sweave noweb"). You might want to capitalize the OR - or I should read more closely. I had article(noweb) with the initial Lyx install and kept trying to get the article(Sweave noweb) to also load but never did. Just proceeded with the article(noweb) and all was good. Thanks again.

steve said...

Dear Yildi

I have spent the last few days trying to get Lyx / Sweave to work and after reading whatever info I could get my hands on, including the various posts on this forum as well as Gregor Gorjanc's blog I have managed to get to the point where Lyx reports that the pdf file does not exist.

Looking in the Temp directory I can see the filename.nw file but there is also a file marked filename.tex.pdf-dep (not sure if this is useful in identifying the problem)

R is ver 2.10.0 and Lyx is 1.6.2 and MikTeX 2.7 on Windows XP

I think that I have followed the info provided by Frank Liu and Paul Johnson correctly ( even though I found some of it quite contradictory)

The only as aspect that I have not carried out is implementing a "Unix
Shell" in Windows since except for Gregor's info, there is no reference to this in the other posts.

I am sure that its something rather
simple that I am not doing, but at this stage I can't see the wood for the trees.

Regards

Jeff Laake said...

I have discovered a couple of issues that may be relevant to the steves' posting and others. I found that on XP machines where I have used these instuctions that the form of the instruction for Rweave.bat does not work. It worked fine for Vista but not for XP for me. Instead you may want to try:

“C:\Program files\R\R-2.10.1\bin\Rterm –no-save
–f “C:/Program Files/Lyx 1.6.5/bin/MakeSweave.R” “%1”

but change the paths as needed for your installation. Also, if it is hanging at Waiting for Latex run 1, this may be an issue with a missing Latex package. This happened to me on some machines and found that texi2dvi was hanging and error messages are turned off. I debugged into the R function texi2dvi and found that it was due to missing the ragged2e package which you may need to install with Miktex. That package is in the Latex preamble for Lyx. Using Miktex/Maintenance/Settings install package ms after selecting the packages tab and starting the package manager. Right clicking on the package will give you the option to install. Hope some of this helps. I have put together some instructions that fill in some aspects in the above instructions and will get them posted somewhere on the web in the coming weeks.

Tony Meissner said...

I followed Jeff Kaake's inststruction and got the example files to work eg Normal-01.lyx. However, when I created my own file, it did not work. It did not recognise that I had inserted Sweave code chunk and also gave me an error when I inserted the inline expression. I am running MiKTeK 2.8, LyX 1.6.5 and R-2.10.1. on Vista. Any suggestions. Also I noticed that the menu bar of LyX under Documents I do not have a settings option.

Anonymous said...

Hy
many thx for your post
I got it going on Windows 7 (64 bits)
MiKTeK 2.8, LyX 1.6.5 and R-2.10.1
without any problems

Ben Ward said...

Hi, I've followed your instructions for getting LyX and Sweave to Work, the only differences being that I used the "Stangle(filename)" line in my MakeSweave.R file, as mentioned in "Lyx and Sweave with R on Windows XP or Vista" by Jeff Laake. I keep getting an error when I want to produce a PDF, this happened several times and I've corrected all of the errors with path's and such but it still refuses to work. I ran LyX from command shell so I could try and trace any errors and it's served me well in sorting it out, but now I'm stuck, when I try and create a PDF I get this in my dos window:

C:\Users\Ben>LyX

C:\Users\Ben\AppData\Loca\Temp\lyx_tmpdir.Hp4780\lyx_tmpbuf0>"C:\Program Files\R\R-2.10.1\bin\Rterm" --no-save --args "newfile1.nw" 0<"C:/Program Files/LYX 1.6.5/bin/MakeSweave.R"

Then I get R version 2.10.1 (2009-12-14) load up.

Then I get:
> library(tools)
> args <- commandArgs()
> filename <- args[length(args)]
> Sweave(filename)
Writing to file newfile1.tex
Processing code chunks ...
1 : term verbatim eps pdf

You can now run LaTeX on 'newfile1.tex'
> Stangle(filename)
Writing to file newfile1.R
> basename <- sub("\\.(Rnw|Rtex|nw)$", "",filename)
> texi2dvi(paste(basename, ".tex",sep=""), pdf=TRUE)
Error in texi2dvi(paste(basename, ".tex", sep = ""), pdf = TRUE) :
running 'texi2dvi' on 'newfile1.tex' failed

LaTeX errors:
!pdfTeX error: pdflatex (file C:/Users/Ben/AppData/Local/Temp/lyx_tmpdir.Hp4780
/lyx_tmpbuf0/newfile1-001.pdf): PDF inclusion: required page does not exist <0>

==> Fatal error occurred, no output PDF file produced!
Execution halted
Error: Cannot convert file
-----------------------------------
An error occurred whilst running Rweave "newfile1.nw"


There appears to be an error at the end that I can't figure out. I'm computer literate but a Biology student, I hoped to get it working so I can combine R to produce "Reporducible Research". I'm currently using a Windows 7 laptop. Any help would be hugely appreciated. I know the instructions are aimed at XP/Vista useres but I've stuggled to find ant Windows 7 help and the organisational system isn't drasically different from Vista in my opinion.

Thanks. (benjamin.ward@bathspa.org)

Benjamin Ward said...

I've sorted the problem now, I had an error with some of the block code I was trying to publish, I had echo off which I shouldn't have done.

Unknown said...

Tony-

If you haven't found a solution yet, you need to set the Document/Settings to article(noweb) and then use Tex inserts for the code chunks. You might want to start by using one of the example files and rename it and simply change the code in the code chunks until you work out hwo to do it on you own.

M. said...

Hi,

Excellent manual :) one of the bests I have seen!

I was able to follow everything very clearly and everything appeared to work fine.

Still, when I run Lyx from shell and open the 'Gamma.lyx', click on the pdf(latex), the lyx stop working and does not happen anything else. I have to force shutdown of Lyx, because it stops working and the program gets all white-

I have Miktex 2.8, lyx 1.6 and R2.10 on win XP prof.

any tip?

Small doubts, from my large ignorance on latex, dos, etc.:
1) where is supposed to be the 'texmf' (point 3)? I could not find one and I created one at 'c:/texmf/...'. I suppose it is ok, because I got the check msdos ok.
2)on point (9), the Gamma file, we are suppose to record it as 'Gamma.lyx', correct? when I do this, I successfully open it in lyx... but nothing else.
3) on tip 1, I don't have a 'Local/Temp/...' dir. My Sweave results are stored in 'C:\Documents and Settings\Marta' but I guess that is ok too.

Thank you,
All the best,
Marta

yildi said...

Dear Marta,

texmf can also stay under the MikTex folder in Program Files. You must really use the standard texmf file that is created during the installation of MikTex, otherwise it will not be able to find your new files. You should also refresh the file database using the configuration tool of Miktex that is installed under your Start menu.

Concerning other comments about different versions of Windows, I have only tested these instructions under XP SP2 and Mac OSX and both worked. I do not remember well if I had to do anything special for the OSX installation (except, adapting the folders and commands for OSX).

Jeff Laake said...

If you are having problems where you get an error after changing the filename of your LyX document and you are using the MakeSweave.R file that is described here and I included in my writeup, then continue reading. I've been having this problem and while I haven't diagnosed how it is happenning I have discovered a fix. What I found was the value of "filename" was being modified somehow in the call to Sweave and it was using the old filename but I have no idea where it was getting it. Maybe this is only a problem on my Vista machine, but if you run it to this use the following MakeSweave.R file instead:

library(tools)
args <- commandArgs()
filename <- args[length(args)]
basename <- sub("\\.(Rnw|Rtex|nw)$", "",filename)
Sweave(filename)
Stangle(paste(basename, ".nw",sep=""))
texi2dvi(paste(basename, ".tex",sep=""), pdf=TRUE)

yildi said...

Good news : In Lyx 2.0 SWeave will be integrated through a module. You can already test it using the LyX 2 betas (ftp://ftp.lyx.org/pub/lyx/devel/lyx-2.0/). It works out of the box. Thanks again to the brave LyX team!

Ben Ward said...

Hi Murat, I've recentl been forced to use Windows 7 in my work,when before I had LyX and Sweave, as well as the babel bit of org-mode for R set up in Emacs and ESS. Unfortunately, I'm not confident with setting things up in Windows 7.

I used TeXLive and have actually followed your instructions successfully and it seems to be working so theres no problem as such, but I was wondering, if it was possible, for LyX to work with Sweave, the way I had it working in Linux Here: http://blog.nextbiomotif.com/2009/10/primer-to-sweave-with-lyx.html
I Believe the two methods to do roughly the same thing - to use Sweave.sty and such to create a PDF, but the method I used for inux, did not require the creation of a Noweb to PDF converter, or the creation of the .bat and .R file.

Your method has worked beutifully, but I just wondered if you had any opinion or insight into the similarities and differences between the two methods.

Regards,
Ben Ward.

yildi said...

Hi Ben,

I have not tried the files proposed on CRAN, but the general approach seems similar to the one of Johnson and Liu. If Rterm is on the path, maybe this shortcut could also work for Windows. My preferred approach has become Lyx 2 (see my post above). Have you tried the last beta under Win7? With this new approach, having a Sweave document is as simple as adding the corresponding module to the document (using the "Tools/Document properties" menu item). It then nicely compiles with PDFLateX.

yildi said...

I have now included a link to Jeff Laake's document as a complement to my blog post on Lyx and Sweave, since I am not able to update it anymore (I have switched to OSX).