How to get the zoom level from the leaflet map in R/shiny?
You can access the zoom level using input$mapid_zoom
(see here).
In your observe
, you could do:
observe({
sel_site <- df[df$site == input$site,]
isolate({
new_zoom <- 4
if(!is.null(input$map_zoom)) new_zoom <- input$map_zoom
leafletProxy('map') %>%
setView(lng = sel_site$lng, lat = sel_site$lat, zoom = new_zoom)
})
})
setting the zoom level to 7.25 in shiny leaflet r
Use leaflet map options:
zoomSnap
: how small you can define the zoom level.zoomDelta
: how much the view zooms when clicking zoom control buttons
leaflet(options = leafletOptions(zoomSnap = 0.25, zoomDelta=0.25)) %>%
addTiles()%>%
setView(lng=-82.706838, lat=40.358615, zoom=7.25)
R Shiny Leaflet How to Change Circles Size Based on Zoom Level
Alright, this ended up being more involved than I thought but I finally got it to work. There is an observer that is the key. I also had to learn what leafletProxy() is (research it if you don't know). Finally, clearing the shapes with clearShapes() in the observer was key to getting this working when zooming both in and out. See code below.
## app.R ##
library(leaflet)
library(shinydashboard)
library(shinydashboardPlus)
library(dplyr)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
leafletOutput("map", width = "100%", height = "500px")
)
)
server <- function(input, output,session) {
df <- data.frame("Lat"=c(32.921821,32.910853,32.793803,32.995084,32.683745,32.759999,32.800652,32.958861,32.835963,32.762578,32.649651,32.862843,32.862217,32.936876,32.963381),
"Long"=c(-96.840609,-96.738831,-96.689232,-96.857858,-96.825345,-96.684475,-96.794144,-96.816111,-96.676371,-96.897331,-96.944426,-96.754719,-96.856976,-96.752718,-96.770249))
observeEvent(
eventExpr = input$map_zoom, {
print(input$map_zoom) # Display zoom level in the console
leafletProxy(
mapId = "map",
session = session
)%>% clearShapes() %>%
addCircles(data=df,lng = ~Long, lat = ~Lat,
weight = case_when(input$map_zoom <=4 ~1,
input$map_zoom ==5 ~2,
input$map_zoom ==6 ~3,
input$map_zoom ==7 ~5,
input$map_zoom ==8 ~7,
input$map_zoom ==9 ~9,
input$map_zoom >9 ~11),
opacity = 1, fill = TRUE, fillOpacity = 1 )
}
)
output$map <- renderLeaflet({
leaflet() %>%
addTiles(urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>') %>%
setView(lng = -96.84, lat = 32.92, zoom = 6)
})
}
shinyApp(ui, server)
shiny leaflet display labels based on zoom level
A few remarks on your code if you like.
If you wrap the zoom in a reactive function, reference it like mapscale()
. Use the normal if
statement in R and the ~
in front of the variable. Then you should be fine.
Reproducible example:
library(shiny)
library(leaflet)
df <- data.frame(
location_name = c('S1', 'S2'),
lng = c(-1.554136, -2.10401),
lat = c(47.218637, 47.218637),
stringsAsFactors = FALSE
)
ui <- shinyUI(
fluidPage(
leafletOutput(outputId = 'map')
)
)
server <- shinyServer(function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles()
})
observeEvent(
eventExpr = input$map_zoom, {
print(input$map_zoom) # Display zoom level in the console
leafletProxy(
mapId = "map",
session = session
) %>%
clearMarkers() %>%
addMarkers(
data = df,
lng = ~lng,
lat = ~lat,
label = if(input$map_zoom < 6) ~location_name
)
}
)
})
shinyApp(
ui = ui,
server = server
)
In shiny, how to fix (lock) leaflet map view zoom and center?
You were nearly there. There is just one mistake in you app:
You'll need to change
center <- reactive({
ifelse(is.null(input$map01_bounds),
c(179.462, -20.64275),
c((input$map01_bounds$bounds$north + input$map01_bounds$bounds$south)/2.0,
(input$map01_bounds$bounds$east + input$map01_bounds$bounds$west)/2.0))
})
to
center <- reactive({
if(is.null(input$map01_center)){
return(c(179.462, -20.64275))
}else{
return(input$map01_center)
}
})
The first reason being the ifelse
does not work when length of vector more than 1 and second is that input$map01_center
gives you the center.
Hope it helps!
Shiny leaflet display point weight based on zoom level breaks down when using if-else statement
Try case_when
as
weight = case_when(input$map_zoom <=8 ~2, input$map_zoom >8 ~3)
This works fine for me:
observeEvent(eventExpr = input$mymap_zoom, {
print(input$mymap_zoom) # Display zoom level in the console
mywt <- case_when(input$mymap_zoom <=8 ~1, input$mymap_zoom >8 ~6)
print(mywt)
leafletProxy(
mapId = "mymap" , session = session
)%>%
clearShapes() %>%
addCircles(data=df,lng = ~Long, lat = ~Lat,
weight = mywt ,
opacity = 1, fill = TRUE, fillOpacity = 1 )
}
)
You just need clearShapes()
Related Topics
Apply Grouped Model Back Onto Data
Plotting Multiple Curves Same Graph and Same Scale
Rm(List=Ls()) Doesn't Completely Clear the Workspace
Replace Accented Characters in R with Non-Accented Counterpart (Utf-8 Encoding)
How to Fill Nas with Locf by Factors in Data Frame, Split by Country
Functions Available for Tufte Boxplots in R
Why Does Rendering a PDF from Rmarkdown Require Closing Rstudio Between Renders
R Fails After Installing Gtk and Rgtk2
How to Clean Twitter Data in R
Histogram with "Negative" Logarithmic Scale in R
How to Get Parameters from Config File in R Script
How to Display Widgets Inline in Shiny
Removing Rows in R Based on Values in a Single Column
Generate Observers for Dynamic Number of Inputs
Copy Upper Triangle to Lower Triangle for Several Matrices in a List
How to Unscale the Coefficients from an Lmer()-Model Fitted with a Scaled Response