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
})
}
data[input$inpSuppl]
, and nothing will be changed.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?