85

I am facing problem while conecting R with internet in my office. May be this due to LAN settings. I tried the almost all possible ways I come across in the web (see below) but still in vain.

  • Method1: Invoking R using --internet2

  • Method2: Invoking R by setting ~/Rgui.exe http_proxy=http:/999.99.99.99:8080/ http_proxy_user=ask

  • Method3: Setting Setinternet2=TRUE

  • Method4:

    curl <- getCurlHandle()
    curlSetOpt(.opts = list(proxy = '999.99.99.99:8080'), curl = curl)
    Res <- getURL('http://www.cricinfo.com', curl = curl)
    

In above all methods I can able to load packages directly from CRAN also able to download files using download.file command

But using getURL(RCurl), readHTMLTable(XML), htmlTreeParse(XML) commands I am unable to extract web data. I am getting ~<HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD>~ error.

How to set LAN proxy settings for XML package in R?

techraf
  • 64,883
  • 27
  • 193
  • 198
AVSuresh
  • 1,562
  • 2
  • 15
  • 14
  • If you've tried everything and it still doesn't work, then speak to your network administrator; they may need to set up a firewall exception to allow traffic through from your machine. – Richie Cotton Jun 28 '11 at 13:39

12 Answers12

51

On Mac OS, I found the best solution here. Quoting the author, two simple steps are:

1) Open Terminal and do the following:

export http_proxy=http://staff-proxy.ul.ie:8080
export HTTP_PROXY=http://staff-proxy.ul.ie:8080

2) Run R and do the following:

Sys.setenv(http_proxy="http://staff-proxy.ul.ie:8080")

double-check this with:

Sys.getenv("http_proxy")

I am behind university proxy, and this solution worked perfectly. The major issue is to export the items in Terminal before running R, both in upper- and lower-case.

Geek On Acid
  • 6,330
  • 4
  • 44
  • 64
  • 2
    And if running RStudio, then open it from the Terminal after step 1, perhaps via "open /Applications/RStudio.app". – Glenn Oct 10 '13 at 01:34
  • Hi, how can I set it back to default. I tried your solution, and now I am using wifi with no proxy but I can't install a package anymore in RStudio. I tried `Sys.setenv(http_proxy="")`, also I run the step 1 again in the terminal setting it to empty string `""`. Double-check it and it returns `""`, but still can't install. Although I can install a package in terminal, but not in RStudio anymore. – Al-Ahmadgaid Asaad Feb 11 '15 at 13:55
  • 2
    @Al-AhmadgaidAsaad Strange, the behaviour indicates the problem on Rstudio side but I guess you should loose those proxy changes when you quit Rstudio without saving the session. Have you tried that? Otherwise I'm not sure... – Geek On Acid Feb 11 '15 at 20:30
  • 1
    I did what you suggest, clear everything from my workspace. And now it works! Thanks! – Al-Ahmadgaid Asaad Feb 12 '15 at 03:29
  • 1
    Hi @GeekOnAcid, is also possible to put the code of the second option into the file ~/.Rprofile on mac and Linux which is loaded upon session initiation. – dmeu May 22 '15 at 12:32
  • You don' t need the "Sys.setenv", try : Sys.getenv("http_proxy"), Sys.getenv("https_proxy"), Sys.getenv("no_proxy") before setting anything. – Tristan Nov 08 '18 at 16:07
  • Depending on your proxy server, you might also have to set `Sys.setenv(https_proxy=...)` (for secured HTTPS connections). – mzuther Jun 14 '23 at 08:04
26

For RStudio just you have to do this:

Firstly, open RStudio like always, select from the top menu:

Tools-Global Options-Packages

Uncheck the option: Use Internet Explorer library/proxy for HTTP

And then close the Rstudio, furthermore you have to:

  1. Find the file (.Renviron) in your computer, most probably you would find it here: C:\Users\your user name\Documents. Note that if it does not exist you can creat it just by writing this command in RStudio:

    file.edit('~/.Renviron')
    
  2. Add these two lines to the initials of the file:

    options(internet.info = 0)
    
    http_proxy="http://user_id:password@your_proxy:your_port"
    

And that's it..??!!!

techraf
  • 64,883
  • 27
  • 193
  • 198
Elias
  • 743
  • 7
  • 16
  • Depending on your proxy server, you might also have to set `https_proxy=...` (for secured HTTPS connections). – mzuther Jun 14 '23 at 08:07
24

The problem is with your curl options – the RCurl package doesn't seem to use internet2.dll. You need to specify the port separately, and will probably need to give your user login details as network credentials, e.g.,

opts <- list(
  proxy         = "999.999.999.999", 
  proxyusername = "mydomain\\myusername", 
  proxypassword = "mypassword", 
  proxyport     = 8080
)
getURL("http://stackoverflow.com", .opts = opts)

Remember to escape any backslashes in your password. You may also need to wrap the URL in a call to curlEscape.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • 1
    if the options specified are globally set with `curlSetOpt(.opts=opts)` are these also used for other connections such as with connections from `XML` package e.g.: `htmlTreeParse()` or how do I force them to use the proxy? thanks! – Seb Nov 20 '12 at 13:23
  • I have situations similar to this. I have proxy like this (ip masked) https :// domain\user:pass@172.122.37.15:8080. I want to configure in such a way that all internet facing operations use this proxy. Any lead? – Indranil Gayen Mar 08 '18 at 05:24
11

I had the same problem at my office and I solved it adding the proxy in the destination of the R shortcut; clik on right button of the R icon, preferences, and in the destination field add

"C:\Program Files\R\your_R_version\bin\Rgui.exe" http_proxy=http://user_id:passwod@your_proxy:your_port/

Be sure to put the directory where you have the R program installed. That works for me. Hope this help.

Manuel Ramón
  • 2,490
  • 2
  • 18
  • 23
8

This post pertains to R proxy issues on *nix. You should know that R has many libraries/methods to fetch data over internet.

For 'curl', 'libcurl', 'wget' etc, just do the following:

  1. Open a terminal. Type the following command:

    sudo gedit /etc/R/Renviron.site
    
  2. Enter the following lines:

    http_proxy='http://username:password@abc.com:port/'
    https_proxy='https://username:password@xyz.com:port/'
    

    Replace username, password, abc.com, xyz.com and port with these settings specific to your network.

  3. Quit R and launch again.

This should solve your problem with 'libcurl' and 'curl' method. However, I have not tried it with 'httr'. One way to do that with 'httr' only for that session is as follows:

library(httr)
set_config(use_proxy(url="abc.com",port=8080, username="username", password="password"))

You need to substitute settings specific to your n/w in relevant fields.

techraf
  • 64,883
  • 27
  • 193
  • 198
adityakaran
  • 81
  • 1
  • 1
7

Inspired by all the responses related on the internet, finally I've found the solution to correctly configure the Proxy for R and Rstudio.

There are several steps to follow, perhaps some of the steps are useless, but the combination works!

  1. Add environment variables http_proxy and https_proxy with proxy details.

    variable name: http_proxy
    variable value: https://user_id:password@your_proxy:your_port/
    
    variable name: https_proxy
    variable value: https:// user_id:password@your_proxy:your_port
    
  2. If you start R from a desktop icon, you can add the --internet flag to the target line (right click -> Properties)

    e.g."C:\Program Files\R\R-2.8.1\bin\Rgui.exe" --internet2

  3. For RStudio just you have to do this:

    Firstly, open RStudio like always, select from the top menu:

    Tools-Global Options-Packages

    Uncheck the option: Use Internet Explorer library/proxy for HTTP

  4. Find the file (.Renviron) in your computer, most probably you would find it here: C:\Users\your user name\Documents.

    Note that: if it does not exist you can create it just by writing this command in R:

    file.edit('~/.Renviron')
    

    Then add these six lines to the initials of the file:

    options(internet.info = 0)
    
    http_proxy = https:// user_id:password@your_proxy:your_port
    
    http_proxy_user = user_id:password
    
    https_proxy = https:// user_id:password0@your_proxy:your_port
    
    https_proxy_user = user_id:password
    
    ftp_proxy = user_id:password@your_proxy:your_port
    
  5. Restart R. Type the following commands in R to assure that the configuration above works well:

    Sys.getenv("http_proxy")
    
    Sys.getenv("http_proxy_user")
    
    Sys.getenv("https_proxy")
    
    Sys.getenv("https_proxy_user")
    
    Sys.getenv("ftp_proxy")
    
  6. Now you can install the packages as you want by using the command like:

    install.packages("mlr",method="libcurl")
    

    It's important to add method="libcurl", otherwise it won't work.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Mina HE
  • 1,666
  • 2
  • 11
  • 4
  • This didn't work for me exactly. What DID work was this: Sys.setenv("http_proxy"="proxy.usps.gov:8080"). Note that there's no colon-backslash, nor a space, nor a user-name. – Boyce Byerly Jul 07 '20 at 19:35
6

If you start R from a desktop icon, you can add the --internet flag to the target line (right click -> Properties) e.g.

"C:\Program Files\R\R-2.8.1\bin\Rgui.exe" --internet2 
techraf
  • 64,883
  • 27
  • 193
  • 198
Jack
  • 373
  • 1
  • 6
  • 14
6

On Windows 7 I solved this by going into my environment settings (try this link for how) and adding user variables http_proxy and https_proxy with my proxy details.

jtromans
  • 4,183
  • 6
  • 35
  • 33
2

Simplest way to get everything working in RStudio under Windows 10:

Open up Internet Explorer, select Internet Options:

enter image description here


Open editor for Environment variables:

enter image description here


Add a variable HTTP_PROXY in form:

HTTP_PROXY=http://username:password@localhost:port/

Example:

HTTP_PROXY=http://John:JohnPassword@localhost:8080/    

enter image description here


RStudio should work:

enter image description here

Contango
  • 76,540
  • 58
  • 260
  • 305
  • 1
    is there a simple way to switch between home and office for the Rstudio proxy setting? Both for Windows and Mac ? – user5249203 Apr 07 '18 at 16:46
0

Tried all of these and also the solutions using netsh, winhttp etc. Geek On Acid's answer helped me download packages from the server but none of these solutions worked for using the package I wanted to run (twitteR package).

The best solution is to use a software that let's you configure system-wide proxy.

FreeCap (free) and Proxifier (trial) worked perfectly for me at my company.

Please note that you need to remove proxy settings from your browser and any other apps that you have configured to use proxy as these tools provide system-wide proxy for all network traffic from your computer.

Rishi Dua
  • 2,296
  • 2
  • 24
  • 35
0
  1. Find your R home with R.home("home")
  2. Add following lines to Renviron.site in your R home
http_proxy=http://proxy.dom.com/
http_proxy_user=user:passwd

https_proxy=https://proxy.dom.com/
https_proxy_user=user:passwd
  1. Open R -> R reads Renviron.site in its home -> it should work :)
bathyscapher
  • 1,615
  • 1
  • 13
  • 18
-1

My solution on a Windows 7 (32bit). R version 3.0.2

Sys.setenv(http_proxy="http://proxy.*_add_your_proxy_here_*:8080")

setInternt2

updateR(2)
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Dan
  • 21
  • 3