Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to remove GeoRaster when using shiny/leafletproxy? #56

Open
jzadra opened this issue Apr 1, 2022 · 1 comment
Open

How to remove GeoRaster when using shiny/leafletproxy? #56

jzadra opened this issue Apr 1, 2022 · 1 comment

Comments

@jzadra
Copy link

jzadra commented Apr 1, 2022

As stated in the title, I need to be able to remove a GeoRaster and replace it with a different one in a shiny app. Is there a way to do this?

@trafficonese
Copy link
Contributor

The leaflet methods hideGroup/showGroup/clearGroup should work.
Here is an example:

library(shiny)  
library(leaflet)
library(leafem)

tif = system.file("tif/L7_ETMs.tif", package = "stars")
x1 = read_stars(tif)
x1_b3 = x1[, , , 3] # band 3

ui <- fluidPage(
  actionButton("hide", "Hide Group"),
  actionButton("show", "Show Group"),
  actionButton("clea", "Remove Group"),
  actionButton("add1", "Add new Group"),
  leafletOutput("map")
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
      leaflet() %>%
        addTiles() %>%
        leafem:::addGeoRaster(
          x1_b3
          , opacity = 1
          , group = "initial"
          , colorOptions = colorOptions(
            palette = grey.colors(256)
          )
        )
  })
  observeEvent(input$hide, {
    leafletProxy("map") %>% 
      hideGroup("initial")
  })
  observeEvent(input$show, {
    leafletProxy("map") %>% 
      showGroup("initial")
  })
  observeEvent(input$clea, {
    leafletProxy("map") %>% 
      leaflet::clearGroup("initial")
  })
  observeEvent(input$add1, {
    x1_b2 = x1[1, , , 1] # band 2
    leafletProxy("map") %>% 
      leaflet::clearGroup("initial") %>% 
      leafem:::addGeoRaster(
        x1_b2
        , opacity = 1
        , group = "added"
        , colorOptions = colorOptions(
          palette = grey.colors(256)
        )
      )
  })
}
shinyApp(ui, server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants