SymbolixAU / mapdeck

R interface to Deck.gl and Mapbox
https://symbolixau.github.io/mapdeck/articles/mapdeck.html
362 stars 40 forks source link

visible prop #203

Open SymbolixAU opened 5 years ago

SymbolixAU commented 5 years ago

The visible prop is a cheap way to temporarily disable a layer

https://deck.gl/#/documentation/developer-guide/performance-optimization?section=minimize-data-changes

SymbolixAU commented 4 years ago

Still noticing this is slower, especially when setting to FALSE vs clearing the layer

library(shiny)
library(shinydashboard)
library(mapdeck)

ui <- dashboardPage(
    dashboardHeader()
    , dashboardSidebar(
        actionButton(inputId = "roads", label = "roads")
    )
    , dashboardBody(
        mapdeckOutput(outputId = "map")
    )
)

library(sfheaders)
df <- sfheaders::sf_to_df( roads )
df <- rbind( df, df, df, df)
nrow( df )
# 231028

Visible

server <- function(input, output) {

    ## initialise a map
    output$map <- renderMapdeck({
        mapdeck( location = c(144.9, -37), zoom = 5 )
    })

    ## use an observer to add and remove layers
    observeEvent({input$roads},{

        mapdeck_update(map_id = "map") %>%
            add_scatterplot(
                data = df
                , lon = "x"
                , lat = "y"
                , layer_id = "myRoads"
                , fill_colour = "RIGHT_LOC"
                , visible = ( input$roads %% 2 == 1 )
            )
    })
}

shinyApp(ui, server)

Clear

server <- function(input, output) {

    ## initialise a map
    output$map <- renderMapdeck({
        mapdeck( location = c(144.9, -37), zoom = 5 )
    })

    ## use an observer to add and remove layers
    observeEvent({input$roads},{

        if( input$roads %% 2 == 1 ) {
        mapdeck_update(map_id = "map") %>%
            add_scatterplot(
                data = df
                , lon = "x"
                , lat = "y"
                , layer_id = "myRoads"
                , fill_colour = "RIGHT_LOC"
            )
           } else {
                  mapdeck_update(map_id = "map") %>%
                      clear_scatterplot(layer_id = "myRoads")
    })
}

shinyApp(ui, server)