qfes / rdeck

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

Permit non-tidyeval for value updating. #117

Open JosiahParry opened 3 months ago

JosiahParry commented 3 months ago

I'm finding tidyeval to be very limiting with use with rdeck. The current use case is this:

I have a set of polygons that I want to visualize with with rdeck. The elevation will be associated with a vector of values related to each geometry. I will be reactively updating the vector of values via a reactive expression in shiny.

However, due to the reliance on tidy-eval, the vector of values must be a column in the data frame that is provided. So instead of only updating a vector of numeric values I have to update a whole dataframe of detailed polygons which is a much heavier lift than the attributes associated with it.

anthonynorth commented 3 months ago

The limitation here is that update_x_layer() replaces the in-client layer data if supplied. #120

It would also be useful to keep additional columns in data that aren't used for any purpose when the layer is initially serialised. That would allow for fast switching between elevation columns without passing any data. #121

This is currently possible by abusing the tooltip

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),
    pickable = TRUE,
    # we need to keep the `bar` column
    tooltip = c(foo, bar) 
  )

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

  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)