Primul tău shinydashboard cu elemente interactive
Ai văzut cum pot fi adăugate elemente interactive într-un shinydashboard, dar și cum pot fi incluse tabele de date interactive.
În acest exercițiu, anunțurile Airbnb au fost stocate ca un data frame numit listings, iar harta geospațială a fost stocată ca un obiect leaflet numit m_london. Antetul și bara laterală au fost stocate ca header, respectiv sidebar.
Funcții ajutătoare personalizate:
make_plots: Generează box plot-uri sau violin plot-urinum_listings: Calculează numărul de anunțuri în funcție de intervalul selectatnum_private_rooms: Calculează numărul de camere private ca procent din totalul anunțurilor, în funcție de intervalul selectatmedian_price: Calculează prețul median în funcție de intervalul selectat Pentru a afla mai multe despre o funcție ajutătoare, o poți rula (de exemplu,make_plots) în consolă.
Acest exercițiu face parte din cursul
Crearea dashboardurilor cu shinydashboard
Instrucțiuni pentru exercițiu
- Adaugă un grafic
plotlyde tip box plot/violin plot cu numele"plots". - Adaugă un tabel de date cu numele
"table". - Adaugă o hartă
leafletcu numele"map". - Setează argumentele corecte în
dashboardPage().
Exercițiu interactiv practic
Încearcă acest exercițiu completând acest cod de exemplu.
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)