4

正如 Yihui Xie 在这篇文章中所解释的那样,当使用 RStudio IDE 的编译 PDF 按钮从 .Rnw 文件生成 PDF 时,会knit()使用globalenv()新的 R 会话。有没有办法让这个新的 R 会话使用packrat我的项目的库(甚至是knitr我的库中包含的版本packrat)而不是我的个人用户库,以确保最大程度的可重复性?我想新的 R 会话必须链接到项目本身,但我不知道如何有效地做到这一点。

我知道我可以直接使用该knit()功能而不是 Compile PDF 按钮,这样knit()就可以使用我的 current globalenv(),但我不喜欢这种解决方案,因为它的可重复性较低。

4

2 回答 2

3

我想我自己遇到了问题,但我想与其他可以确认我是正确的人分享,并可能帮助改进我的解决方案。

我的具体问题是我的 .Rnw 文件位于整个项目的子目录中。当编译 PDF 按钮创建一个新的 R 会话时,它是在这个子目录中创建的,因此找不到.Rprofile将初始化的文件packrat。我认为最简单的解决方案是在我的子目录中创建一个.Rprofile文件,其中包含

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

我之前必须在项目级别更改工作目录,source("packrat/init.R")因为文件本身引用了该目录...

任何人都可以看到更好的解决方案?

于 2018-02-15T16:28:56.420 回答
0

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.

于 2018-10-23T20:02:10.523 回答