How to Tell the R Interpreter How to Use the Proxy Server

How do I tell the R interpreter how to use the proxy server?

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

R connect via proxy server

If you are using Windows you can set it up by defining the proxy in Internet Explorer and add --internet2 to the R startup shortcut (or add setInternet2(TRUE) to R\etc\RProfile.site) and then it uses the system wide settings.

If on Windows have you tried this? If not on Windows which OS are you using?

How to tell R to use proxy auto config script (PAC) in Windows

There are several internet clients available in R so it depends on what you are using.

A pac file is not a proxy server. It is just a piece of JavaScript that the client needs to execute to calculate the required proxy server for a given URL. So your code above is definitely wrong.

Companies use pac when different proxy servers are required to connect different hosts (e.g. a special intranet proxy). Have a look at the source code if your pac file to see what's going on. The curl package implements an actual PAC client in the ie_get_proxy_for_url() function. So you could wrap that to automatically find and set the correct proxy for a curl handle (see also blog):

curl_with_proxy <- function(url, verbose = TRUE){
proxy <- ie_get_proxy_for_url(url)
h <- new_handle(verbose = verbose, proxy = proxy)
curl(url, handle = h)
}

And then use it like this:

con <- curl_with_proxy("https://httpbin.org/get")
readLines(con)

If it turns out your pac file simply returns proxy.<my.domain>:8080 for any URL you might be able to set in an environment variable, but this only works for libcurl based clients:

Sys.setenv(http_proxy_user = "userid:password")
Sys.setenv(http_proxy = "proxy.<my.domain>:8080")

If you can't get it to work, please describe your problem in this github issue. Perhaps your case can help us improve this part of the curl package.



Related Topics



Leave a reply



Submit