Formatting Popup Text in R

I am following this tutorial here (Leaflet for R - Popups and Labels):

library(htmltools)
library(leaflet)

df <- read.csv(textConnection(
    "Name,Lat,Long
Samurai Noodle,47.597131,-122.327298
Kukai Ramen,47.6154,-122.327157
Tsukushinbo,47.59987,-122.326726"
))

leaflet(df) %>% addTiles() %>%

    addMarkers(~Long, ~Lat, popup = ~htmlEscape(Name))

Now, I want to the popups to display the information about the name, the longitude and the latitude (i.e. title + value) - I would like it to say:

  • Name = Insert Restaurant Name Here

(new line)

  • Longitude = Insert Longitude Name Here

(new line)

  • Latitude = Insert Latitude Here

I thought that this could be done as follows:

leaflet(df) %>% addTiles() %>%

addMarkers(~Long, ~Lat, popup = ~htmlEscape(df$Name, df$Lat, df$Long))

But this is giving me the following error:

Error in htmlEscape(df$Name, df$Lat, df$Long) : unused argument (df$Long)

I tried to read about this function (htmlEscape function - RDocumentation), but there does not seem to be too much information on how to use it. I thought that maybe this might require "combining" all the arguments together:

leaflet(df) %>% addTiles() %>%

addMarkers(~Long, ~Lat, popup = ~htmlEscape(c(df$Name, df$Lat, df$Long)))

But now this only displays the final argument (and that too, without the title).

Is "htmlescape()" able to handle multiple arguments?

Thank you!

You could do this instead. You could also use paste() if you don't want to use glue().

library(htmltools)
library(leaflet)
library(glue)

df <- read.csv(textConnection(
  "Name,Lat,Long
Samurai Noodle,47.597131,-122.327298
Kukai Ramen,47.6154,-122.327157
Tsukushinbo,47.59987,-122.326726"
))

# labels
label_text <- glue(
  "<b>Name: </b> {df$Name}<br/>",
  "<b>Longitude: </b> {df$Long}<br/>",
  "<b>Latitude: </b> {df$Lat}<br/>") %>%
  lapply(htmltools::HTML)

# the map
leaflet(df) %>% addTiles() %>%
  addMarkers(~Long, ~Lat, popup = label_text)

image

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.