Question on Sprintf, leaflet and wrapping text.

Hello,

I am not the most skilled at R, but I am trying to make a map with leaflet, and I cannot figure out how to get the text in my labels to wrap. I'm providing a simplified code using Sprintf that has my label. Ive tried str_wrap for my variable... but maybe i don't fully understand how to use that. I've also tried inserting brackets like div style = 'width: 250; height: 250; word-wrap: break-word'></div. If anyone has advice, I would really appreciate it.

x <- c("this is a lot of text", "this is really long text that i need to break up", "wow i cannot believe how long this text is, i wish it would not make my label so big")

labels2 <- sprintf("Description: %s",
x) %>%
lapply(htmltools::HTML)

***I am not providing the leaflet code in a replicable way, because I think it'd have to upload a shapefile... But let me know if that is an issue. Below is the code I am using for my map making.

map <-leaflet(sites) %>%
clearBounds() %>%
addProviderTiles(providers$Esri.WorldImagery) %>%
addPolygons(
fillColor = ~pal(Effort),
weight = 1,
opacity = .8,
color = ~pal(Effort),
highlightOptions = highlightOptions(
weight = 2,
color = ~pal(Effort),
fillOpacity = 0.7),
label = labels,
labelOptions = labelOptions(
style = list(padding = "3px 8px"),
direction = "auto")) %>%
addLegend("bottomright",
title = "Level of Effort",
pal = pal,
values = ~Effort,
opacity = 1)

If it is not practical to share your shapefile - then the old comrade, the North Carolina shapefile that ships with sf will have to come to the rescue. Don't worry, he is used to being the shapefile of the last resort :slight_smile:

You have basically two options that I can think of:

  • force maximum size of the popup window, via popupOptions of your addPolygons call (and have Javascript handle it)
  • wrap the strings on R side, e.g. via the string::str_wrap() you mentioned; you will have to keep in mind that your code will be rendered as HTML, so you will have to replace your line breaks (which are ignored by HTML) by <br> HTML tags.

Both appraches work, each in his own way. For concrete example consider the following piece of code:

library(sf)
library(dplyr)
library(leaflet)

# the one & only NC shapefile... often imitated, never surpassed 
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% 
  # a constant, because why not?
  mutate(label_text = "wow i cannot believe how long this text is, i wish it would not make my label so big")

# this is ugly, bad bad label!
leaflet(data =  nc) %>% 
  addProviderTiles("Stamen.Toner") %>% 
  addPolygons(popup = ~label_text)

# wrap via popup options
leaflet(data =  nc) %>% 
  addProviderTiles("Stamen.Toner") %>% 
  addPolygons(popup = ~label_text,
              popupOptions = popupOptions(maxWidth = 50))

# wrap via stringr::str_wrap 
leaflet(data =  nc) %>% 
  addProviderTiles("Stamen.Toner") %>% 
  addPolygons(popup = ~gsub("\n", "<br>",
                           stringr::str_wrap(nc$label_text,
                                             width = 3)))

Which one you choose will be ultimately up to you (personally the popupOptions approach seems the least hassle to me, but at the end it is your call to make).

Thank you SO Much! I ended up going with the "Pop Up", "Label Options" - weidly I didn't have to convert it to popupoptions. But my text looks nicely formated and I can actually use my map now! Thank you so much.

Also, I won't forget NC next time =)

map <-leaflet(sites) %>%
  clearBounds() %>%
  addProviderTiles(providers$Esri.WorldImagery) %>%
  addPolygons(
              fillColor = ~pal(Effort),
              weight = 1,
              opacity = .8,
              color = ~pal(Effort), 
              highlightOptions = highlightOptions(
                weight = 2,
                color = ~pal(Effort),
                fillOpacity = 0.7),
              popup = ~labels,
              labelOptions = labelOptions(maxWidth = 50)) %>%
  addLegend("bottomright", 
            title = "Level of Effort",
            pal = pal,
            values = ~Effort,
            opacity = 1) 

Glad to be of service :slight_smile: