Allow map_* to treat NULLs as NAs #231
Comments
This ground has been explored some before (#110 Default value to replace NULL), which lead to the Does this help? library(purrr)
list(list("a" = 1L), list("b" = 2L)) %>% map_int("a", .null = NA_integer_)
#> [1] 1 NA |
I totally missed that (and this issue) - my apologies. I missed it in part because it's not in the parameters section of the documentation. Should it be added there? It is mentioned under the |
OK, so on further inspection, the list(list("a" = 1L), list("a" = NULL)) %>% map_int("a", .null = NA)
#> Error: Result 2 is not a length 1 atomic vector Reading through #110, it seems like the discussion lost the need to treat |
Good point about explicit |
I also think actual NULLs should be replaced by |
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-replace
operatorIdeally,
map_*
andflatten_*
would treatNULL
s asNA
s, possibly by default or if not then by an argument. It could throw a warning when this occurs.The options I see are:
NULL
entriesNA
s whereNULL
s 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
unlist
does). Hence the suggestion for option 3.The text was updated successfully, but these errors were encountered: