Shiny Server
RでWebアプリケーションを開発するためのパッケージShinyをWebアプリケーションとして動作させるためのサーバ。オンプレミスの有償版は金額が高く、ホスティングサービスは無償アカウントもあるがデータを外部のサーバに置くのは問題が多いため、オープンソース版を使う。いくつかのOSではパッケージが用意されているが、Gentoo向けには無いのでソースからビルドする。
事前準備
Pythonと親和性が高いGentooでは特に事前にインストールするものはない。R自体はインストールする必要がある。
ビルドとインストール
基本的にマニュアル通り。以前は1ステップ必要な手順が抜けていて、コミュニティの書き込みを参照する必要があったが、修正されているようだ。
> git clone https://github.com/rstudio/shiny-server.git > cd shiny-server > mkdir tmp > cd tmp > ../external/node/install-node.sh # 環境変数をセット > DIR=`pwd` > PATH=$DIR/../bin:$PATH > cmake -DCMAKE_INSTALL_PREFIX=/usr/local ../ > make > mkdir ../build > (cd .. && ./bin/npm install) > (cd .. && ./bin/node ./ext/node/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js rebuild) > make install # 設定ファイルを配置するディレクトリの作成 > mkdir /etc/shiny-server > cp ../config/default.config /etc/shiny-server/shiny-server.conf
shinyユーザを作成し、各種ディレクトリ等を作成する。
今回は、/home/shinyをホームディレクトリとしたshinyユーザを作成し、各種ディレクトリもここに集約する。shinyユーザにはシェルを与えずログインできないようにしておく。
- ${shiny_home}/log
- ${shiny_home}/server
- ${shiny_home}/lib
- ${shiny_home}/bookmarks
設定
/etc/shiny-server/shiny-server.confを上記ディレクトリ構成にあわせて設定する。
マニュアルにはないが、bookmarksディレクトリを書き込み可能な形で設定しておかないと、サーバが起動しない。
# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;
# Define a server that listens on port 3838
server {
listen 3838 0.0.0.0;
# Define a location at the base URL
location / {
# Host the directory of Shiny Apps stored in this directory
site_dir /home/shiny/server;
# Log all Shiny output to files in this directory
log_dir /home/shiny/log;
# Bookmark dir
bookmark_state_dir /home/shiny/bookmarks;
# When a user visits the base URL rather than a particular application,
# an index of the applications available in this directory will be shown.
directory_index on;
}
}
起動スクリプト
Gentoo OpenRC用の起動スクリプト。
注意点は、start-stop-daemonのオプション「–background」「–make-pidfile」をつけておくこと。
shinyは勝手にバックグラウンドにならないのでbackgroundオプションで切り離し、 pidファイルも生成しないのでmake-pidfileでpidfileオプションで指定した場所にpidファイルを吐き出させる。これでstop関数が正常に動作する。
#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
description="R Shiny Server"
pidfile="/var/run/shiny-server.pid"
command="/usr/local/shiny-server/bin/shiny-server"
depend() {
use net
}
start() {
ebegin "Starting Shiny Server"
start-stop-daemon --start --quiet --background --make-pidfile --pidfile ${pidfile} --exec ${command} \
>> /home/shiny/log/shiny-server.log 2>&1
eend $?
}
stop() {
ebegin "Stopping Shiny Server"
start-stop-daemon --stop --quiet --pidfile ${pidfile}
eend $?
}
