Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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/

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.

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" \
  "$@"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top