A Nice Example
We will use the same toilets dataset.
We mutate the table to add the columns for any specific filters that
we require.
toilets <- toilets |>
mutate(Accessibility = if_else(isHandicappedAccessible == 0, "Handicapped Accessible", "Not Handicapped Accessible")) |>
mutate(BabyFacilities = if_else(hasChangingTable == 0, "Changing table", "No changing table"))
We would like to see the distribution of the toilets across Berlin,
based on whether they are paid or not.
#We assign different colours to different districts.
color_options <- c(unique(toilets$District))
#We filter the toilets based on whether they are free or paid.
price_options <- c(unique(toilets$Free))
price_filter <- dccChecklist(
id = 'price-filter',
options = price_options,
value = price_options #default, choose all
)
#We can also add additional filters like accessibility for the handicapped and child facilities:
handicap_options <- c(unique(toilets$Accessibility))
handicap_filter <- dccChecklist(
id = 'handicap-filter',
options = handicap_options,
value = handicap_options #default, choose all
)
baby_options <- c(unique(toilets$BabyFacilities))
baby_filter <- dccChecklist(
id = 'baby-filter',
options = baby_options,
value = baby_options #default, choose all
)
We assign the graph to a new variable and give instructions for
callback based on the input(s) we provided.
#Output component(s)
toilets_map <- dccGraph(id = 'toilets-map')
#Callback(s)
app |> add_callback(
output('toilets-map', 'figure'),
list(
#input('district-dropdown', 'value'),
input('price-filter', 'value'),
input('handicap-filter', 'value'),
input('baby-filter', 'value')
),
function(price_input, handicap_input, baby_input) { #district_input
#function(color_input) {
toilets <- toilets |>
filter(Free %in% price_input) |>
filter(Accessibility %in% handicap_input) |>
filter(BabyFacilities %in% baby_input)
fig = toilets |>
filter(District != "NA") |>
plot_ly(lat = ~Latitude, lon = ~Longitude, type = "scattermapbox",
split = ~District, size = 5, #toilets[[color_input]]
hoverinfo = "text+name", hovertext = ~Street) |>
layout(
title = "Berlin public toilets",
mapbox = list(
style = 'open-street-map',
zoom = 10,
center = list(lat = ~median(Latitude), lon = ~median(Longitude)))
)
return(fig) #not needed but just to be explicit
}
)
We also need to define the aesthetics of the dashboard:
#3 Front-end layout
layout_elements <- list(
h1("IDS Workshop - Plotly-Dash test"),
div("Using the Berlin Public Toilets dataset to test Scatter Map functionality..."),
br(),
div("Type:"),
price_filter,
br(),
div("Accessibility:"),
handicap_filter,
br(),
div("Baby facilities:"),
baby_filter,
br(),
toilets_map
)
Finally, we run the app:
#4 Main app
app |> set_layout(layout_elements)
app |> run_app()
We can also add additional filters to the data, as and when
required.
Q.5. Find the distribution of paid public toilets in
Berlin, that are also handicapped accessible and have a changing
table.
Q.6. You have just submitted the materials for the
IDS workshop on Monday and it is officially the end of the mid-term exam
week. You go out with your friends to grab a few beers. And the weather
is really cold. Are there any free toilets in Mitte that you could use
after a grabbing some drinks with your friends?