2

When running R from the terminal, we sometimes see

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

Is there a universal, efficient, and memorable way to install.packages("example") without needing to memorise urls or interact with a dialogue box?

That is, is there a way to install an R package which is:

  1. universal (works on any/all installations of R, irrespective of operating system)
  2. Requires no memorisation of url(s)
  3. Requires no interaction with duologue box(es)
  4. Requires no files (e.g. .Rprofile) to be created or edited

Lastly, ideally, a method which is short and memorable (or easy use without having to look it up).

Here's a pseudo code example of an ideal solution (where 'force' is pseudocode for 'choose the most obvious defaults and press on at all costs!)

force(install.packages("example"))
stevec
  • 41,291
  • 27
  • 223
  • 311
  • @user2554330 Thanks. The [first](https://stackoverflow.com/a/11488224/5783745), [second](https://stackoverflow.com/a/11488727/5783745), [seventh](https://stackoverflow.com/a/30490863/5783745), and [eighth](https://stackoverflow.com/a/48881323/5783745) answers require memorization of a url (strictly disallowed in my question above), and the others set options or require interacting with the popup dialogue. The related question is about *how to respond to*, whereas mine is around *how do avoid having to provide it altogether* – stevec Dec 03 '20 at 21:22
  • @user2554330 please reopen if you agree – stevec Dec 03 '20 at 21:22
  • 1
    No, the 2nd one requires you to choose a mirror (you should use the updated suggestion) just once, then put it in your `.Rprofile` file. You don't need to memorize anything. – user2554330 Dec 03 '20 at 21:25
  • @user2554330 that fails to do what I want, which is a "universal solution". If I run a shiny app on heroku, there is no such thing as .Rprofile. If I'm on someone else's computer showing them how to do things, it's not appropriate to edit their .Rprofile the way I like it to be. – stevec Dec 03 '20 at 21:27
  • 2
    I think the answer then based on your first comment is simply "no, there is not a way to avoid having to provide it". You have to provide it someway, somehow. You either have to enter it, or the computer you are on has to be configured to know what to do. It's not like you hit ESC a bunch and it goes away and works. There is also .Rprofile.site if you are more of an admin and don't want to mess with the user's setup. –  Dec 03 '20 at 21:33
  • @Adam please vote to reopen so you can answer. I still hold hope that someone has worked out a way though.. – stevec Dec 03 '20 at 21:38
  • If @Adam's comment doesn't satisfy you, then edit your question to make it clear what resources you allow. Obviously the package needs to be installed from somewhere, and normally that choice is a CRAN mirror, but it doesn't have to be. Are you willing to write a function that simulates choosing the first entry in the `chooseCRANmirror` dialog? That's possible, but it seems to me like a worse solution than just memorizing "cloud.r-project.org": how will you install it in heroku or someone else's computer? – user2554330 Dec 03 '20 at 21:41
  • @user2554330 ideally, what I was hoping for, was some wrapper function vaguely analogous to `invisible()` or `force()` that would just attempt a brute force installation, preferably by avoiding any dialogues and selecting defaults. I agree there's a reasonable chance that the only solutions are ones that make it *more* complicated rather than less. But I can't be very *sure* of that unless the question is posed and people given a chance to respond – stevec Dec 03 '20 at 21:44
  • @user2554330 I will edit the question now to include additional constraints – stevec Dec 03 '20 at 21:44

1 Answers1

3

If you run help("install.packages"), you can see that the default value for the repository is repos = getOption("repos"). If you follow this trail to help("getOption"), it provides some more insight. Here is what it says for the repos option.

repos:

URLs of the repositories for use by update.packages. Defaults to c(CRAN="@CRAN@"), a value that causes some utilities to prompt for a CRAN mirror. To avoid this do set the CRAN mirror, by something like local({r <- getOption("repos"); r["CRAN"] <- "http://my.local.cran"; options(repos = r)}).

You can see this by going into the 'etc/' subdirectory of your R installation. There is a file there called 'repositories'. While some other repos (e.g., R-Forge) have default URLs, CRAN does not. It shows @CRAN@ as referenced by the help file.

The R documentation advises you that some utilities, such as what you are experiencing at the command line, will prompt you for a mirror, unless the option is explicitly set. The documentation does not indicate an alternative work around.

The reason why there cannot be a function to tell it to use the "most obvious default" is that there is actually no default. So a method such as your hypothetical force() would not be possible.


An edit with a bit more info:

You can use a few helpers from utils to set the repos option. I am not sure if it is easy enough to remember for your criteria, but there is chooseCRANmirror() and getCRANmirrors().

# this should work
chooseCRANmirror(ind = 1)
install.packages("example")

# or this clunky approach
install.packages("example", repos = getCRANmirrors()[1,"URL"])

But honestly at that point you may be better off just remembering repos = https://cloud.r-project.org/.

  • That is fair. The only thing I can think of is that there for some reason is a package on CRAN that has a function with a wrapper that has a mirror preset. Then you could do `magicpackage::install_packages("example")`. But even `remotes::install_cran()` which would be my first guess uses the same `repos = getOption("repos")` default. –  Dec 03 '20 at 22:11
  • I think the magic package idea definitely sounds interesting, but it would ironically require downloading (presumably from CRAN), which would require selecting a mirror.. – stevec Dec 03 '20 at 22:14
  • 2
    I like this answer. The only thing I'd add is a response to the `force(install.packages())` suggestion: you can't force a function to accept defaults when there aren't any. – user2554330 Dec 03 '20 at 22:31
  • 1
    @Adam, now one disagreement: `cloud.r-project.org` should last forever, whereas `cran.rstudio.com` will only live as long as RStudio (which hopefully is the same, but not necessarily...) – user2554330 Dec 03 '20 at 22:34