qfes / rdeck

Deck.gl widget for R
https://qfes.github.io/rdeck
MIT License
97 stars 0 forks source link

[json] Optionally keep data columns which aren't used by any layer props #121

Open anthonynorth opened 3 months ago

anthonynorth commented 3 months ago

Optionally keep unused columns when serialising layers.

Example:

add_scatterplot_layer(
  data = data.frame(foo = 1),
  # ensure "foo" is serialised
  .keep = "foo"
)

This allows for shiny applications to make accessor changes (e.g. changing the column used in get_elevation) without re-supplying any data.

library(shiny)
library(rdeck)

ui <- fillPage(
  rdeckOutput("map", height = "100%"),
  absolutePanel(
    top = 10, left = 10,
    selectInput("point_variable", "variable", c("foo", "bar"), "foo")
  )
)

point_data <- vctrs::data_frame(
  position = xy(runif(1e4, -180, 180), runif(1e4, -85, 85)),
  foo = runif(1e4),
  bar = rnorm(1e4)
)

map <- rdeck() |>
  add_scatterplot_layer(
    id = "point",
    data = point_data,
    get_radius = 5,
    radius_units = "pixels",
    get_fill_color = scale_color_linear(foo),
    # we will need these columns!
    .keep = c(foo, bar) 
  )

server <- function(input, output, session) {
  output$map <- renderRdeck(map)

  # when point_variable is updated, we update `get_fill_color`, using data already in the client
  observe({
    point_variable <- input$point_variable
    variable_range <- point_data[[point_variable]]
    rdeck_proxy("map") |>
      update_scatterplot_layer(
        id = "point",
        get_fill_color = scale_color_linear(!!point_variable, limits = variable_range)
      )
  })
}

shinyApp(ui, server)