无法安装 rmarkdown 包

Unable to install rmarkdown packages

这是我得到的...

** Installing R Package Dependencies for R Markdown: 'rmarkdown', 'stringi'
[1/2] Installing rmarkdown...
trying URL 'http://cran.rstudio.com/src/contrib/rmarkdown_2.9.tar.gz'
Content type 'application/x-gzip' length 3246617 bytes (3.1 MB)
================================================[2/2] Installing stringi...

downloaded 3.0 MB

Error in download.file(url, destfile, method, mode = "wb", ...) : 
  download from 'http://cran.rstudio.com/src/contrib/rmarkdown_2.9.tar.gz' failed
In addition: Warning messages:
1: In download.file(url, destfile, method, mode = "wb", ...) :
  downloaded length 3152276 != reported length 3246617

2: In download.file(url, destfile, method, mode = "wb", ...) :
  URL 'http://cran.rstudio.com/src/contrib/rmarkdown_2.9.tar.gz': Timeout of 60 seconds was reached

Warning in download.packages(pkgs, destdir = tmpd, available = available,  :
  download of package ‘rmarkdown’ failed

trying URL 'http://cran.rstudio.com/bin/macosx/contrib/4.1/stringi_1.7.3.tgz'
Content type 'application/x-gzip' length 14636855 bytes (14.0 MB)

=========
downloaded 2.7 MB

Error in download.file(url, destfile, method, mode = "wb", ...) : 
  download from 'http://cran.rstudio.com/bin/macosx/contrib/4.1/stringi_1.7.3.tgz' failed
In addition: Warning messages:
1: In download.file(url, destfile, method, mode = "wb", ...) :
  downloaded length 2835937 != reported length 14636855

2: In download.file(url, destfile, method, mode = "wb", ...) :
  URL 'http://cran.rstudio.com/bin/macosx/contrib/4.1/stringi_1.7.3.tgz': Timeout of 60 seconds was reached
Warning in download.packages(pkgs, destdir = tmpd, available = available,  :
  download of package ‘stringi’ failed

您可以在第一次提到 Timeout of 60 seconds was reached 后停止所有输出。看来您遇到了网络故障,而 R 没有很好地适应它们。

三点建议:

  • 根据@Phil 的回答增加 R 的超时时间;
  • 继续尝试 install.packages(..) 直到它最终成功,希望幸运最终会向你微笑;或者
  • 从外部下载,验证下载的文件,然后手动安装。

假设第三个,然后使用 firefox/chrome/safari(或任何您喜欢的),将 R 控制台中存在的 URL 手动 下载到一个目录。我将假定为本地目录,但您可以将它们保存到您喜欢的任何位置。

AP <- available.packages()
AP[AP[,1] %in% c("rmarkdown", "stringi"),c("Package", "Version", "MD5sum", "Repository")]
#           Package     Version MD5sum                             Repository                            
# rmarkdown "rmarkdown" "2.9"   "acd82da6a09f8e6564595dfa9039e032" "https://cran.rstudio.com/src/contrib"
# stringi   "stringi"   "1.7.3" "94c37221840cd9f5b7545c585d0c6b07" "https://cran.rstudio.com/src/contrib"
system("md5sum rmarkdown_2.9.tar.gz")
# acd82da6a09f8e6564595dfa9039e032 *rmarkdown_2.9.tar.gz
# [1] 0

(并重复 stringi)。

假设校验和匹配,那么你可以做

install.packages(c("rmarkdown_2.9.tar.gz", "stringi_1.7.3.tgz"), repos = NULL)

安装下载的文件。

错误是达到了 60 秒的超时(即下载时间太长)。解决方法是增加 timeout 时间:

options(timeout=120)

如果需要,也可以更长