views:

42

answers:

2

I am having trouble finding clear documentation on how to set up a batch file for a Sweave document on Windows XP.

I am using the batch files that are found here

I have created a batch file names run.bat which contains the following:

Sweave myFile.Rnw

The first thing I do in my Sweave file after setting the wd is read in a dataset using the RODBC package:

library(RODBC)
fetch <- odbcConnect("myDatabase")
myData <- "select * from myTable"
x <- sqlQuery(fetch, myData)
odbcCloseAll()

When I run my batch file, I receive the following error:

Error: chunk 2
Error in library(RODBC) : there is no package called RODBC
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
    there is no package called 'xtable'

Obviously these packages do exist and are functional, but something isn't right in the permissions or it isn't finding the right directories. Any thoughts?

Related question here

+1  A: 

What does this return when you type it in R (in the GUI) ?

 .libPaths()

What does it show when you run in the Rnw file?

The documentation in help(Startup) will have several suggestions as to where you can set R_LIBS and its variants.

Dirk Eddelbuettel
`.libPaths()` returns `"C:/Program Files/R/R-2.11.0/library"` when running from the GUI using Eclipse. When running directly from the R GUI console, it returns `"C:/PROGRA~1/R/R-211~1.0/library"`. Checking out the help page for `Startup` now...
Chase
That would be the same, albeit printed differently. Do you actually have the RODBC package installed ?
Dirk Eddelbuettel
Well, yes and no. `RODBC` is installed in the version of R that I use with eclipse or directly with the R GUI. I am able to run the script with no trouble from either of these. However, I noticed that the DOS prompt was referring to a Revolution Enterprise iteration of R that I had installed. I uninstalled Revolution Enterprise since I don't use it, and now am receiving the error `Error: R not found`. I have modified the default directory for R so I can save separate instances of R ('R\R-2.11` and `R\R-2.10`..). I need to modify the Sweave.bat file to find my R directory...will look into now
Chase
Adjust your environment variables like PATH accordingly and you should be fine. This is *your local admin problem* so you need to fix it.
Dirk Eddelbuettel
A: 

I found an immediate solution to my problem, though understand there are certain limitations to this method. I abandoned using the CRAN provided .bat files for dynamically finding the appropriate path to R and hardcoded the path to R as such:

"C:\Program Files\R\R-2.11.0\bin\Rterm.exe" --vanilla <%run.r>%run.r
 del *.log
 del *.aux

This .bat file will fire up R, run the script run.r which contains two commands:

Sweave("myFile.Rnw")
tools::texi2dvi("myFile.tex", pdf=TRUE)

and then cleans up the intermediate .log and .aux files from the LaTeX output. Obviously, the bulk of the work is done in myFile.Rnw but this will atleast let me click one button, perform my analysis, and generate the PDF file.

If someone can think of a way to make this more efficient, I'd appreciate it.

Chase