How to properly deal with the working dir with knitr when R and LaTeX docs have different paths?

18 Views Asked by At

I have the following basic path structure:

project/
project/Rscripts/
project/LaTeX/

In my project/Rscripts/ dir, I have a main script that I source() in my main LaTeX doc in from project/LaTeX/. This R script also uses source() to call more scripts found in /project/Rscripts/.

The problem is that the default working directory when knitting is the directory of the LaTeX doc, so to source my main R script, I have to do source("../Rscripts/main.R"). This would be fine except that it will not find the additional R scripts as the commands in main.R are basically source("additional.R").

This leaves me with two options that I know of:

  1. Use source("../Rscripts/additional.R") in my main.R file, but this breaks things if I decide to work on the R stuff on their own as the working directory will be project/Rscripts/ when doing so.
  2. Use setwd("../Rscripts/") in my LaTeX doc, but I've always been told to never mess with working directories like this.

(2) really seems to be the more workable approach, but is there another way to do this without coding a change to my working directory, or is that not such a bad thing to do in this case?

0

There are 0 best solutions below