RStudio
Pre-launch
Login to the cluster and download a singularity/apptainer image from the rocker project (https://hub.docker.com/r/rocker/rstudio/tags). Pick one with the desired version of R. For example:
Download image for R 4.5
[user@larcc-login1 ~]$ module load apptainer/1.3.4-gcc-11.5.0 [user@larcc-login1 ~]$ apptainer pull docker://rocker/rstudio:4.5 [user@larcc-login1 ~]$ ls rstudio_4.5.sif
Download image for latest R
[user@larcc-login1 ~]$ module load apptainer/1.3.4-gcc-11.5.0 [user@larcc-login1 ~]$ apptainer pull docker://rocker/rstudio:latest [user@larcc-login1 ~]$ ls rstudio_latest.sif
Create the following batch script, modifying the
CHANGE ME!!!section and slurm parameter as appropriate1#!/bin/sh 2#SBATCH --job-name="rstudio-server" 3#SBATCH --time=08:00:00 4#SBATCH --signal=USR2 5#SBATCH --nodes=1 6#SBATCH --partition=compute 7#SBATCH --mem=515002 8#SBATCH --output=/home/%u/rstudio-server.%j.out 9#SBATCH --error=/home/%u/rstudio-server.%j.err 10 11module load apptainer/1.3.4-gcc-11.5.0 12 13## CHANGE ME!!! ############################################# 14# You can set this to a specific R version if you would like 15# For example, VERSION_OF_R=4.5 16VERSION_OF_R=latest 17# Folder where the apptainer/singularity instances are saved 18FOLDER_CONTAINER_INSTANCES=$HOME 19############################################################ 20 21CONTAINER=${FOLDER_CONTAINER_INSTANCES}/rstudio_${VERSION_OF_R}.sif 22 23if ! [ -f $CONTAINER ]; then 24 printf "Please run apptainer pull docker://rocker/rstudio:${VERSION_OF_R} and, once the download finishes, make sure the rstudio_${VERSION_OF_R}.sif file is placed in ${FOLDER_CONTAINER_INSTANCES}" 1>&2 25fi 26 27# Create temporary directory to be populated with directories to bind-mount in the container 28# where writable file systems are necessary. Adjust path as appropriate for your computing environment. 29workdir=$(mktemp -d) 30 31# Set R_LIBS_USER to an existing path specific to rocker/rstudio to avoid conflicts with 32# personal libraries from any R installation in the host environment 33 34cat > ${workdir}/rsession.sh <<END 35#!/bin/sh 36export R_LIBS_USER=\${HOME}/R/rocker-rstudio/${VERSION_OF_R} 37mkdir -p "\${R_LIBS_USER}" 38## custom Rprofile & Renviron (default is \$HOME/.Rprofile and \$HOME/.Renviron) 39# export R_PROFILE_USER=/path/to/Rprofile 40# export R_ENVIRON_USER=/path/to/Renviron 41exec /usr/lib/rstudio-server/bin/rsession "\${@}" 42END 43 44chmod +x ${workdir}/rsession.sh 45 46export APPTAINER_BIND="${workdir}/rsession.sh:/etc/rstudio/rsession.sh" 47 48# Do not suspend idle sessions. 49# Alternative to setting session-timeout-minutes=0 in /etc/rstudio/rsession.conf 50# https://github.com/rstudio/rstudio/blob/v1.4.1106/src/cpp/server/ServerSessionManager.cpp#L126 51export APPTAINERENV_RSTUDIO_SESSION_TIMEOUT=0 52 53export APPTAINERENV_USER=$(id -un) 54export APPTAINERENV_PASSWORD=$(openssl rand -base64 15) 55# get unused socket per https://unix.stackexchange.com/a/132524 56# tiny race condition between the python & singularity commands 57readonly PORT=$(python3 -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()') 58cat 1>&2 <<END 591. SSH tunnel from your workstation using the following command: 60 61 ssh -N -L 8787:`hostname | sed 's/larcc-/larcc-hs-/'`:${PORT} ${APPTAINERENV_USER}@larcc.hpc.louisville.edu 62 63 and point your web browser to http://localhost:8787 64 652. log in to RStudio Server using the following credentials: 66 67 user: ${APPTAINERENV_USER} 68 password: ${APPTAINERENV_PASSWORD} 69 70When done using RStudio Server, terminate the job by: 71 721. Exit the RStudio Session ("power" button in the top right corner of the RStudio window) 732. Issue the following command on the login node: 74 75 scancel -f ${SLURM_JOB_ID} 76END 77 78singularity exec --cleanenv \ 79 --scratch /run,/tmp,/var/lib/rstudio-server \ 80 --workdir ${workdir} \ 81 ${CONTAINER} \ 82 rserver --www-port ${PORT} \ 83 --auth-none=0 \ 84 --auth-pam-helper-path=pam-helper \ 85 --auth-stay-signed-in-days=30 \ 86 --auth-timeout-minutes=0 \ 87 --server-user=$(whoami) \ 88 --rsession-path=/etc/rstudio/rsession.sh 89printf 'rserver exited' 1>&2
Launch RStudio Server
Assume the script from the pre-launch step is located at
~/rstudio-server.sbatch. Then, submit it to slurm withsbatch ~/rstudio-server.sbatch.The script will print to the standard error file (the one indicated in the
#SBATCH --erroroption in the sbatch file) the instructions on how to connect to the RStudio web instance. Example output:1. SSH tunnel from your workstation using the following command: ssh -N -L 8787:larcc-hs-cpu1:48221 user@larcc.hpc.louisville.edu and point your web browser to http://localhost:8787 2. log in to RStudio Server using the following credentials: user: user password: XnUPB9E0OVvjPfNH0nup
Note
48221is a randomly picked port (see line 51 of the script) and the passwordXnUPB9E0OVvjPfNH0nupis randomly generated (see line 49 of the script). The ssh command in line 3 of the example output uses the login node as a proxy and reach the allocated compute node.