LoginSignup
0
1

Docker上のRでプロットする(WSL2)

Last updated at Posted at 2023-05-26

Docker公式イメージr-baseを利用するとお手軽にRの実行環境を立ち上げられます。ただし、plotなどGUIを表示する関数はこのままでは使用できません。

> docker pull r-base:4.3.0
> docker run --interactive --tty --rm r-base:4.3.0
> barplot(0:10)
# 何も表示されない

Windowsでは、docker runコマンドに--envおよび--volumeオプションを加えることで、WSL2でplotのGUIを表示できます。

このコマンドはWSL2の環境変数とディレクトリを参照するため、必ずWSL2のシェルから実行してください。また、WSL2のシェルからdockerコマンドを利用するためには、DockerのSettings > Resources > WSL integration > Enable integration with my default WSL distroを有効にする必要があります。

$ docker run --interactive --tty --rm --env "DISPLAY=${DISPLAY:-:0.0}" --volume /mnt/wslg/.X11-unix/:/tmp/.X11-unix r-base:4.3.0
> barplot(0:10)

image.png

余談ですが、WSL2でDockerコンテナのGUIを表示させる方法はPythonなどでも役に立ちます。

$ docker pull python:3.11
$ docker run --interactive --tty --rm --env "DISPLAY=${DISPLAY:-:0.0}" --volume /mnt/wslg/.X11-unix/:/tmp/.X11-unix python:3.11
Python 3.11.3 (main, May 23 2023, 13:25:46) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter as tk
>>> tk.Tk().mainloop()

image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1