La tua prima shinyApp
Hai visto come input e output possono essere combinati in una shinyApp. Hai anche visto come una shinyApp può essere costruita per comunicare i risultati dello studio sul sonno.
Il tuo obiettivo ora è creare una shinyApp che riporti due risultati principali:
- La distribuzione delle ore di sonno
- Le ore di sonno mediane tra diversi gruppi di età
In questo esercizio, abbiamo salvato i dati come un data frame di dplyr chiamato sleep, e le librerie shiny e tidyverse sono già state caricate.
Ora tocca a te creare la tua shinyApp!
Questo esercizio fa parte del corso
Creare dashboard con shinydashboard
Istruzioni dell'esercizio
- Aggiungi due output di tipo grafico chiamati "histogram" e "barchart" al
mainPanel. - Nell'argomento
choiceNamesincheckboxGroupInput(), sostituisci il contenuto con icone chiamate "calendar", "briefcase" e "gift". - Definisci i due output, uno chiamato
histograme un altro chiamatobarchart. - Usa la funzione
shinyApp()per renderizzare la shiny app.
esercizio interattivo pratico
Prova questo esercizio completando questo codice di esempio.
ui <- fluidPage(
titlePanel("Sleeping habits in America"),
fluidRow(
# Place two plots here, called "histogram" and "barchart"
mainPanel(___("histogram"), ___("barchart")),
inputPanel(sliderInput("binwidth",
label = "Bin width",
min = 0.1, max = 2,
step = 0.01, value=0.25),
checkboxGroupInput("days", "Choose types of days:",
# Replace the list elements with icons called "calendar", "briefcase" and "gift"
choiceNames = list("All days",
"Non-holiday weekdays",
"Weekend days/holidays"),
choiceValues = list("All days",
"Nonholiday weekdays",
"Weekend days and holidays"))),
"In general, across the different age groups, Americans seem to get adequate daily rest." ))
server <- function(input, output, session) {
# Define the histogram and barchart
output$histogram <- ___({
ggplot(sleep, aes(x=`Avg hrs per day sleeping`)) +
geom_histogram(binwidth = input$binwidth, col='white') +
theme_classic()
})
___ <- ___({
filter(sleep, `Type of Days` %in% input$days) %>%
group_by(`Type of Days`, `Age Group`) %>%
summarize(`Median hours` = median(`Avg hrs per day sleeping`)) %>%
ggplot(aes(x = `Median hours`, y = `Age Group`, fill = `Type of Days`)) +
geom_col(position = 'dodge') + theme_classic()
})
}
# Use shinyApp() to render the shinyApp
___