mgirlich / tibblify

Rectangle Nested Lists
https://mgirlich.github.io/tibblify/
GNU General Public License v3.0
67 stars 2 forks source link

Error in `guess_tspec()` with implicitly missing values #171

Closed cphaarmeyer closed 1 year ago

cphaarmeyer commented 1 year ago

I have a JSON with a lot of implicitly missing values. tibblify() errors while guessing the spec. But actually guessing is possible with the internal function guess_tspec_object_list(), but is_object_list() returns FALSE. Looks to me like a common enough case to "just work".

library(tibblify)

lst <- list(list(a = 1), list(b = 2), list(c = 3))

tibblify(lst)
#> Error in `guess_tspec_list()`:
#> ! Cannot guess spec.
#> ✔ The object is a list.
#> ✖ It doesn't meet the criteria of `tibblify:::is_object_list()`.
#> ✖ It doesn't meet the criteria of `tibblify:::is_object()`.
#> ℹ Try to check the specs of the individual elements with `purrr::map(x,
#>   guess_spec)`.

#> Backtrace:
#>     ▆
#>  1. └─tibblify::tibblify(lst)
#>  2.   └─tibblify::guess_tspec(x, inform_unspecified = TRUE, call = current_call())
#>  3.     └─tibblify:::guess_tspec_list(...)
#>  4.       └─cli::cli_abort(...)
#>  5.         └─rlang::abort(...)

guess_tspec(lst)
#> Error in `guess_tspec_list()`:
#> ! Cannot guess spec.
#> ✔ The object is a list.
#> ✖ It doesn't meet the criteria of `tibblify:::is_object_list()`.
#> ✖ It doesn't meet the criteria of `tibblify:::is_object()`.
#> ℹ Try to check the specs of the individual elements with `purrr::map(x,
#>   guess_spec)`.

#> Backtrace:
#>     ▆
#>  1. └─tibblify::guess_tspec(lst)
#>  2.   └─tibblify:::guess_tspec_list(...)
#>  3.     └─cli::cli_abort(...)
#>  4.       └─rlang::abort(...)

spec <- tibblify:::guess_tspec_object_list(lst)

tibblify(lst, spec = spec)
#> # A tibble: 3 × 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1    NA    NA
#> 2    NA     2    NA
#> 3    NA    NA     3

Created on 2022-11-25 with reprex v2.0.2

mgirlich commented 1 year ago

I have a look into this.

mgirlich commented 1 year ago

Previously, the guesser tried to be a bit too smart and required fields to exist often enough in order for it to actually guess an object. This is difficult to get right and rather the user should be able to easily decide that later on. I have a better workflow in mind 😄 PR #174 is a first step towards an improved guessing API.

cphaarmeyer commented 1 year ago

Now it works. Thanks!