EStark
EStark

Reputation: 193

Change default library path for one session only in R

I created a project-specific library installing all required packages using:

install.packages("dplyr", lib = "<random folder>")

I want my script to use functions (for this session only) from that specific <random folder> using double colon, e.g dplyr::group_by()

Is there a way to do this?

! Let us ignore the option to use miniCRAN or renv for now.

Upvotes: 1

Views: 577

Answers (1)

Kevin Ushey
Kevin Ushey

Reputation: 21285

Functions in R that load packages do so by querying the active library paths, as provided by .libPaths():

> .libPaths()
[1] "/Users/kevinushey/Library/R/4.0/library"
[2] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library"

You can customize the library paths used in a particular session with the same function -- just pass it the library paths you wish to use. For example:

> dir.create("~/r-lib")
> .libPaths("~/r-lib")
> .libPaths()
[1] "/Users/kevinushey/r-lib"
[2] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library"

Then library(), :: and so on will load packages from those library paths.

Note that system-wide and site-wide library paths will always be appended to your requested library paths -- see ?.libPaths for more details.

Upvotes: 1

Related Questions