cstawitz / roomba

General purpose API response tidier
https://cstawitz.github.io/roomba/index.html
Other
138 stars 11 forks source link

When keep = any, keep rows that are completely null and assign them NA in all cols #66

Open aedobbyn opened 6 years ago

aedobbyn commented 6 years ago

Maybe this requires another argument like keep_all_rows.

In the example below, I think we should allow the user some way to end up with 4 rows instead of 3. The third row corresponding to baz should be all NAs.

It would be useful in cases I can imagine for the user to know that they got 4 distinct outputs back and that one of them was fully null.

library(magrittr)

some_json <- '
{
  "foo": [{
    "a": 1,
    "b": 2,
    "c": 3
  }],
  "bar":[{
    "a": 4,
    "b": null,
    "c": 6
  }],
  "baz": [],
  "bing":[{
    "c": 12
  }]
}
'

lst <- some_json %>% jsonlite::fromJSON()

lst %>% roomba::roomba(cols = c("a", "b", "c"), keep = any)
#>    a b c
#> 1  1 2 3
#> 2  4 NA 6
#> 3  NA NA 12

So what I think this should look like is:

#>    a  b  c
#> 1  1  2  3
#> 2  4  NA 6
#> 3  NA NA NA
#> 4  NA NA 12

Mostly useful when the user knows they're passing x number inputs to an API and expecting x outputs, even if some of those outputs are empty.