Hi Everyone!
I am currently creating a Shiny app using a combination of the R Leaflet and R Shiny packages. I am able to create a static map but I am having trouble with making my map interactive. I am very new to both this community and coding so please bare with me.
I think I know exactly where my code is having trouble and I have been trying to trouble shoot it for the past day but I feel like I am running into a brick wall repeatedly. I believe I have provided everything in this post.
The Data:
- Vancouver's Shape Files (I downloaded the whole ShapeFile Data set in "Export"):
Local area boundary — City of Vancouver Open Data Portal
In my code this is "vancouver "
- Vancouver's 2016 Census Data:
I made some changes to the census data, and the file I used is this:
Census_all_flip - Google Sheets
In my code this is "census_all "
(I downloaded the "Census local area profiles 2016 (CSV)")
Source Data: Census local area profiles 2016 — City of Vancouver Open Data Portal
My code:
## Library of installed packages
library(leaflet)
library(magrittr)
library(sf)
library(geojsonio)
library(htmltools)
library(htmlwidgets)
library(stringi)
library(RColorBrewer)
library(shiny)
library(shinyWidgets)
# I read the shape file of vancouver's local-area-boundary
vancouver <- read_sf('~/Desktop/local-area-boundary/local-area-boundary.shp')
census_all <- read.csv('~/Desktop/Census_all_flip.csv')
#
is.element(census_all$name, vancouver$name) %>%
all()
# Merging vancouver with census data
vancouver <- merge(vancouver, census_all, by = 'name', all.x = F)
paletteNum <- colorNumeric('Blues', domain = NULL)
# Labeling
vancouverLabels <- sprintf('<b>%s</b><br/>%g people',
vancouver$name, vancouver$X0.to.14.years) %>%
lapply(function(x) HTML(x))
vancouver <- cbind(vancouver, matrix(vancouverLabels, ncol = 1, dimnames = list(c(), c('vancouverLabels'))))
# Integrating Leaflet with Shiny
ui <- fluidPage(
# Title
titlePanel("Census Data"),
#Leaflet Map
leafletOutput("mymap"),
absolutePanel(
pickerInput(
inputId = "agegroup",
label = "Select an Age Group",
choices = c("X0.to.14.years", "X0.to.4.years", "X5.to.9.years", "X10.to.14.years", "X15.to.64.years")),
)
)
server <- function(input, output, session) {
## (SOMETHING WRONG HERE)
filteredData <- reactive({
vancouver[input$agegroup,]
})
## (SOMETHING WRONG HERE)
# This reactive expression represents the palette function,
# which changes as the user makes selections in UI.
paletteNum<- reactive({
colorNumeric('blues', vancouver[input$agegroup,])
})
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$CartoDB.PositronNoLabels) %>%
setView(lng = -123.11934, lat = 49.24966, zoom = 11) %>%
addPolygons(data = vancouver,
color = 'white',
weight = 1,
smoothFactor = .3,
fillOpacity = .75,
fillColor = ~paletteNum(vancouver$X0.to.14.years),
label = ~vancouverLabels,
labelOptions = labelOptions(
style = list(color = 'gray30'),
textsize = '10px'),
highlightOptions = highlightOptions(
weight = 3,
color = 'dodgerblue'
)
) %>%
addLegend(pal = paletteNum, values = vancouver$X0.to.14.years,
title = '<small>2016 Vancouver Census Data <br> Population: Ages 0 to 14 </small>',
position = 'bottomleft')
})
}
shinyApp(ui, server)
I know I also need a reactive output with leafletProxy where my fillColor will update depending on which age group is selected, and also my legend, however, I feel super stuck with the code I have already. If anyone could provide some pointers I would be so grateful. I at sorry I cannot provide the data in an easier manner... I have tried and I think I made it worse trying to create a dummy dataframe to showcase this problem.
The problem I have ran into seems to be coming from my "filteredData" and "paletteNum" functions but I have no clue what I am doing wrong even though I know something is wrong.