如何在 Ubuntu 上設定 Shiny Server


Shiny 是一個 R 包,允許 Linux 使用者將 R 程式碼轉換為互動式網頁並在網上釋出文件。您可以獲取您的 Shiny 應用程式並在您的組織或全球範圍內共享。本文介紹瞭如何在 Ubuntu 上設定 Shiny Server。

在 Ubuntu 上安裝 Shiny Server

在安裝 Shiny Server 之前,我們需要安裝 shiny R 包。要安裝 R 包,請使用以下命令 -

$ sudo apt-get install r-base

示例輸出應如下所示 -

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
apport-hooks-elementary contractor javascript-common libgda-5.0-4 libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1
libindicate5 libjs-jquery libnoise-core0 libtagc0 mysql-server-5.7 mysql-server-core-5.7
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
bzip2-doc cdbs debhelper dh-strip-nondeterminism dh-translations gfortran gfortran-5 intltool libblas-common libblas-dev libblas3 libbz2-dev
libfile-stripnondeterminism-perl libgfortran-5-dev libgfortran3 liblapack-dev liblapack3 liblzma-dev liblzma5 libmail-sendmail-perl
libncurses5-dev libpcre3-dev libpcre32-3 libpcrecpp0v5 libpng12-dev libreadline-dev libreadline6-dev libsys-hostname-long-perl libtinfo-dev
po-debconf python-scour python-six r-base-core r-base-dev r-base-html r-cran-boot r-cran-class r-cran-cluster r-cran-codetools
r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial
r-cran-survival r-doc-html r-recommended
.......................................................................

我們將使用 GDebi 工具安裝 Shiny Server。要安裝 GDebi,請使用以下命令 -

$ sudo apt-get install gdebi-core

示例輸出應如下所示 –

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
apport-hooks-elementary contractor javascript-common libgda-5.0-4 libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1
libindicate5 libjs-jquery libnoise-core0 libtagc0 mysql-server-5.7 mysql-server-core-5.7
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
gdebi-core
0 upgraded, 1 newly installed, 0 to remove and 180 not upgraded.
Need to get 9,716 B of archives.
After this operation, 135 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 gdebi-core all 0.9.5.7ubuntu1 [9,716 B]
Fetched 9,716 B in 0s (31.5 kB/s)
Selecting previously unselected package gdebi-core.
(Reading database ... 189573 files and directories currently installed.)
Preparing to unpack .../gdebi-core_0.9.5.7ubuntu1_all.deb ...
Unpacking gdebi-core (0.9.5.7ubuntu1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up gdebi-core (0.9.5.7ubuntu1) ...
...........................................................................................

要下載 Shiny Server,請使用以下命令 -

$ wget https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.4.2.786-amd64.deb

示例輸出應如下所示 –

--2016-05-16 11:38:20-- https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.4.2.786-amd64.deb
Resolving download3.rstudio.org (download3.rstudio.org)... 54.230.190.35, 54.230.190.248, 54.230.190.84, ...
Connecting to download3.rstudio.org (download3.rstudio.org)|54.230.190.35|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 53653388 (51M) [application/octet-stream]
Saving to: ‘shiny-server-1.4.2.786-amd64.deb’

shiny-server-1.4.2.786-amd64.deb 100%[===================================================================>] 51.17M 408KB/s in 2m 26s

2016-05-16 11:40:48 (358 KB/s) - ‘shiny-server-1.4.2.786-amd64.deb’ saved [53653388/53653388]

現在使用 GDebi 安裝使用以下命令下載的檔案 -

$ sudo gdebi shiny-server-1.4.2.786-amd64.deb

示例輸出應如下所示 –

Reading package lists... Done
Building dependency tree
Reading state information... Done
Reading state information... Done

Shiny Server
Shiny Server is a server program from RStudio, Inc. that makes Shiny applications available over the web. Shiny is a web application framework for the R statistical computation language.
Do you want to install the software package? [y/N]:y
Selecting previously unselected package shiny-server.
(Reading database ... 189587 files and directories currently installed.)
Preparing to unpack shiny-server-1.4.2.786-amd64.deb ...
Unpacking shiny-server (1.4.2.786) ...
Setting up shiny-server (1.4.2.786) ...
Creating user shiny
Adding LANG to /etc/systemd/system/shiny-server.service, setting to en_IN
Created symlink from /etc/systemd/system/multi-user.target.wants/shiny-server.service to /etc/systemd/system/shiny-server.service.
shiny-server.service - ShinyServer
Loaded: loaded (/etc/systemd/system/shiny-server.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2016-05-16 11:42:45 IST; 36ms ago
Process: 1583 ExecStartPost=/bin/sleep 3 (code=exited, status=0/SUCCESS)
Main PID: 1584 (shiny-server)
Tasks: 8 (limit: 512)
CGroup: /system.slice/shiny-server.service
      ├─1582 /bin/bash -c /opt/shiny-server/bin/shiny-server --pidfile=/var/run/shiny-server.pid >>                   /var/log/shiny-server.log 2>&1
      └─1584 /opt/shiny-server/ext/node/bin/shiny-server /opt/shiny-server/lib/main.js --pidfile=/var/run/shiny-server.pid

May 16 11:42:42 linux systemd[1]: Starting ShinyServer...
May 16 11:42:45 linux systemd[1]: shiny-server.service: Supervising process 1584 which is not our child. We'll most likely not notice...t exits.
May 16 11:42:45 linux systemd[1]: Started ShinyServer.
Hint: Some lines were ellipsized, use -l to show in full.

Shiny Server 現在應該已安裝並正在埠 3838 上執行。您應該能夠在 http://your_server_ip:3838/ 上看到預設的歡迎螢幕。示例輸出應如下所示 -


託管互動式 R Markdown 文件

此時,您應該有一個可以託管 Shiny 應用程式的正常執行的 Shiny Server,但它還無法託管互動式 R markdown 文件,因為 rmarkdown R 包尚未安裝。Shiny Server 帶有一個示例互動式文件,可在以下位置獲取

Shiny Server 帶有一個示例互動式文件,可在 http://your_server_ip:3838/sample-apps/rmd/ 獲取。如果您現在轉到該 URL,您將看到一個錯誤。


要解決上述問題,請使用以下命令 -

$ sudo su - -c "R -e \"install.packages('rmarkdown', repos='http://cran.rstudio.com/')\""

示例輸出應如下所示 –

R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
...........................................................................................

現在,Shiny Server 也設定為執行互動式 R markdown 文件以及 Shiny 應用程式。要驗證互動式文件的工作原理,請轉到 http://your_server_ip:3838/sample-apps/rmd/。

您現在擁有一個功能齊全的 Shiny Server,可以託管 Shiny 應用程式或互動式文件。Shiny Server 的配置檔案位於 /etc/shiny-server/shiny-server.conf。預設情況下,它配置為在 /srv/shiny-server/ 目錄中提供服務。這意味著放置在 /srv/shiny-server/app_name 的任何 Shiny 應用程式都可以在 http://your_server_ip:3838/app_name/ 上公開訪問。

閱讀本文後,您將能夠理解 - 如何在 Ubuntu 上設定 Shiny Server。在我們的下一篇文章中,我們將推出更多基於 Linux 的技巧和提示。繼續關注!

更新於: 2020年1月20日

767 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告