tidyr::unnest with ptype loss of precision

I'm really struggling to figure out how ptype/.ptype args are supposed to work in the tidyverse. I have a super-simple example that's raising a type conversion error, but I can't figure out why there's any type conversion happening at all :-/

Here's my data frame (tibble) with a nested tibble:

x <- tibble::tibble(
  foo = "foo",
  bar = list(
    tibble::tibble(bar1 = as.double(1:3), bar2 = as.double(3:1))
  )
)

And when I blindly unnest(), it does what I expect:

unnest(x, bar)

# # A tibble: 3 x 3
#   foo    bar1  bar2
#   <chr> <dbl> <dbl>
# 1 foo       1     3
# 2 foo       2     2
# 3 foo       3     1

But now let's say I want to be somewhat type-safe, so I'll provide the unnesting prototype:

unnest(x, bar, ptype = tibble::tibble(bar1 = double(0), bar2 = double(0)))

... which matches the types in x$bar exactly. Unfortunately, I get this error:

# Error: Can't convert from <data.frame<bar:tbl_df<
#   bar1: double
#   bar2: double
# >>> to <tbl_df<
#   bar1: double
#   bar2: double
# >> due to loss of precision.
# Dropped variables: `bar`
# Run `rlang::last_error()` to see where the error occurred.

This feels very cryptic, since converting double to double should not incur a loss of precision. (And even if it did, it'd be nice if there was a way to override/force that to be acceptable ... but I digress.)

What I really glean from the error is that I'm probably just not using ptype as intended, and there aren't a lot of good examples in the tidyverse docs on how best to use the various ptype arguments. Can anyone explain to me how my ptype argument above is breaking that unnest()?

(This is with recent versions of tidyr (v1.1.0) and vctrs (0.3.4), BTW.)

This looks like a bug. I'm pretty sure that the way you're supplying the prototype is correct. I'd suggest waiting for someone else to weigh in on this and then consider filing an issue on the tidyr repo.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Taking your suggestion (and not finding another solution in the interim), here's the github issue if you'd like to follow along:

2 Likes