视频演示:http://mpvideo.qpic.cn/0bc37aadyaaanqaakvelqjrvb6gdht4aapaa.f10002.mp4?
Shiny 是一个为 R 模型提供 Web 交互界面的应用框架,非常容易编写应用,不要求有 Web 开发技能。Shiny 由 RStudio 公司开发,通过 CRAN 下载安装,利用R语言轻松开发交互式Web应用。简单讲:快速搭建交互应用界面(可以发布形成固定网页)。
#安装Shiny程序包
install.packages("shiny")
2.学习目录
P-1:初步认识shiny app的结构
一个文件夹,加上包含Shiny命令的app.R文件,再加上用到的数据文件和R脚本等, 就称为ShinyApp。app.R总是由三部分组成:
P-2:进一步认识UI页面布局
P-3:输入对象 <*input>
P-4:server呈现 <render*> ui输出 <*Output>
render* 与*Output成对出现,一般 <render*>用在server中,讲计算/绘图结果表达(转换),然后通过再ui代码块中使用<*Output>姜server中表达的结果展现出来。
P5:响应模式
非立即响应输入
P6:ui界面主题
library(bslib)
参考资料
# rf1 https://mastering-shiny.org/
# rf2 https://shiny.rstudio.com/tutorial/#level-up
P-1:认识shiny app的结构——ui、server、shinyApp
####################### P-1:structure,ui server shinyApp #############################
library(shiny)
# ??library(shiny)
ui <- fluidPage(
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
P-2:进一步认识UI页面
####################### P-2:ui布局/样式 #############################
# rf https://shiny.rstudio.com/articles/layout-guide.html
# rf https://shiny.rstudio.com/tutorial/written-tutorial/lesson2/
library(shiny)
# ??library(shiny)
# sidebarLayout带侧边栏的页面
# ??fluidPage
# ??sidebarLayout
# width:The width of the sidebar and main panel.
# By default, the sidebar takes up 1/3 of the width,
# and the main panel 2/3. The total width must be 12 or less.
ui <- fluidPage(
titlePanel("my first shiny app"),
sidebarLayout(
sidebarPanel(
numericInput(inputId = "in_1",label = "Input your Num",value = 10,min = 0,max = 100)
),
mainPanel(
plotOutput("plot_1"),
plotOutput("plot_2")
)
)
)
server <- function(input, output, session) {
output$plot_1 <- renderPlot(plot(rnorm(input$in_1)))
output$plot_2 <- renderPlot(hist(rnorm(input$in_1)))
}
shinyApp(ui, server)
## 补充更多ui样式界面:fluidRow tabsetPanel navlistPanel navbarPage
## flowLayout(), splitLayout(), verticalLayout(),fixedPage(), and fixedRow()
# ??fluidRow fixedRow
# UI demonstrating column layouts
# column(width, ..., offset = 0)
# offset:The number of columns to offset this column from the end of the previous column.
library(shiny)
ui <- fluidPage(
theme = bslib::bs_theme(bootswatch = "yeti"),
title = "Hello Shiny!",
fluidRow(
column(width = 4,
numericInput("in_1",label = "输入数值",value = 100)
),
column(width = 3, offset = 2,
plotOutput("Plot_1")),
column(width = 3,offset = 0,
plotOutput("Plot_2")
)
),
fluidRow(
column(width = 2,
numericInput("in_2",label = "输入数值",value = 100)
),
column(width = 3, offset = 2,
plotOutput("Plot_3")),
column(width = 3,offset = 0,
plotOutput("Plot_4")
)
)
)
server <- function(input, output) {
output$Plot_1 <- renderPlot({hist(rnorm(input$in_1))})
output$Plot_2 <- renderPlot({plot(rnorm(input$in_1))})
output$Plot_3 <- renderPlot({hist(rnorm(input$in_2))})
output$Plot_4 <- renderPlot({plot(rnorm(input$in_2))})
}
shinyApp(ui, server)
P-3:输入对象 <*input>
####################### P-3:输入对象 <*input> #############################
## rf https://shiny.rstudio.com/tutorial/written-tutorial/lesson3/
numericInput() #数值
sliderInput() #滑动条
textInput() #文本
actionButton()
submitButton() #提交按钮
actionLink()
checkboxGroupInput()
dateInput()
dateRangeInput()
fileInput()
P-4:server呈现 <render*> ui输出 <*Output>
####################### P-4:server呈现 <render*>
####################### ui输出 <*Output>
####################### 成对出现,一一对应
# 表
renderDataTable()
dataTableOutput()
#图
renderPlot()
plotOutput()
# 文本
renderText()
textOutput()
#
renderUI()
uiOutput()
# ui <- fluidPage(
# uiOutput("moreControls")
# )
#
# server <- function(input, output) {
# output$moreControls <- renderUI({
# tagList(
# sliderInput("n", "N", 1, 1000, 500),
# textInput("label", "Label")
# )
# })
# }
# shinyApp(ui, server)
P5:响应模式
####################### P5:响应函数
# reactive() 观测到*
library(shiny)
ui <- fluidPage(
textInput("a","","A"),
textInput("z","","Z"),
textOutput("b"))
server <- function(input,output){
re <- reactive({
paste(input$a,input$z)})
output$b <- renderText({
re()
})
}
shinyApp(ui, server)
# eventReactive() 事件响应
library(shiny)
ui <- fluidPage(
textInput("a","","A"),
actionButton("go","Go"),
textOutput("b")
)
server <- function(input,output){
re <- eventReactive(
input$go,{input$a})
output$b <- renderText({
re()
})
}
shinyApp(ui, server)
P6:ui界面主题
######################### P6:ui风格
# rf https://shiny.rstudio.com/articles/themes.html
library(shiny)
library(bslib)
ui <- fluidPage(
title = "Hello Shiny!",
theme = bs_theme(bootswatch ="simplex"), #bootswatch_themes() Get a list of themes.
fluidRow(
column(width = 4,
numericInput("in_1",label = "输入数值",value = 100)
),
column(width = 3, offset = 2,
plotOutput("Plot_1")),
column(width = 3,offset = 0,
plotOutput("Plot_2")
)
)
)
server <- function(input, output) {
output$Plot_1 <- renderPlot({hist(rnorm(input$in_1))})
output$Plot_2 <- renderPlot({plot(rnorm(input$in_1))})
}
shinyApp(ui, server)
# shinydashboard
library(shinydashboard)
shinyApp(
ui = dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(),
title = "Dashboard example",
skin = "purple" #skin = c("blue", "black","purple", "green", "red", "yellow")
),
server = function(input, output) { }
)