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:
[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"
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.
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")
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.
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","/home/wsuser","YOUR_PATH",.libPaths()))
Note that:
- 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.- The preferred default location in RStudio is
/home/wsuser/R/library
. However, this path might not exist in Shiny deployments. To run the same.libPaths
command in both RStudio and in Shiny deployments, add/home/wsuser
as the second element in Shiny deployments. ThelibPaths()
command automatically eliminates duplicates and directories that don't exist.
- The preferred default location in RStudio is
- 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, replaceYOUR_PATH
by a path like/mnts/YourRpackages
.
Parent topic: RStudio