Restricting Zoom Options in Leaflet Maps - r

I followed the tutorial here (https://rstudio.github.io/leaflet/) and made this map:
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R") %>% setView(174.768, -36.852, zoom = 12)
When I run this code, the map comes out like this:
However, I would like the map to have the "zoom out" option disabled (I would like to keep the "zoom in" option, as well as the side-to-side scroll option).
Is there some way that prevents the user from being abled to "zoom out" from the specified zoom level, and only allows the user to "zoom in"?
I tried:
# source: https://stackoverflow.com/questions/36365897/r-leaflet-zoomcontrol-option
m <- leaflet((options = leafletOptions(zoomControl = FALSE,
minZoom = 3, maxZoom = 3,
dragging = FALSE)) %>%
addTiles() %>%
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")) %>% setView(174.768, -36.852, zoom = 12)
But this gives me an error: Error in dispatch(map, method, leaflet = { : Invalid map parameter
Thank you!

Related

How to resize large images in R Leaflet marker popups?

My code:
library(leaflet)
df <- as.data.frame(read.csv("arts.csv"))
file <- as.character(df$url)
leaflet() %>% addTiles()
%>% addMarkers(data = df, lng = ~lon, lat = ~lat,
popup = paste0("<img src = ", file, ">"))
%>% popupOptions(maxWidth = "auto")
I used some code snippet from here: Image in R Leaflet marker popups
My question is: how can I easily resize large images appearing in popups?
The illustartion of the problem:
For Example, I would like to make this photo of Cave Rapa Nui smaller to fit the pop-up.
Thank you in advance!
You can adjust the size by using the HTML width attribute.
Edit your code from above to e.g.
map <- leaflet() %>%
addTiles() %>%
addMarkers(data = df,
lng = ~lon,
lat = ~lat,
popup = paste0("<img src = ", file, " width = 300>"))
map # print the map

Leaflet not generating properly in dynamic R markdown document

I have asked this question before here.
Initially the focus was on a point map, which the answer fixed. However looking at clustered and heatmaps, the issue persists.
Point generates fine (fixed via previous question)
Clustered do not appear on map
Heatmap doesn't show at all (whitespace)
Minimal code example:
library(tidyverse)
library(leaflet)
library(leaflet.extras)
leaflet()
long <- as.numeric(c("0.005638", "0.005648", "0.005658"))
lat <- as.numeric(c("51.62879", "51.62889", "51.62879"))
data1 <- data.frame(long, lat)
filtered_list <- 1:3
cat("## Tabs {.tabset .tabset-fade .tabset-pills}", "\n")
for (estates in filtered_list){
cat("###", estates, "\n")
cat("\n\n\n")
cat("This is where the map will go ")
cat("1 ")
# generate leaflet plot
page <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat)
)
cat(as.character(page))
cat("2 ")
page1 <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat, clusterOptions = markerClusterOptions())
)
cat(as.character(page1))
cat("3 ")
page2 <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat) %>%
addHeatmap(
lng = data1$lat, lat = data1$long,
blur = 20, max = 5, radius = 40
)
)
cat(as.character(page2))
}
The maps render fine in R Studio, e.g.

Display David Rumsey' Georeferencer tiles in R leaflet

I am trying tu use David Rumsey' Georeferencer tiles in R leaflet package without success (Only the OSM basemap is displayed).
According to the package vignette I tried :
library(leaflet)
# XYZ direct link
leaflet() %>%
setView(0.65, 0, zoom = 5) %>%
addTiles() %>%
addTiles("https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/map.json?key=D7AwmpRP1H6pUic6DIK3")
and
library(leaflet)
# WMS tiles
leaflet() %>%
addTiles() %>% setView(0.65, 0, zoom = 5) %>%
addWMSTiles(
"https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/wmts?key=D7AwmpRP1H6pUic6DIK3&SERVICE=WMTS&REQUEST=GetCapabilities",
layers = "1", # I assume
options = WMSTileOptions(format = "image/png", transparent = FALSE),
attribution = "")
Notes :
- The access at the map links need free registration.
- I used the 2.1 package release.
- The vignette's addWMSTiles example is working on my computer.
I solve my problem using the addLayersControl function that can allow me to choose the layer to display.
library(leaflet)
m <- leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels, group = "DarkMatter") %>%
addTiles("https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/map/{z}/{x}/{y}.png?key=D7AwmpRP1H6pUic6DIK3", group ="H") %>%
addLayersControl(
baseGroups = c("DarkMatter", "H"), position ="topleft")
m

Blank popup in leaflet R

I am trying to map San Francisco's crimes in a map. The below code intends to map every crime (lat, lng) and when the marker is clicked, show the "category" column of the dataset.
Right now the below code shows a blank text box when I click the marker.
Can anybody help?
sf <- read.csv("https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/sanfrancisco_incidents_summer_2014.csv")
crime <- data.frame(lat = c(sf$Y),
lng = c(sf$X))
cat <- c(sf$Category)
library(leaflet)
crime %>%
leaflet() %>%
addTiles() %>%
addMarkers(popup = paste(sf$Category), clusterOptions = markerClusterOptions())
Try the following:
sf <- read.csv("https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/sanfrancisco_incidents_summer_2014.csv")
library(leaflet)
sf %>%
leaflet() %>%
addTiles() %>%
addMarkers(lat = ~Y, lng = ~X, popup = ~Category, clusterOptions = markerClusterOptions())
I'm not sure what your issue is, but using the formula syntax allows leaflet to build the list of pop-up labels on its own and does not require calling paste explicitly or subsetting the original dataframe.

Loading WMS layer in R leaflet using addWMSTiles

I'm trying to add WMS tiles in the R leaflet package - not a problem using this example geoserver WMS:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>% addWMSTiles(
"http://sedac.ciesin.columbia.edu/geoserver/wms",
layers = "energy:energy-pop-exposure-nuclear-plants-locations_plants",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
tileOptions(tms = TRUE),
attribution = "")
However, when I try using WMS from the National Map, I continue to get empty leaflet results despite multiple attempts at trying to set parameters correctly for url and layers:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>%addWMSTiles(
"http://basemap.nationalmap.gov/arcgis/services/USGSHydroNHD/MapServer/WmsServer?",
layers = "0",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "")
I've not used leaflet before outside the R leaflet package, so this may be a very novice mistake in setting my parameters in leaflet using this type of WMS
You just need to be zoomed in a little further for the layer to show up. Try this:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 7) %>%addWMSTiles(
"http://basemap.nationalmap.gov/arcgis/services/USGSHydroNHD/MapServer/WMSServer?",
layers = "0",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "")

Resources