Search code examples
rr-leaflet

Place in a different line phrases inside paste()


How can I place in a different line Country Rate and Denominator in the label in R?

paste0("County:"
        , "Belgium","<br>",
        
        "Rate:"
        , 4.56,
        
        "Denominator:"
        , 2.58
        
 )

this is going to be used inside a leaflet map gor hoverinfo I do not know if this is important.

leaflet(oh_sp) %>%
      setView(lng = -118.2437, lat = 34.0522, zoom = 7)%>%
      addProviderTiles("CartoDB.Positron")%>%
      addPolygons(
        fillColor = ~pal(Rate),
        weight = 2,
        opacity = 1,
        color = "white",
        dashArray = "3",
        fillOpacity = 0.7,
        highlightOptions = highlightOptions(
          weight = 5,
          color = "#666",
          dashArray = "",
          fillOpacity = 0.7,
          bringToFront = TRUE),
        label = paste0("County:"
                        , oh_sp$County,
                        
                        "Rate:"
                        , oh_sp$Rate,
                        
                        "Denominator:"
                        , oh_sp$Denominator

        )

Solution

  • In case of leaflet you have to add line breaks using an (additional) <br> tag as you already did for the first line. However, to render the the label as HTML you also have to wrap in HTML() and in case of a vector of labels you have to use lapply to apply HTML() on each element of the vector.

    library(leaflet)
    
    leaflet() %>%
      addTiles() %>%
      addMarkers(
        lng = c(174.768, 174.7),
        lat = c(-36.852, -36),
        label = lapply(
          paste0(
            "County: ", c("Belgium", "Somewhere"), "<br>",
            "Rate: ", c(4.56, 2.58), "<br>",
            "Denominator: ", c(2.58, 4.56)
          ),
          HTML
        )
      )
    

    enter image description here