How do I remove a selection made using Shiny and mapedit by clicking again on the feature?

独自空忆成欢 提交于 2021-02-10 06:57:43

问题


Using Shiny, leaflet, and mapedit packages I can generate a graph with multiple series using the code below.

Intuitively I would like to click on a selected map icon a second time and the associated data is removed from the graph. Essentially the click event can be toggled on or off.

Does anyone have any suggestions?

# devtools::install_github("r-spatial/sf")
# devtools::install_github("r-spatial/mapview@develop")
# devtools::install_github("bhaskarvk/leaflet.extras")
# devtools::install_github("r-spatial/mapedit")
library(tidyverse)
library(sf)
library(leaflet)
library(mapedit)
library(mapview)
library(shiny)
library(shinyjs)

locnCoord <-
  data.frame(location = c('Sit1','Site2','Site3'),
             lat=c(-18.1, -18.3, -18.4),
             lon=c(145.8, 145.9, 145.9)) %>%
  mutate(depth = runif(3))

locnSF <- st_as_sf(locnCoord, coords = c('lon','lat'), crs="+proj=longlat +datum=WGS84 +no_defs")

#### User input

ui <- fluidPage(
  shinyjs::useShinyjs(),
  shinyjs::extendShinyjs(text = "shinyjs.refresh = function() { location.reload(); }"),
  fluidRow(
    # edit module ui
    column(6,
           selectModUI("selectmap"),
           actionButton("refresh", "Refresh Map")
           ),
    column(6,
           h3("Point of Depth"),
           plotOutput("selectstat")
           )
    )
  )

#### Server

server <- function(input, output, session) {

  observeEvent(input$refresh, {
    shinyjs::js$refresh()
  })

  g_sel <- callModule(
    selectMod,
    "selectmap",
    leaflet() %>%
      addTiles() %>%
      addFeatures(
        data = locnSF,
        layerId = ~location,
        stroke = TRUE,
        color = 'orange',
        fill = TRUE,
        fillColor = 'black',
        radius=10)
  )

  rv <- reactiveValues(selected=NULL)

  observe({
    gs <- g_sel()

    if(length(gs$id) > 0) {
      rv$selected <- locnSF %>% filter(location %in% gs$id)
    } else {
      rv$selected <- NULL
    }
  })

  output$selectstat <- renderPlot({
    ggplot()
    if(!is.null(rv$selected) && nrow(rv$selected) > 0) {
      ggplot(data=rv$selected, aes(location, depth))+
        geom_point(color='red', size=5)
    } else {
      ggplot()
    }
  })
}
shinyApp(ui, server)

回答1:


This works. In your server try this:

observe({
   gs <- g_sel()

   if(length(gs$id) > 0) {
     site_select <- c(gs$selected)
     rv$selected <- locnSF %>% filter(location %in% gs$id) %>% 
       mutate(keeps = site_select) %>% filter(keeps == "TRUE")
   } else {
     rv$selected <- NULL
   }
})

This is edited from your code above. What I did was add a new column called keeps via mutate that holds a logical term for if the site was selected, and then filtered only theTRUE (i.e. currently selected) observations. When you deselect a site, the term in keeps turns to FALSE, thus is omitted from rv$selected.

Hope this helps.



来源:https://stackoverflow.com/questions/50847985/how-do-i-remove-a-selection-made-using-shiny-and-mapedit-by-clicking-again-on-th

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