Open URL in new TAB when we click on a state in a map

Hi All,

I am trying to implement opening url in a new tab when a particular state in a map is clicked in rstudio leaflet.

I see in the below link which talks about how to open url in a new tab once you click on something. But I want to implement the same in RStudio leaflet. Please help.

Below is the map code. Let me know if you need anymore information.

library(leaflet)
library(sp)
library(albersusa)

spdf <- rmapshaper::ms_simplify(usa_sf(), keep = 0.1)
pal <- colorNumeric("Blues", domain = spdf$pop_2014)
epsg2163 <- leafletCRS(
crsClass = "L.Proj.CRS",
code = "EPSG:2163",
proj4def = "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs",
resolutions = 2^(16:7))

leaflet(spdf, options = leafletOptions(crs = epsg2163)) %>%
addPolygons(weight = 1, color = "#444444", opacity = 1,
fillColor = ~pal(pop_2014), fillOpacity = 0.7, smoothFactor = 0.5,
label = ~paste(name, pop_2014),
labelOptions = labelOptions(direction = "auto"))

JFYI: for installing albersusa package, refer this link: https://github.com/hrbrmstr/albersusa

Thanks a lot in advance. Looking forward to hear from someone.

Opening links in new window is not a leaflet thing, but a HTML thing - you need to specify target=\"_blank\" in an anchor tag used in your popup argument.

I am sorry I don't quite follow the way your url is constructed and used (I can't see the <a> tag anywhere in your code) so excuse me when I use my own example.

It is based on my earlier answer on "the other site", with very minor changes.

library(dplyr)   # for mutate & pipe
library(tmap)    # for the metro dataset
library(leaflet) # interface to leaflet

data("metro") # from the tmap package

metro <- metro %>% 
  mutate(label = paste("marvel at ", name, " and follow <a href=\"https://community.rstudio.com\", target=\"_blank\">RStudio Community</a>"))

leaflet(data = metro) %>%
  addTiles() %>%
  addCircleMarkers(popup = ~label)

But I don't want the links on the popups. Once you click on the state, it should take me to the hyperlink. That is what I am looking for. If you have a sample code that would showcase where once you click on the state it will lead to the hyperlink. That would be great.

Thanks in advance.

While what you describe is doable it would require a bit of custom js to handle the on click events & execute window.open(); so doable yes, but I don't have an example on hand right now.

1 Like

Ok. Let me know if you get one. It would be great or if you come across any links on the Internet that would help to implement this, please share.

Thanks a lot jlacko for your help.

leaflet has an onRender functionality for arbitrary javascript.
But I don't know much javascript nor the internal representation of the leaflet, so maybe ask on the github.

Ok Sir. I will take a look at that. Thanks a lot for your quick response. You are really amazing.

Could you provide some advice on the below question?

Thanks in advance.

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