Typy výstupů

Tvorba dashboardů pomocí shinydashboard

Png Kee Seng

Researcher

Výstupní a vykreslovací funkce

  • ShinyApp obsahuje výstupy
  • Pro každý výstup je třeba volat dvojici funkcí

    • První funkce se nachází v UI
    • Určuje umístění výstupu
    • Výstupní funkce mají typicky tvar:
      <type>output(<identifying label>, ...)
      
  • Druhý kód je definován ve funkci server

    • Má tvar:
      output$<identifying label> <- render<type>(...)
      
Tvorba dashboardů pomocí shinydashboard

Příklad: Studie spánku

  • Nejprve importujeme datovou sadu
    library(tidyverse)
    sleep <- read_csv("../data/Time Americans Spend Sleeping.csv")  
    
  • Sloupce datové sady
    $ 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" ...
    
Tvorba dashboardů pomocí shinydashboard

textOutput

  • Nejprve vložte textOutput do UI
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  textOutput("textlabel"))

server <- function(input, output) {
}

shinyApp(ui, server)
Tvorba dashboardů pomocí shinydashboard

renderText

  • Pro vykreslení textového výstupu vložte renderText do serveru
  • Je nutné použít notaci 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)
Tvorba dashboardů pomocí shinydashboard

Vykreslený textOutput

Ukázka textového výstupu bez vstupních interakcí.

Tvorba dashboardů pomocí shinydashboard

textOutput s interakcemi

  • Pro interakci mezi vstupy a výstupy je nutné volat vstupy pomocí input$
input$<type>
Tvorba dashboardů pomocí shinydashboard

textOutput s interakcemi

  • Jako příklad uvažujme textový vstup.
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)
Tvorba dashboardů pomocí shinydashboard

textOutput s interakcemi

Textový výstup s interakcemi.

Tvorba dashboardů pomocí shinydashboard

plotOutput

  • Vložte plotOutput() do UI
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

plotOutput("plotlabel"))
server <- function(input, output) { } shinyApp(ui, server)
Tvorba dashboardů pomocí shinydashboard

renderPlot

  • renderPlot() je nutné umístit do server()
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)
Tvorba dashboardů pomocí shinydashboard

Vykreslený plotOutput

Ukázka plotOutput.

Tvorba dashboardů pomocí shinydashboard

plotOutput se selectInput

  • Uživatelské vstupy lze využít k vytváření různých výsledků
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  selectInput("selectlabel", 
              "Select an option",
              choices = c("Histogram", "Boxplot")),
  plotOutput("plotlabel"))
Tvorba dashboardů pomocí shinydashboard

plotOutput se selectInput

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)
Tvorba dashboardů pomocí shinydashboard

plotOutput se selectInput

Ukázka plotOutput se selectInput

Tvorba dashboardů pomocí shinydashboard

plotOutput se sliderInput

  • Uvažujme další příklad se 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)
Tvorba dashboardů pomocí shinydashboard

plotOutput se sliderInput

Ukázka plotOutput se sliderInput

Tvorba dashboardů pomocí shinydashboard

Ikony v shiny

  • shiny obsahuje také ikony použitelné v shinyApp

Některé ikony v shiny

1 https://fontawesome.com/icons
Tvorba dashboardů pomocí shinydashboard

Ikony v shiny: příklad

  • Použijte icon("...")
  • Připomeňme dřívější příklad
  • Např. 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)
Tvorba dashboardů pomocí shinydashboard

Vykreslená aplikace s ikonou

Ukázka ikony

Tvorba dashboardů pomocí shinydashboard

Lass uns üben!

Tvorba dashboardů pomocí shinydashboard

Preparing Video For Download...