I create a leaflet app. For one particular value within my data, I want to use a custom color. For the remaining colors, I want to use a ramp. As I don't know any better, I create a simple color function with an ifelse-statement.
Here is sample-code:
library(leaflet)
colors <- function(x){
return(ifelse(x==1422, #if it was founded in 1422
"#ff7400", #return this orange, else use colorNumeric
colorNumeric(palette="viridis",
domain=unique(breweries91$founded) %>% setdiff(1422),
na.color=NA)(x)
)
)
}
breweries91 %>% leaflet() %>% addTiles() %>% addCircleMarkers(fillColor =~colors(founded),
weight=0,
fillOpacity = 1)
the result looks as expected, with one point in orange.
But it throws the following warning, as if the Brewery from 1422 still passes through colorNumeric()
Warning message:
In colorNumeric(palette = "viridis", domain = unique(breweries91$founded) %>% :
Some values were outside the color scale and will be treated as NA

One possibility, that better handles missing values, would be:
as indicated here: ifelse shows warning based on the else function even when the test condition is met