简体   繁体   English

使用带有knitr的packrat库和rstudio编译PDF按钮

[英]Using packrat libraries with knitr and the rstudio compile PDF button

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. 正如上文由艺辉谢这篇文章 ,当一个人使用RStudio IDE的编译PDF按钮,从.Rnw文件生成PDF, knit()使用globalenv()一个新的R会话。 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? 有没有办法让这个新的R会话使用我的项目的packrat库(甚至是我的packrat库中包含的knitr版本)而不是我的个人用户库来确保最大程度的可重复性? 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. 我想新的R会话必须与项目本身相关联,但我不知道如何有效地做到这一点。

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. 我知道我可以直接使用knit()函数而不是编译PDF按钮,这样, knit()将使用我当前的globalenv() ,但我不喜欢这个解决方案,因为它的可重现性较差。

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. 我的具体问题是我的.Rnw文件位于我整个项目的子目录中。 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 . 当“编译PDF”按钮创建新的R会话时,它将在此子目录中创建,因此无法找到将初始化packrat.Rprofile文件。 I think the easiest solution would be to create a .Rprofile file in my subdirectory which contains 我认为最简单的解决方案是在我的子目录中创建一个.Rprofile文件,其中包含

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... 我必须在source("packrat/init.R")之前更改项目级别的工作目录,因为文件本身引用了目录...

Anybody can see a better solution? 有谁能看到更好的解决方案?

P., 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. 我不知道这个解决方案是否适用于knitr包,但我99%肯定它适用于所有其他包,因为它似乎对我来说。

(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. 我有我的项目文件夹,但我的工作目录一直是我的.rnw文件所在的子文件夹 ,位于我的项目文件夹的子目录中。

The link to Yihiu Xie's answer was very helpful. 链接到Yihiu Xie的答案非常有帮助。

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). 但是当我的R library()调用不在工作目录中并且packrat无法解析.rnw文件时,我不确定是否可以使用packrat (我使用source()从chunck调用library.R文件source() .rnw文件)。 A few notes: 几点说明:

  • I wanted to use a .Rproj file to open the project and have project-a/working as the working directory 我想使用.Rproj文件打开项目,并将project-a /作为工作目录
    • If this was true then packrat can find the library.R script 如果这是真的,那么packrat可以找到library.R脚本
    • But the .rnw file still defaults to its own working directory when compiling 但是.rnw文件在编译时仍然默认为自己的工作目录
  • 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 我想到了一个.Rprofileknitr::opts_knit$set(root.dir = "..")的工作,但我不认为它的工作原理像乳胶命令input\\ ,默认回目录包含.rnw文件
    • I thought this was insufficient because then you have two working directories, one for your r chunks and one for your latex! 我认为这是不够的,因为那时你有两个工作目录,一个用于你的r chunks ,一个用于你的乳胶!

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 . 由于.rnw总是设置工作目录,我将我的library.R脚本放在与我的.rnw文件相同的目录中,该文件在project-a / working / rnw中创建了packrat文件夹。 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. 我99%肯定这是有效的,因为当我在项目-a / working / rnw文件夹中创建packrat文件夹时没有重新定位library.R文件它收到一个错误,没有找到包,我无法编译.rnw文件。

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 . 免责声明我对packrat相对较新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM