RStudio Server
Overview
With this recipe you’ll learn how to create a Viash component that allows you to easily edit files in RStudio by running RStudio Server, the browser based interface of RStudio. This recipe consists of a config file and a bash script to create a component.
Ingredients
For this recipe, you’ll need:
Creating the component
Config file
Create a new folder named rstudio_server and create a new file named config.vsh.yaml inside of it. Add the following as its content:
functionality:
name: rstudio_server
version: 1.0
description: "Run RStudio Server and work in a given directory"
resources:
- type: bash_script
path: script.sh
arguments:
- name: "--io"
alternatives: ["-i"]
type: file
description: Directory to mount
direction: input
default: ./
platforms:
- type: docker
image: rocker/tidyverse:4
port: [ "8787:8787" ]
Here’s a quick rundown:
- A single resource is used, a bash script named script.sh.
- This component takes a single file argument named –io. This is the directory where the files you want to edit are located and will be passed to RStudio Server to use as its working directory. By default, this will be the folder from which you run the component.
- This component runs on the Docker platform. It’s using the rocker/tidyverse image as its base, which comes with RStudio and RStudio Server. Port 8787 gets exposed to make it available from outside the container.
Script
Next up is the bash script. Create a file named script.sh in the rstudio_server directory and add this as its content:
#!/bin/bash
set -e
echo "Starting rstudio from workdir $par_io"
echo "server-working-dir=$par_io" >> /etc/rstudio/rserver.conf
echo "auth-none=1" >> /etc/rstudio/rserver.conf
echo "USER=rstudio" >> /etc/environment
ln -s $par_io /home/rstudio/workdir
echo "Trying to run RStudio Server on http://localhost:8787"
/init
In a nutshell:
set -e
will stop the execution of the script if any errors are encountered.- The first
echo
call is simply a reminder of the directory you passed. - The three last
echo
calls are used to set up RStudio Server. The first one sets the working directory, the second one disables authentication and the last one adds an environment variable. ln
creates a symbolic link from /home/rstudio/workdir to the given directory./init
starts RStudio Server.
Running the component
Run the component by calling viash run
on the config file and passing
a directory path:
viash run config.vsh.yaml -- --io ~
You should see output similar to this:
Starting rstudio from workdir [/viash_automount/PATH-YOU-PASSED]
Trying to run RStudio Server on http://localhost:8787
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] userconf: executing...
...
[cont-init.d] userconf: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
Now navigate to http://localhost:8787 in a web browser and you’ll be greeted by the web interface of RStudio. This is a full version of RStudio as you’d expect it from the desktop application.