226

I'm trying to install a package through the R prompt by doing the following:

install.packages('RMySQL')

But the output is as follows:

--- Please select a CRAN mirror for use in this session ---

And nothing else! I can't find a solution to this very basic problem. What am I supposed to type in order to select a CRAN mirror?

EDIT:

OS: Mac-OS X 10.6.8 R Version: 2.15.0

Thomas
  • 43,637
  • 12
  • 109
  • 140
Jim Johnson
  • 2,469
  • 2
  • 15
  • 13
  • 1
    it should come up with a list of numbers from 1 - 100 coresponding to the mirror , try typing 84 and hit enter – pyCthon Jul 14 '12 at 23:28
  • 1
    also add what OS you are using , and which version of R – pyCthon Jul 14 '12 at 23:29
  • 1
    Either a window should pop up with selections or a list of selections should appear in the console. Did you check for a separate window with choices? How are you running R? – sayhey69 Jul 14 '12 at 23:29
  • @pyCthon I typed 84, but nothing is returned. – Jim Johnson Jul 14 '12 at 23:31
  • @sayhey69 I'm simply running R by calling the prompt from my terminal. I just type in r, and the interpreter comes up. I've never had any separate windows popping up asking for choices. – Jim Johnson Jul 14 '12 at 23:32
  • in that case the list of mirrors should appear in the console. fwiw 70 is the mirror at Berkeley that I use. What OS do you use? If you are using linux try sudo apt-get update. – sayhey69 Jul 15 '12 at 00:05
  • it's because you need X windows to allow Rconsole can throw a graphical dialog... R should detect you don't have windowing, and fall back to printing a list and asking for input, but doesn't... – tim Jun 02 '15 at 15:45

11 Answers11

342

You should either get a window with a list of repositories or a text menu with some options. But if that is not appearing, you can always specify the mirror from where to download the packages yourself by using repos parameter. By doing that, R will not ask you anymore about the repository. Example:

install.packages('RMySQL', repos='http://cran.us.r-project.org')

Here you have a list of mirrors for R.

betabandido
  • 18,946
  • 11
  • 62
  • 76
  • 2
    For me, the repos pop-up came, I clicked a mirror, and then nothing happened; meanwhile, this worked fine. Programmatic solution > interactive solution, as usual! Thanks! – MichaelChirico Oct 06 '16 at 22:22
  • 1
    And here are some mirrors to chose from, to be kind to cran: [cran.r-project.org/mirrors.html](https://cran.r-project.org/mirrors.html) – hobs Apr 20 '17 at 19:12
  • I found this a useful workaround to install a recent package into MRO. I spent a day trying to get a recent build of a package installed but the `checkpoint` function did not work as described using Anaconda's distribution of R, v3.4.3, with the snapshot set to 2017-09-01. – Steven C. Howell Jul 16 '18 at 20:56
123

Here is what I do, which is basically straight from the example(Startup) page:

## Default repo
local({r <- getOption("repos")
       r["CRAN"] <- "https://cran.r-project.org" 
       options(repos=r)
})

which is in ~/.Rprofile.

Edit: As it is now 2018, we can add that for the last few years the URL "https://cloud.r-project.org" has been preferable as it reflects a) https access and b) an "always-near-you" CDN.

Edit 2: And these days we want https:// so updated accordingly.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 6
    I think this should be the accepted answer. It Just Works(tm), in all cases, including on headless systems (servers, IoT, etc.). Thanks, Dirk. – Jesse Adelman Oct 07 '18 at 22:45
  • 8
    And with the hindsight of several years later we now a proper CDN that is guaranteed to be network-close to everybody, and we use `https` now so make this `https://cloud.r-project.org` as the URL. – Dirk Eddelbuettel Oct 07 '18 at 22:51
  • 2
    I hope you don't mind, Dirk, but I just edited your post to reflect your suggestion. – Jesse Adelman Oct 08 '18 at 03:05
  • If I had wanted that I could have done it four hours ago myself. I prefer to let the historical record speak. But because vandals will come, I put a postscriptum in. – Dirk Eddelbuettel Oct 08 '18 at 03:08
  • 1
    Edit queue now full. I think the ideal answer here would 1) have the new URL in the main code block to enable easy copy-paste and 2) include this useful note as well: https://stackoverflow.com/a/48152179/8400969 – Michael Roswell Nov 22 '22 at 14:48
74

I'm a fan of:

chooseCRANmirror()

Which will print the list of mirrors in the output (no worrying a popup window since you are running it from the terminal) and then you enter the number you want.

Jared
  • 3,510
  • 3
  • 25
  • 28
  • 1
    Oops, do ```chooseCRANmirror(81)``` and it brings up a prompt for you to enter a number. All in the console. – Jared Apr 04 '13 at 03:09
  • 1
    Note: this doesn't appear to update either /etc/R/Rprofile.site or ~/.Rprofile. (So, it is not good for choosing an alternative old mirror that has disappeared. Better to edit the above files directly.) – Darren Cook Jun 10 '13 at 02:48
  • @DarrenCook You might be right but I find a lot of people can be intimidated by those files. I know I was. – Jared Jun 14 '13 at 22:48
  • 6
    `chooseCRANmirror(ind=81)` is what Jared probably meant. It will bypass the prompt. – Casey Watson Feb 19 '14 at 20:54
  • 2
    Can set `options(menu.graphics = FALSE)` in `.Rprofile` too – Andy Barbour May 27 '16 at 16:54
31

I use the ~/.Rprofile solution suggested by Dirk, but I just wanted to point out that

chooseCRANmirror(graphics=FALSE)

seems to be the sensible thing to do instead of

chooseCRANmirror(81)

, which may work, but which involves the magic number 81 (or maybe this is subtle way to promote tourism to 81 = UK (Bristol) :-) )

vijucat
  • 2,059
  • 1
  • 15
  • 17
13

Repository selection screen cannot be shown on your system (OS X), since OS X no longer includes X11. R tries to show you the prompt through X11. Install X11 from http://xquartz.macosforge.org/landing/. Then run the install command. The repo selection prompt will be shown.

Cahit Gungor
  • 1,477
  • 23
  • 30
  • 1
    This should be the accepted answer. The Apple support article about X11 support in OS X is at http://support.apple.com/kb/HT5293?viewlocale=en_US&locale=en_US – Jere Käpyaho Mar 25 '14 at 11:39
  • The problem is likely that the window doesn't show because of X11, but arguably, `chooseCRANmirror()`/`repos` parameter are much simpler solutions. – Ben Mosher Apr 18 '14 at 00:48
9

I used

chooseCRANmirror(81)

it gives you a prompt to select the country. Then you can do a selection by typing the country mirror code specified there.

Ferdi
  • 540
  • 3
  • 12
  • 23
Manoj Rammoorthy
  • 1,384
  • 16
  • 16
4

If you need to set the mirror in a non-interactive way (for example doing an rbundler install in a deploy script) you can do it in this way:

First manually run:

chooseCRANmirror()

Pick the mirror number that is best for you and remember it. Then to automate the selection:

R -e 'chooseCRANmirror(graphics=FALSE, ind=87);library(rbundler);bundle()'

Where 87 is the number of the mirror you would like to use. This snippet also installs the rbundle for you. You can omit that if you like.

Kyle
  • 735
  • 1
  • 5
  • 16
3

You could also disable all graphical menus by running this or placing it in your Rprofile

options(menu.graphics = FALSE)
Extrapolator
  • 343
  • 3
  • 7
3

A drop down menu should pop up for you to select from (or you will get a bunch of numbers to choose from), whether you are using R in the terminal or an IDE such as RStudio. This is supported on Windows, Mac OS, and most Linux systems. However, it may require additional configuration or dependencies such as X-windows.

To enable X-windows when using remote access use the following -XY flags:

ssh -XY username@server.domain

There is often a default repo but this can be specified if you have any issue, such as running scripts or Rmarkdown/knitr. You can use the repo opset the mirror or repository for CRAN each time you install with:

install.packages("package", repo="<your.nearest.mirror>")

It is advisable to use the nearest mirror to your location for faster downloads. For example:

install.packages("RMySQL", repos="https://cran.stat.auckland.ac.nz/")

You can also set the repos option in your session so you only need to it once per interactive session (or script). You can check whether repos is configured with:

options(repos)

If you get "Error in options(repos) : object 'repos' not found" then you can set the repository option. For example:

options(repos = "https://cran.stat.auckland.ac.nz/")

Then it should work to install packages like usual. For Example:

install.packages("RMySQL")

As mentioned by others, you can configure the repository in your .Rprofile file and have this work across all of your scripts. It's up to you whether your prefer these "global" options on your system or "local" options in your session or script. These "local" options take more time to use each session but have the benefit of making others able to use your scripts if they don't have your .Rprofile.

Tom Kelly
  • 1,458
  • 17
  • 25
0

I had, on macOS, the exact thing that you say: A 'please select' prompt and then nothing more.

After I opened (and updated; don't know if that was relevant) X-Quartz, and then restarted R and tried again, I got an X-window list of mirrors to choose from after a few seconds. It was faster the third time onwards.

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61
0

Add into ~/.Rprofile

local({r <- getOption("repos")
    r["CRAN"] <- "mirror_site"  #for example, https://mirrors.ustc.edu.cn/CRAN/
    options(repos=r)
    options(BioC_mirror="bioc_mirror_site") #if using biocLite
})