Search code examples
rr-leaflet

How do I insert more expansive dataframe columns with text or a table or ggplot2 ouput into a popup in R / Leaflet?


I am at the very beginning of learning my first programming language, which is R. I am really keen to learn leaflet stuff and made some good progress over the last couple of nights.

Getting stuck with populating more expansive popup content within AddPolylines function. I have got it to populate the Asset_Name from my data frame.

However, how would I approach the following few options:

  1. Using two columns from my data frame together in one popup e.g.
  addPolylines(
    data = df$Geometry,
    color = "red",
    weight = 5,
    popup = df$Asset_Name & df$Asset_Description,
    group = "Polylines"
  ) %>%

This errors in R, I think because I am using the wrong operator.

  1. How could I use html and call columns from data frame in a popup? e.g.
"Asset Name:" df$Asset_Name </br>
"Asset Description:" df$Asset_Description

I am trying to read the documentation: https://rstudio.github.io/leaflet/popups.html However, I am still lost.

Please help, many thanks for reading.

library(leaflet)
library(sf)

setwd("C:/Users/XXXX/Documents/R Programming Training/Leaflet Learning")

df <- read.csv("CLBWKT_LinesOnly.csv") # ordinary data frame

df <- st_as_sf(df, wkt = "Geometry") # convert to spatial data frame

m <- leaflet(df) %>%
  addTiles(group = "OSM (default)") %>%
  addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
  addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
  addProviderTiles('Esri.WorldImagery', group = "Sat View") %>% 
  
  addPolylines(
    data = df$Geometry,
    color = "red",
    weight = 5,
    popup = df$Asset_Name,
    group = "Polylines"
  ) %>% 
  
  addMiniMap(
    minimized = FALSE,
    toggleDisplay = TRUE,
    position = "bottomleft"
  ) %>% 
  
  addLayersControl(
    baseGroups = c("OSM (default)", "Toner", "Toner Lite", "Sat View"),
    overlayGroups = c("Polylines", "Markers"),
    position = "topleft",
    options = layersControlOptions(collapsed = TRUE)
  )
m
structure(list(Asset_ID = 100001:100004, System_Name = c("System 1", 
"System 1", "System 2", "System 3"), Asset_Description = c("Spoon", 
"Fork", "Knife", "Spoon"), Asset_Name = c("Asset Name 1", "Asset Name 2", 
"Asset Name 3", "Asset Name 4"), Condition = 1:4, Geometry = structure(list(
    structure(c(-1.68639290511313, -1.68635836567153, -1.68565219769999, 
    -1.68565171774367, 55.1667596590412, 55.1667573239408, 55.1667106840643, 
    55.1667388899525), dim = c(4L, 2L), class = c("XY", "LINESTRING", 
    "sfg")), structure(list(structure(c(-1.68034047322624, -1.68038289179782, 
    -1.68045906926982, -1.68048512895859, -1.68053513999523, 
    -1.68058793251902, -1.6806304470928, -1.68068494033555, -1.68080486828078, 
    -1.68090160208891, -1.68098744619037, -1.681059243053, -1.68120417228514, 
    -1.68130363603522, -1.68144793922962, -1.68149890595872, 
    -1.68158988492711, -1.6816730354649, -1.68176169843031, -1.68178287840857, 
    -1.68180681439964, -1.68184330248185, -1.68200852094169, 
    -1.68211520890884, -1.68229203857493, -1.68246049811455, 
    -1.68265612784607, -1.68275449461309, -1.68286754214383, 
    -1.68296114395775, -1.68301728003276, 55.1690912809677, 55.1691070637455, 
    55.1691323332199, 55.169140494827, 55.1691515333531, 55.16916064358, 
    55.1691674400557, 55.1691754997488, 55.1691873362278, 55.1692364993597, 
    55.1692634651086, 55.1692745764327, 55.1692974575058, 55.1693124957266, 
    55.1693322244598, 55.1693390426607, 55.169338928231, 55.1693570893883, 
    55.1693777287495, 55.1693813027131, 55.1693859396785, 55.1693971186269, 
    55.1695004809808, 55.1695393201381, 55.1696303433464, 55.1697228191642, 
    55.1698040413541, 55.1698414188748, 55.1698879828757, 55.1699450529148, 
    55.1699800665141), dim = c(31L, 2L)), structure(c(-1.67991922565183, 
    -1.67992292824692, -1.67992871281608, -1.67994732968346, 
    -1.67996521661522, -1.67998904788041, -1.6800139147732, -1.68004425890084, 
    -1.68007241769249, -1.68010884196028, -1.68012858924333, 
    -1.6801329331177, 55.168869598948, 55.1688759382411, 55.1688828149423, 
    55.1688994020405, 55.1689122671015, 55.1689300988942, 55.1689468146592, 
    55.1689633192048, 55.1689780828675, 55.168996915352, 55.169006643819, 
    55.1690087472255), dim = c(12L, 2L))), class = c("XY", "MULTILINESTRING", 
    "sfg")), structure(c(-2.94368279166516, -2.94320298450354, 
    -2.9432258084364, -2.94327592253373, -2.94337226543638, -2.94347479608075, 
    -2.94341228732479, -2.9437491617188, -2.9438547187328, -2.9441710582173, 
    -2.94411183934458, -2.94402313529268, -2.94392094322372, 
    -2.94390413426354, -2.94396915978398, -2.94408578851656, 
    -2.94421922790838, -2.94438187987448, -2.94481032409716, 
    -2.94532148602622, -2.94571861309263, -2.9458163103159, -2.94576694513058, 
    -2.94533487138086, -2.9450376750598, -2.94432397728998, -2.94397161839561, 
    -2.94368279166516, 53.6793020108077, 53.678857027523, 53.6787355609611, 
    53.6786927163903, 53.6786616359016, 53.6786790221214, 53.6781670777615, 
    53.6781583610737, 53.678539583114, 53.6782975501525, 53.6781615690503, 
    53.6780773665502, 53.6780751396856, 53.678008564149, 53.6779474085154, 
    53.6779101041121, 53.6779393741804, 53.6779047395532, 53.6778801367235, 
    53.6779003636815, 53.6778456833108, 53.677875234542, 53.6779514282698, 
    53.6782701826325, 53.6784544569505, 53.6789694863035, 53.6791996753384, 
    53.6793020108077), dim = c(28L, 2L), class = c("XY", "LINESTRING", 
    "sfg")), structure(c(-2.77026256525726, -2.77032326917765, 
    -2.77041002789989, -2.77043165522445, -2.77039592888796, 
    -2.77006080010197, -2.76950415373603, -2.76906915073792, 
    -2.76780760934842, -2.76748858307524, -2.76743110189235, 
    53.9067111927087, 53.9069910447305, 53.9070736254268, 53.9072398520057, 
    53.9073167479499, 53.9076341040454, 53.908007526416, 53.9081504365657, 
    53.9084934043941, 53.9086858978806, 53.9087804581627), dim = c(11L, 
    2L), class = c("XY", "LINESTRING", "sfg"))), n_empty = 0L, crs = structure(list(
    input = NA_character_, wkt = NA_character_), class = "crs"), class = c("sfc_GEOMETRY", 
"sfc"), precision = 0, bbox = structure(c(xmin = -2.9458163103159, 
ymin = 53.6778456833108, xmax = -1.67991922565183, ymax = 55.1699800665141
), class = "bbox"), classes = c("LINESTRING", "MULTILINESTRING", 
"LINESTRING", "LINESTRING"))), row.names = c(NA, -4L), class = c("sf", 
"data.frame"), sf_column = "Geometry", agr = structure(c(Asset_ID = NA_integer_, 
System_Name = NA_integer_, Asset_Description = NA_integer_, Asset_Name = NA_integer_, 
Condition = NA_integer_), class = "factor", levels = c("constant", 
"aggregate", "identity")))

Solution

  • The are several options to add your popups. Personally I would add a column with the labels to your dataframe. In my code below I use sprintf for that but paste0 would also work.

    Note: I also made your example more minimal to focus on the popup issue by getting rid of all the unnecessary layers.

    library(leaflet)
    library(sf)
    
    df <- st_as_sf(df, wkt = "Geometry") # convert to spatial data frame
    
    df$popup <- with(df, sprintf("Asset Name: %s</br>Asset Description: %s", Asset_Name, Asset_Description))
    
    leaflet(df) %>%
      addTiles(group = "OSM (default)") %>%
      addPolylines(
        color = "blue",
        weight = 20,
        popup = ~popup, 
        group = "Polylines"
      )
    

    enter image description here