Il tuo primo shinydashboard con elementi interattivi
Hai visto come aggiungere elementi interattivi in un shinydashboard e anche come inserire tabelle di dati interattive.
In questo esercizio, gli annunci Airbnb sono stati salvati in un data frame chiamato listings, e la mappa geospaziale è stata salvata come oggetto leaflet chiamato m_london. L'header e la barra laterale sono stati salvati come header e sidebar.
Funzioni helper personalizzate:
make_plots: crea box plot o violin plotnum_listings: calcola il numero di annunci, in base all'intervallo selezionatonum_private_rooms: calcola la percentuale di camere private sul totale degli annunci, in base all'intervallo selezionatomedian_price: calcola il prezzo mediano, in base all'intervallo selezionato Per saperne di più su una funzione helper, puoi eseguirla (ad esempiomake_plots) nella console.
Questo esercizio fa parte del corso
Creare dashboard con shinydashboard
Istruzioni dell'esercizio
- Aggiungi box plot/violin plot
plotlychiamati"plots". - Aggiungi una tabella di dati chiamata
"table". - Aggiungi una mappa
leafletchiamata"map". - Imposta gli argomenti corretti in
dashboardPage().
esercizio interattivo pratico
Prova questo esercizio completando questo codice di esempio.
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)