8

I'm a Windows user. A few weeks ago I installed R and Rstudio along with many packages. Today there was a message that new packages were not installed.

Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/src/contrib:
  cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/src/contrib/PACKAGES'

I reinstalled R but this did not solve the problem.

Warning in install.packages :

  InternetOpenUrl failed: 'Can not connect to server'
Roman Patutin
  • 2,171
  • 4
  • 23
  • 27
AntonCH
  • 272
  • 2
  • 3
  • 13
  • 2
    For those who want to close this question: it's a genuine problem tied to RStudio (as explained in my answer), it pops up often, and it can be solved programattically. Hence I consider this a valid SO question. – Joris Meys Jul 15 '17 at 08:25
  • Try to issue the command `ping www.stats.ox.ac.uk`. I got a "Request timed out.". – user2314737 Jul 15 '17 at 09:02
  • @user2314737 the point is that RStudio shouldn't try to reach that site in the first place. pinging it will only tell you what R is telling you: the website can't be reached. – Joris Meys Jul 15 '17 at 09:08
  • This actually happened couple of times in the past. Does anybody know why `stats.ox.ac.uk` is down? – rbm Jul 15 '17 at 15:12

3 Answers3

17

This is something that pops up in R and RStudio only once in a while. RStudio changes quite a few settings, and the option "repos" is one of them. On Windows, the following is added

EDIT: It's not RStudio adding this extra repository. The repository is kindly provided by Dr. Brian Ripley for packages that for some reason can't be made available on CRAN (license, not building out of the box, requiring additional software, ...). This is called "CRANextra" in the settings:

> getOption("repos")
                                CRAN                            CRANextra 
         "https://cran.rstudio.com/" "http://www.stats.ox.ac.uk/pub/RWin" 
attr(,"RStudio")
[1] TRUE

So RStudio tries to access a specific repository when run on Windows, but that repository has had some connection issues in the past; it isn't always reachable, and when it's not, the warnings you report are issued.

You can get this warning to stop by resetting this option:

options(repos = "https://cran.rstudio.com") # or a repo of your choice.

Which allows you to install packages without the warning:

> install.packages("fortunes")
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/fortunes_1.5-4.zip'
Content type 'application/zip' length 202721 bytes (197 KB)
downloaded 197 KB

package ‘fortunes’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Joris\AppData\Local\Temp\Rtmpu0febg\downloaded_packages

Even when this warning is displayed, packages still get installed from the rstudio CRAN mirror. The warning is reported as a bug, and RStudio has promised to tackle it soon.

EDIT: More information on the CRANextra repository in R FAQ (last paragraph):

Some CRAN packages that do not build out of the box on Windows, require additional software, or are shipping third party libraries for Windows cannot be made available on CRAN in form of a Windows binary packages. Nevertheless, some of these packages are available at the “CRAN extras” repository at https://www.stats.ox.ac.uk/pub/RWin/ kindly provided by Brian D. Ripley. Note that this repository is a default repository for recent versions of R for Windows.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • Uau dudem i love you, I tried everything I could in the internet, but nothing worked. Its very fast and effective way, all is worked! THANK U VERY MUCH! – AntonCH Jul 15 '17 at 08:28
  • @AntonCH You're welcome. Keep in mind though you have to set that option again every time you restart R, until the people from RStudio solved the problem in their next release. – Joris Meys Jul 15 '17 at 08:32
  • 1
    So that's a bug! I always thought there was something wrong with my settings, because it always gave me the warnings but downloaded from CRAN. – samkart Jul 15 '17 at 08:45
  • 1
    @ShubhradeepMajumdar it is a bug and the bug is that there is something wrong with the settings ;-) Because also the standard RStudio CRAN mirror is set, installation does proceed from the normal CRAN mirror. It's the CRANExtra that causes the problem, and I never understood why they added it in the first place. – Joris Meys Jul 15 '17 at 08:58
  • @ShubhradeepMajumdar I have to correct myself: It's not RStudio but R itself actually adding that extra CRAN repo. I also understand now why it's added, see teg this question: https://stackoverflow.com/questions/20048528/how-many-cranextra-mirror-in-the-world – Joris Meys Jul 17 '17 at 15:55
  • Thank you @JorisMeys. I, too, now get the point. Adding the extra for Windows support seems legit and thoughtful. But, why can't they just suppress the warnings if it gives an `http` error, every time. I haven't seen any of my `install.packages()` going smoothly, or via that repo. – samkart Jul 18 '17 at 15:50
  • Hi, can help me with this post please? https://stackoverflow.com/questions/60785166/unable-to-install-a-package-package-in-r When I execute the `getOption("repos")`, I get two repos.. One is CRAN and other is OHDSI. – The Great Mar 23 '20 at 03:54
5

In the mean while R Studio fixes the bug, a temporal solution for not having to especifie the CRAN repository every time we start an R session, is to edit your "Rprofile.site" file and add this line

options(repos = getOption("repos")["CRAN"])

That way every time an R session is started the CRANextra repository ("http://www.stats.ox.ac.uk/pub/RWin") is removed automatically. Just remember to delete or comment with # this line after the bug is solved.

Andres
  • 2,413
  • 1
  • 13
  • 18
  • Hi @Andres, can help me with this related post?. It is related to the current post. https://stackoverflow.com/questions/60785166/unable-to-install-a-package-package-in-r – The Great Mar 23 '20 at 03:53
  • When I execute the `getOption("repos")`, I get two repos.. One is CRAN and other is OHDSI. – The Great Mar 23 '20 at 03:54
0

I was facing similar issue and the fix that worked for me is that, in RStudio I've opened tools -> Global Options -> Packages -> Primary CRAN Repository -> Set Global.

Also make sure you're not using installed.packages this happens to a lot of people because of RStudio typing suggestions. You need to use install.packages("<package_name>")

Dharman
  • 30,962
  • 25
  • 85
  • 135
Abhishek Boorugu
  • 164
  • 1
  • 1
  • 12