分享

浏览器可以访问镜像,R却无法下载R包怎么办?

 生物_医药_科研 2018-12-05

问题描述

能够用浏览器访问镜像站点,但是在安装R包时遇到如下问题,

  1. # CRAN

  2. Warning in install.packages :

  3.  unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/src/contrib:

  4.  cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/src/contrib/PACKAGES'

  5. Warning in install.packages :

  6.  unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/src/contrib:

  7.  cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/src/contrib/PACKAGES'

  8. Warning in install.packages :

  9.  package ggtree is not available (for R version 3.5.1)

  10. Warning in install.packages :

  11.  unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.5:

  12.  cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.5/PACKAGES'

  13. # Bioconductor

  14. Error: Bioconductor version cannot be validated; no internet connection?

  15. In addition: Warning messages:

  16. 1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :

  17.  there is no package called GSEABase

  18. 2: In file(con, 'r') : InternetOpenUrl failed: '??'

  19. 3: In file(con, 'r') : InternetOpenUrl failed: 'on'

解决思路

第一步,确认R能否真的能够下载数据。检索到R用 download.file进行文件下载,

  1. download.file(url = 'https://upload-images./upload_images/2013053-6e5c996e3a0d4c93.png',

  2.              destfile = 'test.png')

发现无法直接下载内容,证明R在连接网络时出现了问题

  1. trying URL 'https://upload-images./upload_images/2013053-6e5c996e3a0d4c93.png'

  2. Error in download.file(url = 'https://upload-images./upload_images/2013053-6e5c996e3a0d4c93.png',  :

  3.  cannot open URL 'https://upload-images./upload_images/2013053-6e5c996e3a0d4c93.png'

  4. In addition: Warning message:

  5. In download.file(url = 'https://upload-images./upload_images/2013053-6e5c996e3a0d4c93.png',  :

  6.  InternetOpenUrl failed:

第二步,根据报错信息, 'InternetOpenUrl failed'进行检索,找到一种解决思路,也就是指定R访问网络的方法为 libcurl

  1. download.file(url = 'https://upload-images./upload_images/2013053-6e5c996e3a0d4c93.png',

  2.              destfile = 'test.png', methods='libcurl')

能够能够解决问题。

于是我修改 ~/.Rprofile,增加如下内容

  1. options('download.file.method'='libcurl')

  2. # # getOption('download.file.method')

  3. options('url.method'='libcurl')

  4. # getOption('url.method')

  5. # options(internet.info = 0) # 进行HTTP传输的诊断,默认是2,只会显示最后的出错信息

也就是Windows尽量设置默认的method为libcurl,因为wininet未必一直支持HTTPS。 来自于:https://github.com/r-lib/remotes/issues/45#issuecomment-262955721

注意:对于Windows系统, method='libcurl'时可能会出现问题,也就是Windows系统不提供有效的CA certificate bundle, 也就是说默认情况下,Windows的certificates是没有被验证过的。也就是 Sys.getenv('CURL_CA_BUNDLE')返回结果为空,建议 Sys.setenv(CURL_CA_BUNDLE=file.path(Sys.getenv('R_HOME'),'/etc/curl-ca-bundle.crt'))打开验证。

如果没有如下图片中所指的文件,可以从https://raw./bagder/ca-bundle/master/ca-bundle.crt下载curl-ca-bundle.crt的备份。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多