Leaflet map legend in R Shiny app has no colors

When I try to add a legend to a card flyer for a card flyer (using the Flyer for the R package) built into the Shiny app, the legend does not display the colors of the color palette. Instead, it only displays the colors specified for the NA values, in this case white.

legend without colors

The application performs the following actions:

  • First it filters the data set based on user inputs
  • It then generates a choropleth chart from the filtered data.

This is the code I used to create the legend:

addLegend(position = "bottomleft", pal = pal, values = shp.data()$stat.selected, title = "Legend", opacity = .5) 

Where pal is a quantile color palette as shown below

 pal <-colorQuantile(c("#B2FF66","#66CC00","#4C9900","#336600","#193300"), NULL, n = 5, na.color="#FFFFFF") 

shp.data() is a reactive expression, which is a file file filtered based on user inputs, and stat_selected is the specific statistics that the user selects to display on colors.

I get the following warnings:

 Warning in is.na(x) : is.na() applied to non-(list or vector) of type 'NULL' Warning in is.na(values) : is.na() applied to non-(list or vector) of type 'NULL' 

First I tried to make a legend, following the example on the sheet for page R, and used the values = ~stat.selected for the addLegend function, but I got this error:

 Error in UseMethod("doResolveFormula") : no applicable method for 'doResolveFormula' applied to an object of class "NULL" 
+7
r shiny leaflet
source share
3 answers

I managed to display the colors by changing the way I was referring to the column of values ​​in the arguments to the AddLegend function. I put the stat.selected variable in double brackets, which seemed to fix the problem:

 addLegend(position = "bottomleft", pal = pal, values = shp.data()[[stat.selected]], title = "Legend", opacity = 1 ) 

For clarification, the stat.selected variable comes from the following switch statement:

  stat.selected <- isolate(switch(input$var.stat, "Total employment" = "tot_emp", "Mean annual wage" = "a_mean", "Mean hourly wage" = "h_mean", "Location quotient" = "loc_quotient" ) 

where "tot_emp" , "a_mean" , "h_mean" and "loc_quotient" are the column names in the shp.data spatial polygon data frame.

I assume the problem was that I was trying to pass the column name of the variable using $ .

I'm still pretty new to R, so if anyone can explain why the example in the documentation for Leaflet for R does not work, I would appreciate it.

+2
source share

I used to have a simple snippet that showed how to add legends. I did not use the value ~ to legend values, as the norm. I made a traditional DataFrame $ column and it works great.

Now this is an update to see how it all fits together. Here is a full-fledged comparison run after creating all the variable cuts, etc. The final cleared data frame was named zipData p>

 # create a full popup # add some HTML for editing the styles zipData$popUp <- paste('<strong>',zipData$Street, '</strong><br>', 'TIV = $',prettyNum(zipData$tiv, big.mark = ',',preserve.width = 'none'), '<br>', 'City: ', zipData$city, '<br>', 'YrBuilt = ', zipData$YearBuilt, '<br>', 'Construction = ', zipData$ConstructionCode, '<br>', 'Occupancy = ', zipData$OccupancyCode, '<br>', 'Premium = $' , prettyNum(zipData$Premium, big.mark = ',',preserve.width = 'none') , '<br>', 'GrossArea = ', prettyNum(zipData$GrossArea, big.mark = ',', preserve.width = 'none'), '<br>', 'RoofYr = ', zipData$RoofYearBuilt, '<br>') # set color scale for key factor colorsConst <- colorFactor(rainbow(4), zipData$ConstructionCode) # color scales for numerical bins colorstivValue <- colorFactor(palette = 'Accent', zipData$tivValueLvl) colorsYrBuilt <- colorFactor(palette = 'Spectral', zipData$yrBuiltLvl) colorsRoofYrBuilt <- colorFactor(palette = "YlOrRd", zipData$roofYrBuiltLvl) # begin the leaflet map construction # create the map opbject m <- leaflet() %>% addTiles() %>% # add different tiles for different color schemes addProviderTiles(providers$OpenStreetMap, group = 'Open SM') %>% addProviderTiles(providers$Stamen.Toner, group = 'Toner') %>% addProviderTiles(providers$CartoDB.Positron, group = 'CartoDB') %>% addProviderTiles(providers$Esri.NatGeoWorldMap, group = 'NG World') %>% setView(lng = -90, lat = 30, zoom = 10) %>% ############################## # this section is for plotting the variables # each variable below is a layer in the map # construction addCircleMarkers(data = zipData, lat = ~Lat, lng = ~Lon, color = ~colorsConst(ConstructionCode), popup = zipData$popUp, radius = 5, group = 'Construction') %>% # tiv addCircleMarkers(data = zipData, lat = ~Lat, lng = ~Lon, color = ~colorstivValue(tivLvl), popup = zipData$popUp, radius = ~tiv/20000, group = 'Bldg Value') %>% # year built addCircleMarkers(data = zipData, lat = ~Lat, lng = ~Lon, color = ~colorsYrBuilt(yrBuiltLvl), popup = zipData$popUp, radius = ~YearBuilt/250, group = 'Yr Built') %>% ###################################### # layer control addLayersControl( baseGroups = c('Open SM', 'Toner', 'Carto DB', 'NG World'), overlayGroups = c('Construction', 'TIV', 'Yr Built' ), options = layersControlOptions(collapsed = F) ) %>% ################################################# add the legends for each of the variables # construction addLegend('bottomright', pal = colorsConst, values = zipData$ConstructionCode, title = 'Construction Code', opacity = 1) %>% # tiv addLegend('bottomleft', pal = colorstivValue, values = zipData$tivLvl, title = 'TIV', opacity = 1) %>% # year built addLegend('topleft', pal = colorsYrBuilt, values = zipData$yrBuiltLvl, title = 'Yr Built', opacity = 1) m # Print the map 

Part of the map is shown below.

This displays the level control and build legend.

+8
source share

I had the same message

 Error in UseMethod("doResolveFormula") : no applicable method for 'doResolveFormula' applied to an object of class "NULL" 

from

  data <- data.frame(lng1 = c(1, 2, 3), lng2 = c(2, 3, 4), lat1 = c(1, 2, 3), lat2 = c(2, 3, 4), values = c(1, 2, 3)) pal_grid <- colorNumeric(palette = "YlGn", domain = data$values) leaflet() %>% addRectangles(lng1 = data$lng1, lat1 = data$lat1, lng2 = data$lng2, lat2 = data$lat2, fillColor = ~pal_grid(data$values), fillOpacity = 0.2, weight = 2, opacity = 0.5) 

The solution is to provide the sheets with the data you use to create an element in the main leaflet() call or in a call to any element that you add after that.

  • In the main flyer () call:

     data <- data.frame(lng1 = c(1, 2, 3), lng2 = c(2, 3, 4), lat1 = c(1, 2, 3), lat2 = c(2, 3, 4), values = c(1, 2, 3)) pal_grid <- colorNumeric(palette = "YlGn", domain = data$values) leaflet(data = data) %>% addRectangles(lng1 = data$lng1, lat1 = data$lat1, lng2 = data$lng2, lat2 = data$lat2, fillColor = ~pal_grid(data$values), fillOpacity = 0.2, weight = 2, opacity = 0.5) 
  • At the time of adding items:

     data <- data.frame(lng1 = c(1, 2, 3), lng2 = c(2, 3, 4), lat1 = c(1, 2, 3), lat2 = c(2, 3, 4), values = c(1, 2, 3)) pal_grid <- colorNumeric(palette = "YlGn", domain = data$values) leaflet() %>% addRectangles(data = data, lng1 = data$lng1, lat1 = data$lat1, lng2 = data$lng2, lat2 = data$lat2, fillColor = ~pal_grid(data$values), fillOpacity = 0.2, weight = 2, opacity = 0.5)` 
+2
source share

All Articles