-1

I am trying to make an interactive Leaflet map in R. I have a shapefile that looks something like this:

# my_shapefile

Simple feature collection with 6 features and 5 fields
geometry type: MULTIPOLYGON
dimensions : xy
bbox: xmin ... y min ... xmax ... ymax...
CRS: EPSG:  4326

ID: A, B, A
Geometry : MULTIPOLYGON ((( 55.23, -71.22 ....
value :  0.12, 0.15, 0.13

I made the following map:

#taken from here: https://cengel.github.io/R-spatial/mapping.html
library(sf)
library(leaflet)
library(classInt)
library(RColorBrewer)

sf <- sf::st_read("my_shapefile.shp", options = "ENCODING-WINDOWS-1252")

#note: "value" comes from another file that I merged to the shapefile

st_trans <- st_transform(sf, 4326)

breaks_qt <- classIntervals(sf$value, n=7, style "quantile")

pal_fun <- colorQuantile("YlOrRd", NULL, n = 5)

# here is the problem I think

p_popup <- paste0("<strong> Value : </strong>", sf$value)

leaflet(st_trans) %>%
addPolygons(
stroke = FALSE,
fillColor = ~ pal_fun(value),
fillOpacity = 0.8, smoothFactor = 0.5,
popup = p_popup,
group = "my_group") %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB.DarkMatter", group = "Carto") %>%
addLegend("bottomright", 
colors = brewer.pal(7, "YlOrRd"),
labels = paste0("up to ", format(breaks_qt$brks[-1], digits = 2)),
title = 'my legend') %>%
addLayersControl(baseGroups = c("OSM", "Carto"), overlapGroups = c("my_group"))

This map runs fine, the problem is that when I move the mouse over the map, the popup for "value" appears with many decimal points - I would like to restrict the number of decimal points to only two. The "value" variable was calculated by dividing two variables together, so I suspect that the many decimal points are coming from this.

I first tried to turn off the decimal points using the code found here (Formatting Decimal places in R), but this did not work:

#did not work
options(digits=2)

Next, I tried to manually keep only two decimal points, but this also did not work (i.e. the visualization is still displaying many decimal points):

# did not work
sf$value = substr(sf$value, 1,4)

Finally, I found this post here (R Leaflet Legend: colorBin- removing decimals in between breaks) and tried to follow this advice for restricting the number of decimal points:

#did not work

leaflet(st_trans) %>%
addPolygons(
stroke = FALSE,
fillColor = ~ pal_fun(value),
fillOpacity = 0.8, smoothFactor = 0.5,
popup = p_popup,
group = "my_group") %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB.DarkMatter", group = "Carto") %>%
addLegend("bottomright", labFormat = labelFormat(prefix = '', suffix = '', between = ' &ndash; ', digits = 3, big.mark = ',', transform = identity),
colors = brewer.pal(7, "YlOrRd"),
labels = paste0("up to ", format(breaks_qt$brks[-1], digits = 2)),
title = 'my legend') %>%
addLayersControl(baseGroups = c("OSM", "Carto"), overlapGroups = c("my_group"))
  • Can someone please show me what I am doing wrong and what I can do to ensure that the popup in the leaflet map only contains two decimal points?
1
  • Please try and make your examples minimal - remove everything that doesn't reproduce the problem. In this case, most of the styling and stuff added to the map can go. It makes it easier for us to focus on the problem. Ideally you should also use sample data or simple data generated in code.
    – Spacedman
    Commented Aug 4, 2022 at 10:47

2 Answers 2

1

Are you able to use the format() approach listed as an answer in this thread:

Formatting Decimal places in R

x = 1.20000
format(round(x, 2), nsmall = 2)
[1] "1.20"

where x is your input of choice.

0

Use one of the functions for formatting decimals to construct your popups.

Minimal example:

sf = st_as_sf(data.frame(x=1:10, y=1:10, value=runif(10)), crs=4326, coords=1:2)
p_popup <- paste0("<strong> Value : </strong>", sprintf("%.2f", sf$value))
leaflet(sf) %>% addMarkers( popup=p_popup)

The values are:

> sf$value
 [1] 0.3315254 0.9618285 0.8060112 0.2322543 0.7385210 0.6962690 0.8464728
 [8] 0.7286911 0.6193918 0.1502202

and the popups are like this:

enter image description here

Note 0.7385210 is rounded to 0.74. Adjust format string to suit your application.

Not the answer you're looking for? Browse other questions tagged or ask your own question.