在闪亮的情况下,如何修复(锁定)传单地图视图缩放和居中?
In shiny, how to fix (lock) leaflet map view zoom and center?
我正在构建与此类似的应用 one。在地图上,如果放大然后更改 slider/input,缩放级别将自动重置为默认值。我想在不更改缩放级别的情况下呈现地图的新实例,直到用户将其更改回来。理想情况下,我会添加一个按钮来将缩放重置为原始设置。
我查看了这些帖子:, 2, and 。
第三个 link 中的代码对我来说很有意义,但仍然没有用。他们 code, based on the comment, should have fixed the zoom issue but not the centering - neither worked for me. Below, I modified the original 应用尽可能接近我的应用。我还实现了两个更改以尝试实现所需的地图视图行为 - 我添加了两个反应函数:缩放和居中。这是修改后的 repex:
library(shiny)
library(ggplot2)
library(plotly)
library(leaflet)
qDat <- quakes
ui <- fluidPage(
titlePanel("pyData Shiny Demo"),
sidebarLayout(
sidebarPanel(
h3("Fiji Earthquake Data"),
selectInput("select01", "Select earthquakes based on:",
choices=c("Magnitude"="mag",
"Depth"="depth"),
selected="mag"),
conditionalPanel(condition="input.select01=='mag'",
sliderInput("sld01_mag",
label="Show earthquakes of magnitude:",
min=min(qDat$mag), max=max(qDat$mag),
value=c(min(qDat$mag),max(qDat$mag)), step=0.1)
),
conditionalPanel(condition="input.select01=='depth'",
sliderInput("sld02_depth",
label="Show earthquakes of depth:",
min=min(qDat$depth), max=max(qDat$depth),
value=c(min(qDat$depth),max(qDat$depth)), step=5)
),
plotlyOutput("hist01")
),
mainPanel(
leafletOutput("map01"),
dataTableOutput("table01")
)
)
)
server <- shinyServer(function(input, output) {
qSub <- reactive({
if (input$select01=="mag"){
subset <- qDat[qDat$mag>=input$sld01_mag[1] & qDat$mag<=input$sld01_mag[2],]
}else{
subset <- qDat[qDat$depth>=input$sld02_depth[1] & qDat$depth<=input$sld02_depth[2],]
}
subset
})
output$hist01 <- renderPlotly({
ggplot(data=qSub(), aes(x=stations))+
geom_histogram(binwidth=5)+
xlab("Number of Reporting Stations")+
xlim(min(qDat$stations), max(qDat$stations))+
ylab("Count")+
ggtitle("Earthquakes near Fiji")
})
output$table01 <- renderDataTable({
qSub()
})
zoom <- reactive({
ifelse(is.null(input$map01_zoom),3,input$map01_zoom)
})
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))
})
pal <- colorNumeric("YlOrRd", domain=c(min(quakes$mag), max(quakes$mag)))
output$map01 <- renderLeaflet({
leaflet(data=qSub()) %>%
addTiles() %>%
addLegend("bottomright", pal = pal, values = ~mag,
title = "Earthquake Magnitude",
opacity = 1)
})
observe({
leafletProxy("map01") %>%
clearShapes() %>%
#setView(lng = 179.462, lat = -20.64275, zoom = 3) %>%
setView(lng = center()[1],
lat = center()[2],
zoom = zoom()) %>%
addCircleMarkers(
data=qSub(),
radius = 2,
color = ~pal(mag),
stroke = FALSE, fillOpacity = 1, popup=~as.character(mag))
})
})
shinyApp(ui = ui, server = server)
关于如何实现这一目标的任何提示?
谢谢!
你快到了。您的应用程序中只有一个错误:
你需要改变
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))
})
到
center <- reactive({
if(is.null(input$map01_center)){
return(c(179.462, -20.64275))
}else{
return(input$map01_center)
}
})
第一个原因是当向量的长度超过 1 时 ifelse
不起作用,第二个原因是 input$map01_center
给你中心。
希望对您有所帮助!
我正在构建与此类似的应用 one。在地图上,如果放大然后更改 slider/input,缩放级别将自动重置为默认值。我想在不更改缩放级别的情况下呈现地图的新实例,直到用户将其更改回来。理想情况下,我会添加一个按钮来将缩放重置为原始设置。
我查看了这些帖子:
第三个 link 中的代码对我来说很有意义,但仍然没有用。他们 code, based on the comment, should have fixed the zoom issue but not the centering - neither worked for me. Below, I modified the original 应用尽可能接近我的应用。我还实现了两个更改以尝试实现所需的地图视图行为 - 我添加了两个反应函数:缩放和居中。这是修改后的 repex:
library(shiny)
library(ggplot2)
library(plotly)
library(leaflet)
qDat <- quakes
ui <- fluidPage(
titlePanel("pyData Shiny Demo"),
sidebarLayout(
sidebarPanel(
h3("Fiji Earthquake Data"),
selectInput("select01", "Select earthquakes based on:",
choices=c("Magnitude"="mag",
"Depth"="depth"),
selected="mag"),
conditionalPanel(condition="input.select01=='mag'",
sliderInput("sld01_mag",
label="Show earthquakes of magnitude:",
min=min(qDat$mag), max=max(qDat$mag),
value=c(min(qDat$mag),max(qDat$mag)), step=0.1)
),
conditionalPanel(condition="input.select01=='depth'",
sliderInput("sld02_depth",
label="Show earthquakes of depth:",
min=min(qDat$depth), max=max(qDat$depth),
value=c(min(qDat$depth),max(qDat$depth)), step=5)
),
plotlyOutput("hist01")
),
mainPanel(
leafletOutput("map01"),
dataTableOutput("table01")
)
)
)
server <- shinyServer(function(input, output) {
qSub <- reactive({
if (input$select01=="mag"){
subset <- qDat[qDat$mag>=input$sld01_mag[1] & qDat$mag<=input$sld01_mag[2],]
}else{
subset <- qDat[qDat$depth>=input$sld02_depth[1] & qDat$depth<=input$sld02_depth[2],]
}
subset
})
output$hist01 <- renderPlotly({
ggplot(data=qSub(), aes(x=stations))+
geom_histogram(binwidth=5)+
xlab("Number of Reporting Stations")+
xlim(min(qDat$stations), max(qDat$stations))+
ylab("Count")+
ggtitle("Earthquakes near Fiji")
})
output$table01 <- renderDataTable({
qSub()
})
zoom <- reactive({
ifelse(is.null(input$map01_zoom),3,input$map01_zoom)
})
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))
})
pal <- colorNumeric("YlOrRd", domain=c(min(quakes$mag), max(quakes$mag)))
output$map01 <- renderLeaflet({
leaflet(data=qSub()) %>%
addTiles() %>%
addLegend("bottomright", pal = pal, values = ~mag,
title = "Earthquake Magnitude",
opacity = 1)
})
observe({
leafletProxy("map01") %>%
clearShapes() %>%
#setView(lng = 179.462, lat = -20.64275, zoom = 3) %>%
setView(lng = center()[1],
lat = center()[2],
zoom = zoom()) %>%
addCircleMarkers(
data=qSub(),
radius = 2,
color = ~pal(mag),
stroke = FALSE, fillOpacity = 1, popup=~as.character(mag))
})
})
shinyApp(ui = ui, server = server)
关于如何实现这一目标的任何提示?
谢谢!
你快到了。您的应用程序中只有一个错误:
你需要改变
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))
})
到
center <- reactive({
if(is.null(input$map01_center)){
return(c(179.462, -20.64275))
}else{
return(input$map01_center)
}
})
第一个原因是当向量的长度超过 1 时 ifelse
不起作用,第二个原因是 input$map01_center
给你中心。
希望对您有所帮助!