R 在shiny中,如何固定(锁定)传单地图视图缩放和居中?
我正在构建一个类似的应用程序。在地图上,如果放大然后更改滑块/输入,缩放级别将自动重置为默认值。我希望渲染地图的新实例,而不更改缩放级别,直到用户将其更改回来。理想情况下,我会添加一个按钮,将缩放重置为原始设置 我看了这些帖子:,和 第三个链接中的代码对我来说是有意义的,但仍然不起作用。根据这条评论,蒂尔本应该解决缩放问题,但没有解决定心问题——这两个问题对我都不起作用。下面,我修改了应用程序,使其尽可能接近我的应用程序。为了实现所需的地图视图行为,我还实现了两个更改—我添加了两个被动功能:缩放和居中。以下是修改后的repex:R 在shiny中,如何固定(锁定)传单地图视图缩放和居中?,r,shiny,r-leaflet,R,Shiny,R Leaflet,我正在构建一个类似的应用程序。在地图上,如果放大然后更改滑块/输入,缩放级别将自动重置为默认值。我希望渲染地图的新实例,而不更改缩放级别,直到用户将其更改回来。理想情况下,我会添加一个按钮,将缩放重置为原始设置 我看了这些帖子:,和 第三个链接中的代码对我来说是有意义的,但仍然不起作用。根据这条评论,蒂尔本应该解决缩放问题,但没有解决定心问题——这两个问题对我都不起作用。下面,我修改了应用程序,使其尽可能接近我的应用程序。为了实现所需的地图视图行为,我还实现了两个更改—我添加了两个被动功能:缩放
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)
库(闪亮)
图书馆(GG2)
图书馆(绘本)
图书馆(单张)
qDat你就快到了。您的应用程序中只有一个错误:
你需要换衣服
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确实解决了这个问题。谢谢不确定是否注意到贴图在加载或移动滑块时渲染缓慢。我的应用程序显示的数据比repex中使用的数据多,速度也慢得多。有什么改进的建议吗?隔离反应变量center
和zoom
应该会使代码运行得更快一些。
center <- reactive({
if(is.null(input$map01_center)){
return(c(179.462, -20.64275))
}else{
return(input$map01_center)
}
})