Run multiple instances of RStudio in a web browser
Asked Answered
L

5

18

I have RStudio server installed on a remote aws server (ubuntu) and want to run several projects at the same time (one of which takes lots of time to finish). On Windows there is a simple GUI solution like 'Open Project in New Window'. Is there something similar for rstudio server?

Simple question, but failed to find a solution except this related question for Macs, which offers

Run multiple rstudio sessions using projects

but how?

Limonite answered 8/4, 2014 at 13:18 Comment(0)
C
18

While running batch scripts is certainly a good option, it's not the only solution. Sometimes you may still want interactive use in different sessions rather than having to do everything as batch scripts.

Nothing stops you from running multiple instances of RStudio server on your Ubuntu server on different ports. (I find this particularly easy to do by launching RStudio through docker, as outlined here. Because an instance will keep running even when you close the browser window, you can easily launch several instances and switch between them. You'll just have to login again when you switch.

Unfortunately, RStudio-server still prevents you having multiple instances open in the browser at the same time (see the help forum). This is not a big issue as you just have to log in again, but you can work around it by using different browsers.

EDIT: Multiple instances are fine, as long as they are not on the same browser, same browser-user AND on the same IP address. e.g. a session on 127.0.0.1 and another on 0.0.0.0 would be fine. More importantly, the instances keep on running even if they are not 'open', so this really isn't a problem. The only thing to note about this is you would have to log back in to access the instance.

As for projects, you'll see you can switch between projects using the 'projects' button on the top right, but while this will preserve your other sessions I do not think the it actually supports simultaneous code execution. You need multiple instances of the R environment running to actually do that.

UPDATE 2020 Okay, it's now 2020 and there's lots of ways to do this.

For running scripts or functions in a new R environment, check out:

  • the callr package

  • The RStudio jobs panel

  • Run new R sessions or scripts from one or more terminal sessions in the RStudio terminal panel

  • Log out and log in to the RStudio-server as a different user (requires multiple users to be set up in the container, obviously not a good workflow for a single user but just noting that many different users can access the same RStudio server instance no problem.

Of course, spinning up multiple docker sessions on different ports is still a good option as well. Note that many of the ways listed above still do not allow you to restart the main R session, which prevents you from reloading installed packages, switching between projects, etc, which is clearly not ideal. I think it would be fantastic if switching between projects in an RStudio (server) session would allow jobs in the previously active project to keep running in the background, but have no idea if that's in the cards for the open source version.

Clyde answered 4/12, 2014 at 1:41 Comment(9)
One can take advantage of browser 'profiles' instead of different browsers as well.Mesh
Using different browser 'profiles' on the same browser or different browsers altogether does not work. As soon as you try to sign in on the 2nd browser/tab, a popup comes on the first tab stating "This browser was disconnected from the R session because another browser connected (only one browser at a time may be connected to an RStudio session). You may reconnect using the button below." Rstudio server version - 0.99.467.Unformed
here is an example command - docker run -d -p 9090:8787 -v /home/user/analytics:/home/dockeruser/analytics -v /mnt/libs:/home/dockeruser/libs --name mydocker -e USER=dockeruser -e PASSWORD=dockerpassw -e ROOT=TRUE rocker/hadleyverseLimonite
@Clyde Am I understanding you correctly, that using dockers to have multiple instances of the R environment is the only way to have multiple instances running simultaneously?Orphaorphan
@JamesHirschorn There's lots of ways to have multiple instances of the R environment. For doing so from a single RStudio session, just run "R" in any number of the "terminal" session tabs. Or check out callr. Also check out the RStudio "Jobs" panel for running scripts.Clyde
@Clyde My question wasn't clear: I have one RStudio server, and want to be able to have two sessions running at the same time accessed remotely in two different web browsers (or web browser windows), as the OP was asking. For this, is docker necessary?Orphaorphan
@JamesHirschorn I am running two instances of RStudio server on two different ports (rserver --www-port 8080 and rserver --www-port 8081). I have installed the Multi-Account Containers Plugin on Firefox. If I create two container tabs in Firefox, I can access both sessions simultaneouslyWhaler
@Whaler I tried your suggestion, and strarted two instances of rserver on two different ports. However, the behaviour is as if there is only one server running: If I access the other port, even from a different browser (Chrome), it disconnects the R session in the first port, and then shows me the first session I was working on in the Chrome browser on the second port.Orphaorphan
@JamesHirschorn yeh since I wrote this comment, I noticed that what I wrote was not enough... :( I am running rstudio-server in singularity (github.com/nickjer/rstudio-singularity, where I bind the config directory of each instance to another directory. You can try to start rserver with a different HOME variable or so to make it create config directories in different places? I will post my script in an answerWhaler
L
4

Often you don't need several instances of Rstudio - in this case just save your code in .R file and launch it using ubuntu command prompt (maybe using screen)

Rscript script.R

That will launch a separate R session which will do the work without freezing your Rstudio. You can pass arguments too, for example

# script.R - 
args <- commandArgs(trailingOnly = TRUE)

if (length(args) == 0) {
  start = '2015-08-01'
} else {
  start = args[1]  
}

console -

 Rscript script.R 2015-11-01
Limonite answered 11/6, 2014 at 14:29 Comment(1)
Yeah, but what if you want to run parts of scripts simultaneously?Mascagni
C
4

I think you need R Studio Server Pro to be able to log in with multiple users/sessions.

You can see the comparison table below for reference.

https://www.rstudio.com/products/rstudio-server-pro/

Clash answered 30/8, 2017 at 0:4 Comment(0)
S
1

Installing another instance of rstudio server is less than ideal.

Linux server admins, fear not. You just need root access or a kind admin.

Create a group to use: groupadd Rwarrior

Create an additional user with same home directory as your primary Rstudio login:

useradd -d /home/user1 user2

Add primary and new user into Rwarrior group:

gpasswd -a user2 Rwarrior

gpasswd -a user1 Rwarrior

Take care of the permissions for your primary home directory:

cd /home

chown -R user1:Rwarrior /home/user1

chmod -R 770 /home/user1

chmod g+s /home/user1

Set password for the new user: passwd user2

Open a new browser window in incognito/private browsing mode and login to Rstudio with the new user you created. Enjoy.

Stilwell answered 20/12, 2015 at 19:32 Comment(1)
This used to work well, but does not with the latest version of Rstudio!Debor
W
0

I run multiple RStudio servers by isolating them in Singularity instances. Download the Singularity image with the command singularity pull shub://nickjer/singularity-rstudio

I use two scripts:

run-rserver.sh:

  • Find a free port
#!/bin/env bash
set -ue

thisdir="$(dirname "${BASH_SOURCE[0]}")"

# Return 0 if the port $1 is free, else return 1
is_port_free(){
  port="$1"
  set +e
  netstat -an | 
    grep --color=none "^tcp.*LISTEN\s*$" | \
    awk '{gsub("^.*:","",$4);print $4}' | \
    grep -q "^$port\$"
  r="$?"
  set -e
  if [ "$r" = 0 ]; then return 1; else return 0; fi
}

# Find a free port
find_free_port(){
    local lower_port="$1"
    local upper_port="$2"
    for ((port=lower_port; port <= upper_port; port++)); do
      if is_port_free "$port"; then r=free; else r=used; fi
      if [ "$r" = "used" -a "$port" = "$upper_port" ]; then
        echo "Ports $lower_port to $upper_port are all in use" >&2
        exit 1
      fi
      if [ "$r" = "free" ]; then break; fi
    done
    echo $port
}

port=$(find_free_port 8080 8200)

echo "Access RStudio Server on http://localhost:$port" >&2


"$thisdir/cexec" \
    rserver \
    --www-address 127.0.0.1 \
    --www-port $port

cexec:

  • Create a dedicated config directory for each instance
  • Create a dedicated temporary directory for each instance
  • Use the singularity instance mechanism to avoid that forked R sessions are adopted by PID 1 and stay around after the rserver has shut down. Instead, they become children of the Singularity instance and are killed when that shuts down.
  • Map the current directory to the directory /data inside the container and set that as home folder (this step might not be nessecary if you don't care about reproducible paths on every machine)
#!/usr/bin/env bash
# Execute a command in the container
set -ue

if [ "${1-}" = "--help" ]; then 
echo <<EOF
Usage: cexec command [args...]

Execute `command` in the container. This script starts the Singularity
container and executes the given command therein. The project root is mapped 
to the folder `/data` inside the container. Moreover, a temporary directory
is provided at `/tmp` that is removed after the end of the script.

EOF
exit 0
fi

thisdir="$(dirname "${BASH_SOURCE[0]}")"
container="rserver_200403.sif"

# Create a temporary directory
tmpdir="$(mktemp -d -t cexec-XXXXXXXX)"
# We delete this directory afterwards, so its important that $tmpdir
# really has the path to an empty, temporary dir, and nothing else!
# (for example empty string or home dir)
if [[ ! "$tmpdir" || ! -d "$tmpdir" ]]; then
  echo "Error: Could not create temp dir $tmpdir"
  exit 1
fi
# check if temp dir is empty (this might be superfluous, see
# https://codereview.stackexchange.com/questions/238439)
tmpcontent="$(ls -A "$tmpdir")"
if [ ! -z "$tmpcontent" ]; then
  echo "Error: Temp dir '$tmpdir' is not empty"
  exit 1
fi

# Start Singularity instance
instancename="$(basename "$tmpdir")"

# Maybe also superfluous (like above)
rundir="$(readlink -f "$thisdir/.run/$instancename")"
if [ -e "$rundir" ]; then
  echo "Error: Runtime directory '$rundir' exists already!" >&2
  exit 1
fi
mkdir -p "$rundir"

singularity instance start \
  --contain \
  -W "$tmpdir" \
  -H "$thisdir:/data" \
  -B "$rundir:/data/.rstudio" \
  -B "$thisdir/.rstudio/monitored/user-settings:/data/.rstudio/monitored/user-settings" \
  "$container" \
  "$instancename"

# Delete the temporary directory after the end of the script
trap "singularity instance stop '$instancename'; rm -rf '$tmpdir'; rm -rf '$rundir'" EXIT
singularity exec \
  --pwd "/data" \
  "instance://$instancename" \
  "$@"
Whaler answered 26/5, 2020 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.