Your first shinydashboard with interactive elements
You have seen how interactive elements can be added in a shinydashboard
, and also how interactive data tables can be added.
In this exercise, Airbnb listings have been stored as a data frame called listings
, and the geospatial map has been stored as a leaflet
object called m_london
. The header and side bar have been stored as header
and sidebar
.
Custom helper functions:
make_plots
: Plots box plots or violin plotsnum_listings
: Computes the number of listings, based on the range selectednum_private_rooms
: Computes the number of private rooms as a percentage of all listings, based on the range selectedmedian_price
: Computes the median price, based on the range selected To find out more about a helper function, you can run it (likemake_plots
) in the console.
This exercise is part of the course
Building Dashboards with shinydashboard
Exercise instructions
- Add a
plotly
box plots/violin plots called"plots"
. - Add a data table called
"table"
. - Add a
leaflet
map called"map"
. - Set the correct arguments in
dashboardPage()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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)