Displaying a URL saved in a dataframe in a label

Good Morning R Community

As mentioned in my previous query, I am very new to R.

I currently have a choropleth map created using leaflet which has labels for each Local Authority Area. At the moment, the label displays the name and the mean rank for the authority (from the same dataframe). I want to add in an additional column from said dataframe, which contains a URL for a web page containing additional information pertaining to each Local Authority (32 Local Authorities, each with their own individual page).

I can get the url to display, but not as a clickable hyperlink. Each time I try to add the html tags in as per the guidance on https://rstudio.github.io/leaflet/popups.html, I get various "object n not found" errors on execution.

The map showing the labels as they stand can be found here:

https://rpubs.com/hibsgirl42/609494

Here is my code snippet for the label in question. The field containing the URL is project_2004$councilProfile

addPolygons(data = project_2004, # sixth group
              fillColor = ~pal(meanSIMDrank6),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = "3",
              fillOpacity = 0.7,
              highlight = highlightOptions(
                weight = 2,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE),
              group = "SIMD2004",
              label=~paste(project_2004$lad17nm,
                           round(project_2004$meanSIMDrank6),
                           project_2004$councilProfile),
              labelOptions = labelOptions(textsize = "12px",
                                          direction = "auto")) %>%

Not sure whether I have got this completely wrong / should be using a pop-up instead? Any help would be very much appreciated.

Thanks in advance

1 Like

Yes, you would be better off using popup. The popup vs label controversy in leaflet is a major mess.

Displaying an clickable link is a pretty common use case; you might find an inspiration in this earlier post:

Note that you will get away with a less complex construction of the anchor tag than in the linked example, but an anchor tag there must be.

5 Likes

Hello again and thanks for your prompt response.

I have tried adding the html tags as per your example

addPolygons(data = project_2004, # sixth group
              fillColor = ~pal(meanSIMDrank6),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = "3",
              fillOpacity = 0.7,
              highlight = highlightOptions(
                weight = 2,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE),
              group = "SIMD2004",
              label=~paste(project_2004$lad17nm,
                           round(project_2004$meanSIMDrank6),
                           <"a href =\project_2004$councilProfile\, target=\"_blank\">Population</a>"),
              labelOptions = labelOptions(textsize = "12px",
                                          direction = "auto")) %>%

But I'm getting the following errors:

For info, this is how the URLs I'm trying to reference are stored in the Excel file - do I perhaps need to add quotes / html tags in there?

yes, you need to play a bit with with the quotation marks.
I believe something like

~paste(project_2004$lad17nm,
                           round(project_2004$meanSIMDrank6),
                           "<a href =\"", project_2004$councilProfile, "\", target=\"_blank\">Population</a>")

should do the trick but as I don't have access to your data I am shooting blind. The quotation marks are tricky, they need to be escaped by the forward slash (normal double quote means start / end of a string, but double quote preceded by a forward slash is included in the string as a quotation mark character).

1 Like

Hello again and thank you for your continuing advice. :grinning:

I amended the code as per your suggestion, the script no longer errors but I'm getting a long string including the HTML tags in my label:

pic3

Do I need to add a library so R treats this code as HTML?

looks promising :slight_smile:

Have you changed it from label to a popup? if not try - it should help.

If yes you may have to tweak the content - it is being rendered as text, not as HTML and there might be a typo.

No (additional) library is necessary, this is now in HTML zone (out of scope of R).

1 Like

Hello once again. Code now changed from a label to a popup, HTML tabs now being handled correctly, have added in a few more so the popups read better.

addPolygons(data = project_2004, # sixth group
              fillColor = ~pal(meanSIMDrank6),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = "3",
              fillOpacity = 0.7,
              highlight = highlightOptions(
                weight = 2,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE),
              group = "SIMD2004",
              popup =~paste("Local Authority: ",project_2004$lad17nm,"<br>",
                           "Mean SIMD: ",round(project_2004$meanSIMDrank6),"<br>",
                           "<a href =\"",project_2004$councilProfile, "\", target=\"_blank\">Population Details</a>"),
              popupOptions = popupOptions(textsize = "12px",
                                          direction = "auto")) %>%

Everything running as expected :grinning:, thank you so much for taking the time to advise!

Glad to be of service!

It took a while, but the result looks presentation worthy. Good!

Makes me think of my favorite fly fishing quote from Norman Maclean "To him, all good things - trout as well as eternal salvation - came by grace; and grace comes by art; and art does not come easy" :slightly_smiling_face:

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