Sorting out Sweave in Eclipse/StatET

Using Sweave to produce pretty-looking documentation for R is awfully handy. It takes a little tweaking to get set up in Eclipse and StatET though. I followed the information in Jeromy Anglim’s webpage to originally get Sweave set up. The following is my experience getting it working with Eclipse 3.5, StatET 0.9.1.b201011060900E35sw, Sweave Add-on for StatET 0.9.0.b201011060900E35sw, MiKTeK 2.9, and R 2.12.0 i386 on Windows 7 x64 circa November 2010.

I initially wrote a .Rnw file to be run through Sweave and converted to a pdf. I then set up Eclipse to process the .Rnw file using Sweave. I went to Run>External Tools>External Tool Configurations…
There I created a new configuration under the Sweave Document Processing tree item, called Sweave2 in the image below.

External configuration. The stock settings are shown here.

Next I used Run>External Tools to choose my Sweave document processing configuration. It returned an error in the R console:

That was a failure.

The error reported there is
Error: chunk 1
Error : '\R' is an unrecognized escape in character string starting "D:\R"

This means that while processing chunk 1 in my .Rnw file, R freaked out because Sweave interpreted the first part of the path to the directory of the .Rnw file (D:\R\Misc_scripts\test2.Rnw) as a special-character escape switch (“\R”) which is meaningless in this context. Recall that R expects Windows file paths to be entered with forward slashes (D:/R/Misc_scripts/test2.Rnw), because the backslash \ is reserved for escape characters.

This appears to be a result of the path being supplied by Eclipse to R using the backslashes (D:\\R\\Misc_scripts\\test2.Rnw). Normally the double backslashes should work in R, but this isn’t apparently isn’t the case with this interaction between Sweave and Eclipse. The bug in R-2.12.0 was fixed in the newer patch, R-2.12.1 though, so you could leave the original Sweave(file = "${resource_loc:{source_file_path}}") code in place now. However, there are several other reasons to run the Sweave command from the same working directory as your .Rnw file, particularly if you’re lazy about coding in relative path names to access data files or if you like to keep your Sweave output figures in a separate folder with a relative path name specified in the .Rnw document.

My alteration is to modify the Eclipse external tool configuration so that Eclipse only passes the name of the file, without a path prepended onto it. This can be done in the Run>External Tools>External Tool Configuration window, by altering the code in the “Run command in active R Console” code so that it reads Sweave(file = "${resource_name}") as shown in the image:

Change the command passed to R to only include the resource name.

In doing this, you now have to have your working directory in the R console set to be the same as the directory where the .Rnw file lives that you’re trying to process.

Having solved that minor problem, the next issue was that my .tex file was being created, but when texi2dvi was being called by Eclipse in the R console, MiKTeK could not find a Sweave.sty file to help process the .tex file. That manifested itself with this error window:

MiKTeK can't find the Sweave.sty file, so this window pops up in Eclipse.

One way to fix this error would be to place a copy of the Sweave.sty file in the same directory as your .Rnw file. The basic Sweave.sty file for R can typically be found in your R directory under share/texmf/tex/latex/Sweave.sty

Alternately, you can inform MiKTeK of where your Sweave.sty file lives (in that same share/texmf/tex/latex/ directory) using the directions found here: http://docs.miktex.org/manual/localadditions.html#id573835. My version of that fix is as follows:

Open the MiKTeK Settings control panel by going to Start>MiKTeK 2.9>Maintenance>Settings. Click on the Roots tab when the control panel opens.

Opon the MiKTeK settings window.

On the Roots tab, choose “Add” at the bottom. Browse to the share/texmf/ directory in your R installation.

Choose the /share/texmf directory in your current R installation folder.

After doing this, you should see that directory path in the Roots tab:

The R /share/texmf directory path is now in the list of MiKTeX directories.

Now I am able to use Eclipse to invoke Sweave to automatically create a .tex file and convert it to a pdf.

The final output of my test file.

It’s entirely possible that pointing MiKTeK to that R /share/texmf/ directory will come back to bite me in the future, but for now it appears to work. If I upgrade to a future version of R, it will be necessary to point MiKTeK at the new /share/texmf/ directory so that a current version of Sweave.sty will be in use, in case changes are made to the functioning of Sweave.

Yet another stumbling block I’ve run into is how R and Sweave deal with R/S expressions within the body of the text, particularly expressions that contain a $ sign. For example, if I try
to return the mean value from a column of a data frame ‘df’ in the text of my .Rnw file:

blah blah blah \Sexpr{mean(df$column, na.rm = TRUE)} blah blah blah.

The TeX document will often freak out because it thinks the $ sign is supposed to signal the start of a chunk of math code. This can be fixed by adding an extra option to the Sweave call that I invoke through Eclipse/StatEt. While you have a .Rnw file active in the text editor of Eclipse, go to the menu Document>Create/Edit Profiles (alternately, you can get there by going to the menu Run>External Tools>External Tool Configurations). In the window that opens, click on the Sweave processing profile you’ve created (see figure below). In the “Run command in active R Console:” box, change the code snippet to read:
Sweave(file = "${resource_name}", syntax = "SweaveSyntaxNoweb")

Add in the syntax = "SweaveSyntaxNoweb" option to streamline the interpretation of R code chunks in the text of your .Rnw document.

Making that change should allow the whole R/Sweave/Eclipse/MikTeX/StatET rigamarole work correctly.

2 Responses to “Sorting out Sweave in Eclipse/StatET”

  1. Steven said:

    Feb 05, 11 at 06:37

    Hi,

    I have a question regarding the use of Sweave with Eclipse: When I edit a .Rnw file in Eclipse, then I have no line wrapping. So, for example, when I have a paragraph, this paragraph is represented as one single line in Eclipse (which is not very handy when you would like to write a paper). Do you know how to solve this issue?

    Thanks in advance, regards
    Steven

  2. Luke Miller said:

    Feb 06, 11 at 19:55

    It doesn’t appear that Eclipse/StatET supports line wrapping currently. You’ll find references to line wrapping sprinkled throughout the Eclipse Preferences menus, but I have yet to find a combination of options that actually wraps lines. I believe you are stuck putting in your own line breaks for the time being.