I solved this (and other issues) using renv and Authorization header.
renv helps you to create virtual environments for R projects, locking all packages (and their versions) used in the project.
You can read about authentication headers here: Web API Authentication Basic vs Bearer
You can make renv work with private repos by adding an .Rprofile
file to the project like so:
source("renv/activate.R")
local({
project_repos <- c(
CRAN = "https://cloud.r-project.org",
PRIVATE_CRAN = "https://your-private-cran.io"
)
options(repos = project_repos)
options(
renv.download.headers = function(url) {
if (grepl(paste0("^", project_repos["PRIVATE_CRAN"]), url))
return(c(Authorization = paste0("Bearer", Sys.getenv("AUTH_TOKEN"))))
})
This will automatically add the authorization header every time you access a file from the private CRAN.
If you made all the prerequisites, installing a package will look like this:
> renv::install("private_packge@2.7.2")
Retrieving 'https://your-private-cran.io/Cran-local/src/contrib/Archive/private_packge/2.7.2/private_packge_2.7.2.tar.gz' ...
OK [file is up to date]
Installing private_packge [2.7.2] ...
OK [built from source]
Moving private_packge [2.7.2] into the cache ...
OK [moved to cache in 1.3 milliseconds]
The prerequisites are:
- Install and configure renv for your project.
- Your private CRAN should be able to accept Authorization header.
- The authentication token is stored in an environment variable called
AUTH_TOKEN
.
- Note that I use Bearer authentication, it means I got a dedicated token from my private CRAN. If you wish to use
username:password
, then you need to use Basic authentication and the environment variable called AUTH_TOKEN
should be a base64 encoding of username:password
.