For this part of My Computer Workbench, I would like to explain the programming language I am using. Additionally, this section covers trick for R and how to use R with reproducibility in mind.
I mostly use R and Rstudio mostly for my downstream analysis and visualization. However, I have encountered program dependency problems when running R on Linux. As a solution, I have started using dockerized RStudio to create an RStudio server.
Here is the code for running dockerized RStudio.
cd /home/namthip
mkdir R
cd R
mkdir host-site-library
host-site-library
, to install custom R packageschmod a+w -R /home/namthip/R/host-site-library
sudo docker run \
-v /home/namthip:/home/rstudio/namthip \
-v /home/namthip/R/host-site-library:/usr/local/lib/R/host-site-library \
-e R_LIBS_USER=/home/namthip/R/host-site-library \
-e PASSWORD=<MY_PASSWORD> \
-p 8787:8787 \
bioconductor/bioconductor_docker:RELEASE_3_15
I pull a Docker image from bioconductor (as suggested by Tommy in his blog post). I think this Docker image is great. They explain clearly how to mount a volume (directory). I had tried other Docker images and found that some cannot mount the volume, while others cannot install new packages. This is my own RStudio server on my computer.
Please note that the path that you use in the dockerized R is the path in the container, otherwise dockerized R can’t find your destination. You can easily find the path by using Terminal in the dockerized R studio. Going to the directory you want, then use pwd
command to find the path of that directory.
One big problem I found with R version issues is the compatibility between the R version and package version. Sometimes we need a newer version, but sometimes the old one works fine. To switch between R versions, I use this trick.
Docker allows us to pull a specific version of a docker image, as shown in the picture (or this link.)
The version of R will be shown in the docker file for each version. From the picture below, “R version 4.4.1” is stated on line number 7.
From this point, I pull “R version 4.4.1” from docker, mount the same volume, and initialize a Dockerized R Studio and open RStudio Server using the command line below.
sudo docker run \
-v /home/namthip:/home/rstudio/namthip \
-v /home/namthip/R/host-site-library:/usr/local/lib/R/host-site-library \
-e R_LIBS_USER=/home/namthip/R/host-site-library \
-e PASSWORD=<MY_PASSWORD> \
-p 8787:8787 \
bioconductor/bioconductor_docker:RELEASE_3_19-R-4.4.1
After launching the container, we need to access RStudio Server by opening an internet browser and typing localhost:8787
. The login page for RStudio Server will appear. The username will always be rstudio
and the password is the one you defined.
If you start using R, it is good to do so following this suggestion “Best Practice for R”. I started using R quite from scratch; I had to fix so many things afterward.
I found a easier way to use R studio server in Docker. Previously, I have used very long command shown above in Step 3
sudo docker run \
-v /home/namthip:/home/rstudio/namthip \
-v /home/namthip/R/host-site-library:/usr/local/lib/R/host-site-library \
-e R_LIBS_USER=/home/namthip/R/host-site-library \
-e PASSWORD=<MY_PASSWORD> \
-p 8787:8787 \
bioconductor/bioconductor_docker:RELEASE_3_15
Instead of this, we will use one command line docker compose up -d
.
However, before that easy step, we have to prepare docker-compose.yml
file first. Use information below, save, and named it exactly this docker-compose.yml
file.
version: "3.8"
services:
rstudio:
image: bioconductor/bioconductor_docker:RELEASE_3_15
ports:
- "8787:8787"
volumes:
- /home/namthip:/home/rstudio/namthip
- /home/namthip/R/host-site-library:/usr/local/lib/R/host-site-library
environment:
- R_LIBS_USER=/home/namthip/R/host-site-library
- PASSWORD=<MY_PASSWORD>
docker-compose.yml
sudo docker compose up -d
Here, we already open Rstudio server in port 8787.
I can use Rstudio server by opening an internet browser and typing localhost:8787
.
docker-compose.yml
again. sudo docker compose down
Note: Don’t forget turn it off before close your computer.