2

When running install.packages("somepackage") from terminal, the pop out gui to select a CRAN mirror can be inconvenient and somewhat unnecessary.

Is there a way to call install.packages("somepackage") (from R in the terminal) so as to avoid having to select a mirror via the gui, but doesn't require naming a specific mirror address/mirror?

stevec
  • 41,291
  • 27
  • 223
  • 311
  • Correct me if I'm wrong but what you want is using ``install.packages()`` without naming a mirror so R is choosing the appropriate mirror itself? – Gainz Sep 10 '19 at 12:28
  • @Gainz yes perfect – stevec Sep 10 '19 at 12:28
  • @Gainz There is no such thing as "the appropriate mirror". The choice is arbitrary and ideally results in load balancing. – Roland Sep 10 '19 at 12:29
  • @Roland I mean in Canada R will most likely not use the Taiwan mirror of the Cran. – Gainz Sep 10 '19 at 12:31
  • @Roland *any* mirror is good. I usually select one geographically close to me, but to save 0.5 seconds (through faster download speed), it's hardly worth messing around outside the terminal and reading the sentence asking for selection – stevec Sep 10 '19 at 12:31
  • @user5783745 Try using the following url : ``https://cloud.r-project.org``. – Gainz Sep 10 '19 at 12:33
  • Like this : ``install.packages("names", repos = "https://cloud.r-project.org")`` from what I read it should give you a mirror that is close to you most of the timeé – Gainz Sep 10 '19 at 12:35
  • 1
    Don't use CRAN without mirror. Use the first mirror in the list, [RStudio's 0Cloud](https://stackoverflow.com/questions/16875174/what-does-the-0-cloud-mirror-stands-for-in-r). – Roland Sep 10 '19 at 12:35
  • @Gainz we could put any mirror's url in the `repos` option but ideally I would like to not to have to know a CRAN mirror off the top of my head – stevec Sep 10 '19 at 12:36
  • @Roland is there any way to select the first without the gui opening and selecting, as in, using code (and conveniently). E.g. something like `install.packages("names", repos = NULL)` (<- that doesn't work, just an example of what would be nice) – stevec Sep 10 '19 at 12:37
  • @user5783745 The url I gived you gives a mirror that is close to you (or at least one that is always going to be near). – Gainz Sep 10 '19 at 12:38
  • read this https://stackoverflow.com/a/11488727/10447055 – Gainz Sep 10 '19 at 12:38
  • @Gainz ah I see. That's helpful. Any way to do that without having to remember a url? `repos = 'chooseforme' would be handy – stevec Sep 10 '19 at 12:39
  • @Gainz I thought `install.packages("names", repos = chooseCRANmirror())` would do as desired, but even it requires (manual) selection from a list it presents – stevec Sep 10 '19 at 12:40
  • you could randomly choose a CRAN mirror like this `install.packages("names", repos = sample(getCRANmirrors()$URL, 1))` – Tom Constant Sep 10 '19 at 12:41
  • @user5783745 Yes because that function return a list of the available mirrors. Can I know why you can't just add manually the name of the cloud mirror or select 1 when using ``chooseCRANmirrors()``? – Gainz Sep 10 '19 at 12:45
  • @Gainz, to be honest, because I won't remember it when I need it – stevec Sep 10 '19 at 12:46
  • @Gainz not that `install.packages("names", repos = getCRANmirrors()$URL[1])` is much more memorable – stevec Sep 10 '19 at 12:46
  • @user5783745 Yes it is the same thing! I simply wanted to put the repos's link but you could use this without any problem. – Gainz Sep 10 '19 at 12:47
  • @Gainz I was hoping for something shorter and more easily remembered than both `install.packages("names", repos = "https://cloud.r-project.org")` and `install.packages("names", repos = getCRANmirrors()$URL[1])`. ideally something like `install.packages("names", repos ="best")`, but it sounds this is not possible so your solution and a slightly simpler version of Tom's are best – stevec Sep 10 '19 at 12:51
  • 1
    @user5783745 See my answer, thats the only way I know of setting it "automatically" but you will need to modify your ``.Rprofile`` file. – Gainz Sep 10 '19 at 12:52

1 Answers1

3
install.packages("names", repos = "https://cloud.r-project.org")

You could also set the mirror you want with :

options(repos=structure(c(CRAN="getCRANmirrors()$URL[1]")))

in a .Rprofile file so your R will automatically use this repos when you will call :

install.packages().

stevec
  • 41,291
  • 27
  • 223
  • 311
Gainz
  • 1,721
  • 9
  • 24