Конечно. Я предлагаю сделать это до создания палитры. Палитры и так достаточно хитрые. См. Минимальный пример ниже:
library(leaflet)
library(maps)
library(shiny)
ui <- fluidPage(
leafletOutput("map_1"),
selectInput(inputId = "input_species", label = "Species Selection", choices = c("Species_1", "Species_2", "Species_3"))
)
server <- function(input, output, session) {
#Load a map of the US from the 'map' package (runs once when apps starts)
shp_map = map("state", fill = TRUE, plot = FALSE)
#Make up a dataframe with some data for three species for each state (runs once when apps starts)
df_data <- data.frame(state = unique(shp_map$names), Species_1 = sample(100:199, 63), Species_2 = sample(200:299, 63), Species_3 = sample(300:399, 63))
#Create map
output$map_1 <- renderLeaflet({
df_map <- df_data
#Create a column called species selected based on which is selected in the dropdown box
df_map$Species_Selected <- df_map[, paste(input$input_species)]
#Create a palette function
palette <- colorNumeric(palette = "Blues", domain = df_map$Species_Selected)
#Use the palette function created above to add the appropriate RGB value to our dataframe
df_map$color <- palette(df_map$Species_Selected)
#Create map
map_1 <- leaflet(data = shp_map) %>%
addPolygons(fillColor = df_map$color, fillOpacity = 1, weight = 1, color = "#000000", popup = paste(sep = "", "<b>", paste(shp_map$names), " ", "</b><br>", df_map$Species_Selected))
map_1
})
}
shinyApp(ui, server)