Adding custom R packages
You can install the following types of R packages to use in RStudio:
To add custom RStudio packages, you must have the correct permissions to install package types.
You can use the following command to see the library locations for these package types:
> .libPaths()
This command returns the following locations:
Example output for runtime 23.1 for watsonx release 2.0.0:
[1] "/home/wsuser/R/library"
[2] "/cc-home/_global_/R"
[3] "/opt/conda/envs/R-4.2-rt23.1/lib/R/extra-library"
[4] "/opt/ibm/spark/R/lib"
[5] "/opt/conda/envs/R-4.2-rt23.1/lib/R/site-library"
[6] "/opt/conda/envs/R-4.2-rt23.1/lib/R/library"
[7] "/opt/not-FIPS-compliant/R/library"
From watsonx release 2.0.1, .libPaths()
output includes the runtime specific library path for both local and global libraries. Here is an example output for runtime 24.1:
[1] "/home/wsuser/R/library/24.1" "/home/wsuser/R/library"
[3] "/cc-home/_global_/R/24.1" "/cc-home/_global_/R"
[5] "/opt/conda/envs/R-RT24.1/lib/R/extra-library" "/opt/ibm/spark/R/lib"
[7] "/opt/conda/envs/R-RT24.1/lib/R/site-library" "/opt/conda/envs/R-RT24.1/lib/R/library"
[9] "/opt/not-FIPS-compliant/R/library"
Global R packages
Required permissions: You must be a Cloud Pak for Data cluster administrator to install global R packages to the /cc-home/_global_/R
folder.
For watsonx release 2.0.0:
You can install global R packages to the /cc-home/_global_/R
folder using the following procedure:
install.packages("prophet", lib="/cc-home/_global_/R")
For watsonx release 2.0.1:
You can install global R packages to the /cc-home/_global_/R
folder using the following procedure:
install.packages("prophet", lib="/cc-home/_global_/R",Sys.getenv("RUNTIME_TRAIN"),sep="/")
Additionaly, you can select the /cc-home/_global_/R
folder as the default installation location for all users of RStudio. After you have installed all global R packages to this location, RStudio users can load packages from this
location to use in their RStudio sessions.
For watsonx release 2.0.0: If you want to use global R packages in a deployed Shiny app, add this line to your code:
.libPaths(c("/cc-home/global/R/",.libPaths()))
User R packages
All RStudio users can install custom R packages to their home folder at /home/wsuser/R/library
. User R packages are not shared with other users or with other projects.
User R packages take precedence over global and system R packages in the runtime image.
System R packages
System R packages are pre-installed to the /opt/conda/envs/R-4.2-rt23.1/lib/R/site-library
and the /opt/conda/envs/R-4.2-rt23.1/lib/R/library
folders.
RStudio users can only load packages from those locations. To override system R packages, users need to install packages as global or user R packages.
R packages located in a Storage volume
To install packages located in any user-defined storage volume:
- Create a corresponding Connection asset in the project. For details, refer to Managing assets in projects.
- Restart RStudio if it is running.
- Install the package. For example:
install.packages("zoo", lib="/mnts/YourRpackages")
To load a package located in a user-defined storage volume, use this sample code:
library("zoo", lib.loc="/mnts/YourRpackages")
Shiny apps added to a deployment space can also use packages located in a storage volume. The deployment space must have a valid Connection asset for the storage volume. For details, refer to Deploying Shiny apps from a deployment space.
How to add a custom path to .libPaths
If you have custom packages in a storage volume, you can register their custom destination by using the .libPaths()
command in your applications. This will enable you to load a package by using a plain library
command,
without having to provide the lib.loc
parameter each time.
To register a custom path in your application, use the following template:
.libPaths(c("/home/wsuser/R/library","<your path>",.libPaths()))
- A plain
install.packages()
command writes files to the first path from.libPaths()
by default. - It is recommended that you begin the list of paths in .
libPaths()
with a local directory that gets reset when you start a new RStudio session or Shiny deployment. This makes sure that your applications do not accidentally change packages in a permanent directory. - In user-defined storage volumes, the mount path usually begins with
/mnts
followed by the volume name. To register a custom path for user-defined storage volumes, replace<your path>
with a real path. For example/mnts/YourRpackages
.
Parent topic: RStudio