CER
CER

Reputation: 889

Leaflet colorQuantile 'breaks' are not unique

I work on some data with shiny and leaflet and run into a problem which seems to pop up here and there but I could not find a solution or implement the pieces I found

Lets use

library(leaflet)

a <- c(5,4,1,1,2,1,1,1)

vector a is a result of some filter in the app and can change. It works if the values are well distributed but in the above example it crashes the app when using following

pal <- colorQuantile("YlGn", a, n = 5)

And using

pal(a)

in

  leafletProxy("myMap", data = myData) %>%
      clearShapes() %>%
      addPolygons(data = theData,
                  fillColor = pal(a), 
                  fillOpacity = 0.8, 
                  color = "#BDBDC3", 
                  weight = 2,
                  popup = borough_popup)  

with

Error in cut.default(x, binsToUse, labels = FALSE, include.lowest = TRUE, : 'breaks' are not unique

I found some GitHub comments here but struggle to implement it into a working solution.

I need some way to get around this problem as I cannot determined beforehand how much bins may work.

Upvotes: 4

Views: 3810

Answers (2)

MrAnswerer
MrAnswerer

Reputation: 1

I've had to deal with this in a second-hand code I've inherited, though in mine it was colorBin being called after getting the quantiles beforehand.

I made it work by making a new function by copying colorBin's source code with the only change being replacing the cut() with .bincode() (and removing its label = FALSE argument). Quantiles with the same floor will receive the same colour, and I haven't run into any issue since.

Upvotes: -1

Tim Pekan
Tim Pekan

Reputation: 55

Following the github example - did you try this?

quantileNum <- 5

probs <- seq(0, 1, length.out = quantileNum + 1)
bins <- quantile(a, probs, na.rm = TRUE, names = FALSE)

while (length(unique(bins)) != length(bins)) {
 quantileNum <- quantileNum - 1
 probs <- seq(0, 1, length.out = quantileNum + 1)
 bins <- quantile(a, probs, na.rm = TRUE, names = FALSE)
}

pal <- colorBin("YlGn", bins = bins)

Upvotes: -1

Related Questions