This article summarizes how to build an RStuido environment. I wanted to manage it easily because it was a big deal, so I investigated how to manage it easily with Docker-compose.
I referred to the following page.
blog -Manage RSTUDIO using DOCKER-COMPOSE -Installation of RStudio Server-Data Analysis Study Group
Docker-hub
In this article, I will not explain in detail about Docker and how to install it. (Because there are many other easy-to-understand articles.) This time, we will build the environment using Dockerfile and Docker-compose.
Dockefile Configuration file for creating your own Docker image (like a recipe)
Docker-compose A tool for easily building Docker images and starting containers
Dockerfile On Dockerhub, an image file called Rocker for building an R environment is distributed. Among them, this time we will build the environment based on the image file in which the tidyverse library is already installed. (Because tidyverse libraries are very convenient)
In addition, Japanese fonts will be installed so that garbled characters will not occur even if Japanese is included in the legend such as graphs.
#Base image
FROM rocker/tidyverse:3.6.3
#Switch OS environment to Japanese locale
ENV LANG ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8
RUN sed -i '$d' /etc/locale.gen \
&& echo "ja_JP.UTF-8 UTF-8" >> /etc/locale.gen \
&& locale-gen ja_JP.UTF-8 \
&& /usr/sbin/update-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja"
RUN /bin/bash -c "source /etc/default/locale"
RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
#Install Japanese fonts
RUN apt-get update && apt-get install -y \
fonts-ipaexfont \
fonts-noto-cjk
Docker-compose Next is Docker-compose. Set any password in "environment". Also, in "volumes", the directory "R" that exists in the current directory of the host OS is mounted. By mounting, the files created in RStudio will remain there, and even if you delete the container, the files will not disappear.
version: '3'
services:
rstudio:
build: .
image: tidyverse:3.6.3_jp
container_name: tidyverse_jp
environment:
#Set any password
- PASSWORD=*****
ports:
- 8787:8787
volumes:
- ./R:/home/rstudio
tty: true
stdin_open: true
You can start the environment just by executing the following command in the directory where the Dockerfile and Docker-compose files created earlier exist.
docker-compose up -d
You can connect to the RS studio environment by launching the container and entering the browser with the URL below. http://localhost:8787 The default Username is "rstudio", and you can sign in by entering the Password you set earlier.
Since the Japanese font has been installed, check if there are any garbled characters.
library(ggplot2)
scatter <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width))
scatter + geom_point(aes(color=Species, shape=Species)) +
xlab("Iris calyx length") + ylab("Width of calyx") +
ggtitle("The length and width of the calyx")
I was able to confirm that Japanese was plotted without any problems.
Next In the future, I would like to upload articles related to R.
Recommended Posts