4
votes

As explained by Yihui Xie in this post, when one uses the Compile PDF button of the RStudio IDE to produce a PDF from a .Rnw file, knit() uses the globalenv() of a new R session. Is there a way that this new R session would use the packrat libraries of my project (even the version of knitr included in my packrat libraries) instead of my personal user libraries to ensure a maximum level of reproducibility? I guess that the new R session would have to be linked to the project itself, but I don't know how to do this efficiently.

I know I could directly use the knit() function instead of the Compile PDF button and, that way, knit() would use my current globalenv(), but I don't like this solution since it's less reproducible.

2

2 Answers

3
votes

I think I got the problem myself, but I want to share with others who could confirm I'm right, and possibly help improve my solution.

My specific problem is that my .Rnw file is in a sub-directory of my whole project. When the Compile PDF button creates a new R session, it is created in this sub-directory, thus not finding the .Rprofile file that would initialize packrat. I think the easiest solution would be to create a .Rprofile file in my subdirectory which contains

temp <- getwd()
setwd("..")
source("packrat/init.R")
setwd(temp)
rm(temp)

I have to change the working directory at the project level before source("packrat/init.R") because the file itself refers to the directory...

Anybody can see a better solution?

0
votes

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.