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
Related
Anyone created a leaflet map in Code Workbook using r-Leaflet? I have a functioning script that runs (also double checked in R) but how do I get it to visualise and then use in a Report etc. I have tried various tweaks on what may get it to run but no success - any ideas
leaflet_map <- function(map_data) {
library(leaflet)
data<-map_data
# first cut the continuous variable into bins
# these bins are now factors
data$Fill_rateLvl <- cut(data$Fill_rate,
c(0,.5,0.6,0.7,0.8,0.9,1), include.lowest = T,
labels = c('<50%', '50-60%', '60-70%', '70-80%', '80-90%','90-100%'))
# then assign a palette to this using colorFactor
# in this case it goes from red for the smaller values to yellow and green
# standard stoplight for bad, good, and best
FillCol <- colorFactor(palette = 'RdYlGn', data$Fill_rateLvl)
m<-leaflet() %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron)%>%
setView(lng = -0, lat = 50, zoom = 8) %>%
addCircleMarkers(data = data, lat = ~lat, lng = ~long,
color = ~FillCol(Fill_rateLvl), popup = data$Lead_employer,
radius = ~sqrt(Fill_rate*50), group = 'Fill rate') %>%
addLegend('bottomright', pal = FillCol, values = data$Fill_rateLvl,
title = 'Fill rate for next two weeks',
opacity = 1)
return(NULL)
}
I am not familiar with R in code workbook, but it sounds to me that you need to materialize your leaflet map as a dataset and then consume it in some sort of map compatible UI.
For example slate has a map widget which is backed by leaflets. You can find documentation and examples for it in https://www.palantir.com/docs/foundry/slate/widgets-map/
The background to the question is as follows:
I have a shiny app that presents data on a leaflet map. That data can be filtered and presented in different ways. However, when the user zooms in and/or moves the map, and THEN filters the data, the map will revert back to the initial map view:
output$mymap <- renderLeaflet({
leaflet() %>%
setView(lng = 0, lat = 30, zoom = 4) %>% #setting the view over ~ center of North America
addTiles(options = providerTileOptions(noWrap = TRUE))
})
What I am looking for is some way of returning the position of the map such that when the data is updated in a proxy leaflet function I can set the view as well. The following is what I'm trying to achieve (I've made up a move equivalent of input$mymap_click).
observeEvent(input$mymap_move, {
move <- input$mymap_move
mlat <- move$lat
mlon <- move$lon
mzoom <- move$zoom
leafletProxy('mymap') %>%
setView(lng = mlng, lat = mlat, zoom = mzoom) %>%
addMarkers(data = data.join,
lat = data.join$a.lat,
lng = data.join$a.lon,
icon = icon.func[data.join$SYMBOL_CODE],
popup = data.join$ANCHOR)
})
Once your map is initialised, you can access the current state of your map via input$mymap_center and input$mymap_zoom (and if you wish you can access input$mymap_bounds)
There are just 2 things you need to pay attention to:
You have to check whether initialisation is complete yet, and if not give manual parameters (or Shiny won't know what starting position to use)
Unless you isolate the lookup, your map will re-render every time you change the view, which is probably not what you want (it makes your app really slow, and unworkable to the user)
So putting it all together, I think this code should do what you wish:
output$mymap <- renderLeaflet({
isolate({
if ("mymap_center" %in% names(input)) {
mapparams <- list(center = input$mymap_center,
zoom = input$mymap_zoom)
} else {
mapparams <- list(center = list(lng=0, lat=30),
zoom = 4) #setting the view over ~ center of North America
}
})
leaflet() %>%
setView(lng = mapparams$center$lng, lat = mapparams$center$lat, zoom = mapparams$zoom) %>%
addTiles(options = providerTileOptions(noWrap = TRUE))
})
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
I'm building an interactive map in Leaflet using R, and I would like to use the Leaflet.MarkerCluster.PlacementStrategies sub-plugin to control the placement of markers within a cluster (when "spiderfied" on mouse click over the cluster), and always lay them out in a circle around the cluster icon, rather than along a spiral as it happens when there are more than 8 markers in a cluster with the standard Leaflet.MarkerCluster plugin that comes embedded in the leaflet htmlWidget. The code I'm using to build the map is something like this:
library(leaflet)
data_example <- data.frame(name = rep("site A", times = 14),
lon = rep(14.25000, times = 14),
lat = rep(40.83333, times = 14),
issue = paste("Issue", LETTERS[1:14]),
severity = sample(c("Mild", "Moderate", "Severe"), size = 14, replace = TRUE))
issues_pal <- colorFactor(brewer.pal(3, "RdYlBu"), levels = c("Mild", "Moderate", "Severe"), reverse = TRUE)
leaflet(data_example, options = leafletOptions(maxZoom = 7)) %>%
setView(lng = 8, lat = 50, zoom = 4) %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
addCircleMarkers(
lng = ~ lon,
lat = ~ lat,
radius = 10,
color = "grey",
weight = 2,
fillColor = ~ issues_pal(severity),
#stroke = FALSE,
fillOpacity = 0.8,
label = ~ paste(issue, severity),
clusterOptions = markerClusterOptions(
spiderfyDistanceMultiplier = 1.1,
spiderLegPolylineOptions = list(weight = 0)),
group = df,
clusterId = df
)
In the map, when one clicks on the cluster icon, the markers are shown placed along a spiral path originating from the cluster. This is because the Leaflet.MarkerCluster plugin that control the clustering of markers in leaflet, place them along a circle only when there are up to 8 markers. But the Leaflet.MarkerCluster.PlacementStrategies should allow to specify a different placement strategy, and to have markers laid out in a circle even if they are more than 8, as shown in this page and here.
Since the sub-plugin is not yet included in leaflet package, I'd like to use the method highlighted here to use in R any leaflet JS plugins, but I'm struggling to find a way to make it work for my case. According to the method, I should first of all include the following in my code:
placement.strategies <- htmlDependency(
"Leaflet.MarkerCluster.PlacementStrategies",
"0.0.1",
src = c(href = "https://github.com/adammertel/Leaflet.MarkerCluster.PlacementStrategies/blob/master/dist/"),
script = "leaflet-markercluster.placementstrategies.src.js"
)
registerPlugin <- function(map, plugin) {
map$dependencies <- c(map$dependencies, list(plugin))
map
}
And then I should call registerPlugin(placement.strategies) in the leaflet pipe. However, I can't figure out how to adapt the following custom JS code to my needs:
leaflet() %>% setView(lng = 8, lat = 50, zoom = 4) %>%
# Register plugin on this map instance
registerPlugin(placement.strategies) %>%
# Add your custom JS logic here. The `this` keyword
# refers to the Leaflet (JS) map object.
onRender("function(el, x) {
L.esri.basemapLayer('Topographic').addTo(this);
}")
I've tried to simply specify in the call to addCircleMarkers() the elementsPlacementStrategy = 'one-circle' as a clusterOption within the markerClusterOptions(), but it does not work. I suspect the JS code I need to call from should somehow modify the option property elementsPlacementStrategy for the cluster, but how to do that I don't know. Can anyone help with this? Thank you in advance!
post scriptum
I have found a way around to get the circle placement without using the subplugin, that involves modifying in the leaflet.markercluster.js file the _circleSpiralSwitchover property to a number greater than 14 (that is the number of markers I have in the cluster). While this workaround achieves the result I wanted, it is sub-optimal, because unless I decide to modify the default .js file that is located in:
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/leaflet/htmlwidgets/plugins/Leaflet.markercluster
I would need to remake the edit on the .js file every time I export the map with saveWidget.
I am able to retrive a Direction map with the below Rscript, but I am unable to find function for making it Interactive.
longitude <- c( 77.605855,77.606800,77.596843,77.575793 )
latitude <- c(12.956580,12.966157, 12.964777,12.964473)
d <- as.data.frame(cbind(longitude,latitude))
map <- get_googlemap(center = c(lon = 77.605855, lat = 12.956580), zoom = 14,
size = c(500, 500), scale = 2,maptype = c("roadmap"),markers = d, path = d)
Below are the functionalities that I need to have in my map.
1. Interactive Zoom.
2. Auto Center so that all the markers are visible.
3. OnClick on the marker I would like to show title -say for Eg. "This is Your Car".
The documentation leaflet referred by #SymbolixAU at the comments helped me to arrive at the solution, below is my code to address my requirements mentioned in the question.
library(leaflet)
longitute <-c(77.605855,77.606800,77.596843,77.596747,77.596296,77.595738,77.594944 )
latitude <- c(12.956580,12.966157, 12.964777,12.964323,12.963570,12.962964, 12.962399)
d <- as.data.frame(cbind(longitute,latitude))
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=d$longitute, lat=d$latitude, popup="New Point",)
m<- addPolylines(m , lng = d$longitute, lat = d$latitude)
m
So this is my Output.