Shiny Server
#為什么要使用Shiny Server
Shiny Server使用戶可以在網絡上托管和管理Shiny應用程序。Shiny是一個R包,運用反應式編程模型
簡化Web程序開發。Shiny Server可以通過不同的URLs 和端口管理不同的R程序。
使用Shiny Server的好處有:
- 同時托管多個應用程序,每個應用程序都有自己的URL。
- 支持不支持WebSocket的Web瀏覽器,包括Internet Explorer 8和9。
- 用戶能夠開發和管理自己的Shiny應用程序。
- R進程崩潰或終止并不影響下一個用戶請求該應用程序。
Shiny Server專業版(Shiny Server Professional)有跟多的功能:
- 確保您的應用程序受到保護,并且只能由經過身份驗證的用戶訪問。
- 一個Shiny應用程序可以支持多個用戶同時訪問。
- 通過使用Web儀表板監視Shiny應用程序的性能和使用情況,可以深入了解它們。
- 使用SSL對發送到應用程序和從應用程序獲取的數據進行安全加密。
- 了解和管理當前和歷史應用程序資源利用率,以更好地配置和優化應用程序。
- 通過根據并發會話數配置多進程Shiny應用程序,微調專用于應用程序每個用戶的資源。
- 使用運行狀況檢查端點監視Shiny Server的運行狀況。
#系統要求
Shiny Server當前僅能在Linux 操作系統運行,并且只支持64位系統:
- RedHat Enterprise Linux(和CentOS)6及更高版本
- Ubuntu 14.04及更高版本
- SUSE Linux Enterprise 12及更高版本
我們目前僅支持x86-64體系結構。
#安裝
Shiny Server安裝之前需要先安裝 R 和Shiny 包。
- R 安裝
Shiny Server建議安裝R版本3.0或更高版本。 安裝最新版本的R,應首先按照以下說明將CRAN存儲庫添加到系統中: - Debian: https://cran.rstudio.com/bin/linux/debian/
- Ubuntu: http://cran.rstudio.com/bin/linux/ubuntu/README.html
$ sudo apt-get install r-base
注意:如果您沒有按如上所述添加CRAN Debian或Ubuntu存儲庫,則此命令將安裝與您當前系統版本相對應的R版本。可能是老版本的R。
- Shiny包安裝
$ sudo su - -c "R -e \"install.packages('shiny', repos='http://cran.rstudio.com/')\""
>library("shiny")
注:不要使用install.packages("shiny")
安裝;install.packages("shiny")
會安裝到當前用戶下,shiny app就不能啟動了。
- 安裝gdebi(用于安裝Shiny Server及其所有依賴項)和Shiny Server。
$ sudo apt-get install gdebi-core
$ wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.14.948-amd64.deb
$ sudo gdebi shiny-server-1.5.14.948-amd64.deb
Shiny Server將會被安裝到/opt/shiny-server/,主要的執行程序在/opt/shiny-server/bin/shiny-server。
- shiny-server命令
#啟動
$ sudo systemctl start shiny-server
#停止
$ sudo systemctl stop shiny-server
#重啟
$ sudo systemctl restart shiny-server
#查看狀態
$ sudo systemctl status shiny-server
#服務器重新初始化,但不會中斷服務器當前進程或任何連接。
$ sudo systemctl kill -s HUP --kill-who=main shiny-server
$ sudo reload shiny-server
#shiny-server狀態
$ sudo systemctl status shiny-server
- 查看shiny-server
打開瀏覽器輸入: http://IP:端口/
HTTP
IP:使用ip addr
查看
端口默認:3838。
shiny
- shiny-server配置
shiny-server 配置文件
/etc/shiny-server/shiny-server.conf
/opt/shiny-server/config/default.config
$ vi /etc/shiny-server/shiny-server.conf
# Define the user we should use when spawning R Shiny processes
run_as shiny;
# Define a top-level server which will listen on a port
server {
# Instruct this server to listen on port 3838
listen 3838;
# Define the location available at the base URL
location / {
#### PRO ONLY ####
# Only up tp 20 connections per Shiny process and at most 3 Shiny processes
# per application. Proactively spawn a new process when our processes reach
# 90% capacity.
utilization_scheduler 20 .9 3;
#### END PRO ONLY ####
# Run this location in 'site_dir' mode, which hosts the entire directory
# tree at '/srv/shiny-server'
site_dir /srv/shiny-server;
# Define where we should put the log files for this location
log_dir /var/log/shiny-server;
# Should we list the contents of a (non-Shiny-App) directory when the user
# visits the corresponding URL?
directory_index on;
}
}
# Setup a flat-file authentication system. {.pro}
auth_passwd_file /etc/shiny-server/passwd;
# Define a default admin interface to be run on port 4151. {.pro}
admin 4151 {
# Only permit the user named `admin` to access the admin interface.
required_user admin;
}
##從配置文件內容可以看出:
- shiny app存放在site_dir /srv/shiny-server;
- 運行日志存放在/var/log/shiny-server
- 用戶認證信息放置在/etc/shiny-server/passwd
##創建用戶
sudo /opt/shiny-server/bin/sspasswd /etc/shiny-server/passwd admin
#Shiny Server不僅可以托管shiny app,也可以托管R Markdown
sudo su - -c "R -e \"install.packages('rmarkdown',repos='http://mirror.bjtu.edu.cn/cran/')\""
#注:因為設置的是shiny server,還應該給給shiny app開發者一些當前文件操作權限。