I'd like to include multiple elements in the marker labels in a Leaflet map I'm creating for a Shiny app. Is there a way to do this without just pasting everything into one big, messy string? Ideally, the elements of these labels would be stacked like a table (albeit without lines or borders).
Here's a toy example. In the code below, I use the message
field as the label, but I'd like to be able to show date
and count
as well. Any thoughts or examples?
library(shiny)
library(dplyr)
library(leaflet)
dat <- data.frame(lat = 39.5,
lng = -98.35,
message = "Hi, world!",
date = Sys.Date(),
count = 32,
stringsAsFactors = FALSE)
ui <- fluidPage(
leafletOutput("my_map")
)
server <- function(input, output) {
output$my_map <- renderLeaflet({
leaflet() %>%
addProviderTiles("Stamen.Toner") %>%
setView(lat = 39.5, lng = -98.35, zoom = 6) %>%
addCircleMarkers(data = dat,
lat = ~lat, lng = ~lng,
label = ~message,
radius = 10, fillOpacity = 3/4, stroke = FALSE, color = 'steelblue')
})
}
shinyApp(ui, server)