Hi! I want to make a map from a geojson file of eviction rates by county and when you hover over the county, it tells you the name and eviction rate for the county. However, I can't get it to work when I try to make it reactive. I am using data from the Princeton Eviction lab that I downloaded. It has a bunch of variables for different years. For instance, er-01 means the eviction rate in 2001. er-16 means the eviction rate in 2016. The data is from 2000 until 2016. Here is the reprex. Essentially, I want the fill of each county to be the eviction rate and update by year when the user selects a year. Any help is appreciated!
The following code returns this error: Error in as.vector(x, "list") :
cannot coerce type 'closure' to vector of type 'list'
library(shiny)
library(leaflet)
library(htmltools)
library(sf)
ui <- fluidPage(theme = shinytheme("flatly"),
navbarPage("Rental Landscape of Ohio",
# Adding a tab for my about page
tabPanel("Eviction Map",
sidebarPanel(
selectInput("mapvar",
label = "Choose",
# the following are var names so sf$er.00 is eviction rate in 2000 and er. 01 is 2001
choices = c("2000" = "sf$`er.00`", "2001" = "sf$`er.01`",
"2002" = "sf$`er.02`", "2003" = "sf$`er.03`")
)
),
mainPanel(
leafletOutput("evicmap")))))
server <- function(input, output){
sf <- read_sf(
"https://raw.githubusercontent.com/amalabdi/milestone_8/master/counties.geojson")
pal_val <- reactive({
as.character(input$mapvar)
})
pal<- reactive ({
colorNumeric("viridis", pal_val(), domain = NULL)
})
# creating labels for each county so when I hover over it, it tells me the eviction rate
labels <- reactive({sprintf(
"<strong>%s</strong><br/>%g percent eviction rate",
sf$n, input$mapvar
)}) %>%
lapply(htmltools::HTML)
output$evicmap <- renderLeaflet({
leaflet(sf) %>%
addProviderTiles(providers$Stamen.Toner) %>%
addPolygons(color = ~pal()(pal_val),
fillColor = ~pal()(pal_val),
highlight = highlightOptions(weight = 5,
fillOpacity = 0.7,
bringToFront = TRUE),
label = labels()
) %>%
addLegend(pal = pal(), values = pal_val())
})
}
if it helps, here is the code that I am trying to update so it changes by year. It currently is using er.03 which is 2003 eviction rate:
library(shiny)
library(leaflet)
library(htmltools)
library(sf)
sf <- read_sf(
"https://raw.githubusercontent.com/amalabdi/milestone_8/master/counties.geojson")
pal_val <- sf$er.03
pal <- colorNumeric("viridis", pal_val)
labels <- sprintf(
"<strong>%s</strong><br/>%g percent eviction rate",
sf$n, sf$er.03
) %>%
lapply(htmltools::HTML)
leaflet(sf) %>%
addProviderTiles(providers$Stamen.Toner) %>%
addPolygons(color = ~pal(pal_val),
fillColor = ~pal(pal_val),
highlight = highlightOptions(weight = 5,
fillOpacity = 0.7,
bringToFront = TRUE),
label = labels
) %>%
addLegend(pal = pal, values = pal_val)