输出类型

使用 shinydashboard 构建仪表板

Png Kee Seng

Researcher

Output 与 render 函数

  • 在 shinyApp 中会有输出
  • 每个输出需要一对函数

    • 首先在 UI 中的函数
    • 用于决定输出的位置
    • 此类输出函数通常为:
      <type>output(<identifying label>, ...)
      
  • 第二段代码在 server 中定义

    • 形式为:
      output$<identifying label> <- render<type>(...)
      
使用 shinydashboard 构建仪表板

示例:睡眠研究

  • 先导入数据集
    library(tidyverse)
    sleep <- read_csv("../data/Time Americans Spend Sleeping.csv")  
    
  • 列如下
    $ index                   : num [1:945] 0 1 2 3 4  ...
    $ Year                    : num [1:945] 2003 2004 2005 2006 2007 ...
    $ Period                  : chr [1:945] "Annual" "Annual" "Annual" "Annual" ...
    $ Avg hrs per day sleeping: num [1:945] 8.57 8.55 8.62 8.63  ...
    $ Standard Error          : num [1:945] 0.018 0.026 0.023 0.024  ...
    $ Type of Days            : chr [1:945] "All days" "All days" "All days" ...
    $ Age Group               : chr [1:945] "15 years and over" "15 years and over" ...
    $ Activity                : chr [1:945] "Sleeping" "Sleeping" "Sleeping" ...
    $ Sex                     : chr [1:945] "Both" "Both" "Both" ...
    
使用 shinydashboard 构建仪表板

textOutput

  • 先在 UI 中放置 textOutput
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  textOutput("textlabel"))

server <- function(input, output) {
}

shinyApp(ui, server)
使用 shinydashboard 构建仪表板

renderText

  • 要渲染文本输出,在 server 中使用 renderText
  • 需要使用 output$ 语法
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  textOutput("textlabel"))

server <- function(input, output) { output$textlabel <- renderText("Sleep deprivation is a serious health issue in many major cities around the world.") }
shinyApp(ui, server)
使用 shinydashboard 构建仪表板

已渲染的 textOutput

无输入交互的文本输出演示。

使用 shinydashboard 构建仪表板

带交互的 textOutput

  • 为实现输入与输出的交互,需要用 input$ 调用输入
input$<type>
使用 shinydashboard 构建仪表板

带交互的 textOutput

  • 以文本输入为例。
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

textInput("textlabel", "Tell me your name", value = "", placeholder = "Don"),
textOutput("textoutlabel")) server <- function(input, output) { output$textoutlabel <- renderText(paste0("Hello ",
input$textlabel,
"Sleep deprivation is a serious health issue in many major cities around the world.")) } shinyApp(ui, server)
使用 shinydashboard 构建仪表板

带交互的 textOutput

带交互的文本输出。

使用 shinydashboard 构建仪表板

plotOutput

  • 在 UI 中放置 plotOutput()
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

plotOutput("plotlabel"))
server <- function(input, output) { } shinyApp(ui, server)
使用 shinydashboard 构建仪表板

renderPlot

  • server() 中使用 renderPlot()
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  plotOutput("plotlabel"))

server <- function(input, output) {

output$plotlabel <- renderPlot({ ggplot(sleep) + geom_histogram(aes(x = `Avg hrs per day sleeping`)) })
} shinyApp(ui, server)
使用 shinydashboard 构建仪表板

已渲染的 plotOutput

plotoutput 演示。

使用 shinydashboard 构建仪表板

带 selectInput 的 plotOutput

  • 用户输入可产生不同结果
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  selectInput("selectlabel", 
              "Select an option",
              choices = c("Histogram", "Boxplot")),
  plotOutput("plotlabel"))
使用 shinydashboard 构建仪表板

带 selectInput 的 plotOutput

server <- function(input, output) {
  output$plotlabel <- renderPlot({
    if (input$selectlabel == "Histogram"){

ggplot(sleep) + geom_histogram(aes(x = `Avg hrs per day sleeping`))
} else if (input$selectlabel == "Boxplot"){
ggplot(sleep) + geom_boxplot(aes(x = `Avg hrs per day sleeping`))
} }) } shinyApp(ui, server)
使用 shinydashboard 构建仪表板

带 selectInput 的 plotOutput

带 selectInput 的 plotOutput 演示

使用 shinydashboard 构建仪表板

带 sliderInput 的 plotOutput

  • 看另一个 sliderInput 示例
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

sliderInput("sliderlabel", "Average hours of sleep", min = 7.5, max = 11, value = c(7.5, 11), step = 0.02),
plotOutput("plotlabel")) server <- function(input, output) {
output$plotlabel <- renderPlot({ filter(sleep, `Avg hrs per day sleeping` >= input$sliderlabel[1], `Avg hrs per day sleeping` <= input$sliderlabel[2]) %>% ggplot() + geom_boxplot(aes(x=`Avg hrs per day sleeping`)) })
} shinyApp(ui, server)
使用 shinydashboard 构建仪表板

带 sliderInput 的 plotOutput

带范围的 plotOutput 演示

使用 shinydashboard 构建仪表板

shiny 中的图标

  • shiny 也包含可在应用中使用的图标

shiny 中的一些图标

1 https://fontawesome.com/icons
使用 shinydashboard 构建仪表板

shiny 图标:示例

  • 使用 icon("...")
  • 回顾先前示例
  • 如:icon("bed")
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  sliderInput("sliderlabel",

list(icon("bed"),
"Select an option:"), min = 7.5, max = 11, value = c(7.5, 11), step = 0.02), plotOutput("plotlabel")) server <- function(input, output) { output$plotlabel <- renderPlot({ filter(sleep, `Avg hrs per day sleeping` >= input$sliderlabel[1], `Avg hrs per day sleeping` <= input$sliderlabel[2]) %>% ggplot() + geom_boxplot(aes(x=`Avg hrs per day sleeping`)) }) } shinyApp(ui, server)
使用 shinydashboard 构建仪表板

带图标的已渲染应用

图标演示

使用 shinydashboard 构建仪表板

Passons à la pratique !

使用 shinydashboard 构建仪表板

Preparing Video For Download...