-
Notifications
You must be signed in to change notification settings - Fork 283
Closed
Description
When working with sparse nested lists (like JSON), it is common to have missing keys or NULL values, which are difficult to coerce into a desired type with purrr.
For example:
list(list("a" = 1L), list("b" = 2L)) %>% map_int("a")
#> Error: Result 2 is not a length 1 atomic vector
We can achieve the desired effect by mapping twice and using the null-replace
operator
> list(list("a" = 1L), list("b" = 2L)) %>% map("a") %>% map_int(`%||%`, NA)
#> [1] 1 NA
Ideally, map_*
and flatten_*
would treat NULL
s as NA
s, possibly by default or if not then by an argument. It could throw a warning when this occurs.
The options I see are:
- Throw an error
- Return a vector that excludes the
NULL
entries - Return a vector with appropriate
NA
s whereNULL
s were encountered
Currently, purrr implements option 1, which will lead to difficult to debug failures when working with JSON, and a lot of duplicated code to prevent it. I believe option 2 is very dangerous (this is what unlist
does). Hence the suggestion for option 3.
Metadata
Metadata
Assignees
Labels
No labels