Correction exemple de Shiny Dashboard

Correction du TD Shiny Dashboard

Correction

ui.r

library(shiny)
library(shinydashboard)

dashboardPage(skin = "green",
  dashboardHeader(title = "Star Wars"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Stats", tabName = "stats", icon = icon("grip")),
      menuItem("Graphiques", tabName = "graphiques", icon = icon("chart-column"))
    ),
    sliderInput("nbIndividus", "Nb individus", 1, 87, 10),
    selectInput("color", "Color for distribution:",
                c("Hair" = "hair_color",
                  "Skin" = "skin_color",
                  "Eye" = "eye_color"))
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "stats",
              fluidRow(
                valueBox(42, "Box fixe", icon = icon("bars")),
                valueBoxOutput("individusBox"),
                valueBoxOutput("dimensionsBox"),
                infoBoxOutput("heightBox"),
                infoBoxOutput("massBox")
              )
      ),
      tabItem(tabName = "graphiques",
              fluidRow(
                box(
                  title = "Height & mass correlation", status = "primary", solidHeader = TRUE,
                  plotlyOutput("correlation")
                ),
                box(
                  title = "Distribution of color", status = "warning",
                  plotlyOutput("count")
                )
              )
      )
    )
  )
)

server.r

library(shiny)
library(ggplot2)
library(plotly)
library(dplyr)

function(input, output, session) {
  
  dataset <- starwars

  filtered <- reactive({
    slice_head(dataset, n = input$nbIndividus)
  })
  
  output$individusBox <- renderValueBox(
    valueBox(nrow(filtered()), "Individus", icon = icon("list-ul"), color = "red")
  )
  
  output$dimensionsBox <- renderValueBox(
    valueBox(ncol(filtered()), "Dimensions", icon = icon("table-columns"), color = "yellow")
  )
  
  output$heightBox <- renderInfoBox(
    infoBox("Average Height (cm)", round(mean(filtered()$height, na.rm = TRUE), 1), icon = icon("arrow-up"), color = "purple")
  )
  
  output$massBox <- renderInfoBox(
    infoBox("Median mass (kg)", median(filtered()$mass, na.rm = TRUE), icon = icon("weight-hanging"), color = "black")
  )

  output$correlation <- renderPlotly(
    ggplotly(ggplot(filtered(), aes(x = mass, y = height, color = species)) + geom_point())
  )
  
  output$count <- renderPlotly(
    ggplot(filtered(), aes(x = eval(as.name(input$color)))) + geom_bar() + xlab(input$color)
  )
  
}
Conception de visualisations de données
Conception de visualisations de données
Printemps 2024

Cours de conception de visualisations de données du printemps 2024.