How do I tell the R interpreter how to use the pro

2019-01-08 14:08发布

I'm trying to get R (running on Windows) to download some packages from the Internet, but the download fails because I can't get it to correctly use the necessary proxy server. The output text when I try the Windows menu option Packages > Install package(s)... and select a CRAN mirror is:

> utils:::menuInstallPkgs()
--- Please select a CRAN mirror for use in this session ---
Warning: unable to access index for repository http://cran.opensourceresources.org/bin/windows/contrib/2.12
Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.12
Error in install.packages(NULL, .libPaths()[1L], dependencies = NA, type = type) :
    no packages were specified
In addition: Warning message:
In open.connection(con, "r") :
    cannot open: HTTP status was '407 Proxy Authentication Required'

I know the address and port of the proxy, and I also know the address of the automatic configuration script. I don't know what the authentication is called, but when using the proxy (in a browser and some other applications), I enter a username and password in a dialog window that pops up.

To set the proxy, I tried each of the following:

  • Sys.setenv(http_proxy="http://proxy.example.com:8080")
  • Sys.setenv("http_proxy"="http://proxy.example.com:8080")
  • Sys.setenv(HTTP_PROXY="http://proxy.example.com:8080")
  • Sys.setenv("HTTP_PROXY"="http://proxy.example.com:8080")

For authentication, I similarly tried setting the http_proxy_user environment variable to:

  • ask
  • user:passwd
  • Leaving it untouched

Am I using the right commands in the right way?

标签: r proxy
3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-08 14:33

You have two options:

  1. Use --internet2 or setInternet2(TRUE) and set the proxy details in the control panel, in Internet Options
  2. Do not use either --internet2 or setInternet2(FALSE), but specify the environment variables

EDIT: One trick is, you cannot change your mind between 1 and 2, after you have tried it in a session, i.e. if you run the command setInternet2(TRUE) and try to use it e.g. install.packages('reshape2'), should this fail, you cannot then call setInternet2(FALSE). You have to restart the R session.

As of R version 3.2.0, the setInternet2 function can set internet connection settings and change them within the same R session. No need to restart.


When using option 2, one way (which is nice and compact) to specify the username and password is http_proxy="http://user:password@proxy.example.com:8080/"

In the past, I have had most luck with option 2

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-08 14:52

install.packages("RCurl")

that will solve your problem.

查看更多
走好不送
4楼-- · 2019-01-08 14:55

If you want internet2 to be used everytime you use R you could add the following line to the Rprofile.site file which is located in R.x.x\etc\Rprofile.site

utils::setInternet2(TRUE)
查看更多
登录 后发表回答