I am looking for a way to define labels in my leaflet map dynamically, such that they don't need to be precalculated but are generated just in time.
Let us assume we want to show the current time on the label:
leaflet() %>%
addCircleMarkers(lat=0, lng=0, label=lubridate::now())
However, this will always show the time of the map creation. Is there a way to delay the evaluation until the mouseover is actually shown?
Related
I am trying to create an interactive map of football stadiums in the UK with the leaflet package in R. I understand that I can use a custom image as the icon on the map. For a given stadium, I would like to use the badge of the club that plays there as the icon. Initially I am trying to solve the simpler problem of just using one custom image for all the stadiums with the hope that once I can solve this, I will be able to generalise and have one custom image per stadium. I will provide a simplified data set of just two stadiums for the purposes of this question.
So far I have tried using this picture (https://upload.wikimedia.org/wikipedia/en/e/e5/AFC_Bournemouth_%282013%29.svg) of the Bournemouth badge however it does not appear on the map when run. I think R cannot properly access this image but I am not sure what types it can access or how it accesses them.
So far my code looks like this
library(leaflet)
# Create Data
finalData <- data.frame(Club = c("A.F.C. Bournemouth", "A.F.C. Wimbledon"), Latitude = c("50.7352","51.4051"), Longitude = c("-1.83839","-0.281984"), stringsAsFactors = FALSE)
# Create map using leaflet
m <- leaflet() %>%
addTiles()
# Attempt to create custom icon using the linked picture
BournemouthIcon = makeIcon("https://upload.wikimedia.org/wikipedia/en/e/e5/AFC_Bournemouth_%282013%29.svg", iconWidth = 8, iconHeight = 8)
m %>% addMarkers(lat = as.numeric(finalData$Latitude),
lng = as.numeric(finalData$Longitude),
clusterOptions = markerClusterOptions(),
icon = BournemouthIcon)
I expect to see the 2 stadiums plotted on the map with the Bournemouth badge as the marker but instead the markers are blank.1 It appears R knows where to plot the points (since if you zoom out it detects the cluster of points), however it does not know what to mark the point with.2 This links back to my misunderstanding of how R is reading the picture I supplied. Any help with how to properly build a custom marker and then pass it to addMarkers would be greatly appreciated. Furthermore some guidance on generalising this to having a unique custom picture for each marker would also be great!
Does anyone have some sample code I can look at that creates an interactive map in R/Shiny? I would like to be able to have the map zoom in to a specific US State based on the user selection in a drop down
I am trying to overlay a heatmap of polygons on my map and then be able to zoom into different parts of it depending on user input.
The code works fine when I remove the heatmap. The user is able to jump around from place to place (based on a dropdown list).
With the polygons it works the first time fully,
the second dropdown choice causes just a map to show with no polygons and the location does not shift,
and the third dropdown choice nothing shows.
output$HMmapstation <- renderLeaflet({
x <- NA
x <- input$HMstation #picking the place to view
x <- leaflet_map %>% setView(preloaded_stations[which(preloaded_stations$name == x),]$longitude,
preloaded_stations[which(preloaded_stations$name == x),]$latitude,
zoom = 14) %>% clearShapes()
x <- x %>% addPolygons(data=polys_dat, color = ~pal(polys_dat#plotOrder),
smoothFactor=.1, stroke = FALSE, fillOpacity = 0.1)
x
})
The leaflet map is created globally above the server and is as follows:
leaflet_map <- (leaflet() %>%
addProviderTiles("OpenStreetMap.BlackAndWhite",
options = providerTileOptions(noWrap = TRUE,minZoom=9)) %>%#~pal(seq(1,537,1))~rainbow(50, start=0,end=.3, alpha = NULL)
setMaxBounds(-0.715485, 51.252031, 0.514984, 51.745313))
I just cant seem to figure out why it would work once and then stop. Especially since without the polygon it works as expected.
Thank you in advance for any direction you can give me.
Leaflet is an amazing addition to R's quiver, I am starting to use it quite a bit at work and at times I also struggle with lost functionality as layers build. I do not have an answer for you working directly in R, but I have a suggestion.
One of the things that I am discovering as I become a Javascript D3 developer is that the last object into a JS visualization is on top.
So this means as you stack polygons, lines, and other objects and data onto a map, it is possible to BLOCK the visibility/functionality of objects below it.
If you are familiar with Javascript, you could open the devtools in chrome and look to see how the layers are being applied to the map.
As you do this, try to think about how R will call out to JS to compile the web graphic it makes and then see if you can re-order something in your R stack, or turn a shape into an outline so that the shape below retains function.
And it is possible after the graphic is "extruded' from R to go in and manually tweak the javascript controlling the features you are using with a lot more control than you have in R itself. I have done some editing on the resulting file set successfully.
There is a LOT more help in working on leaflet in JS directly here and around the web. You may be able to find a quicker answer that way.
The fix ended up being to put the leaflet in a reactive rather than a renderLeaflet.
I also re-ordered based on bethanyP's response so thank you!
Background: I am currently drawing a leaflet map using the following code:
library(leaflet)
leaflet()%>%
addProviderTiles("Stamen.TerrainBackground")%>%
setView(lng=-81,lat=45,zoom=6)
Problem: The problem is that it takes around 4 seconds to have the map appear. This is an undesirable feature when I integrate the map with Rshiny, given that the website appears blank until the map finally loads.
I'm wondering two things:
1) Is there an open source map that is known to load quickly compared to other maps.
2) If 1) is not possible, I have noticed that in google maps, the tiles of the map start appearing in sequence. At least like that the user is able to know that the map is loading. Is there a way of having the tiles be drawn sequentially like that?
If you mean the tiles are slow to load, that is most likely either a problem with the tile provider or a problem with your connection. Leaflet itself is usually very fast in my experience.
As far as the tile provider, I've always had good success with open street map in JS.
With R it looks like this link https://rstudio.github.io/leaflet/ has the following example using OSM:
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
So calling "addTiles() %>% should use open street map as default.
I've played with the mouse click event in leaflet using the following code:
mymap=leaflet()
selected_site=eventReactive(input$mymap_marker_click,{
event <- input$mymap_map_marker_click
return(mydt[Long==event$lng & Lat==event$lat,get("sites")])
})
to display a plot from a data.table after a click on a point of the map.
Now, I'd like to select an entire area then display data binding to it.
In other terms, I'd like to select not a couple of (Lat,Long) but a series of (Lat1, Long1)....(Latn,Longn)
Is there a mean to do it in leaflet ?
Thanks in advance,
You just have to instantiate a L.Polygon per area, and attach an event handler to it: http://playground-leaflet.rhcloud.com/zab/edit?html,console,output
Keep in mind that you can make areas transparent (zero opacity but fill option still set to true) and still get mouse events from them.