数据-R语言-图表-决策-Linux-PythonR语言问题集锦R包说明

R包安装函数"install.packages&quo

2018-12-03  本文已影响604人  xuzhougeng

今天解决解决了一个R包安装的问题,并且硬着头皮把install.packagesdownload.file的说明从头到位看了一遍,应该再也没有一个R包安装能为难到我了。

问题实例

问题描述

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

# CRAN
Warning in install.packages :
  unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/src/contrib:
  cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/src/contrib/PACKAGES'
Warning in install.packages :
  unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/src/contrib:
  cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/src/contrib/PACKAGES'
Warning in install.packages :
  package ‘ggtree’ is not available (for R version 3.5.1)
Warning in install.packages :
  unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.5:
  cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.5/PACKAGES'
# Bioconductor
Error: Bioconductor version cannot be validated; no internet connection?
In addition: Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘GSEABase’
2: In file(con, "r") : InternetOpenUrl failed: '??'
3: In file(con, "r") : InternetOpenUrl failed: 'on'

解决思路

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

 download.file(url = "https://img.haomeiwen.com/i2013053/6e5c996e3a0d4c93.png",
              destfile = "test.png")

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

trying URL 'https://img.haomeiwen.com/i2013053/6e5c996e3a0d4c93.png'
Error in download.file(url = "https://img.haomeiwen.com/i2013053/6e5c996e3a0d4c93.png",  : 
  cannot open URL 'https://img.haomeiwen.com/i2013053/6e5c996e3a0d4c93.png'
In addition: Warning message:
In download.file(url = "https://img.haomeiwen.com/i2013053/6e5c996e3a0d4c93.png",  :
  InternetOpenUrl failed: 

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

download.file(url = "https://img.haomeiwen.com/i2013053/6e5c996e3a0d4c93.png",
              destfile = "test.png", methods="libcurl")

能够解决问题。

深入学习install.packages()

为了让自己能够更好解决R包安装问题,需要深入学习install.packages的原理(BiocManager::install本质上也是调用install.packages)。

先仔细阅读install.packages()的参数:

pkgs: 默认是包名,比如说"Matrix", 会自动从CRAN上检索对应的包,然后进行下载。如果你希望指定安装本地包,或者一个具体的网络地址,参考代码如下:

# from url resource
install.packages("https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/3.5/Matrix_1.2-15.zip", repos=NULL)
# from local
install.packages("~/../Desktop/Matrix_1.2-15.zip", repos = NULL)

关于Secure URLs

对于https://或者ftps://这类URL,R在下载数据时会尝试对网站的证书进行验证。通常会调用操作系统安装的CA root certificates完成。

对于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.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt下载curl-ca-bundle.crt的备份。

关于代理

wininet调用系统中的Internet Option处理代理(proxy). 或者用Sys.setenv设置环境变量http_proxy,ftp_proxy

互联网选项Internet Options

解决报错的一些建议

上一篇 下一篇

猜你喜欢

热点阅读