This will set the .libPaths()
by either adding a new path to it if
standAlone = FALSE
, or will concatenate c(libPath, tail(.libPaths(), 1))
if standAlone = TRUE
. Currently, the default is to make this new
.libPaths()
"sticky", meaning it becomes associated with the current
directory even through a restart of R. It does this by adding and/updating
the .Rprofile
file in the current directory. If this current directory
is a project, then the project will have the new .libPaths()
associated
with it, even through an R restart.
Arguments
- libPaths
A new path to append to, or replace all existing user components of
.libPath()
- standAlone
Logical. If
TRUE
, all packages will be installed to and loaded from thelibPaths
only. NOTE: IfTRUE
, THIS WILL CHANGE THE USER'S.libPaths()
, similar to e.g., thecheckpoint
package. IfFALSE
, thenlibPath
will be prepended to.libPaths()
during theRequire
call, resulting in shared packages, i.e., it will include the user's default package folder(s). This can be create dramatically faster installs if the user has a substantial number of the packages already in their personal library. DefaultFALSE
to minimize package installing.- updateRprofile
Logical or Character string. If
TRUE
, then this function will put several lines of code in the current directory's.Rprofile
file setting up the package libraries for this and future sessions. If a character string, then this should be the path to an.Rprofile
file. To reset back to normal, runsetLibPaths()
without alibPath
. Default:getOption("Require.updateRprofile", FALSE)
, meaningFALSE
, but it can be set with an option or within a single call.- exact
Logical. This function will automatically append the R version number to the
libPaths
to maintain separate R package libraries for each R version on the system. There are some cases where this behaviour is not desirable. Setexact
toTRUE
to override this automatic appending and use the exact, unalteredlibPaths
. Default isFALSE
- verbose
Numeric or logical indicating how verbose should the function be. If -1 or -2, then as little verbosity as possible. If 0 or FALSE, then minimal outputs; if
1
or TRUE, more outputs;2
even more. NOTE: inRequire
function, whenverbose >= 2
, also returns details as ifreturnDetails = TRUE
(for backwards compatibility).
Value
The main point of this function is to set .libPaths()
, which will
be changed as a side effect of this function. As when setting options
,
this will return the previous state of .libPaths()
allowing the user to
reset easily.
Details
This details of this code were modified from https://github.com/milesmcbain. A different, likely non-approved by CRAN approach that also works is here: https://stackoverflow.com/a/36873741/3890027.
Examples
if (FALSE) { # \dontrun{
if (Require:::.runLongExamples()) {
opts <- Require:::.setupExample()
origDir <- setwd(tempdir())
td <- tempdir()
setLibPaths(td) # set a new R package library locally
setLibPaths() # reset it to original
setwd(origDir)
# Using standAlone = FALSE means that newly installed packages
# will be installed
# in the new package library, but loading packages can come
# from any of the ones listed in .libPaths()
# will have 2 or more paths
otherLib <- file.path(td, "newProjectLib")
setLibPaths(otherLib, standAlone = FALSE)
# Can restart R, and changes will stay
# remove the custom .libPaths()
setLibPaths() # reset to previous; remove from .Rprofile
# because libPath arg is empty
Require:::.cleanup(opts)
unlink(otherLib, recursive = TRUE)
}
} # }