P.,
I don't know if this solution works for even the knitr
package, but I am 99% sure it works for all other packages as it seems to for me.
(I believe) I have a very similar problem. I have my project folder, but my working directory has always been the sub folder where my .rnw file is located, in a subdirectory of my project folder.
The link to Yihiu Xie's answer was very helpful.
Originally I wanted a project folder such as:
project-a/
working/
data/
datas.csv
analysis/
library.R
rscripts.R
rnw/
report.rnw
child/
preamble.rnw
packrat/
But I'm not sure if that is possible with packrat
when my R library()
calls are not in the working directory and packrat
cannot parse the .rnw file (I call the library.R file from a chunck using source()
in my .rnw file). A few notes:
- I wanted to use a .Rproj file to open the project and have project-a/working as the working directory
- If this was true then
packrat
can find the library.R script
- But the .rnw file still defaults to its own working directory when compiling
- I thought an .Rprofile with
knitr::opts_knit$set(root.dir = "..")
would work but I don't think it works for latex commands like input\
, it defaults back to the directory containing the .rnw file
- I thought this was insufficient because then you have two working directories, one for your
r chunks
and one for your latex!
Since .rnw always sets the working directory, I put my library.R script in the same directory as my .rnw file which creates the packrat
folder in project-a/working/rnw. I am 99% sure this works because when I created the packrat
folder in the project-a/working/rnw folder WITHOUT relocating the library.R file it received an error that no packages could be found and I could not compile the .rnw file.
project-a/
working/
data/
datas.csv
analysis/
rscripts.R
rnw/
report.rnw
library.R
packrat/
child/
preamble.rnw
Again, unless I am overlooking something or misunderstanding which packages are being used, this seems to have worked for me. Disclaimer here that I am relatively new to packrat
.