Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing RStudio environment in Docker container

Tags:

docker

r

rstudio

I use RStudio in Docker container in Windows 10 Pro. I use RStudio rocker/rstudio image pulled from https://hub.docker.com/u/rocker/.

To start container I executed command:

docker run -d -p 8787:8787 -v //c/Users/<My name>/Documents/R/Rprojects:/home/rstudio/ rocker/rstudio

And then I can access the server from my browser by following link: http://localhost:8787/. Everything works fine.

What I want to do is some customization of the RStudio environment. In particular, I changed Tools/Global options/Editor theme to 'Pastel on Dark'. I applied this option but it persists only when the container alive. When I restart the container custom options are all gone.

My projects are saved in the folder that I indicated when running container, but global options are not.

So, how can I save also global options on my hard drive. Maybe I need to expose another folder on my drive which will connect to container folder where RStudio saves global options?

Is it possible to predefine global options in dockerfile as a new layer in docker image?

like image 857
Alexander Avatar asked Apr 05 '18 08:04

Alexander


People also ask

How do I set an environment variable on an existing container?

Use the -e , --env , and --env-file flags to set simple (non-array) environment variables in the container you're running, or overwrite variables that are defined in the Dockerfile of the image you're running.

How do I set an environment variable in Docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

Does Docker work with R?

Docker is designed to enclose environments inside an image / a container. What this allows, for example, is to have a Linux machine on a Macbook, or a machine with R 3.3 when your main computer has R 3.5.


1 Answers

If, like me, you use an ephemeral container (using the --rm flag), then the container gets deleted when stopped. This is a good thing as it ensures a 100% clean environment every time but it means settings are not preserved from session to session.

Unlike many popular IDE, rstudio settings are not stored in a user-accessible transparent json, although they are working on it.

A workaround is copying over the settings to the right location:

  • keybindings: /home/rstudio/.R/rstudio/keybindings/rstudio_bindings.json
  • general settings (such as theme): /home/rstudio/.rstudio/monitored/user-settings

To set it up:

  1. Launch rstudio in a container
  2. Set your desired settings
  3. Back up the 2 files listed above somewhere on your host.
  4. Copy over the config files every time you start rstudio - see script below.

I have created a quick launch shortcut pointing to the following script which is easily adapted. It starts a container named rstudio and copies over the settings I have backed up (in my case from /home/asac/projects/rstudio-config)

#!/bin/bash                                     
                         
echo Running rstudio on localhost:8787                 
docker run -d --rm -p 8787:8787 -e PASSWORD=<pwd> \                       
-v /home/asac/projects:/home/rstudio/projects \                          
-v /home/asac/data:/home/rstudio/data \                                  
--name rstudio asachet/shiny-dev                                         
                                                                         
echo Copying over rstudio settings                                       
docker exec rstudio mkdir /home/rstudio/.R/rstudio/keybindings -p        
docker cp /home/asac/projects/rstudio-config/user-settings rstudio:/home/rstudio/.rstudio/monitored/user-settings
docker cp /home/asac/projects/rstudio-config/rstudio_bindings.json rstudio:/home/rstudio/.R/rstudio/keybindings/rstudio_bindings.json
                                                                         
echo Launching browser                                                   
xdg-open http://localhost:8787      

NEW IN 2020

With RStudio v1.3, there is a new file ~/.config/rstudio/rstudio-prefs.json which controls all of the settings. You can copy it between machines or hand-edit it.

More details in the RStudio Server PR which got ported to RStudio in version 1.3.

like image 138
asachet Avatar answered Sep 28 '22 10:09

asachet