一、样本app
#文件名为app.R
library(shiny)
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("Shiny Text"),
# Sidebar layout with a input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Selector for choosing dataset ----
selectInput(inputId = "dataset",
label = "Choose a dataset:",
choices = c("rock", "pressure", "cars")),
# Input: Numeric entry for number of obs to view ----
numericInput(inputId = "obs",
label = "Number of observations to view:",
value = 10)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Verbatim text for data summary ----
verbatimTextOutput("summary"),
# Output: HTML table with requested number of observations ----
tableOutput("view")
)
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
# Return the requested dataset ----
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
# Generate a summary of the dataset ----
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
# Show the first "n" observations ----
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
二、方法
1、部署在官方的服务器上
#本地安装rsconnect
install.packages('rsconnect')
library(rsconnect)
#想要最新版本可以用以下命令安装该包
if(!require("devtools"))
install.packages("devtools")
devtools::install_github("rstudio/rsconnect")
#通过以下网址创建shinyapp.io账户
https://www.shinyapps.io/
#复制该代码到R窗口下运行
rsconnect::setAccountInfo(name='genomenetwork',
token='869F3553609D3A17C74FA157528D58C8',
secret='<SECRET>')
#将工作目录设置到样本app的文件下
setwd("~/Downloads/shiny-examples-master/001-hello")
#当运行以下命令成功运行程序时,目录设置正确
shiny::runApp()
#停止应用,运行以下代码部署app
library(rsconnect)
deployApp()
#部署完成自动弹出网页
https://genomenetwork.shinyapps.io/001-hello/
2、部署在自己服务器上
#购买一个vps虚拟机
#购买域名,没有的话,用ip访问
#连接vps,官方推荐ubuntu系统,其他系统可能要多折腾
#安装R和依赖文件
sudo apt-get update
sudo apt-get install r-base
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libxml2-dev
R
#安装shiny包
#install.packages("shiny")
#手动添加镜像源
install.packages('shiny', repos='https://cran.rstudio.com/')
#如果你的app.R有其他加载的包,尽量都要用R装上去
#安装shiny-server
sudo apt-get install gdebi-core
wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.9.923-amd64.deb
sudo gdebi shiny-server-1.5.9.923-amd64.deb
访问 端口3838,如果页面出现,且右侧有图则安装完成
#部署app
#将文件app.R上传到服务器
uu@client:~/shinyapp$ pwd
/home/uu/shinyapp
uu@client:~/shinyapp$ tree demo1
demo1
└── app.R
0 directories, 1 file
#修改配置文件,使app.R生效
sudo vim /etc/shiny-server/shiny-server.conf
# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;
access_log /var/log/shiny-server/access.log default; # 增加记录访问
preserve_logs true; # 禁止自动清除日志
# Define a server that listens on port 3838
server {
listen 80;
# 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;
}
#指向app.R代码位置
location /demo1{ #/demo1 访问app.R的网址后缀
app_dir /home/uu/shinyapp/demo1; #app.R所在文件夹
log_dir /var/log/shiny-server/demo1;#app.R日志所在文件夹
}
#重启服务器
sudo systemctl restart shiny-server
#应用启动失败,一般是依赖包问题,好好查看报错信息
#日志
sudo cat /var/log/shiny-server/demo/
#重启
sudo systemctl restart shiny-server;
#文件配置
sudo vim /etc/shiny-server/shiny-server.conf
由于Shiny Server为了保证性能,所以非敏感性的错误日志被设置了自动清除,每当你出现了错误,要去看日志定位问题的时候,这个日志就刚好被自动清除了。坑很大!!都开始怀疑人生了。
所以,你在调试时需要修改一个参数,保证日志不会被自动清除。
sudo vi /etc/shiny-server/shiny-server.conf
run_as shiny;
access_log /var/log/shiny-server/access.log default; # 增加记录访问
preserve_logs true; # 禁止自动清除日志
# Define a server that listens on port 3838
server {
listen 3838;
# 省略
}
参考
#部署在官方服务器上
https://docs.rstudio.com/shinyapps.io/
#部署在自己服务器上
https://www.rstudio.com/products/shiny/download-server/
http://blog.fens.me/r-shiny-aws/