It could be frustrating when I have to temporarily switch to another project to work with in RStudio Server while the current session is still useful to keep. However, multi-session support is part of RStudio Server Pro but not the free version.
In recent months, I have switched my development environment from RStudio Server to VSCode (see my previous posts about languageserver and vscode-R). In VSCode, I can choose to always send commands to active terminal. This allows me to start as many R sessions as I need, and still benefit from the features provided by vscode-R session watcher.
To have better multi-session experience in VSCode, I recommend that you use radian as the default terminal, and enable the following VSCode settings:
{
"r.alwaysUseActiveTerminal": true,
"r.bracketedPaste": true,
"r.sessionWatcher": true,
}
r.alwaysUseActiveTerminal
ensures that pressing Ctrl (Command) + Enter will always send to the active terminal rather than trying to create an R terminal if there is none.r.bracketedPaste
ensures the command chunk can be correctly sent to radian.r.sessionWatcher
enables R session watcher for better interactive functionality.
The simplest way to use multi-session is to create more than one terminals in VSCode and start an R session in each of them. However, if I quit VSCode, then all terminals and R sessions are gone. To make R sessions persistent, we need a terminal multiplexer. For example, screen and tmux are two most popular candicates on Linux and macOS. Personally, I perfer [tmux] for its stability and flexibility. On Windows, I don’t see an easy way to do the same unless you have acccess to a Linux virtual machine, Docker or Windows Subsystem for Linux (WSL).
To make R session watcher better work with multiple sessions I start in VSCode terminal,
I need to put some code into my ~/.Rprofile
:
Sys.setenv(TERM_PROGRAM = "vscode")
source(file.path(Sys.getenv(
if (.Platform$OS.type == "windows") "USERPROFILE" else "HOME"),
".vscode-R", "init.R")
)
We set environment variable TERM_PROGRAM = "vscode"
to ensure that R session watcher is initialized on each interactive session I
start in the terminal especially in a tmux window where this environment variable may be overwritten by tmux.
Following is a simple demonstration of using tmux to work with multiple R sessions in a remote server in VSCode:
To get started, install tmux from your software repository.
For example, on Ubuntu:
sudo apt install tmux
or on macOS (I assume you have Homebrew installed):
brew install tmux
By typing tmux
in the terminal, you should be able to start a tmux session and create a default tmux window.
Now any command you run in tmux window is persistent as long as the tmux window exists and the tmux server is alive.
If you are unfamiliar with tmux yet, I recommend that you go through the Tmux Cheat Sheet & Quick Reference.
tmux is highly customizable and tpm makes it much easier to use existing plugins. After some customization of my tmux config, I make my tmux look much more convinient and easier to work with than it first appears.
Now I can start a tmux session, create multiple tmux windows and start as many R sessions or other programs as I need. In a tmux window, I can also split it into several tmux panes and it is amazing that I could do the same thing in each pane.
Please note that my shortcut keys to split window are modified to Ctrl + b, -
and Ctrl + b, |
to do horizontal
split and vertical split, respectively. You may change the keys to whatever make you feel most comfortable with.
If the tmux session is on a remote server, then even if your editor crashes, the session is still alive. The editor process and R language server process are independent from the interactive R sessions user work with. Therefore, the code editor is much more responsive and it is always safe to restart the editor and language server.