问题
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