r - 在 Shiny 的情况下,如何修复(锁定)传单 map View 缩放和居中?

标签 r shiny r-leaflet

我正在构建与此类似的应用 one .在 map 上,如果您放大然后更改 slider /输入,缩放级别将自动重置为默认值。我想在不更改缩放级别的情况下呈现 map 的新实例,直到用户将其更改回来。理想情况下,我会添加一个按钮来将缩放重置为原始设置。

我查看了这些帖子:1 , 2 , 和 3 .

第三个链接中的代码对我来说很有意义,但仍然没有用。蒂尔code ,根据评论,应该解决缩放问题而不是居中 - 对我来说都不起作用。下面,我修改了original应用程序尽可能接近我的应用程序。我还实现了两个更改以尝试实现所需的 map View 行为 - 我添加了两个 react 函数:缩放和居中。这是修改后的 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 给你中心。

希望对您有所帮助!

关于r - 在 Shiny 的情况下,如何修复(锁定)传单 map View 缩放和居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48397262/

相关文章:

r - 如何防止 matplotlib 循环使用线条样式?

一个 dockerfile 中的 RStudio 和 Shiny

r - 使用 slider 在 Shiny 的应用程序中清除传单标记

r - 创建缩放限制以不显示 map 数据的功能尚不可用

R:为传单 map 添加标题

r - 传单 map 防止浏览器滚动

r - 使用条件求 R 中的累积和

r - knit 命令不起作用而 knit HTML 按钮起作用

顶部渲染 Shiny 选择下拉菜单

python - 具有可变宽度元素的堆​​积条形图?