qfes / rdeck

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

Gracefully handle NA values #118

Open JosiahParry opened 3 months ago

JosiahParry commented 3 months ago

Repro:

Using a column with NA values the error message is provided:

Error in `add_polygon_layer()`:
! Failed to create layer
Caused by error in `validate_get_elevation.default()`:
! Assertion failed: is.numeric(accessor_data) && all_finite(accessor_data)
✖ Column `frequency` is invalid for accessor `get_elevation`; it must be a <numeric> vector

The main part of the error message says "it must be a vector". I only just now noticed it said "all_finite(accessor_data)". The error message should indicate the true cause of the error. In this case the presence of NA values.

I've attached a shapefile you can use to repro the issue.

library(sf)
library(rdeck)
deck_df <- read_sf("~/downloads/deck-df.shp")

rdeck(
  initial_view_state = view_state(
    center = {
      sf::st_bbox(deck_df) |>
        sf::st_as_sfc() |>
        sf::st_centroid()
    }[[1]],
    zoom = 10,
    bearing = 45,
    pitch = 40.5
  )
) |> 
  add_polygon_layer(
    data = deck_df,
    id = "1234",
    coverage = 1,
    auto_highlight = TRUE,
    extruded = TRUE,
    get_polygon = geometry,
    get_elevation = n
  )

rdeck(
  initial_view_state = view_state(
    center = {
      sf::st_bbox(deck_df) |>
        sf::st_as_sfc() |>
        sf::st_centroid()
    }[[1]],
    zoom = 10,
    bearing = 45,
    pitch = 40.5
  )
) |> 
  add_polygon_layer(
    data = deck_df,
    id = "1234",
    coverage = 1,
    auto_highlight = TRUE,
    extruded = TRUE,
    get_polygon = geometry,
    get_elevation = frequency
  )

deck-df.zip

anthonynorth commented 3 months ago

This looks overly strict. NaN, NA, Inf should all render as 0 elevation.

As a temporary workaround, use get_elevation = scale_identity(frequency), or remove the validation rule entirely (hack!) validate_get_elevation.Layer <- function(...) NULL