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)})
Can't permanently change the CRAN repository in R (OS X)
And right at the end of writing this post, it occurred to me to check in R.app's various settings and preferences. And there, in the Startup panel of R.app's preferences, is "Default CRAN mirror", set to http://cran.parentingamerica.com
. Aargh. Thanks, R.app! There is no obvious way to turn that off and have it just use the one set in my .Rprofile
; but at least I can change it there and it works. Posting this to help others who go down the same path.
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/
.
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.
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()
.
install.packages fails in knitr document: trying to use CRAN without setting a mirror
Knitr produces a R session, without a default cran mirror unless you specifically asked for one. We tend to forget we need to set up CRAN for every R session when we use Rstudio because it takes care of it, but only for interactive use, not for knitr.
You could try specifying a mirror as a install.packages
argument:
install.packages("weatherData",repos = "http://cran.us.r-project.org")
Alternatively, you could set up your default CRAN mirror in your .Rprofile
. See this answer.
That said, it is not a good idea to install packages through a knitr document that you will probably compile several times. You should assume people know how to install a missing package if needed, or at least test whether the package is installed before installing it again
if(!require(weatherData)) install.packages("weatherData",repos = "http://cran.us.r-project.org")
Related Topics
Convert a Dataframe to Presence Absence Matrix
How to Learn R as a Programming Language
What Does "S3 Methods" Mean in R
How to Fit a Smooth Curve to My Data in R
Factors in R: More Than an Annoyance
Conditionally Display a Block of Text in R Markdown
How to Tell Cran to Install Package Dependencies Automatically
Write List of Data.Frames to Separate CSV Files with Lapply
How to See Data from .Rdata File
Using Grep to Help Subset a Data Frame
Ggplot2 Heatmaps: Using Different Gradients for Categories
Global Variables in Packages in R
Creating a Local R Package Repository
Convert All Data Frame Character Columns to Factors
Extract Prediction Band from Lme Fit
Code Chunk Font Size in Rmarkdown with Knitr and Latex
Fill Na in a Time Series Only to a Limited Number
How to Add a Number of Observations Per Group and Use Group Mean in Ggplot2 Boxplot