How can I make a leaflet-popup stay when clicking another popup? - r

How can a popup stay open when clicking on another?
The MWE below show a popup when I click on the markers. Good. But when I click on a second marker I don't want the first popup to disappear. It should only disapear when clicking the close [x]. No popups should be visible before clicking on the markers.
library(leaflet)
the.points <- data.frame(latitude=c(63.136353,63.132935,63.128051),
longitude=c(21.928023,21.962601,21.893444),
text=c("One point", "A second point", "The third point"))
p <- leaflet(the.points)
p <- addTiles(p)
p <- addMarkers(p, lng=~longitude, lat=~latitude, popup=~text)
p
I tried with addPopups too, but they are all visible by default. If that can be changed it would be good (not sure how).

Was struggling with the same thing, finally this worked for me:
leaflet() %>% addTiles() %>%
addMarkers(
lng = -118.456554, lat = 34.085,
popup = "One popup",
popupOptions = popupOptions(autoClose = FALSE, closeOnClick = FALSE)) %>%
addMarkers(
lng = -118.456554, lat = 34.065,
popup = "Another popup",
popupOptions = popupOptions(autoClose = FALSE, closeOnClick = FALSE))
)

Related

HTML Popups with hyperlinks based on table

Sorry guys, i have hit a brick wall here and since i am not a programmer i am not sure how to explain this either
Basically 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? Can offer something in return!
Thanks
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
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
)

Shiny + Leaflet - Is it possible to add images to popups based on groups?

Currently, I have a shiny application that creates a leaflet map of species points. When you hover over a point, the species name is displayed both as a label and a popup.
leafletProxy("map", data = filteredData()) %>%
clearMarkers() %>%
addCircleMarkers(popup = ~as.character(species),
label = ~as.character(species),
radius = 6,
stroke = TRUE,
weight = 1,
fillOpacity = 5,
fillColor = ~pal(species),
color = "black")
I have read up on how to add an image to a popup, but this would add the same image to all popups. What I would like to happen is once a point is clicked, a popup appears with the name of the species and a picture (either local file or web-linked, whichever is easiest)- So that each group (species) would have its own associated picture.
Is this possible?
Yes, if you want to each group to have its own image, you need to create a new column which contains URL to your image. And the important part is to use HTML img tag in popup.
See demo below.
data <- data.frame(
lng = c(-1,0,1,2),
lat = c(-1,0,1,2),
label = c("p1","p2","p3","p4"),
# some random images I picked up from google images
# it can be both local or remote
image_link = c(
"https://jessehouwing.net/content/images/size/w2000/2018/07/stackoverflow-1.png",
"https://store-images.s-microsoft.com/image/apps.18496.14408192455588579.aafb3426-654c-4eb2-b7f4-43639bdd3d75.2c522ca4-9686-4ee2-a4ac-cdbfaf92c618?mode=scale&q=90&h=1080&w=1920",
"https://mk0jobadderjftub56m0.kinstacdn.com/wp-content/uploads/stackoverflow.com-300.jpg",
# row number 4 use the same link as row number 1
"https://mk0jobadderjftub56m0.kinstacdn.com/wp-content/uploads/stackoverflow.com-300.jpg"
)
)
library(leaflet)
leaflet(data = data) %>%
addTiles() %>%
addCircleMarkers(
lng = ~lng,
lat = ~lat,
popup = ~paste0(
"<img src='",image_link,"' width='50px' height='50px'>"
)
)
You could also replicate code from here:
https://github.com/abenedetti/bioNPS

How to access polygon information from shapefile leaflet R

I made a map of London using https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london and shiny and R and leaflet. I added an attribute to the shapefile, and now want to be able to highlight the shapefile and print information when the user clicks on a specific polygon.
I looked at shiny leaflet ploygon click event, Marker mouse click event in R leaflet for shiny, and How to implement input$map_marker_click correctly?, and know I need to use ObserveEvent, but have not been able to implement it correctly.
My code is:
library(shiny)
library("rgdal")
library(leaflet)
shapeData <- readOGR('statistical-gis-boundaries-london/ESRI/LSOA_2004_London_Low_Resolution.shp')
shapeData <- spTransform(shapeData, CRS("+proj=longlat +ellps=GRS80"))
shapeData$col=sample(c('red','yellow','green'),nrow(shapeData),1) #add some value you want to map
borough=read.csv('BoroughCentres.csv')
ui=fluidPage(
fluidPage(
leafletOutput('LSOAMap'),
p(),
selectInput('LANAME','Borough',
choices = unique(shapeData$LA_NAME))
)
)
server=function(input, output) {
output$LSOAMap <- renderLeaflet({
llong=borough[borough$Borough==input$LANAME,3]
llat=borough[borough$Borough==input$LANAME,4]
bor=subset(shapeData,shapeData$LA_NAME %in% input$LANAME)
leaflet() %>% addTiles() %>%
setView(lng = llong, lat=llat,zoom=13) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.2,fillColor = bor$col,
highlightOptions = highlightOptions(color='white',weight=1,
bringToFront = TRUE)) %>%
addMarkers(lng = llong,lat=llat,popup=input$LANAME)
})
}
shinyApp(ui, server)
I tried adding, along with session as an argument:
observe({
click <- input$map_marker_click
if (is.null(click))
return()
print(click)
text <-
paste("Lattitude ",
click$lat,
"Longtitude ",
click$lng)
leafletProxy(mapId = "LSOAMap") %>%
clearPopups() %>%
addPopups(dat = click, lat = ~lat, lng = ~lng, popup = text)
# map$clearPopups()
# map$showPopup(click$latitude, click$longtitude, text)
})
to no avail.
What I want is that when a user highlights a specific shape, text pops up and shows the corresponding STWARDNAME from the shapefile.
the first few lines of borough are:
> head(borough)
Borough LA_CODE long lat
1 City of London E09000001 -0.09194991 51.51814
2 Barking and Dagenham E09000002 0.13064556 51.54764
3 Barnet E09000003 -0.20416711 51.61086
4 Bexley E09000004 0.13459320 51.45981
5 Brent E09000005 -0.26187070 51.55697
6 Bromley E09000006 0.03734663 51.38836
You are pretty close but have a few syntax issues which I think are tripping you up.
First you are not properly selecting what STWARDNAME you want from your SPDF so there is no way for R to know which one to show. I added this line
popup = subset(shapeData, LA_NAME == input$LANAME )$STWARDNAME
Also you were passing highlightOptions as an option when it should just be highlight
Finally since you were more interested in mouseover for a polygon, I removed the popup marker and changed to highlight so that the name is shown when you mouseover the polygon:
leaflet() %>% addTiles() %>%
setView(lng = llong, lat=llat,zoom=13) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.2,fillColor = bor$col,
highlight = highlightOptions(color='white',weight=1,
bringToFront = TRUE), label= popup)
Which yields:
#Stedy answer was brilliant, and better, I just went a different way. I added in a layer id vector, and a popup decision to find the STWARDNAME
lid=as.vector(row.names(bor#data))
pu=bor#data[row.names(bor#data)==lid,'STWARDNAME']
, then added in two arguments while making the map layerId = lid,popup = pu,. The pop up showed when the user clicked on an area
bor=subset(shapeData,shapeData$LA_NAME %in% input$LANAME)
lid=as.vector(row.names(bor#data))
pu=bor#data[row.names(bor#data)==lid,'STWARDNAME']
leaflet() %>% addTiles() %>%
setView(lng = llong, lat=llat,zoom=13) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.2,fillColor = bor$col,
layerId = lid,popup = pu,
highlightOptions = highlightOptions(color='white',weight=1,
bringToFront = TRUE)) %>%
addMarkers(lng = llong,lat=llat,popup=input$LANAME)

how to set zoom level/view of leaflet map

I have a map in leaflet in RShiny which have markers plotted and once I click a marker it adds circles to map showing all the related points to the marker.
What I want to do is to set zoom/view of the map in such a way that all the related circles are visible.
The number of circles varies as per markers i.e. some marker have 1 or 2 circles while some have more. Also, the location of circles varies across the USA so they can be placed in one city or another state.
Following is the code I am using to add circles to existing map
proxy <-leafletProxy("map",data = df)%>%
clearMarkers()%>%
addCircleMarkers(lat = ~lat,lng = ~lng,layerId = ~circle_pt,fillColor =
'green',opacity = 0.5,color = 'red',fillOpacity = 1)%>% clearPopups()%>%
addPopups(lat=~lat,lng=~lng,~as.character(circle_pt))
map=original map with markers
df=lat lng of circles with associated properties of selected marker in map
I want to set zoom level as shown in figure 2.
Kindly help me to identify how to calculate optimal zoom level in leaflet in shiny.
Regards,
If you want to set your initial view, you can use:
setView(lng, lat, zoom = zoom_level)
which is straight from the documentation.
Unless you provide more information, nobody will be able to understand the part where you're saying "in such a way that all the related circles are visible."
I'm not sure how you're app works and whats in the original call to leaflet. But maybe the following example might help you.
I store the click on the markers, filter the data according to the clicked layerId, get the min/max lat/long of the resulting data and then use fitBounds() to set the "zoom" level. (You could also use flyToBounds with the same arguments, which should make a smoother transition to the selected markers, but its still too buggy for me at least)
library(shiny)
library(shinyjs)
library(leaflet)
cords <- data.frame(
lng = runif(100, 14, 18),
lat = runif(100, 54, 58),
circle_pt = sample(1:20, size = 100, replace = T)
)
ui <- fluidPage(
leafletOutput("map", height = "700px")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet(data = cords) %>%
addTiles() %>%
addCircleMarkers(lat = ~lat,lng = ~lng, layerId = ~circle_pt, fillColor = 'green',
opacity = 0.5,color = 'red',fillOpacity = 1)
})
observeEvent(input$map_marker_click, {
clickid = input$map_marker_click$id
cordsNew = cords[cords$circle_pt==clickid,]
maxLong = max(cordsNew$lng)
maxLat = max(cordsNew$lat)
minLong = min(cordsNew$lng)
minLat = min(cordsNew$lat)
proxy <-leafletProxy("map", data = cordsNew)
proxy %>%
addCircleMarkers(lat = ~lat,lng = ~lng, layerId = ~circle_pt, fillColor = 'green',
opacity = 0.5,color = 'red',fillOpacity = 1) %>%
fitBounds(minLong,minLat,maxLong,maxLat) %>%
clearPopups() %>%
addPopups(lat=~lat,lng=~lng,~as.character(circle_pt))
})
}
shinyApp(ui = ui, server = server)
may be you can define an interval varint <- findInterval() and that pass to setView
varint <- findInterval()
setView(lng= lng,lat = lat, zoom = varint)
in findInterval, try to put some range of data with the distance between all points
EDIT:
try to calculate the distance between the farthest points that appear.
proxy <-leafletProxy("map",data = df) %>%
setView(
lng = click$lng,
lat=click$lat,
zoom=findInterval(someaverageofyourpoints, c(25,75,100,250,400,750,1000))
)
you can sum other values to findinterval, findinterval()+ 1 .. 2.. 4 ...5 for setting zoom level if findintervalset a tiny value

Remove label background in leaflet maps with R

I want to use static labels in my leaflet markers but I do not want the white background that shows with the labels. Is it possible to show only the text without the white background.
library(leaflet)
temp=data.frame(lng=c(-90,-100),lat=c(30,35))
leaflet(temp) %>% addTiles() %>%
addCircleMarkers(~lng,~lat)%>%
addPopups(~lng, ~lat,'<b>Hello, world</b>!',
options = popupOptions(minWidth = 20, closeOnClick = FALSE, closeButton = FALSE))
Adding label in addMarkers solved my problem.
library(leaflet)
temp=data.frame(lng=c(-90,-100),lat=c(30,35))
leaflet(temp) %>% addTiles() %>%
addMarkers(data=temp, ~lng,~lat,
label=~as.character(lat),
labelOptions = labelOptions(noHide = T,
textOnly = T,textsize = "28px",offset = c(12, -5)))
Yes, but you will find that it works with various degrees of success right out of the Github box. But is it pretty straight forward. You will want to use this and then with the labels setOpacity. It will affect both markers and labels.
Here is the link to a pluggin you need:
Leaflet R Pluggin & directions

Resources