Skip to content

Allow map_* to treat NULLs as NAs #231

@jeremystan

Description

@jeremystan

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:

  1. Throw an error
  2. Return a vector that excludes the NULL entries
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions