Map creation by leaflet + shiny. Updating will return the map to its initial location

折月煮酒 提交于 2021-01-29 21:41:39

问题


I asked a question on the following page, but I will post it again. In shiny + leaflet, setview is updated every time render is executed. I want to prevent this

We are currently creating maps using shiny and leaflet. The shp file is projected on the map. shp file → https://drive.google.com/file/d/1ji2J0vwEE9y8F0kNujmMoKPrf1IWZ7-x/view?usp=sharing

I want to show / hide this shp file by setting the size of the area and the length of the circumference with sliderInput etc.

The sample code is as follows.

library(shiny)
library(leaflet)
library(shinycssloaders)

shinyUI(fluidPage(

  titlePanel("sample sample sample sample"),

  #sidebarLayout settings
  sidebarLayout(
    sidebarPanel(
      sliderInput("area_slider",label = h6("Area settings"),min = 0,max = 5000,value = c(0,5000)),
    ),
    #mainpanel settings
    mainPanel(
      leafletOutput("mymap",height=600)
    )
  )
))

shinyServer(function(input, output) {

  shape_path <- "C:/Users/user/Documents/"
  shp_file <- readOGR(paste0(shape_path,"sample.shp"))

  ##########################

  #map settings
  output$mymap <- renderLeaflet({
    leaflet() %>% 

      #Base Groups
      addTiles(group="OSM")%>%

      #setView
      setView(lng=139.8,lat=35.7,zoom=12)%>%


      addPolygons(data = shp_file %>%
                    subset(shp_file@data$area > input$area_slider[1] & shp_file@data$area < input$area_slider[2]),
                  color = "#808080",
                  group="area")%>%

      #Layers Control
      addLayersControl(
        baseGroups = c("OSM"),
        overlayGroups = c("area"),
        options = layersControlOptions(collapsed = FALSE))

  })

})

Past respondents have indicated that you should use LeafletProxy, I don't know how to use it. I'm glad if you could tell me.

Thank you very much.

来源:https://stackoverflow.com/questions/61024619/map-creation-by-leaflet-shiny-updating-will-return-the-map-to-its-initial-loc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!