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 NULLs as NAs, 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
NAs where NULLs 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.
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:
We can achieve the desired effect by mapping twice and using the
null-replaceoperatorIdeally,
map_*andflatten_*would treatNULLs asNAs, possibly by default or if not then by an argument. It could throw a warning when this occurs.The options I see are:
NULLentriesNAs whereNULLs were encounteredCurrently, 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
unlistdoes). Hence the suggestion for option 3.