我一直在尝试从我的leaflet地图中选择多个区域,并更新选择输入框,随后Go按钮会将用户带到一个新标签页,显示所选区域的信息。
简而言之,我需要让我的地图响应多选,或者将我们之前点击的缓存存储在某个地方,这样每次点击时就不会重新初始化选择输入框。
附上我的代码。
library(shiny)
library(rgdal)
library(leaflet)
library(raster)
library(tmap)
library(shinyjs)
India <- getData("GADM", country = "India", level = 2)
wb <- subset(India, NAME_1 == "West Bengal")
#making a dummy dataframe x
x<-data.frame(wb$NAME_2,wb$ID_2)
KPI<-1:20
x<-cbind(x,KPI)
y<-vector()
edit<-NULL
ui <-fluidPage(
#use shinyjs
#plotOutput("shpPlot"),
leafletOutput("Mapp"),
selectizeInput("indistrict1","select",choices=wb$NAME_2, multiple=TRUE,
options = list(maxItems = 3)),
tabsetPanel(id = "inTabset",
tabPanel("tab_1",actionButton("Button1", "Go")),
tabPanel("tab_2",actionButton("Button2", "Back" ), tableOutput("q"))
)
#tableOutput("my_table")
)
server <- function(input, output, session){
output$Mapp<-renderLeaflet({
leaflet() %>% addPolygons(data = wb, highlightOptions =
highlightOptions(color = "red", weight = 3, bringToFront = TRUE), layerId =
wb$ID_2)
})
#Go Button Work
observeEvent(input$Button1,{
updateTabsetPanel(session, "inTabset", selected = "tab_2")
})
observeEvent(input$Button2,{
updateTabsetPanel(session, "inTabset", selected = "tab_1")
})
#click event work
data<- reactiveValues(clicked = NULL)
z<-reactive({
df <- data.frame(x)
colnames(df) <- c("district","id","kpi")
df
})
observeEvent(input$Mapp_shape_click,{
data$clicked <- input$Mapp_shape_click
y<-subset(z(), id == data$clicked$id)
edit<-unique(as.character(y$district))
updateSelectizeInput(session, "indistrict1",
label = "select",
choices = c(unique(as.character(y$district)),"All
Districts"),
selected = edit,
options = list(maxItems = 3),
server = TRUE
)
})
observeEvent(input$indistrict1,{
output$q<-renderTable({
subset(z(), district %in% input$indistrict1)
})
})
}
shinyApp(ui = ui,server = server)
observeEvent(input$Mapp_shape_click,{..
?我认为你可以删除其中一个。只是为了清晰起见,你想让 leaflet 地图存储所点击的 shapefile,这样你就可以点击多个形状,然后选定的应该显示在表格中,对吗? - SeGa