RMarkdown:为什么我每次编织时都必须重新安装所有软件包?

RMarkdown: Why do I have to reinstall all packages everytime I knit?

我的一个 RMarkdown 脚本中有以下 R 代码:

install.packages("dplyr", repos="http://cran.us.r-project.org")
install.packages("tidyr", repos="http://cran.us.r-project.org")
install.packages("ggplot2", repos="http://cran.us.r-project.org")
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
options(scipen = 999)
source("classify.r")

当我使用 Shift+Ctrl+K "knit" Markdown 到 HTML 时,包已成功安装和加载。然而,当我重新编织它时,每次都会从头开始安装这些包——尽管我已经有了它们,但这需要一段时间。

我正在使用 RStudio。

如何在不评论相应行的情况下避免这种情况?

```{r chunkNameHere, cache=TRUE}
install.packages("dplyr", repos="REPOLINK")
install.packages("tidyr", repos="REPOLINK")
install.packages("ggplot2", repos="REPOLINK")
library("dplyr")
library("tidyr")
library("ggplot2")
```

'cache' 标志应确保不会每次都重新安装软件包。

这是我第一次编织的结果:

  |................................                                 |  50%
  ordinary text without R code

  |.................................................................| 100%
label: chunkNameHere (with options) 
List of 1
 $ cache: logi TRUE



processing file: Untitled.Rmd
trying URL 'REPOLINK'
Content type 'application/x-gzip' length 3870896 bytes (3.7 Mb)
opened URL
==================================================
downloaded 3.7 Mb

trying URL 'REPOLINK'
Content type 'application/x-gzip' length 72424 bytes (70 Kb)
opened URL
==================================================
downloaded 70 Kb

trying URL 'REPOLINK'
Content type 'application/x-gzip' length 2671874 bytes (2.5 Mb)
opened URL
==================================================
downloaded 2.5 Mb


/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc Untitled.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output Untitled.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rmarkdown/rmd/h/default.html --variable 'theme:bootstrap' --include-in-header /var/folders/l2/qcrxbd0s36x7jkk4k2szc9dr0000gn/T//Rtmpcy2bZL/rmarkdown-str8c75262f5f18.html --mathjax --variable 'mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --no-highlight --variable highlightjs=/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rmarkdown/rmd/h/highlight 
output file: Untitled.knit.md


Output created: Untitled.html

这是我第二次 运行 的时候:

  |................................                                 |  50%
  ordinary text without R code

  |.................................................................| 100%
label: chunkNameHere (with options) 
List of 1
 $ cache: logi TRUE


/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc Untitled.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output Untitled.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rmarkdown/rmd/h/default.html --variable 'theme:bootstrap' --include-in-header /var/folders/l2/qcrxbd0s36x7jkk4k2szc9dr0000gn/T//Rtmp0WIDxR/rmarkdown-str8c8a542dd362.html --mathjax --variable 'mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --no-highlight --variable highlightjs=/Library/Frameworks/R.framework/Versions/3.1/Resources/library/rmarkdown/rmd/h/highlight 


processing file: Untitled.Rmd
output file: Untitled.knit.md


Output created: Untitled.html

因为“require returns(无形地)指示所需包是否可用的逻辑”,您可以方便地使用它进行编程以加载包,或者,如果它不可用,(尝试)安装它并在之后加载它。因此,您可以按照以下行修改代码:

if (!require(dplyr)) {
    install.packages("dplyr")
    require(dplyr)
}

这应该加载包,如果它已经可用,或者如果不可用,尝试安装它并在之后加载它。