Jon McCloskey
Jon McCloskey

Reputation: 1

HTML Popups with hyperlinks based on table

Basically I have a Shapefile with thousands of records and each record represents a document (PDF/TIF files).. I am able to subset the shapefile to specific areas of interest then i have a column for the hyperlink to a unique file for that record. I am using a popup function so that when i click i see that box with the path to the file, but i cannot get it to appear as a hyperlink.. Doing tests i can successful add in hyperlinks for websites, but i really need these hyperlinks based on the field from the table e.g. BLR_25K$Hyperlink, just have no idea changing the string to a hyperlink to the document.

Please can anyone help?

m <- leaflet() %>%
    addTiles() %>%
    setView (lng=40.479, lat=52.179, zoom = 3) %>%
    addPolygons(data = BLR_25K, color = "#0058cc", weight = 1, smoothFactor = 0.5, popup = "<a href = BLR_25K$Hyperlink>Product </a>", group = "Belarus 25K", label = lapply(BLR_25K$label, HTML)) %>%
    enter code hereaddLayersControl(overlayGroups = c("Belarus 25K"),
                   options = layersControlOptions(collapsed = TRUE))
m

Upvotes: 0

Views: 91

Answers (1)

Ben
Ben

Reputation: 30494

I think in your case, you want to use paste to add your hyperlink from BLR_25K$Hyperlink along with tags for the popup argument.

For example, you could try:

popup = paste0("<a href = '", BLR_25K$Hyperlink, "'>Product</a>")

Here is full demo of a world map to illustrate, with credit to this answer. This will include popup links to wikipedia for each country.

library(leaflet)
library(sf)

download.file(url = "http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip", 
              destfile = "TM_WORLD_BORDERS_SIMPL-0.3.zip")

unzip(zipfile = "TM_WORLD_BORDERS_SIMPL-0.3.zip")

world.borders <- read_sf(dsn = getwd(), layer = "TM_WORLD_BORDERS_SIMPL-0.3")
world.borders$wiki <- paste0("https://en.wikipedia.org/wiki/", world.borders$NAME)

leaflet() %>%
  addTiles() %>%
  setView(lng = 40.479, lat = 52.179, zoom = 3) %>%
  addPolygons(data = world.borders, 
              color = "#0058cc", 
              weight = 1, 
              smoothFactor = 0.5,
              popup = paste0(
                 "<b>Country: </b>"
                 , world.borders$NAME
                 , "<br>"
                 , "<a href='"
                 , world.borders$wiki
                 , "'>Click Here to View Wiki</a>"
               ), 
              label = ~NAME
              )

Upvotes: 0

Related Questions