Set default CRAN mirror permanent in R
Asked Answered
W

3

131

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.

Waffle answered 12/12, 2011 at 13:30 Comment(4)
See #1190259 for .Rprofile examples.Tollman
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.Martinmas
Following up on @aL3xa's comment, see ?Startup for the really gory details of where `.Rprofile files can be located and which take precedence.Tropine
And please notice the .First and .Last objects.Martinmas
M
153

You can set repos in your .Rprofile to restore your choice every time you start R

Edit: to be more precise:

Add

options(repos=c(CRAN="THE URL OF YOUR FAVORITE MIRROR"))

to your .Rprofile, located in the home directory of your user.

You can find a list of CRAN mirrors here.

Example:

# add this line to ~/.Rprofile, restart your R session so it takes effect
options(repos=c(CRAN="https://cran.r-project.org"))

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"] <- "https://cran.r-project.org"
       options(repos=r)})
Melonie answered 12/12, 2011 at 13:40 Comment(5)
I added extra information, as it wasn't worth an extra answer and a bit too much for a comment.Cochise
I don't think that call to structure is doing anything. Usually it's just a convenient way to add attributes to an object.Flocculent
@rinni: thanks, I googled for the list of URLs found the one I am closest to as you described in the .Rprofile file.Timpani
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.Valerio
where "YOUR FAVORITE MIRROR" is the URL, not the name.Saber
O
1

In one instance, the .Rprofile edit suggested above did not work. However, the following code did:

utils::setRepositories(ind = 0, addURLs = c(CRAN = "YOUR FAVORITE MIRROR"))

where "YOUR FAVORITE MIRROR" is the URL, not the name.

Restart R after editing the .Rprofile. ind = 0 will indicate that you only want the named repository. Additional repositories can be included in the addURLs = option and are comma separated within the character vector.

Overt answered 30/8, 2022 at 18:28 Comment(1)
I'm using RStudio (Version 2023.06.1+524) on Mac (Ventura) and this was what worked for me.Doak
S
-1

In case you are trying to do this in RStudio, you can either do it via the RStudio UI (Tools -> Global Options -> Packages) or use the file ~/.config/rstudio/rstudio-prefs.json and put the following inside for https://cran.rstudio.com/.

{
    "cran_mirror": {
        "name": "Global (CDN)",
        "host": "RStudio",
        "url": "https://cran.rstudio.com/",
        "country": "us",
        "ok": 1,
        "secondary": ""
    }
}

Probably you already have other options set within so you can just add cran_mirror to the list.

The full file on my current system (RStudio Server 2022.02.2 Build 485, Ubuntu 20.04.4 LTS) looks like this:

{
    "initial_working_directory": "~",
    "margin_column": 120,
    "scroll_past_end_of_document": true,
    "highlight_r_function_calls": true,
    "rainbow_parentheses": true,
    "posix_terminal_shell": "bash",
    "default_project_location": "~",
    "jobs_tab_visibility": "shown",
    "source_with_echo": true,
    "save_workspace": "never",
    "load_workspace": false,
    "always_save_history": false,
    "data_viewer_max_columns": 500,
    "cran_mirror": {
        "name": "Global (CDN)",
        "host": "RStudio",
        "url": "https://cran.rstudio.com/",
        "country": "us",
        "ok": 1,
        "secondary": ""
    }
}
Stingo answered 15/6, 2022 at 12:10 Comment(5)
Running rstudio server from the rocker/ml image, this does change the repo that shows up in the tools window for me, but doesn't seem to have any other effects (getOption("repos") still points to the default repo defined in the image, and I have to change the repo manually when using install.packages())... did I miss a step somewhere?Armory
For me getOption("repos") returns "https://packagemanager.rstudio.com/cran/__linux__/focal/2021-05-17" and I'm running rocker/r-ver:4.0.5 at the core. Maybe that helps a bit with comparing what may be going on.Stingo
Actually I tried to change this now too and have run into the same problem. Did you find a solution @niclow?Stingo
Unfortunately not, my workaround has been to include a .Rprofile in a mounted project directory and restart R to load it immediately after starting the container.Armory
FWIW: I have now transitions to using this file /usr/local/lib/R/etc/Rprofile.site. I can change the link in there to the mirror I want and it persists.Stingo

© 2022 - 2024 — McMap. All rights reserved.