部署R语言Shiny 应用:从本地到服务器的飞跃

Shiny 是 R 语言中一个强大的工具,用于创建交互式的 Web 应用程序。它极大地简化了将数据分析结果转化为动态网页的过程。然而,为了让这些应用能够被更广泛的用户访问,我们需要将它们部署到服务器上。本文将详细介绍如何将 Shiny 应用部署到使用 ShinyServer 的服务器上,从而实现从本地到云端的飞跃。

什么是 ShinyServer?

ShinyServer 是 RStudio 公司开发的一个开源服务器软件,专门用于部署和运行 Shiny 应用。它允许用户无需安装 R 或任何依赖包即可访问和使用 Shiny 应用。ShinyServer 有免费的开源版本和商业版本,两者都提供了强大的部署能力。

部署步骤

一、安装R语言
1.1 环境准备
  • 操作系统:ubuntu
  • 版本:16.04
1.2 设置CRAN镜像源
# 编辑sources.list文件
vim /etc/apt/sources.list

# 使用apt的方式安装R,设置CRAN为清华镜像源,在文件的末尾添加一行命令:
# 注意:最后'xenial/'是根据你的ubuntu的版本号而改变的,比如14.04是trusty,16.04则是xenial,
# 其他版本对应关系可查看: https://cran.r-project.org/bin/linux/ubuntu/README.html
deb https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/ubuntu xenial/
1.3 更新apt资源列表
# 更新apt资源列表,这样才能获取到最新的软件包
sudo apt-get update

更新的过程中,出现了xx错误,错误信息如下:

W: GPG error: https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/linux/ubuntu xenial/ InRelease: 
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 51716619E084DAB9

错误中提示:NO_PUBKEY 51716619E084DAB9,即由于没有公钥,无法验证下列签名。一个解决的办法如下:

# 下载导入公钥(注意:这里的51716619E084DAB9是错误中提示的NO_PUBKEY 51716619E084DAB9)
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9

# 导入公钥后,重新执行
sudo apt-get update
1.4 安装
# 使用命令安装R
sudo apt-get install r-base

# 安装完成后,在终端输入`R`,出现如下信息则说明安装成功
R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright (C) 2018 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.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>

二、安装项目需要的R包

安装Shiny包或RMarkdown包及项目中所需要的包

# 如果apps使用的是shiny,则需要安装shiny包;如何使用rmarkdown,则需要安装RMarkdown包。
# 尽量通过如下命令在终端下载包。不要进入到Rscript/Rstudio中下载,因为在Rscript/RStudio
# 中安装好的包,在启动shiny-server时会找不到。
sudo su - -c "R -e \"install.packages('rmarkdown')\""
sudo su - -c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\""

三、安装Shiny Server

下载shiny-server的deb包进行安装,具体步骤如下:

# 安装gdebi,用于解决安装deb包的时候软件依赖问题
sudo apt-get install gdebi-core

# 下载shiny-server的deb包
wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.9.923-amd64.deb

# 使用gdebi安装下载的shiny-server.deb
sudo gdebi shiny-server-1.5.9.923-amd64.deb

安装完成后,访问shiny-server的欢迎页面,在浏览器中输入:http://localhost:3838,如果出现如下页面,则说明安装成功。

四、配置Shiny Server

默认配置文件的路径:/etc/shiny-server/shiny-server.conf

打开shiny-server的默认配置文件,内容如下:

# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

# Define a server that listens on port 3838
server {
  listen 3838;

  # Define a location at the base URL
  location / {

    # Host the directory of Shiny Apps stored in this directory
    site_dir /srv/shiny-server;

    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;

    # 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;
  }
}

配置文件中各参数的说明如下:

  • listen:设置服务端监听的端口
  • location:设置访问的路由
  • site_dir:指定项目代码(shiny或Rmd)的路径
  • log_dir:指定保存日志文件的路径

当客户端请求shiny-server服务器时,默认会运行site_dir指定的文件夹下的index.html,然后该html页面中会调用shiny或rmd的R代码,具体调用代码如下:

<div id="shiny">
    <iframe src="./sample-apps/hello/" style="border: 1px solid #AAA; width: 290px; height: 460px"></iframe>
    <div class="caption">
        When Shiny is properly configured on your server, you'll see a Shiny app above.
    </div>
    <iframe src="./sample-apps/rmd/" style="border: 1px solid #AAA; width: 290px; height: 420px"></iframe>
    <div class="caption">
        With Shiny and <code>rmarkdown</code> installed, you should see a Shiny doc above.
    </div>
</div>

而hello文件夹和rmd文件夹分别存放的是shiny代码和rmd代码,hello文件是shiny代码,里面有两个文件:ui.Rserver.R,而rmd只有一个index.md

因此将自己的shiny应用或md应用对应建立文件夹存放在site_dir指定的目录下即可展示自己的应用,一个shiny应用对应一个文件夹,然后再修改html,指定跳转的路径即可。如果想修改首页,只需要将原来的index.html的内容替换成自己想要展示的页面。

五、Shiny Server命令

# 服务的状态、启动、关闭、重启
systemctl status shiny-server
sudo systemctl start shiny-server
sudo systemctl stop shiny-server
sudo systemctl restart shiny-server

# 默认shiny-server配置文件路径
/etc/shiny-server/shiny-server.conf

# 默认shiny-server日志文件路径
/var/log/shiny-server

# 卸载shiny-server
# 1. 删除软件:
sudo apt-get remove --purge shiny-server
# 2. 删除配置文:
sudo rm -rf /etc/shiny-server/shiny-server.conf
# 3. 删除/srv/目录:
sudo rm -rf /srv/