Dein erstes shinydashboard mit interaktiven Elementen
Du hast gesehen, wie man interaktive Elemente in ein shinydashboard einfügt und wie man interaktive Datentabellen hinzufügt.
In dieser Übung sind Airbnb-Angebote in einem Data Frame namens listings gespeichert, und die Geokarte wurde als leaflet-Objekt m_london gespeichert. Der Header und die Seitenleiste wurden als header bzw. sidebar abgelegt.
Eigene Hilfsfunktionen:
make_plots: erzeugt Boxplots oder Violinplotsnum_listings: berechnet die Anzahl der Angebote basierend auf dem ausgewählten Bereichnum_private_rooms: berechnet den Anteil privater Zimmer an allen Angeboten (in Prozent), basierend auf dem ausgewählten Bereichmedian_price: berechnet den Medianpreis basierend auf dem ausgewählten Bereich Um mehr über eine Hilfsfunktion zu erfahren, kannst du sie (z. B.make_plots) in der Konsole ausführen.
Diese Übung ist Teil des Kurses
Dashboards mit shinydashboard erstellen
Anleitung zur Übung
- Füge
plotly-Boxplots/Violinplots mit dem Namen"plots"hinzu. - Füge eine Datentabelle mit dem Namen
"table"hinzu. - Füge eine
leaflet-Karte mit dem Namen"map"hinzu. - Setze die richtigen Argumente in
dashboardPage().
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
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)