tooltip for htmlwidget leaflet in R - r

I'm using the wonderful leaflet package in R and I'm wondering if there is a way to have a tooltip such that:
1- the user could draw a rectangle and
2- the code could estimate the actual physical size of the rectangle (in, say, feet) given the zoom level of the map and legs of the tooltip rect
## This doesn't work and mixes metaphors from leaflet and ggvis
## It's just pseudocode to give intuition
library(leaflet)
library(ggvis)
lb <- linked_brush()
map <- leaflet() %>% addTiles() %>% addCircleMarkers() %>% lb()
size <- lb()$rect_width * lb()$rect_height * map$zoom_factor

Related

How to plot a large rasterfile quickly in R leaflet with shiny

I obtianed a large rasterfile (.asc file), which I can import in R and plot on a leaflet map. Because the file is pretty big, it takes about 40 seconds before the rasterimage is plotted on the basemap. This in comparison to shapefiles, which are plotted almost instantly.
I am wondering if there is a way to have the rasterfile plotted on the basemap more quickly?
The code I use to plot the file on the map is the following:
r = raster("file.asc")
leaflet() %>% addSearchOSM() %>%
addProviderTiles(providers$CartoDB.Positron,
options = providerTileOptions(noWrap = TRUE),
group = "kaart") %>%
addRasterImage(r, opacity = 0.5)

R leaflet addRasterImage doesn't work at zooms higher than 18

I am wanting to use the addRasterImage() function to a leaflet object in R.
However it only seems to work up-to a zoom level of 18. Please see the example below.
library(raster)
library(leaflet)
library(magrittr)
r <- raster(xmn=-2.8, xmx=-2.79, ymn=54.04, ymx=54.05, nrows=30, ncols=30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- CRS("+init=epsg:4326")
leaflet() %>% addTiles() %>%
setView(lat=54.045,lng=-2.795,zoom=19) %>%
addRasterImage(r, colors = "Spectral", opacity = 0.8)
I know that the addTiles() function naturally only shows tiles up to a zoom of 18, but I was assuming that the raster image should still show at the higher zoom level. Furthermore, there are other tile providers (e.g. HERE) that do have zoom levels that go higher than 18, but the issue with the lack of the Raster image on the leaflet object still persists.
Any help would be greatly appreciated.

Using markerClusterOptions with SpatialPolygons in Leaflet

I am using the package Leaflet to plot around 25.000 polygons on a map. Because of this large number of polygons I want to make use of markerClusterOptions.
This is what I would like to do:
leaflet() %>%
addTiles() %>%
addPolygons(data=sp_polygons,
clusterOptions = markerClusterOptions())
But addPolygons doesn't know clusterOptions.
What is possible, is to do the following
leaflet() %>%
addTiles() %>%
addMarkers(data=coordinates(sp_polygons),
clusterOptions = markerClusterOptions())
But when I zoom in I only markers and not the polygons. Is there a way to use clusterOptions but still show the polygons when zooming in?
To cut a long story short, you could simply create a SpatialPointsDataFrame from the polygon data (as you did above using coordinates) and subsequently display points and polygons on the same map. Here is an example using the mapview package.
library(sp)
library(mapview)
## create spatial points from Switzerland administrative borders
gadmCHE_pts <- SpatialPointsDataFrame(coordinates(gadmCHE),
data = gadmCHE#data,
proj4string = CRS(proj4string(gadmCHE)))
## display data
mapview(gadmCHE_pts, clusterOptions = markerClusterOptions()) +
gadmCHE

R Leaflet change layer order

I'm trying to produce a leaflet map in R with a layer (e.g. WMS tile or raster) and some circles. Since there are many circles in my application, I need to change the order, i.e. the layer should be shown on top of the circles.
Is there a way in R to change the order such that I can bring the WMS tile to the front or sent the circles to the background?
My code looks like the following (in a simplified way):
library(leaflet)
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>%
addCircles(lng=-98,lat=40,radius=100000,color = "red",stroke=FALSE,fillOpacity=1) %>%
addWMSTiles(
baseUrl="http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
layers ="nexrad-n0r-900913",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "Weather data © 2012 IEM Nexrad"
)

How to add a scale bar in the R package leaflet?

I have recently discovered the r package “leaflet” and found a great blog with some basic instruction for creating an interactive map (found here) http://mhermans.net/hiking-gpx-r-leaflet.html.
I have not, however been successful at adding a scale bar on the map. That is, I’d like to add a graphic feature that scales distance as you zoom in and out of the map (e.g. a bar at the bottom of the map that represents 1km). The leaflet site (found here) http://leafletjs.com/reference.html#control-scale-l.control.scale discusses this feature: L.control,scale().
Does anyone know how to add a scale bar??
This is the code for my map so far (The “Add Scale Bar” Does NOT work):
# A map of Track data
Mymap <- leaflet() %>% addTiles() %>%
addPolylines(data=Dofu1) %>%
addPolylines(data=Zak1) %>%
addProviderTiles("Esri.WorldImagery")
# Add a legend
Mymap %>%
addLegend(position = 'topright',
colors = "blue",
labels = 'Buruku Tracks', opacity = 0.5,
title = '')
# Add a Scale Bar
Mymap %>%
addControl(Mymap, "Scale",
position = c("topright"),
layerID = NULL,
className = "Scale",
data - getMapData(Mymap))
This feature has been added to the development version of the leaflet package. See Add support for scale bar. Also, the second argument to addControl expects html as either a character string or html generated from Shiny or htmltools tags. Finally, I think you have a typo in addControl: data - getMapData(Mymap) should be data = getMapData(Mymap)
the scale bar function is implemented and can be added with 'addScaleBar'
leaflet() %>%
addTiles() %>%
addScaleBar()

Resources