5  Working with R

To execute R code on Rossinante, you can use either the RStudio Server interface or directly the R console in the terminal. You can also use the Rscript command.

5.1 RStudio Server

Figure 5.1: RStudio Server interface

To open the RStudio Server interface, you first need to create an SSH tunneling (or port forwarding). This method consists in creating an encrypted SSH connection between a client (your laptop) and a server (Rossinante) through which a service (Rstudio Server) port (8787) can be relayed.

To create an SSH port forwarding, pass the -L option to the ssh client:

# Start an SSH port forwarding ----
ssh -L 8787:localhost:8787 rossinante

The first 8787 is your local port through RStudio Server will be relayed. The second 8787 is the port on Rossinante through RStudio Server is running. In short, localhost is your laptop. Instead of localhost you could have written 127.0.0.1. You can change the first 8787 for any number greater than 1024.

You are now connected to Rossinante.

To start the RStudio Server interface, open a web browser (Firefox, Chrome, etc.) and enter the following URL: http://localhost:8787.

After entering your Rossinante log in information, you are connected to a new RStudio Server instance. You can now use this interface as the one you know (RStudio Desktop).

RStudio Server has two buttons to close the interface: Sign out (B in Figure 5.1) and Quit current R session (C in Figure 5.1). If you click on Sign out, your session will be still active (in background) and you will have access to objects when you will log in again. You need to click on that button if you have launched time consuming analysis. If you click on Quit current R session you will stop all analyses and you were not able to access R objects. Click on that button to terminate your session.

Warning

If you stop your SSH (port forwarding) session, you will close the RStudio Server session. Before shutting down the SSH connection, click on Sign out or Quit current R session to securely close the RStudio Server instance.

Trick

If you have been disconnected from RStudio Server (network crash, power failure, etc.), you may not be able to restart RStudio Server (blank page). In that case, you need to kill your previous R sessions (still active) as follow:

# SSH connection to Rossinante ----
ssh rossinante

# Erase all active R sessions ----
killall -u jdoe rsession

# Hardcore method ----
rm -rf ~/.local/share/rstudio/sessions/active/session-*

5.2 R in the terminal

An alternative is to launch the R console directly from the terminal of Rossinante. And this is very easy.

# SSH connection to Rossinante ----
ssh rossinante

# Launch R console from terminal ----
R

## R version 4.2.3 (2023-03-15) -- "Shortstop Beagle"
## Copyright (C) 2023 The R Foundation for Statistical Computing
## Platform: x86_64-pc-linux-gnu (64-bit)
##
## R is free software and comes with ABSOLUTELY NO WARRANTY.
## You are welcome to redistribute it under certain conditions.
## Type 'license()' or 'licence()' for distribution details.
##
## R is a collaborative project with many contributors.
## Type 'contributors()' for more information and
## 'citation()' on how to cite R or R packages in publications.
##
## Type 'demo()' for some demos, 'help()' for on-line help, or
## 'help.start()' for an HTML browser interface to help.
## Type 'q()' to quit R.

To close your R session:

# Close R session ----
q("no")
The Rscript command

If you want, you can also use the command Rscript to run an R script (or an R expression) without opening the R console, directly from the terminal.

# Run an R expression (option '-e') ----
Rscript -e 'print("El ingenioso Hidalgo don Quijote de la Mancha")'
## [1] "El ingenioso Hidalgo don Quijote de la Mancha"

# Write an R script on the personal folder ----
echo 'print("El ingenioso Hidalgo don Quijote de la Mancha")' > ~/quijote.R

# Print 'quijote.R' file content ----
cat ~/quijote.R
## print("El ingenioso Hidalgo don Quijote de la Mancha")

# Run the R script ----
Rscript ~/quijote.R
## [1] "El ingenioso Hidalgo don Quijote de la Mancha"

# Remove the R script ----
rm ~/quijote.R

5.3 R in a screen

If you launch R in the terminal (or Rscript) and your code takes time to be computed, you cannot use the terminal until the computation is done. The job is running in foreground. Moreover, if your SSH connection is stopped, your R session is aborted and your work is lost.

The solution is to use a terminal multiplexer like screen or tmux. These tools open virtual terminals (screens) in the main terminal and you can navigate between screens and the main terminal. The idea is to create (attach) a new screen, launch your analysis, go back to the main terminal (detach), and work on other stuffs. If you stop your SSH connection, the analysis is still running, and if you make a new SSH connection to Rossinante, you can reattach the screen and resume your R session.

Let’s take an example with screen:

# SSH connection to Rossinante ----
ssh rossinante

# Attach (create) a new screen (named 'r_analysis', for instance) ----
screen -S r_analysis

# Open R console (in the new screen) ----
R

# Launch the analysis ----
source("script_analysis.R")

To detach from the screen session, type CTRL + A and D.

You can list running screens as follow:

# List running screens ----
screen -ls
## There is a screen on:
##         4002.r_analysis (Detached)

To reattach a screen session, use the following command:

# Reattach a screen ----
screen -r r_analysis

To close a screen, type exit in the screen or press CTRL + A and K.

5.4 R packages

Only base R packages are installed and shared among users. Each user has a personal R library in which he can install/update every R packages he wants (independently of other users). This library is located in ~/R/x86_64-pc-linux-gnu-library/4.2/.

NB – Some R packages require system libraries and some of them may be missing on Rossinante. Please contact the administrator to solve the issue.