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

Capture map calls before map has been rendered #771

Merged
merged 6 commits into from
Feb 8, 2022
Merged

Conversation

schloerke
Copy link
Contributor

@schloerke schloerke commented Feb 3, 2022

Allow extra leaflet proxy methods while the map is hidden with outputOptions(output, "map", suspendWhenHidden = FALSE)

Fixes #659

Reprex:

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

ui <- navbarPage("leafletProxy demo",
                 id = "active_tab",
                #  selected = "Map",
   tabPanel("Data",
            actionButton("add_raster", "Upload raster layer"),
            leafletOutput("localMap")
            ),
    tabPanel("Map",
             leafletOutput("hiddenMap"))
   )


server <- function(input, output) {

    # updateNavbarPage(inputId = "active_tab", selected = "Data")

    RV <- reactiveValues(overlay = NULL)

    ## Large (many-celled) raster to be added via leafletProxy()
    observeEvent(input$add_raster, {
        r <- raster(matrix(1:14e4, 350, 400, byrow = TRUE),
                    xmn = -120, xmx = -110, ymn = 40, ymx = 50,
                    crs = CRS("+init=epsg:4326"))
        RV$overlay <- r
    })

    ## Base map
    output$localMap <- renderLeaflet({
        message("rendering local map")
        leaflet() %>%
            addTiles() %>%
            fitBounds(lng1 = -120, lng2 = -100, lat1 = 30, lat2 = 50)
    })
    output$hiddenMap <- renderLeaflet({
        message("rendering hidden map")
        leaflet() %>%
            addTiles() %>%
            fitBounds(lng1 = -120, lng2 = -100, lat1 = 30, lat2 = 50)
    })

    ## ## Uncomment this block to confirm that setting `suspendWhenHidden=FALSE`
    ## ## does not actually work as hoped.
    outputOptions(output, "hiddenMap", suspendWhenHidden = FALSE)

    ## Dynamically updated map element
    observeEvent(RV$overlay, {
        message("Adding raster layer")
        leafletProxy("localMap") %>%
            addRasterImage(RV$overlay, colors = "Spectral", opacity = 0.4)
        leafletProxy("hiddenMap") %>%
            addRasterImage(RV$overlay, colors = "Spectral", opacity = 0.4)
    })
}

shinyApp(ui, server)
  • Click "add raster"
  • Click "Map"
  • Rainbow raster should appear in Map panel

PR:
hidden-leaflet

rstudio/leaflet@main behavior: (No rainbow on Map panel)
hidden-leaflet-bad


PR task list:

  • Update NEWS
  • Add tests (where appropriate)
    • R code tests: tests/testthat/
    • Visual tests: R/zzz_viztest.R
  • Update documentation with devtools::document()

* main:
  Remove bad test script in package.json
  Use faster versions of system.file()/packageVersion()/is_installed() (#768)
  Patch leaflet.css with missing rule about <img> in pane (#770)
@schloerke schloerke marked this pull request as ready for review February 3, 2022 20:13
@schloerke
Copy link
Contributor Author

Failing tests are independent of this PR. Merging

@schloerke schloerke changed the title Capture map calls before map has been rendered. Capture map calls before map has been rendered Feb 8, 2022
@schloerke schloerke merged commit 48c4318 into main Feb 8, 2022
@schloerke schloerke deleted the pending_calls branch February 8, 2022 15:09
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

Successfully merging this pull request may close these issues.

Possible to trigger leaflet map rendering on non-selected tab?
2 participants