Tuesday, May 22, 2012

Sweave Errors with MikTex/Texworks Trying to Locate R Packages

So this is totally a technical post, as I want to get the information out there for those with the same problems. Feel free to stop reading if none of this pertains to you.

My OS: Windows 7
R version: 2.14.2
Miktex/Texworks version: 2.9

I like R and I like latex, and I'm working on some stuff that I'd like to Sweave. I won't go into all the details as there are sites out there to tell you what Sweave can do. I wanted to set up my computer to Sweave directly from miktex as it's a pain to have to sweave in R, then open your .tex file in miktex and typeset there. My initial search turned up lots of sites of which I read a lot of them. The one that gave me the most help is below.

I followed the steps given here: setting up sweave

I tried sweaving an .Rnw file and it worked! (don't get to excited) Here's the R chunk I was sweaving:

<<>>=
R <- matrix(c(1,7,4,2,3,1,8,1),4,byrow=TRUE)
L <- matrix(c(1,4,2,3,2,1),2,byrow=TRUE)
dimnames(R) <- list(c("","","",""),c("",""))
dimnames(L) <- list(c("",""),c("","",""))
R
L
R%*%L
@

I continued working along, typing up my work thinking I was set on the sweaving front until I got to the next piece of code I wanted to put in my document. 

Code chunk 2:
<<>>=
library(faraway)
data(pima)
head(pima)
summary(pima)
@

Well, after trying to sweave this .Rnw file with the chunk of code above, I got an error:

Writing to file document.tex
Processing code chunks with options ...
1 : echo keep.source term verbatim
Error: chunk 1
Error in library(faraway) : there is no package called 'faraway'
Execution halted

 My next step was to try:
<<>>=
install.packages("faraway")
library(faraway)
data(pima)
head(pima)
summary(pima)
@

I thought maybe somehow this would solve the issue, it did not, I got the message:

Writing to file document.tex
Processing code chunks with options ...
1 : echo keep.source term verbatim
Warning in install.packages("faraway") :
'lib = "C:/Program Files/R/R-2.14.2/library"' is not writable
Error: chunk 1
Error in install.packages("faraway") : unable to install packages
Execution halted

So, I started searching around trying to figure out what it meant that my library path was "not writable". I had noticed earlier looking through different paths on my computer that R was storing downloaded packages in two different locations on my computer. 

C:\Users\UserName\Documents\R\win-library\2.14 -- (writable and apparently where R was saving packages I downloaded via install.packages("") through the R console)

C:\Program Files\R\R-2.14.2\library -- (NOT writable, where other libraries were)

It turned out that the package "faraway" was only in the first of these paths, not the second. When I went to copy the package to the second path above, I needed administrative permissions to copy the file over. Since this is my personal computer there was no issue doing this. Once the package was in both locations, I went back to Sweave my .Rnw and POOF! it worked. It seems that the permissions were messing with Miktex trying to find where the packages were installed. 

I hope this helps someone out there, as there is not very good documentation on how to set up LaTeX with Sweave and if there are any snags there is even less documentation.