Seu primeiro shinydashboard com elementos interativos
Você viu como é possível adicionar elementos interativos em um shinydashboard e também como incluir tabelas de dados interativas.
Neste exercício, os anúncios do Airbnb foram armazenados em um data frame chamado listings, e o mapa geoespacial foi armazenado como um objeto leaflet chamado m_london. O cabeçalho e a barra lateral foram armazenados como header e sidebar.
Funções auxiliares personalizadas:
make_plots: Plota boxplots ou violin plotsnum_listings: Calcula o número de anúncios com base no intervalo selecionadonum_private_rooms: Calcula o número de quartos privados como porcentagem de todos os anúncios, com base no intervalo selecionadomedian_price: Calcula o preço mediano, com base no intervalo selecionado Para saber mais sobre uma função auxiliar, você pode executá-la (comomake_plots) no console.
Este exercicio faz parte do curso
Construindo dashboards com shinydashboard
Instruções do exercicio
- Adicione boxplots/violin plots com
plotlychamados"plots". - Adicione uma tabela de dados chamada
"table". - Adicione um mapa
leafletchamado"map". - Defina os argumentos corretos em
dashboardPage().
exercicio interativo prático
Tente este exercicio completando este código de exemplo.
body <- dashboardBody(
tabItems(
tabItem(tabName = "charts",
fluidRow(
valueBoxOutput(outputId = "count"), valueBoxOutput(outputId = "prop"),valueBoxOutput(outputId = "med") ),
fluidRow(
tabBox(side = "left", id = "tabset", height = "500px",
tabPanel("Charts",
# Place plotly object here
fluidRow(box(___("plots", height = 500, width = 600)) ) ),
# Place dataTable object here
tabPanel("Data table", height = "500px", ___("table")) ),
box(side = "right", height = "200px", title = "Welcome to London!",),
box(side = "right", height = "385px", title = "Controls",
sliderInput(inputId = "range", label = "Select price range:",
min = 0, max = 25000,value = c(0,2500)),
selectInput(inputId = "select", label = "Select group:",
choices = c("Box plots", "Violin plots")) ) ) ),
tabItem(tabName = "map",
# Place leaflet object here
fluidRow(box(title = "Map of listings in London", ___("map", height = 600, width = 700))) ) ) )
# Set the correct arguments for dashboardPage()
ui <- dashboardPage(___, ___, ___)
server <- function(input, output) {
output$count <- renderValueBox(valueBox("Number of listings", num_listings(input$range),
icon = icon("house-user") ))
output$prop <- renderValueBox(valueBox("Private rooms", paste0(num_private_rooms(input$range), "% of all listings"),
icon = icon("eye"), color = "orange") )
output$med <- renderValueBox(valueBox("Median price", paste0(median_price(input$range), "£"),
icon = icon("money-bill-alt"), color = "olive") )
output$plots <- renderPlotly(make_plots(input$range, input$select))
output$table <- renderDataTable(filter(listings, price >= input$range[1], price <= input$range[2]) %>% select(c(name, neighbourhood, room_type, price)),
options = list(lengthMenu = c(5, 30, 50)))
output$map <- renderLeaflet(m_london)
}
shinyApp(ui, server)