Skip to content

Commit 95361d7

Browse files
authored
Merge pull request #19 from Sydney-Informatics-Hub/R-tutorial
Add a tutorial for installing R packages in the Jupyter Lab environment
2 parents 5aead78 + 4ac9f4a commit 95361d7

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

_quarto.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ website:
4747
href: notebooks/data_transfer.html
4848
- text: "How to run a terminal using the Command Line Interface (CLI)"
4949
href: notebooks/CLI.html
50+
- text: "How to self-install R packages"
51+
href: notebooks/R_tutorial.html
5052
cookie-consent:
5153
type: express
5254
style: simple

fig/jupyter_landing_page.png

776 KB
Loading

fig/r_load_xgboost_failed.png

129 KB
Loading

fig/r_run_xgboost.png

919 KB
Loading

notebooks/R_tutorial.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
![Jupyter Lab Landing Page](../fig/jupyter_landing_page.png)
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+
![check if the xgboost library is installed](../fig/r_load_xgboost_failed.png){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+
![load xgboost library in an R notebook](../fig/r_run_xgboost.png)

0 commit comments

Comments
 (0)