2

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

      )



  })
brian
  • 75
  • 1
  • 8
  • 1
    Please share a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Urvah Shabbir Aug 15 '18 at 08:35
  • I have asked a reproducible one.Check out : https://stackoverflow.com/questions/51871986/observe-not-responding-to-user-input – brian Aug 16 '18 at 07:53

1 Answers1

0

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.

Adam Wheeler
  • 393
  • 1
  • 11
  • The error is no longer after the edit but there is no reactivity based on the user input that is I want when a user selects input$area there is a color mapping on polygons based on the different categorical variables(mixed,unplanned,planned). – brian Aug 15 '18 at 13:05
  • As urwacfc pointed out, this is a very limited example and is not reproducible. So I cant run it to understand why it is not working. However, I will say that what makes sense to be the issue here is that the value of data in your reactive functions isn't actually changing. can you verify that the input$area actually invalidates and changes the output of fdata? – Adam Wheeler Aug 15 '18 at 14:45
  • I have asked a reproducible one.Check out : https://stackoverflow.com/questions/51871986/observe-not-responding-to-user-input – brian Aug 16 '18 at 07:51