2

i would like to generate colored polygons, with dynamic values based on option choosen form combo box (specific supplier name).

I used leaflet package to print polygons based on 'SpatialPolygonsDataFrame' object class. And it works fine. I have problems with using 'reactive' shiny function - is change object class from 'SpatialPolygonsDataFrame' to 'data.frame' - creatling leflet map is impossible.

Here is part of my code:

ui <- pageWithSidebar(
  sidebarPanel(
    # supplier selection
    selectInput(inputId = "inpSuppl", label = "supplier:",
      choices = sort(x=names(table(db$SUPPLIER)), decreasing=FALSE),
      selected = sort(x=names(table(db$SUPPLIER)), decreasing=FALSE)[[1]]
    )
  ),
  mainPanel(leafletOutput("myMap"))
)

server <- function(input, output, session) {
  myData <- reactive({
    data <- data[data$SUPPLIER==input$inpSuppl, c("A_COLUMN")]
  })

  output$myMap <- renderLeaflet({
    m <- leaflet()
    m <-  addTiles(m)
    m <-  addPolygons(map = m, data = myData(), stroke = FALSE, fillColor = ~pal(mapval))
  })
}

shinyApp(ui=ui, server=server)

Any idea?


Thanks so much. I have another problem - with color palette. i would like to change number of clusters and then color polygons. When i start app everything is all right, but after i change down number of clusters in input field, number of cluster don't work proper (number of cluster don't shrink).

ui <- bootstrapPage(
  leafletOutput("myMap", width = "100%", height = "100%"),
  absolutePanel(top = 100, left = 10, width = "160px",
    # input - supplier
    selectInput(inputId = "inpSuppl", label = "supplier:",
      choices = sort(x=names(table(db$SUPPLIER)), decreasing=FALSE),
      selected = sort(x=names(table(db$SUPPLIER)), decreasing=FALSE)[[1]]
    ),
    # input - variable type
    selectInput(inputId = "inpVar", label = "variable:", choices = c("turnover" = "VAL_1", "basket" = "VAL_2")),
    # numeric input - number of clusters        
    numericInput(inputId="inpClust", label="number of clusters:", value=3, step=1)
  )
)

# --------------------------------------------------------------------------

server <- function(input, output, session) {

  # dynamic number of clusters
  clusters <- reactive({input$inpClust})

  # dynamic data set - adding specific variable from db object to sh2 object
  ld <- reactive({
    # add aditional data to sh2 object (SpatialPolygonsDataFrame class)
    # from db obejct (data.frame class)
    sh2$mapval <- db[db$SUPPLIER==input$inpSuppl, c(input$inpVar)]
    # create clusters
    k <- kmeans(sh2$mapval, clusters())
    # add clusters to sh2 object (SpatialPolygonsDataFrame class)
    sh2$cluster <- k$cluster
    return(sh2)
  })

  # create color pal
  colorpal <- reactive({colorNumeric(palette="YlOrRd", ld()$cluster)})

  # static map elements
  output$myMap <- renderLeaflet({
    m <- leaflet("myMap") # preapare leaflet object
    m <- addTiles(m)
    m <- addPolygons(m, data = sh0, color = "black", weight = 1, fillColor = "black") # country polygon
  })

  # dynamic map elements
  observe({
    pal <- colorpal()
    leafletProxy("myMap", data = ld()) %>%
      addPolygons(stroke = FALSE, fillColor = ~pal(cluster), fillOpacity=0.6, popup = ~paste(mapval)) # powiats polygons
  })

}
6
  • Do you have an example of your data?
    – troh
    Commented Jul 16, 2017 at 18:20
  • I downloaded 'shp file from here: 'codgik.gov.pl/index.php/darmowe-dane/prg.html' PRG – jednostki administracyjne. I would like to 1) filter data based on choosen supplier, 2) dynamic add data to 'SpatialPolygonsDataFrame' objecet 3) print result on leaflet map.
    – Tomasz
    Commented Jul 16, 2017 at 18:24
  • I don't think your link pasted in
    – troh
    Commented Jul 16, 2017 at 18:25
  • 1
    You can pass shape files directly to leaflet without converting to a 'SpatialPolygonsDataFrame'. If the shapefiles are in a names list from the choices you could then use data[input$inpSuppl], and nothing will be changed.
    – troh
    Commented Jul 16, 2017 at 18:30
  • 1
    myData <- reactive({ data <- data[data$SUPPLIER==input$inpSuppl, c("A_COLUMN")] data }) If you tell the reactive function to return instead of just assigning, it should fix your problem. Also, you're only returning one column. Do you need to return more?
    – troh
    Commented Jul 16, 2017 at 18:34

1 Answer 1

3
ui <- pageWithSidebar(
  sidebarPanel(
    # supplier selection
    selectInput(inputId = "inpSuppl", label = "supplier:",
      choices = sort(x=names(table(db$SUPPLIER)), decreasing=FALSE),
      selected = sort(x=names(table(db$SUPPLIER)), decreasing=FALSE)[[1]]
    )
  ),
  mainPanel(leafletOutput("myMap"))
)

server <- function(input, output, session) {
  myData <- reactive({
    data <- data[data$SUPPLIER==input$inpSuppl, c("A_COLUMN")]
    data
  })

  output$myMap <- renderLeaflet({
    m <- leaflet()
    m <-  addTiles(m)
    m <-  addPolygons(map = m, data = myData(), stroke = FALSE, fillColor = ~pal(mapval))
  })
}

shinyApp(ui=ui, server=server)

Per the comments above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.