Вот решение для того, что я думаю, что вы после.Следует помнить, что для маркера Colour доступно всего 19 цветов.Вместо этого вы можете адаптировать решение и изменить iconColor, что позволит вам использовать CSS-корректные цвета (соответственно вы можете использовать цветовые палитры / палитры).
library(shiny)
library(leaflet)
library(data.table)
# read in data and generate new, fake data
DT <- data.table(quakes[1:24,])
DT$cat <- as.factor(sample(c("Car", "Truck", "Train", "Bus"), 24, replace=TRUE))
DT$type <- as.factor(sample(c("Walrus", "Dragon", "Llama"), 24, replace=TRUE))
# 19 possible colors
markerColorPalette <- c("red", "darkred", "lightred", "orange", "beige", "green", "darkgreen", "lightgreen", "blue", "darkblue", "lightblue", "purple", "darkpurple", "pink", "cadetblue", "white", "gray", "lightgray", "black")
ui <- fluidPage(
leafletOutput("mymap"),
p(),
selectInput(inputId="columnSelect", label="Select column", choices=names(DT), selected = "cat")
)
server <- function(input, output, session) {
# create awesome icons
icons <- reactive({
columnLevels <- unique(DT[[input$columnSelect]])
colorDT <- data.table(columnLevels = columnLevels, levelColor = markerColorPalette[seq(length(columnLevels))])
setnames(colorDT, "columnLevels", input$columnSelect)
DT <- colorDT[DT, on = input$columnSelect]
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = DT$levelColor
)
return(icons)
})
output$mymap <- renderLeaflet({
req(icons())
leaflet(DT) %>% addTiles() %>%
addAwesomeMarkers(~long, ~lat, icon=icons(), label=as.character(DT[[input$columnSelect]]))
})
}
shinyApp(ui, server)