How to select a CRAN mirror in R
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.
Avoid being asked for CRAN mirror?
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/
.
install.packages() enforce no CRAN mirror selection?
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()
.
Set default CRAN mirror permanent in R
You can set repos in your .Rprofile to restore your choice every time you start R
Edit: to be more precise:
Add
options(repos=structure(c(CRAN="YOUR FAVORITE MIRROR")))
to your .Rprofile
Alternatively, you can set the mirror site-wide in your Rprofile.site
. The location of the file is given by ?Startup
:
The
path of this file is taken from the value of theR_PROFILE
environment variable (after tilde expansion). If this variable is
unset, the default isR_HOME/etc/Rprofile.site
, which is used if
it exists (which it does not in a 'factory-fresh' installation).
So do Sys.getenv("R_PROFILE")
for the first option, or Sys.getenv("R_HOME")
or R.home()
for the second option. On macOS, the location of the second is /Library/Frameworks/R.framework/Resources/etc/
.
The file may not exist, or you may see the following lines commented out :
# set a CRAN mirror
# local({r <- getOption("repos")
# r["CRAN"] <- "http://my.local.cran"
# options(repos=r)})
So remove the comment marks and change "http://my.local.cran" to the correct website, e.g.:
local({r <- getOption("repos")
r["CRAN"] <- "http://cran.r-project.org"
options(repos=r)})
Related Topics
Add Max Value to a New Column in R
How to Change 'Maximum Upload Size Exceeded' Restriction in Shiny and Save User File Inputs
How to Stop Executing of R Code Inside Shiny (Without Stopping the Shiny Process)
Ggplot2 Shade Area Under Density Curve by Group
What's the Difference Between Lapply and Do.Call
Saving Multiple Ggplots from Ls into One and Separate Files in R
Rvest Error in Open.Connection(X, "Rb"):Timeout Was Reached
Error in Model.Frame.Default: Variable Lengths Differ
Number of Significant Digits in Dplyr Summarise
What Leads the First Element of a Printed List to Be Enclosed with Backticks in R V3.5.1
How to Parametrize Function Calls in Dplyr 0.7
Variable Name Restrictions in R
Change Background and Text of Strips Associated to Multiple Panels in R/Lattice
How to Learn R as a Programming Language
Insert Blanks into a Vector For, E.G., Minor Tick Labels in R