I ended up going with knitr instead of Sweave because it gives more options and better control of where to place the tex files after creation.
Now there is technically a way to have knitr go through a master and compile the individual Rnw files that are included, but I couldn't get it to work. And I know that people use makefiles, but I'm better at R then makefiles so I devised this solution.
I made an R function that checks if the tek file does not exist or is older than the Rnw file. If so, the tex is compiled.
doKnit <- function(file.name)
# create full names of files
rnw <- sprintf("%s.Rnw", file.name)
tex <- sprintf("%s.tex", file.name)
# if the rnw file doesn't exist, just exit and do nothing
## create the tex file if necessary
# if the tex file doesn't exist, or it is older than the rnw file you have to create it
if(!file.exists(tex) || file.info(tex)$mtime < file.info(rnw)$mtime)
# just return NULL
Then I have another function that just loops through a vector of file names, calling the above function for each one.
knitAll <- function(files)
## loop through and knit each chapter file if the tex file is older
for(a in files)
(I know I could vectorize this but that feels like a bit too much overkill).
Then in my master.Rnw I call that function with a list of files (not including the extension) to check for compilation.
# vector of chapters to be knitted
chapters <- c("Introduction",
knitAll(files=sprintf("%s/%s", chapters, chapters))
This block has
include=FALSE to ensure it does not appear in the document.
I know this is a bit hacky, but it works really well. So well that I'm going to include it in my R package of odds and ends.
For what it's worth, doing all this in RStudio really makes life so great. The new preview version even has spell check for Rnw files.
knitr also includes a way to generate PDFs of the individual files, but I haven't mastered that yet.