RStudio

Pre-launch

  1. 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:

    Note

    Apptainer v1.4.2 comes pre-installed in the system, so you don’t need to use a modulefile for it.

    • Download image for R 4.5

    apptainer pull docker://rocker/rstudio:4.5
    [user@larcc-login1 ~]$ ls
    rstudio_4.5.sif
    
    • Download image for latest R

    [user@larcc-login1 ~]$ apptainer pull docker://rocker/rstudio:latest
    [user@larcc-login1 ~]$ ls
    rstudio_latest.sif
    
  2. Create the following batch script, modifying the CHANGE ME!!! section and slurm parameter as appropriate

     1#!/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=cpu384g
     7#SBATCH --mem=12069M
     8#SBATCH --output=/home/%u/rstudio-server.%j.out
     9#SBATCH --error=/home/%u/rstudio-server.%j.err
    10
    11## CHANGE ME!!! #############################################
    12# You can set this to a specific R version if you would like
    13# For example, VERSION_OF_R=4.5
    14VERSION_OF_R=latest
    15# Folder where the apptainer/singularity instances are saved
    16FOLDER_CONTAINER_INSTANCES=$HOME
    17############################################################
    18
    19CONTAINER=${FOLDER_CONTAINER_INSTANCES}/rstudio_${VERSION_OF_R}.sif
    20
    21if ! [ -f $CONTAINER ]; then
    22    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
    23fi
    24
    25# Create temporary directory to be populated with directories to bind-mount in the container
    26# where writable file systems are necessary. Adjust path as appropriate for your computing environment.
    27workdir=$(mktemp -d)
    28
    29# Set R_LIBS_USER to an existing path specific to rocker/rstudio to avoid conflicts with
    30# personal libraries from any R installation in the host environment
    31
    32cat > ${workdir}/rsession.sh <<END
    33#!/bin/sh
    34export R_LIBS_USER=\${HOME}/R/rocker-rstudio/${VERSION_OF_R}
    35mkdir -p "\${R_LIBS_USER}"
    36## custom Rprofile & Renviron (default is \$HOME/.Rprofile and \$HOME/.Renviron)
    37# export R_PROFILE_USER=/path/to/Rprofile
    38# export R_ENVIRON_USER=/path/to/Renviron
    39exec /usr/lib/rstudio-server/bin/rsession "\${@}"
    40END
    41
    42chmod +x ${workdir}/rsession.sh
    43
    44export APPTAINER_BIND="${workdir}/rsession.sh:/etc/rstudio/rsession.sh"
    45
    46# Do not suspend idle sessions.
    47# Alternative to setting session-timeout-minutes=0 in /etc/rstudio/rsession.conf
    48# https://github.com/rstudio/rstudio/blob/v1.4.1106/src/cpp/server/ServerSessionManager.cpp#L126
    49export APPTAINERENV_RSTUDIO_SESSION_TIMEOUT=0
    50
    51export APPTAINERENV_USER=$(id -un)
    52export APPTAINERENV_PASSWORD=$(openssl rand -base64 15)
    53# get unused socket per https://unix.stackexchange.com/a/132524
    54# tiny race condition between the python & singularity commands
    55readonly PORT=$(python3 -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')
    56cat 1>&2 <<END
    571. SSH tunnel from your workstation using the following command:
    58
    59   ssh -N -L 8787:`hostname`:${PORT} ${APPTAINERENV_USER}@zurada.rc.louisville.edu
    60
    61   and point your web browser to http://localhost:8787
    62
    632. log in to RStudio Server using the following credentials:
    64
    65   user: ${APPTAINERENV_USER}
    66   password: ${APPTAINERENV_PASSWORD}
    67
    68When done using RStudio Server, terminate the job by:
    69
    701. Exit the RStudio Session ("power" button in the top right corner of the RStudio window)
    712. Issue the following command on the login node:
    72
    73      scancel -f ${SLURM_JOB_ID}
    74END
    75
    76singularity exec --cleanenv \
    77                 --scratch /run,/tmp,/var/lib/rstudio-server \
    78                 --workdir ${workdir} \
    79                 ${CONTAINER} \
    80    rserver --www-port ${PORT} \
    81            --auth-none=0 \
    82            --auth-pam-helper-path=pam-helper \
    83            --auth-stay-signed-in-days=30 \
    84            --auth-timeout-minutes=0 \
    85            --server-user=$(whoami) \
    86            --rsession-path=/etc/rstudio/rsession.sh
    87printf 'rserver exited' 1>&2
    

Launch RStudio Server

  1. Assume the script from the pre-launch step is located at ~/rstudio-server.sbatch. Then, submit it to slurm with sbatch ~/rstudio-server.sbatch.

  2. The script will print to the standard error file (the one indicated in the #SBATCH --error option 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:cpusm01:48221 user@zurada.rc.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

    48221 is a randomly picked port (see line 51 of the script) and the password XnUPB9E0OVvjPfNH0nup is 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.