|
| 1 | +# Tutorial: Self-installing R packages |
| 2 | +In this tutorial, we will demonstrate how to install R packages in your Run:AI workload. This is particularly useful when you need specific packages that are not included in the pre-installed image. |
| 3 | + |
| 4 | +## Create a Run:AI Jupyter Lab workload |
| 5 | +Follow the instructions in the [Jupyter Lab tutorial](./jupyter_tutorial.md) to create a new workload in Run:AI and connect to the Jupyter Lab interface. You will see the following landing page when the workload is created successfully: |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +There are three ways of accessing R in this default Jupyter Lab environment: |
| 10 | + |
| 11 | +- R notebook |
| 12 | +- R Console |
| 13 | +- Terminal |
| 14 | + |
| 15 | +In this tutorial, we will demonstrate how to use the Terminal application to install R packages. |
| 16 | + |
| 17 | +## Install R packages via Terminal |
| 18 | +1. Open a new Terminal window by clicking on the Terminal icon in the Jupyter Lab interface. |
| 19 | +2. Create a directory in your PVC to store any newly installed R packages. This ensures that the packages persist across sessions and are not removed after the workload is stopped running. |
| 20 | + |
| 21 | + You can do this by creating a subdirectory in `/scratch/${RUNAI_PROJECT}`. For example: |
| 22 | + ```bash |
| 23 | + mkdir -p /scratch/${RUNAI_PROJECT}/my_username/R_libs |
| 24 | + ``` |
| 25 | + which will create a directory named `R_libs` in the project PVC. You may want to change `my_username` to your own username (*e.g.*, your unikey) or preferred directory name. |
| 26 | +3. Set the `R_LIBS_USER` environment variable to point to this new directory. |
| 27 | + ```bash |
| 28 | + export R_LIBS_USER=/scratch/${RUNAI_PROJECT}/my_username/R_libs |
| 29 | + ``` |
| 30 | +4. Start R by executing the command `R` in the terminal. |
| 31 | +5. Check if the libary has already been installed in the image. You can either run |
| 32 | + ```R |
| 33 | + installed.packages() |
| 34 | + ``` |
| 35 | + to print out the full list of installed packages, or try loading the package using |
| 36 | + ```R |
| 37 | + library(package) |
| 38 | + ``` |
| 39 | + You need to replace `package` with the name of the package you want to check. If the package is not installed, you will receive an error message: |
| 40 | + |
| 41 | + {width=60% fig-align="left"} |
| 42 | +5. Install the desired R package using the `install.packages()` function. For example: |
| 43 | + ```R |
| 44 | + install.packages("xgboost", repos="http://cran.ms.unimelb.edu.au/") |
| 45 | + ``` |
| 46 | + This command will download and install the `xgboost` package into the directory specified by `R_LIBS_USER`. |
| 47 | + |
| 48 | +## Verify the installation |
| 49 | +You can verify the installation by loading the package in R: |
| 50 | +```R |
| 51 | +library("xgboost") |
| 52 | +``` |
| 53 | +You can also use the libary in an R notebook or R console by setting the `lib` variable (*e.g.*, `lib="/scratch/sihnextgen/jfan0290/R_libs"`) when loading the library: |
| 54 | + |
| 55 | + |
0 commit comments