RStudio – IDE and Server powered by Singularity

By Staff

Oct 4, 2018 | Blog, How To Guides

RStudio is an Integrated Development Environment (IDE) for the R language. By containerizing RStudio, a scientist can leverage the reproducibility and portability of the Singularity platform, allowing them to build data visualizations and analysis tools and ensure that they behave exactly the same in a new environment.   

The Singularity definition (def) file is:

Bootstrap: library
From: debian:9

%environment
    LC_ALL="C"
    export LC_ALL

%post
    apt-get update
    apt-get -y install systemd systemd-sysv gdebi-core procps libssl1.1 ed wget curl libqt5webkit5 libqt5core5a
    apt-get -y install r-base r-base-dev

    curl https://download1.rstudio.org/rstudio-xenial-1.1.456-amd64.deb > /rstudio-1.1.456-amd64.deb
    apt-get -y install /rstudio-1.1.456-amd64.deb

    wget -O /rstudio-server-stretch-1.1.456-amd64.deb \
      https://download2.rstudio.org/rstudio-server-stretch-1.1.456-amd64.deb
    gdebi -n /rstudio-server-stretch-1.1.456-amd64.deb

    # Setup the "general" CRAN repo
    echo 'local({
 r <- getOption("repos")
 r["CRAN"] = "https://cran.us.r-project.org"
 options(repos = r)
})' >> /etc/R/Rprofile.site

    # We are setting up a normal user for access
    # Login: rstudio  Password: rstudio
    useradd -c "RStudio Account" -u 500 -d /home/rstudio -s /bin/bash rstudio
    echo "rstudio:rstudio" | /usr/sbin/chpasswd

    # Move systemd service file into place
    cp /usr/lib/rstudio-server/extras/systemd/rstudio-server.service /lib/systemd/system/
    systemctl enable rstudio-server

    # Remove unneeded source packages
    rm -f /rstudio-1.1.456-amd64.deb
    rm -f /rstudio-server-stretch-1.1.456-amd64.deb

%runscript
    /usr/bin/rstudio "$@"

%startscript
    /usr/lib/rstudio-server/bin/rserver

The definiton file affords some flexibility, allowing you to run RStudio several different ways:

Instance:

This will start up rstudio-server, on default port of 8787

$ singularity instance start rstudio.sif rs

Run:

 This will run rstudio IDE

$ singularity run rstudio.sif

Exec:

 Any program in the container, but specifically for running R directly.

$ singularity exec rstudio.sif R -f myfile.r

To build the image run:

$ sudo singularity build rstudio.sif rstudio.def

When you use run for the IDE, you may need to execute it like:


$ SINGULARITYENV_DISPLAY=${DISPLAY} \
  singularity run rstudio.sif

This is needed only if the DISPLAY environment variable does not automatically propagate from your host environment to the container.

For a modification of rstudio-server, you can create a rstudio.conf file, and bind mount it into the container. For example to run on port 9999:


$ echo "www-port=9999" > rserver.conf
$ singularity instance start \
  -B rserver.conf:/etc/rstudio/rserver.conf \
  rstudio.sif rs

You will then access your rstudio-server instance at:  https://localhost:9999

Join Our Mailing List

Related Posts

QA and Stability in Singularity

There are many different approaches that can be taken when building software. At one end of the spectrum is the extreme caution and conservatism that’s appropriate, for example, of safety critical code used in vehicles or in real-time operating systems. At the other...

read more

Improve Security in your CI/CD Workflows

In the development world, continuous integration is where members of a team integrate all their work frequently, for example, think of a team all working on the same code base, they are fixing bugs, implementing new features, so to prevent conflicts, all the code is...

read more

Pin It on Pinterest