0

Normally I install R packages with:

install.packages("my_package", repos = "https://cloud.r-project.org")

Is it possible to specify a CRAN mirror with an environment variable

export CRAN_URL="https://cloud.r-project.org"

such that I can call install.packages without repo? e.g.,

install.packages("my_package")

I am open to using pak, as well.

I am aware i can modify my .Rprofile, but I want a solution that is more portable (executable on multiple machines i might login to, where env variables are defined).

I found something about R_CRAN_WEB and R_CRAN_SRC, but setting these doesn't seem to change anything for me.

I apologize if this has already been answered. This seems like a basic question, but I can't find an answer in the following questions:

william_grisaitis
  • 5,170
  • 3
  • 33
  • 40

1 Answers1

3

R has its own set of "options" which you can learn more about via help(options).

In particular, among these is one called repos and by setting its value at startup you can set your repositories. For example in Rprofile.site, or your ~/.Rprofile you could add

## We set the cloud mirror, which is 'network-close' to everybody, as default
local({
    r <- getOption("repos")
    r["CRAN"] <- "https://cloud.r-project.org"
    options(repos = r)
})

(and this was actually taken from Rprofile.site for Debian R package I look after) and then you can check in your session

> options("repos")
$repos
                        CRAN 
"https://cloud.r-project.org" 

> 

so that install.packages() automagically picks a network-close 'cloud' mirror from the content-delivery network (CDN).

This behavior is documented, and even though you may prefer to choose this with an environment variable -- options("repos") it is. Now, you could add code to pick the value you desire from an environment variable and assign it in your ~/.Rprofile and that would be one way. R simply does not do it for you by default.

See help(Startup) for a very detailed discussion of the various startup files.

(Edit: I had accidentally copied cran.r-project.org in from my machine pointing directly at Vienna, a better more portable setting is to use the CDN mirror system cloud.r-project.org as described in the text. Now corrected in the code snippet too.)

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • thanks for your reply. is this the only way? the main reason I asked this question was to avoid having to use config files in a home folder. environment variables are much easier to set and configure. (e.g. if i run R in a docker container, or run things on a remote host, or run things in a browser with r-wasm, where a traditional file system might not exist) – william_grisaitis May 02 '23 at 14:51
  • @william_grisaitis In short, no, not by R. It expects you to write it down in startup file. If you think writing it down for the shell to read as an environment variable is fundamentally better then you could implement a layer that has the shell read its config, set an environment variable so that R can pick it up and set `options(repos=....)`. – Dirk Eddelbuettel May 02 '23 at 14:55
  • thank you for explaining. i apologize for misinterpreting your answer. – william_grisaitis May 02 '23 at 20:23
  • 1
    No worries. I actually think R should just default to `https://cloud.r-project.org` as this 'content-delivery network' already tries to find a local and fast node so we can in fact have a "global" default that is still locally (near) optimal and a perfect fallback. – Dirk Eddelbuettel May 02 '23 at 21:01