2
votes

I want to color map my polygons based on the user input. The column i am using has categorial variables so I am using the colorFactor function which I have tested it is functioning normally. The issue is with the observe function when I load my shiny app it terminates immediately and outputs "Error in addPolygons: unused argument (fillcolor = ~pal(AreaTyp)) in leaflet".My question is how to include reactivity correctly using the observe function. Here is my code:

#INTERACTIVE MAPPING
  #colorfunction
  pal<-colorFactor(rainbow(7),mp$AreaTyp)

  #set data based on user input
  fdata<-reactive({
    data<-mp
    if(input$area!="All"){
      data<-data[data$AreaType==input$area,]
    }
    data
  })



  output$leaf<-renderLeaflet({

    leaflet(fdata()) %>%

      #Initializing the map
      setView(lng=36.092245, lat=-00.292115,zoom=15)%>%

      #Base map
      #Add default OpenStreetMap map tiles
      addTiles(group = "default")%>%
      #addProviderTiles("Esri.NatGeoWorldMap",group = "default")%>%  
      #addProviderTiles("CartoDB.Positron",group = "custom")%>%

      #Overlay map
      addPolygons(
        data = fdata(),
        fillColor = "blue",
        weight = 1, smoothFactor = 0.5,
        opacity = 1.0, fillOpacity = 1.0,
        group = "basepoly",
        highlightOptions = highlightOptions(
          weight = 2,
          color = "red",
          fillOpacity = 0.7,
          bringToFront = TRUE
        ),label =~LIA


      )


  })


  observe({

    leafletProxy("leaf",data = fdata()) %>%


      clearShapes() %>%
      addPolygons(
        weight = 1, smoothFactor = 0.5,
        opacity = 1.0, fillOpacity = 1.0,
        data=fdata(),
        fillcolor = ~pal(AreaTyp),
        label =~LIA

      )



  })
1
Please share a reproducible example: stackoverflow.com/questions/5963269/…Urvah Shabbir
I have asked a reproducible one.Check out : stackoverflow.com/questions/51871986/…brian

1 Answers

0
votes

Change fillcolor = ~pal(AreaTyp) to fillColor = ~pal(AreaTyp)

Let's break down your error.

"Error in addPolygons: unused argument (fillcolor = ~pal(AreaTyp)) in leaflet"

first:

"Error in addPolygons:

this means that addPolygons failed to run. not that the observer failed

second

"unused argument "

This means that you added an argument that addpolygons can not use.

Third

(fillcolor = ~pal(AreaTyp)) in leaflet

This is telling you exactly which argument is wrong.