Shiny app的结构

每个Shiny app包含两部分的文件:ui.R(a user-interface definition),server.R(a server script)。
ui.R定义app界面输入、输出格式和布局,server.R接受输入值,进行数据的处理计算,并将结果传递至ui.R进行展示。

ui.R

shiny包:fluidPage

1
shinyUI(fluidPage(...))

shinydashboard包:dashboardPage

1
2
3
4
5
6
7
shinyUI(dashboardPage(title="title",
dashboardHeader(...),
dashboardSidebar(...),
dashboardBody(...),
skin="green"
)
)

dashboardSidebar与dashboardBody的互动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1、点击Sidebar的"tab1标题"时,Body展示table1
# 2、点击Sidebar的"tab2标题"时,Body展示table2
dashboardSidebar(
sidebarMenu(
menuItem("tab1标题", tabName = "tab1", icon = icon("bar-chart")),
menuItem("tab2标题", tabName = "tab2",icon = icon("th"))
)
)

dashboardBody(
tabItems(
tabItem(tabName = "tab1",dataTableOutput('table1')),
tabItem(tabName = "tab2",dataTableOutput('table2'))
)
)

标签选项卡tabsetPanel,tabPanel

1
2
3
4
tabsetPanel(type = "tabs",
tabPanel("tab1",br(),h1('tab1')),
tabPanel("tab1",br(),h1('tab1'))
)

条件面板

1
2
3
4
conditionalPanel(
condition = "input.breaks == 'custom'",
sliderInput("breakCount", "Break Count", min=1, max=1000, value=10)
)

Row(Column)-based layout

1
2
3
4
5
6
# fluidRow内的元素按行横排布局
# column内的元素按列竖排布局
fluidRow(
column(width = 6, '6_1','6_2','6_3')
column(width = 4, offset = 2,'4 offset 2')
)

server.R

1
2
3
4
5
6
7
shinyServer(function(input, output, session) {
# 接受输入
input_value <- reactive({input$inputId})
# 数据处理、计算
# 输出结果
output$text <- renderText({'结果'})
})

That’s all.
Happy writing!