我的一个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将Markdown“编织”为HTML时,会成功安装和加载包。但是,当我重新编织它时,每次都会从头开始安装软件包 - 虽然我已经有了它们,但这需要一段时间。
我正在使用RStudio。
如何在不必评论相应线条的情况下规避这一点?
答案 0 :(得分:3)
```{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")
```
'缓存' flag应确保每次都不重新安装包。
这是我第一次编织时的输出:
|................................ | 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
答案 1 :(得分:3)
因为" require
返回(不可见)一个逻辑,指示所需的包是否可用"您可以方便地使用它来编程以加载包,或者如果它不可用,(尝试)安装它并在之后加载它。因此,您可以按照以下方式修改代码:
if (!require(dplyr)) {
install.packages("dplyr")
require(dplyr)
}
如果软件包已经可用,则应该加载软件包,如果没有,请尝试安装并随后加载。