0

I'm developing a shiny app to show a choropleth map, using leaflet and a shapefile to add the polygons. Outside shiny everything works fine, but with shiny I got a problem when using addLegend as follows:

mypal <- reactive({
        colorBin(palette = "RdYlBu", domain = get("map_fires")[[paste0("qm_", selectedYear())]], 
                           bins = 5, reverse = TRUE, pretty = FALSE,
                           na.color = "#FFFAFA")
    })


output$map <- renderLeaflet({
        leaflet() %>% 
            addProviderTiles("OpenStreetMap.Mapnik") %>%
            addPolygons(data = map_fires,
                        stroke = TRUE, weight = 1, smoothFactor = 0.2, fillOpacity = 0.3,
                        fillColor = ~mypal()(get("map_fires")[[paste0("qm_", selectedYear())]]),
                        highlight = highlightOptions(
                            weight = 5,
                            color = "#666",
                            fillOpacity = 0.7,
                            bringToFront = TRUE),
                        label= lapply(stats_labels(), HTML), 
                        labelOptions = labelOptions(
                            style = list("font-weight" = "normal", padding = "3px 8px"),
                            textsize = "15px",
                            direction = "auto")) %>%
            addLegend(position = "bottomright", pal = ~mypal(), 
                      values = ~get("map_fires")[[paste0("qm_", selectedYear())]], 
                      opacity = 1)
    })

The code above returns:

Warning: Error in if: argument is of length zero
  104: addLegend
...

I'm using paste0("qm_", selectedYear()) to refer to different columns with yearly data.

I believe the problem is in values = ~get("map_fires")[[paste0("qm_", selectedYear())]], but can't tell why. I have already tried to specify the dataset on addLegend, and also to put a specific column on values = (i.e., without depending on the reactive value of selectedYear()), to no avail.

Outside shiny things work good with the code below (where I keep using get("map_fires")[[paste0("qm_", 2018)]], changing selectedYear() for a specific year, just to better show my case):

mypal <- colorBin(palette = "RdYlBu", domain = get("map_fires")[[paste0("qm_", 2018)]], 
                  bins = 5, reverse = TRUE, pretty = FALSE,
                  na.color = "#FFFAFA")

leaflet() %>% 
    addProviderTiles("OpenStreetMap.Mapnik") %>%
    addPolygons(data = map_fires,
                stroke = TRUE, weight = 1, smoothFactor = 0.2, fillOpacity = 0.3,
                fillColor = ~mypal(get("map_fires")[[paste0("qm_", 2018)]]),
                highlight = highlightOptions(
                    weight = 5,
                    color = "#666",
                    fillOpacity = 0.7,
                    bringToFront = TRUE),
                label= lapply(stats_labels, HTML), 
                labelOptions = labelOptions(
                    style = list("font-weight" = "normal", padding = "3px 8px"),
                    textsize = "15px",
                    direction = "auto")) %>%
    addLegend(position = "bottomright", pal = mypal, 
              values = get("map_fires")[[paste0("qm_", 2018)]],
              opacity = 1)

Am I missing something?

cassianord
  • 11
  • 4
  • why use `get("map_fires")` instead of just `map_fires`? What happens if you change it to that? Also, I would look into this line: `fillColor = ~mypal()(get("map_fires")[[paste0("qm_", selectedYear())]]),` – Phil Nov 02 '19 at 22:42
  • I was running into another problem without `get`, but now I realize I don't really need it. Anyway, removing it and keeping only `map_fires[[paste0("qm_", selectedYear())]]` didn't change the issue. As for the line `fillColor = ~mypal()(get("map_fires")[[paste0("qm_", selectedYear())]])`, it is not the problem, the app works fine with it if I don't add the legend. – cassianord Nov 02 '19 at 23:09
  • Without a sample of your data to reproduce the issue, I wouldn't be able to look into it further. – Phil Nov 02 '19 at 23:18
  • I've added the code and a data sample here: https://github.com/cassianord/fires_sample Thank you so much, Phil! – cassianord Nov 03 '19 at 12:59
  • Within `addLegend()`, what happens if you remove the `~` in the argument `pal = ~mypal()`? i.e. change it to `pal = mypal()` – Phil Nov 03 '19 at 17:56
  • 1
    It worked! I'm almost sure I have tried to remove the `~` before, but probably I had then other problems too that prevented the code from working. Thank you again, @Phil! – cassianord Nov 03 '19 at 22:37

0 Answers0