一藤肢、樣本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)
image.png
二省骂、方法
1丑孩、部署在官方的服務(wù)器上
#本地安裝rsconnect
install.packages('rsconnect')
library(rsconnect)
#想要最新版本可以用以下命令安裝該包
if(!require("devtools"))
install.packages("devtools")
devtools::install_github("rstudio/rsconnect")
#通過以下網(wǎng)址創(chuàng)建shinyapp.io賬戶
https://www.shinyapps.io/
image.png
image.png
#復(fù)制該代碼到R窗口下運(yùn)行
rsconnect::setAccountInfo(name='genomenetwork',
token='869F3553609D3A17C74FA157528D58C8',
secret='<SECRET>')
#將工作目錄設(shè)置到樣本app的文件下
setwd("~/Downloads/shiny-examples-master/001-hello")
#當(dāng)運(yùn)行以下命令成功運(yùn)行程序時(shí)温学,目錄設(shè)置正確
shiny::runApp()
image.png
#停止應(yīng)用仗岖,運(yùn)行以下代碼部署app
library(rsconnect)
deployApp()
#部署完成自動(dòng)彈出網(wǎng)頁(yè)
https://genomenetwork.shinyapps.io/001-hello/
2、部署在自己服務(wù)器上
#購(gòu)買一個(gè)vps虛擬機(jī)
#購(gòu)買域名揽祥,沒有的話,用ip訪問
#連接vps拄丰,官方推薦ubuntu系統(tǒng)俐末,其他系統(tǒng)可能要多折騰
#安裝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")
#手動(dòng)添加鏡像源
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载矿,如果頁(yè)面出現(xiàn)烹卒,且右側(cè)有圖則安裝完成
image.png
#部署app
#將文件app.R上傳到服務(wù)器
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; # 禁止自動(dòng)清除日志
# 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的網(wǎng)址后綴
app_dir /home/uu/shinyapp/demo1; #app.R所在文件夾
log_dir /var/log/shiny-server/demo1;#app.R日志所在文件夾
}
#重啟服務(wù)器
sudo systemctl restart shiny-server
#應(yīng)用啟動(dòng)失敗牡整,一般是依賴包問題敏沉,好好查看報(bào)錯(cuò)信息
#日志
sudo cat /var/log/shiny-server/demo/
#重啟
sudo systemctl restart shiny-server;
#文件配置
sudo vim /etc/shiny-server/shiny-server.conf
由于Shiny Server為了保證性能炎码,所以非敏感性的錯(cuò)誤日志被設(shè)置了自動(dòng)清除,每當(dāng)你出現(xiàn)了錯(cuò)誤潦闲,要去看日志定位問題的時(shí)候,這個(gè)日志就剛好被自動(dòng)清除了辖众『途矗坑很大!啤它!都開始懷疑人生了。
所以变骡,你在調(diào)試時(shí)需要修改一個(gè)參數(shù)芭逝,保證日志不會(huì)被自動(dòng)清除。
sudo vi /etc/shiny-server/shiny-server.conf
run_as shiny;
access_log /var/log/shiny-server/access.log default; # 增加記錄訪問
preserve_logs true; # 禁止自動(dòng)清除日志
# Define a server that listens on port 3838
server {
listen 3838;
# 省略
}
參考
#部署在官方服務(wù)器上
https://docs.rstudio.com/shinyapps.io/
#部署在自己服務(wù)器上
https://www.rstudio.com/products/shiny/download-server/
http://blog.fens.me/r-shiny-aws/