如何在Shiny R中通过外部点击多次更新selectize输入

4
我一直在尝试从我的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
是的,那是一个错误上传的文件,但是确实是我们想要的。我们希望leaflet地图能够存储所点击形状的ID,并在表格中返回所选形状的信息。 - Bijurika Baidya
1个回答

0

这只是一个开始,但可能对您有所帮助。

目前它只监听leaflet地图中的点击事件,但已存储了所点击的形状ID,因此您可以单击多个形状文件,这些文件将在地图中突出显示并显示在表格中。 但是,删除已点击的形状文件尚未实现(通过再次单击形状文件)。目前还无法通过selectInput选择形状文件,selectInput仅会更新所点击的形状文件。

library(shiny)
library(leaflet)
library(raster)
library(DT)

India <- getData("GADM", country = "India", level = 2)
wb <- subset(India, NAME_1 == "West Bengal")

ui <- {fluidPage(
  uiOutput("raumselect"),
  leafletOutput("map"),
  DT::dataTableOutput("mytable")
)}


server <- shinyServer(function(input, output, session) {
  indiashape <- reactiveValues(geom = wb)
  click_list <- reactiveValues(ids = vector())
  selectedLines <- reactiveValues(geom = NULL)

  output$raumselect <- renderUI({
    click <- input$map_shape_click
    sel_lines_name <- as.character(indiashape$geom[as.character(
      indiashape$geom$OBJECTID) %in% click_list$ids, ]$NAME_2)

    selectInput("nameslanes", choices = wb$NAME_2,
                selected = sel_lines_name,
                label = "Select a Line Segment:", multiple = T)
  })

  output$map <- renderLeaflet({
    mapl <- leaflet(options = leafletOptions(doubleClickZoom= FALSE)) %>% 
      addTiles() %>% 
      addPolylines(data = indiashape$geom,
                   layerId = as.character(indiashape$geom$OBJECTID),
                   group = indiashape$geom$TYPE_2,
                   color = "blue",
                   highlightOptions = highlightOptions(color="red", weight = 10, bringToFront = T, opacity = 1))        
    mapl
  })

  observeEvent(input$map_shape_click, {
    click <- input$map_shape_click
    proxy <- leafletProxy("map")

    click_list$ids <- c(click_list$ids, click$id)

    sel_lines <- indiashape$geom[as.character(indiashape$geom$OBJECTID) %in% click_list$ids, ]

    proxy %>% addPolylines(data = sel_lines, 
                           layerId = as.character(sel_lines@data$osm_id), 
                           color="red", weight=5,opacity=1)
    selectedLines$geom <- sel_lines
  })

  output$mytable <- DT::renderDataTable({
    req(selectedLines$geom)

    if (!is.null(selectedLines$geom)) {
      sel_lines <- selectedLines$geom
      datafr <- as.data.frame(cbind(
        as.numeric(sel_lines$OBJECTID),
        as.character(sel_lines$NAME_2)
      ))
    }
    colnames(datafr) <- c("OBJECTID", "Name")
    datatable(data = datafr, rownames = T)
  })
})

shinyApp(ui, server)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接