Richard J. Acton
Richard J. Acton

Reputation: 915

Setting default `.libPaths()` for multiple versions of R

I work in an environment with multiple versions of R available, managing my libraries can be something of a hassle as I have to switch library locations to avoid issues with packages being built under different versions of R.

Is there a way to change my default library location in .libPaths() automatically depending on the version of R i'm using?

Upvotes: 1

Views: 1121

Answers (3)

jakob-r
jakob-r

Reputation: 7292

A slightly shorter version of Richard J. Acton's solution:

version <- paste0(R.Version()$major,".",R.Version()$minor)
libPath <- path.expand(file.path("~/.R/libs", version))
if(!dir.exists(libPath)) {
  warning(paste0("Library for R version '", version, "' will be created at: ", libPath ))
  dir.create(libPath, recursive = TRUE)
}
.libPaths(c(libPath, .libPaths()))

Upvotes: 1

Richard J. Acton
Richard J. Acton

Reputation: 915

I found this trick useful.

keep your locally installed R libraries in a directory named for their version, detect the version when R starts and set .libPaths() accordingly

Edit your .Rprofile file in you home directory to contain something like the following:

version <- paste0(R.Version()$major,".",R.Version()$minor) 

if (version == "3.5.2") {
    .libPaths( c("/path/to/Rlibs/3.5.2", .libPaths()) )
} else if (version == "3.4.3") {
    .libPaths( c("/path/to/Rlibs/3.4.3", .libPaths()) )
}

Updated version that automatically creates a new library folder for a new R version if one is detected, also throws a warning when it does this in case you accidentally loaded a new R version when you weren't intending to.

# Set Version specific local libraries
## get current R version (in semantic format)
version <- paste0(R.Version()$major,".",R.Version()$minor) 
## get username
uname <- Sys.getenv("USER") # USERNAME on windows because why make it easy?
## generate R library path for parent directory
libPath <- paste0("/home/", uname, "/Rlibs/")

setLibs <- function(libPath, ver) {
        libfull <- paste0(libPath, ver) # combine parent and version for full path
        if(!dir.exists(libfull)) { # create a new directory for this R version if it does not exist
                # Warn user (the necessity of creating a new library may indicate an in advertant choice of the wrong R version)
                warning(paste0("Library for R version '", ver, "' Does not exist it will be created at: ", libfull ))
                dir.create(libfull)
        }
        .libPaths(c(libfull, .libPaths()))
}

setLibs(libPath, version)

Upvotes: 4

ricardo
ricardo

Reputation: 8435

I have a standard path structure with a new folder added for each version number, and a folder called pax for packages. To do this you just need to add the following to your .Rprofile.

Rver <- paste0(R.Version()$major, ".", R.Version()$minor)
.libPaths(file.path(paste0(
        "C:/Users/abcd/R/", Rver, "/pax")))

This means you aren't going to grow a forest of if statements if you have multiple versions.

Upvotes: 0

Related Questions