Sua primeira shinyApp
Você viu como entradas e saídas podem ser combinadas em uma shinyApp. Você também viu como uma shinyApp pode ser construída para comunicar resultados do estudo do sono.
Seu objetivo agora é criar uma shinyApp para apresentar dois resultados principais:
- A distribuição das horas de sono
- A mediana de horas de sono entre diferentes faixas etárias
Neste exercício, os dados foram armazenados em um data frame do dplyr chamado sleep, e as bibliotecas shiny e tidyverse já foram carregadas.
Agora é a sua vez de criar sua própria shinyApp!
Este exercicio faz parte do curso
Construindo dashboards com shinydashboard
Instruções do exercicio
- Adicione duas saídas de gráfico chamadas "histogram" e "barchart" ao
mainPanel. - No argumento
choiceNamesemcheckboxGroupInput(), substitua o conteúdo por ícones chamados "calendar", "briefcase" e "gift". - Defina as duas saídas, uma chamada
histograme outra chamadabarchart. - Use a função
shinyApp()para renderizar o aplicativo shiny.
exercicio interativo prático
Tente este exercicio completando este código de exemplo.
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
___