我正在制作一个闪亮的应用程序,我希望用户能够选择进入 Awesome Marker 的 Font Awesome 图标。
这是一个简单的应用程序,可让用户选择标记颜色、图标颜色和图标(名称)。
library(shiny)
library(leaflet)
icon_names <- c("home", "map-pin")
marker_colours <- list(Standard = c('red', 'orange', 'beige', 'green', 'blue', 'purple',
'pink', 'cadetblue', 'white', 'grey', 'black'),
Shades = c('darkred', 'lightred', 'darkgreen', 'lightgreen',
'darkblue', 'lightblue', 'darkpurple', 'lightgray'))
server <- function(input, output) {
output$map <- renderLeaflet({
icons <- awesomeIcons(
icon = input$icon,
iconColor = input$icon_colour,
library = 'fa',
markerColor = input$marker_colour
)
leaflet() %>%
addTiles() %>%
addAwesomeMarkers(lng = 4.9,
lat = 52.38,
icon = icons
)
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("icon", "Icon:",
choices = icon_names, selected = "home"),
selectInput("marker_colour", "Marker colour:",
choices = marker_colours, selected = "red"),
selectInput("icon_colour", "Icon colour:",
choices = c("#ffffff", "#000000"), selected = "#ffffff")
),
mainPanel(leafletOutput("map"))
)
)
shinyApp(ui = ui, server = server)
但现在我想要一种方法来访问 R 包 Leaflet 中可用的所有可能的 FA 图标。
所以是一些代码
icon_names <- c("home", "map-pin")
应该更改为会产生包含所有可用图标的字符串的内容。
我找到了一种从传单包中收集信息的方法。
您应该能够在传单包中找到一个名为 font-awesome.min.css 的文件,您可以在那里提取信息。
file_text <- readr::read_file(
paste0(.libPaths()[1],
"/leaflet/htmlwidgets/plugins/Leaflet.awesome-markers/font-awesome.min.css")
)
图标名称集中在“fa-”和“:”之间。
icon_names <- stringr::str_extract_all(file_text, "(fa-)([^:]+)")[[1]]
快速浏览一下,前 36 个条目不是我想要的,通过查看整个 css 文件可以看到。
icon_names <- icon_names[-(1:36)] %>%
stringr::str_sub(4, -1)
我认为 Leaflet 包中的更改可能会影响这一点。对我来说它适用于:
最好的, 吉杜