2015-01-14 26 views
2

我有以下的R代碼裏面我RMarkdown腳本之一:RMarkdown:爲什麼我每次編織時都必須重新安裝所有包裝?

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 HTML中,包成功安裝並加載。但是,當我重新編織它時,每次都從頭開始安裝軟件包 - 儘管我已經擁有它們,而這需要一段時間。

我正在使用RStudio。

我怎樣才能避免這種情況,而不必評論各自的行?

+3

你可以包含像'if(!require(dplyr))install.packages(「dplyr」)' –

+0

這樣的工作,也許你可以發佈答案? – wnstnsmth

+0

完成:) @wnstnsmth –

回答

3

由於「require收益(不可見)的邏輯指出是否有必要的軟件包可用「,您可以方便地使用它進行編程以加載軟件包,或者如果軟件包不可用,(嘗試)安裝並隨後加載。所以,你可以沿着線的修改代碼:

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

,如果它已經提供這應該裝載包或如果沒有,嘗試安裝它,之後加載它。

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") 
``` 

'cache'標誌應該確保每次都不重新安裝軟件包。

這裏是第一次輸出I針織:

|................................         | 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 
+0

似乎沒有工作,他們得到重新下載,然後安裝(和編譯)。 – wnstnsmth

+0

我添加了第一次和第二次編織的輸出,其中cache = TRUE標誌。正如你所看到的,我第二次編譯「cache:logi TRUE」並返回並且沒有安裝任何軟件包。 –

+0

是的,'cache:logi TRUE'也顯示在我的案例中,但仍然會重新安裝這些軟件包。也許在RStudio中配置錯誤(它也總是要求我重新啓動RStudio)。無論如何,謝謝你的回答。 – wnstnsmth

相關問題