Types de sortie

Créer des tableaux de bord avec shinydashboard

Png Kee Seng

Researcher

Fonctions output et render

  • Dans une shinyApp, il y a des sorties
  • Pour chaque sortie, il faut appeler une paire de fonctions

    • La première est une fonction de l'UI
    • Elle indique où placer la sortie
    • Ce type de fonction prend généralement la forme :
      <type>output(<identifying label>, ...)
      
  • Le second code requis est défini dans la fonction server

    • Il prend la forme :
      output$<identifying label> <- render<type>(...)
      
Créer des tableaux de bord avec shinydashboard

Exemple : étude du sommeil

  • Importons d'abord le jeu de données
    library(tidyverse)
    sleep <- read_csv("../data/Time Americans Spend Sleeping.csv")  
    
  • Voici les colonnes
    $ 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" ...
    
Créer des tableaux de bord avec shinydashboard

textOutput

  • Plaçons d'abord textOutput dans l'UI
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  textOutput("textlabel"))

server <- function(input, output) {
}

shinyApp(ui, server)
Créer des tableaux de bord avec shinydashboard

renderText

  • Pour afficher le texte, placez renderText dans le serveur
  • Nous aurons besoin de la notation 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)
Créer des tableaux de bord avec shinydashboard

textOutput rendu

démo de sortie texte sans interactions d'entrée.

Créer des tableaux de bord avec shinydashboard

textOutput avec interactions

  • Pour permettre des interactions entre entrées et sorties, il faut appeler les entrées via input$
input$<type>
Créer des tableaux de bord avec shinydashboard

textOutput avec interactions

  • Par exemple, prenons une entrée texte.
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)
Créer des tableaux de bord avec shinydashboard

textOutput avec interactions

sortie texte avec interactions.

Créer des tableaux de bord avec shinydashboard

plotOutput

  • Placez plotOutput() dans l'UI
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

plotOutput("plotlabel"))
server <- function(input, output) { } shinyApp(ui, server)
Créer des tableaux de bord avec shinydashboard

renderPlot

  • Il faut placer renderPlot() dans 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)
Créer des tableaux de bord avec shinydashboard

plotOutput rendu

démo plotOutput.

Créer des tableaux de bord avec shinydashboard

plotOutput avec selectInput

  • Les entrées utilisateur peuvent produire des résultats différents
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  selectInput("selectlabel", 
              "Select an option",
              choices = c("Histogram", "Boxplot")),
  plotOutput("plotlabel"))
Créer des tableaux de bord avec shinydashboard

plotOutput avec 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)
Créer des tableaux de bord avec shinydashboard

plotOutput avec selectInput

Démo de plotOutput avec selectInput

Créer des tableaux de bord avec shinydashboard

plotOutput avec sliderInput

  • Voyons un autre exemple avec 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)
Créer des tableaux de bord avec shinydashboard

plotOutput avec sliderInput

Démo de plotOutput avec sliderInput

Créer des tableaux de bord avec shinydashboard

Icônes dans shiny

  • shiny contient aussi des icônes utilisables dans une shinyApp

Quelques icônes dans shiny

1 https://fontawesome.com/icons
Créer des tableaux de bord avec shinydashboard

Icônes dans shiny : un exemple

  • Utilisez icon("...")
  • Rappelez-vous un exemple précédent
  • Par ex. : 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)
Créer des tableaux de bord avec shinydashboard

Application rendue avec icône

Démo d'icône

Créer des tableaux de bord avec shinydashboard

Passons à la pratique !

Créer des tableaux de bord avec shinydashboard

Preparing Video For Download...