Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Allow map_* to treat NULLs as NAs #231
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.
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
> list(list("a" = 1L), list("b" = 2L)) %>% map("a") %>% map_int(`%||%`, NA) #>  1 NA
The options I see are:
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
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