Skip to content

Instantly share code, notes, and snippets.

@mokztk
Last active July 8, 2020 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokztk/ab52f8381790f610d12953b0a4bc73c7 to your computer and use it in GitHub Desktop.
Save mokztk/ab52f8381790f610d12953b0a4bc73c7 to your computer and use it in GitHub Desktop.
Public RStudio Package Manager ( https://packagemanager.rstudio.com/ ) のコンパイル済みバイナリパッケージを使って ubuntu:18.04 に R 4.0.2+RStudio 1.3.x+Radian+頻用パッケージを導入した作業用イメージ
# R 4.0.2 and RStudio server with some packages.
# R and recommended packages from MRAN snapshot on 2020-06-29
# additional packages from Public RStudio Package Manager #297 (2020-06-29)
FROM ubuntu:18.04
# Change environment to Japanese (Character, DateTime, and Fonts)
ENV DEBCONF_NOWARNINGS=yes
RUN set -x \
# install Japanese lung pack (and locales)
&& apt-get update \
&& apt-get install -y language-pack-ja-base language-pack-ja \
# locale settings
&& 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" \
&& /bin/bash -c "source /etc/default/locale" \
&& ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
# install CJK font
&& apt-get install -y --no-install-recommends fonts-noto-cjk \
# cleaning
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install R and RStudio server
ENV MRAN=https://cran.microsoft.com/snapshot/2020-06-29
RUN set -x \
# install essential packages
&& apt-get update \
&& apt-get install -y \
build-essential \
perl \
curl \
openssl \
ca-certificates \
gdebi-core \
psmisc \
python3-pip \
ssh \
git \
# install Microsoft IT SSL SHA2 CA Certificate
&& curl --output "msitwww2(1).crt" "http://www.microsoft.com/pki/mscorp/msitwww2(1).crt" \
&& openssl x509 -in "msitwww2(1).crt" -inform DER -out msitwww2.crt -outform PEM \
&& cp msitwww2.crt /usr/share/ca-certificates/ \
&& echo "msitwww2.crt" >> /etc/ca-certificates.conf \
&& update-ca-certificates \
&& rm *.crt \
# install latest version of R 4.0.2 (4.0.2-1.1804.0 / 2020-06-23)
&& echo "deb ${MRAN}/bin/linux/ubuntu bionic-cran40/" > /etc/apt/sources.list.d/cran.list \
&& apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 51716619E084DAB9 \
&& apt-get update \
&& apt-get install -y r-base littler \
# Install RStudio Server
&& curl --output "rstudio-server.deb" https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.3.959-amd64.deb \
&& gdebi -n rstudio-server.deb \
&& rm *.deb \
# Symlink pandoc & standard pandoc templates for use system-wide
# REF: https://github.com/rocker-org/rocker-versioned/blob/master/rstudio/3.6.3.Dockerfile
&& ln -s /usr/lib/rstudio-server/bin/pandoc/pandoc /usr/local/bin \
&& ln -s /usr/lib/rstudio-server/bin/pandoc/pandoc-citeproc /usr/local/bin \
&& git clone --recursive --branch 2.9.2.1 https://github.com/jgm/pandoc-templates \
&& mkdir -p /opt/pandoc/templates \
&& cp -r pandoc-templates*/* /opt/pandoc/templates \
&& rm -rf pandoc-templates* \
&& mkdir /root/.pandoc \
&& ln -s /opt/pandoc/templates /root/.pandoc/templates \
# install system prerequisites for the Repo's packages
&& apt-get install -y --no-install-recommends \
graphviz \
libgraphviz-dev \
imagemagick \
libmagick++-dev \
libcairo2-dev \
libcurl4-openssl-dev \
libfontconfig1-dev \
libfreetype6-dev \
libgl1-mesa-dev \
libglu1-mesa-dev \
librsvg2-dev \
libssh2-1-dev \
libssl-dev \
libxft-dev \
libxml2-dev \
# cleaning
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
EXPOSE 8787
# Install additional packages
ENV R_INSTALL_STAGED=false
ENV RSPM=https://packagemanager.rstudio.com/all/__linux__/bionic/297
RUN set -x \
# install precompiled binary packages using Public RStudio Package Manager #297 (2020-06-29)
# REF: https://packagemanager.rstudio.com/__docs__/admin/serving-binaries/#binaries-r-configuration-linux
&& ln -s /usr/lib/R/site-library/littler/examples/install2.r /usr/local/bin/install2.r \
&& ln -s /usr/lib/R/site-library/littler/examples/installGithub.r /usr/local/bin/installGithub.r \
&& echo "options(repos = c(RSPM='${RSPM}'))" >> ~/.Rprofile \
&& echo 'options(HTTPUserAgent = sprintf("R/%s R (%s)", getRversion(), paste(getRversion(), R.version$platform, R.version$arch, R.version$os)))' >> ~/.Rprofile \
&& Rscript -e "install.packages(c('docopt', 'BiocManager'))" \
&& Rscript -e "BiocManager::install(c('graph', 'Rgraphviz'))" \
&& install2.r --error --deps TRUE --ncpus -1 --skipinstalled \
xml2 \
devtools \
pacman \
tidyverse \
tidylog \
furrr \
RSQLite \
rmarkdown \
formatR \
caTools \
pROC \
cmprsk \
psych \
clinfun \
car \
MASS \
ggfortify \
ggsci \
tableone \
# since package "export" was removed from CRAN on 2020-02-21,
# install dev version from GitHub repo (commit d29650b / 2020-06-22)
officer \
rvg \
openxlsx \
flextable \
xtable \
rgl \
stargazer \
tikzDevice \
&& installGithub.r tomwenseleers/export@d29650b
# Install radian: A 21 century R console
RUN set -x \
&& pip3 install -U radian
# User settings
RUN set -x \
# add user
&& useradd -m rstudio \
&& echo "rstudio:rstudio" | chpasswd \
# R and RStudio server settiongs
&& cp ~/.Rprofile /home/rstudio/.Rprofile \
&& echo "r-libs-user=/usr/local/lib/R/site-library" >> /etc/rstudio/rsession.conf \
&& echo "r-cran-repos=${RSPM}" >> /etc/rstudio/rsession.conf \
&& echo "auth-timeout-minutes=0" >> /etc/rstudio/rserver.conf \
# Radian settings
&& echo 'options(radian.color_scheme = "monokai")' > /home/rstudio/.radian_profile \
&& echo 'options(radian.auto_match = TRUE)' >> /home/rstudio/.radian_profile \
&& echo 'options(radian.prompt = "\033[0;32mr$>\033[0m ")' >> /home/rstudio/.radian_profile \
&& echo 'options(radian.escape_key_map = list(list(key = "m", value = " %>% ")))' >> /home/rstudio/.radian_profile \
# change premission
&& chown rstudio:rstudio /home/rstudio \
&& addgroup rstudio staff \
&& mkdir /home/rstudio/.rstudio \
&& chown -R rstudio:rstudio /home/rstudio/.rstudio \
&& chown rstudio:rstudio /home/rstudio/.Rprofile \
&& chown rstudio:rstudio /home/rstudio/.radian_profile
WORKDIR /home/rstudio
ENV USER=rstudio \
HOME=/home/rstudio \
LANG=ja_JP.UTF-8 \
LC_ALL=ja_JP.UTF-8
# start RStudio server
CMD ["/usr/lib/rstudio-server/bin/rserver", "--server-daemonize=0"]
@mokztk
Copy link
Author

mokztk commented Jul 8, 2020

options("HTTPUserAgent") の内容

  • 変更前の素の R 4.0.2 : "R (4.0.2 x86_64-pc-linux-gnu x86_64 linux-gnu)"
  • 変更前の R 4.0.2 on RStudio 1.3 : "RStudio Server (1.3.959); R (4.0.2 x86_64-pc-linux-gnu x86_64 linux-gnu)"
  • 変更後 (共通): "R/4.0.2 R (4.0.2 x86_64-pc-linux-gnu x86_64 linux-gnu)"

@mokztk
Copy link
Author

mokztk commented Jul 8, 2020

依存パッケージは https://packagemanager.rstudio.com/client/#/repos/1/overview で Ubuntu 18.04 の Install System Prerequisites for the Repo’s Packages に記載されている内容を depend.txt として保存し、以下のコードで抽出した。

library(tidyverse)
readLines("depend.txt") %>% 
  # パッケージごとにまとめる
  str_c(collapse = "@@") %>% 
  str_split("# ") %>% 
  unlist() %>% 
  .[-1] %>% 
  as_tibble() %>%
  # パッケージ名と依存情報を分ける
  # 依存情報は apt-get install -y の部分を省略
  rowwise() %>% 
  mutate(
    pkg = sub('^([^ ]*).*', '\\1', value),
    req = str_split(value, "requirements:@@")[[1]][2] %>% 
      str_replace_all('(@@|)apt-get install -y ', ', ') %>% 
      str_replace_all('^,\ ', '') %>% 
      str_replace_all('@@@@$', '') %>% 
      str_replace_all('@@', ' && ')
  ) %>% 
  ungroup() %>% 
  select(-value) %>% 
  # 現在の R 環境にインストールされているパッケージのみ抽出
  filter(pkg %in% .packages(all.available = TRUE)) %>%
  # インストールが必要なものを抽出
  pull(req) %>% 
  str_c(collapse = ", ") %>% 
  str_split(", ", simplify = TRUE) %>%
  as.vector() %>% 
  unique()

mokztk/R4.0.2_20200626.Dockerfile 上で実行した結果:

#  [1] "perl"                 "make"                 "libpng-dev"          
#  [4] "pandoc"               "git"                  "libcurl4-openssl-dev"
#  [7] "libssl-dev"           "imagemagick"          "libmagick++-dev"     
# [10] "zlib1g-dev"           "libxml2-dev"          "libicu-dev"          
# [13] "libfreetype6-dev"     "libglu1-mesa-dev"     "libgl1-mesa-dev"     
# [16] "libcairo2-dev"        "libssh2-1-dev"        "librsvg2-dev"        
# [19] "libfontconfig1-dev"   "libxft-dev"          

pandoc は rocker-org/r-versioned/rstudioDockerfile に倣い、RStudio server に含まれるものにシンボリックリンクを貼る形で対応。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment