数据-R语言-图表-决策-Linux-PythonLearning R程序员

Shiny生产环境部署与共享

2018-11-03  本文已影响114人  willnight
result

前段时间看到有人在问怎么让自己的shiny程序让别人能够看到,今天来写篇文章总结下!

shinyApp: http://120.78.66.186:3838/test1/

一.在同一局域网内

在同一局域网内的话很简单只要对方能访问到你的ip和shiny端口就行,直接访问就行

跑shiny服务的主机一般是你自己的机子


macip

访问你应用的主机


windowsip
可以看到在同一网段内的!

这边的话要自己指定host为0.0.0.0,不然默认是127.0.0.1,那别人是访问不到的
runApp("Downloads/csfz/wenjianshangchuan/test1/app.R",port = 7777,host="0.0.0.0")

macshiny
windowsshiny

所以这种方式的话在局域网内是很容易实现的!!

二.在服务器上部署

在服务器上部署的话就很方便共享啦

docker pull quantumobject/docker-shiny

docker run -d -p 3838:3838 -v <你的shinyapp路径>:/srv/shiny-server -ti quantumobject/docker-shiny /bin/bash

进去后输入R,然后安装你需要的依赖包就行

第二种方法
git clone https://github.com/QuantumObject/docker-shiny

修改dockerfile,主要是在安装包那边把你的包加上,然后把repos改成清华的,为了速度。(这边是我加了我的demo的包,各位按需加就行)

#name of container: docker-shiny
#version of container: 0.6.1
FROM quantumobject/docker-baseimage:18.04
MAINTAINER willnight  "willnight@yeah.net"

# Update the container
# Installation of necessary packages/software for this container...
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -q r-base  \
                    r-base-dev \
                    libssl-dev \
                    libsasl2-dev \
                    gdebi-core \
                    libapparmor1 \
                    sudo \
                    libssl1.0.0 \
                    libcurl4-openssl-dev \
                    && apt-get clean \
                    && rm -rf /tmp/* /var/tmp/*  \
                    && rm -rf /var/lib/apt/lists/*

RUN R -e "install.packages('shiny', repos='https://mirrors.tuna.tsinghua.edu.cn/CRAN/')" \
          && update-locale  \
          && wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.9.923-amd64.deb \
          && dpkg -i --force-depends shiny-server-1.5.9.923-amd64.deb \
          && rm shiny-server-1.5.9.923-amd64.deb \
          && mkdir -p /srv/shiny-server; sync  \
          && mkdir -p  /srv/shiny-server/examples; sync \
          && cp -R /usr/local/lib/R/site-library/shiny/examples/* /srv/shiny-server/examples/.

RUN  R -e "install.packages('rmarkdown', repos='https://mirrors.tuna.tsinghua.edu.cn/CRAN/')"
RUN  R -e "install.packages(c('devtools','shinydashboard','shinythemes','DT','openssl','formattable','readxl','mongolite','lubridate','glue','openxlsx','shinydashboardPlus','shinyWidgets'), repos='https://mirrors.tuna.tsinghua.edu.cn/CRAN/')"
RUN  R -e "devtools::install_github('nik01010/dashboardthemes')"
##startup scripts
#Pre-config scrip that may be needed to be run one time only when the container run the first time .. using a flag to don't
#run it again ... use for conf for service ... when run the first time ...
RUN mkdir -p /etc/my_init.d
COPY startup.sh /etc/my_init.d/startup.sh
RUN chmod +x /etc/my_init.d/startup.sh

##Adding daemons to containers
RUN mkdir /etc/service/shiny-server /var/log/shiny-server ; sync
COPY shiny-server.sh /etc/service/shiny-server/run
RUN chmod +x /etc/service/shiny-server/run  \
    && cp /var/log/cron/config /var/log/shiny-server/ \
    && chown -R shiny /var/log/shiny-server \
    && sed -i '113 a <h2><a href="./examples/">Other examples of Shiny application</a> </h2>' /srv/shiny-server/index.html

#volume for Shiny Apps and static assets. Here is the folder for index.html (link) and sample apps.
VOLUME /srv/shiny-server

# to allow access from outside of the container to the container service
# at the ports to allow access from firewall if accessing from outside the server.
EXPOSE 3838

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]
  1. 测试运行

上面的步骤做完运行就很简单了
docker run -d -p 3838:3838 -v /root/Myrpro/:/srv/shiny-server myshiny
效果 http://120.78.66.186:3838/test1/

上一篇下一篇

猜你喜欢

热点阅读