122

How can I set a specific CRAN mirror permanently in R?

I want to set it permanently in my laptop so that when I do install.packages(), it won't ask me again which mirror to choose.

zx8754
  • 46,390
  • 10
  • 104
  • 180
719016
  • 9,060
  • 20
  • 77
  • 151
  • 1
    See http://stackoverflow.com/questions/1189759/expert-r-users-whats-in-your-rprofile for .Rprofile examples. – jthetzel Dec 12 '11 at 13:44
  • 4
    you may want to edit global `Rprofile` file. On *NIX platforms, it's located in `/usr/lib/R/library/base/R/Rprofile`. Just be careful... and note that local `.Rprofile` settings take precedence. – aL3xa Dec 12 '11 at 14:21
  • 1
    Following up on @aL3xa's comment, see `?Startup` for the really gory details of where `.Rprofile files can be located and which take precedence. – Josh O'Brien Dec 12 '11 at 14:42
  • And please notice the `.First` and `.Last` objects. – aL3xa Dec 12 '11 at 14:54

1 Answers1

137

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 the R_PROFILE environment variable (after tilde expansion). If this variable is unset, the default is R_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)})
miguelmorin
  • 4,227
  • 3
  • 23
  • 46
rinni
  • 2,186
  • 1
  • 17
  • 21
  • 1
    I added extra information, as it wasn't worth an extra answer and a bit too much for a comment. – Joris Meys Dec 12 '11 at 15:04
  • 3
    I don't think that call to `structure` is doing anything. Usually it's just a convenient way to add attributes to an object. – Richie Cotton Dec 12 '11 at 15:24
  • @rinni: thanks, I googled for the list of URLs found the one I am closest to as you described in the `.Rprofile` file. –  Dec 12 '11 at 16:24
  • 3
    There does not seem to be a `RProfile.site` file in my R 3.3.1 on Arch Linux x86_64. There is a file named `RProfile` that does not contain the commented out lines mentioned in the answer. – SACHIN GARG Jul 16 '16 at 04:07
  • 8
    where "YOUR FAVORITE MIRROR" is the URL, not the name. – pdb Sep 18 '17 at 22:05