Skip to contents

This function is intended as a more up-to-date implementation of leaflet::awesomeIcons(). Key benefits include having access to modern Font Awesome, Bootstrap and Ionicons icons, and allowing any colour to be used the marker and icon.

Usage

magicIcons(
  icon = "circle",
  markerColor = awesomePalette$blue,
  iconColor = awesomePalette$white,
  marker = "marker",
  markerSize = 30L,
  library = "fontawesome",
  className = NULL,
  dir = tempdir()
)

Arguments

icon

Name of the Font Awesome icon, passed to fontawesome::fa() or bsicons::bs_icon(). A full list of available icons can be found using fontawesome::fa_metadata() or at https://icons.getbootstrap.com/.

markerColor

The color of the marker. Not used when marker = "none".

iconColor

The color of the fontawesome icon.

marker

Defaults to "marker", which uses the standard teardrop shaped marker, similar to leaflet::addMarkers(). Other options are "circle", "square", "star", "heart", and "diamond", which place the icon inside of the respective shape. Also available is "none", which removes the marker entirely and places the icon directly on the map.

markerSize

The size of the marker. Defaults to 30, which is roughly the same size as leaflet::addMarkers().

library

One of "fontawesome", "bootstrap", or "ionicons", defining the icon library of interest. Defaults to "fontawesome".

className

a custom class name to assign to both icon and shadow images

dir

The directory in which markers are saved. By default this is tempdir(), which is a temporary directory after each session. Providing an alternative directory will allow markers to persist between R sessions.

Value

a leaflet::iconList(), to be passed to the icon argument of leaflet::addMarkers()

Details

This function uses magick::image_composite() to knit together the icon and marker symbol of choice, and saves it to a temporary directory. It is also intelligent enough to not try to recreate a marker that already exists, so you may find the use of magicIcons() speeds up the more you use it in a single session.

Author

Jack Davison

Examples

# create map
library(leaflet)
port_talbot %>%
  dplyr::mutate(
    icon = dplyr::case_match(
      site_type,
      "Urban Industrial" ~ "industry",
      "Urban Traffic" ~ "car",
      "Urban Background" ~ "city",
      .default = "x"
    ),
    color = dplyr::case_match(
      site_type,
      "Urban Industrial" ~ "#12436D",
      "Urban Traffic" ~ "#801650",
      "Urban Background" ~ "#28A197",
      .default = "#3D3D3DFF"
    )
  ) %>%
  leaflet() %>%
  addProviderTiles("CartoDB.Voyager") %>%
  addMarkers(
    icon = ~ magicIcons(icon, color, "white"),
    popup = ~site
  )
#> Assuming "longitude" and "latitude" are longitude and latitude, respectively