-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
as_tibble_row() doesn't work with date vectors #797
Comments
To handle arbitrary vectors, library(vctrs)
library(rlang)
vec_chop2 <- function(x) {
stopifnot(
vec_is(x),
# Unimplemented:
!is.data.frame(x),
is_null(dim(x))
)
names <- names(x)
if (is_null(names)) {
vec_chop2_unnamed(x)
} else {
vec_chop2_named(x, names)
}
}
vec_chop2_unnamed <- function(x) {
n <- vec_size(x)
out <- vector("list", n)
for (i in seq_len(n)) {
out[[i]] <- x[[i]]
}
out
}
vec_chop2_named <- function(x, names) {
n <- vec_size(x)
out <- vector("list", n)
out_names <- character(n)
for (i in seq_len(n)) {
out[[i]] <- x[[i]]
out_names[[i]] <- names[[i]]
}
names(out) <- out_names
out
}
fct <- set_names(factor(c("a", "b")), c("foo", "bar"))
as.list(fct)
#> [[1]]
#> foo
#> a
#> Levels: a b
#>
#> [[2]]
#> bar
#> b
#> Levels: a b
vec_chop2(fct)
#> $foo
#> [1] a
#> Levels: a b
#>
#> $bar
#> [1] b
#> Levels: a b
date <- c(as.Date("2020-01-01"), as.Date("2020-01-02"))
date <- set_names(date, c("foo", "bar"))
vec_chop2(date)
#> $foo
#> [1] "2020-01-01"
#>
#> $bar
#> [1] "2020-01-02" as_tibble_row2 <- function(x) {
if (!vec_is_list(x)) {
x <- vec_chop2(x)
}
tibble::as_tibble_row(x)
}
as_tibble_row2(fct)
#> # A tibble: 1 x 2
#> foo bar
#> <fct> <fct>
#> 1 a b
as_tibble_row2(date)
#> # A tibble: 1 x 2
#> foo bar
#> <date> <date>
#> 1 2020-01-01 2020-01-02 |
We could do |
Many thanks @lionel- and @krlmlr for your quick replies and explanations. In your example of date vector, date <- c(as.Date("2020-01-01"), as.Date("2020-01-02"))
date <- set_names(date, c("foo", "bar"))
vec_chop2(date)
#> $foo
#> [1] "2020-01-01"
#>
#> $bar
#> [1] "2020-01-02"
as.list(date)
#> $foo
#> [1] "2020-01-01"
#>
#> $bar
#> [1] "2020-01-02" Why not, for the if (!is_bare_vector(x)) {
if (class(x) == "Date") {
if (!is_vector(x)) {
cnd_signal(error_as_tibble_row_date_vector(x))
}
} else {
cnd_signal(error_as_tibble_row_bare(x))
}
} instead of: if (!is_bare_vector(x)) {
cnd_signal(error_as_tibble_row_bare(x))
} |
@FrancoisR95 We're looking into a more general solution that would work with any vector.
Yes I think we could now expose |
The names of the vector become column names here? |
Yup so we need to remove the inner names and use them as outer names. Also data frames should be special-cased here so I'm not sure where vec-names would come into play. That's why I thought vec-chop2 as outlined above would be appropriate for non-list and non-df inputs. |
Needed for: - tidyverse/tibble#797 - tidyverse/tibble#738 - tidyverse/tibble#730 and perhaps more.
Needed for: - tidyverse/tibble#797 - tidyverse/tibble#738 - tidyverse/tibble#730 and perhaps more.
I don't follow. Yes, I can use inner names as outer names with If we can make |
We also need to remove the inner names otherwise they linger in the columns. Regarding data frames, I assumed |
I have a draft in 22bfa2a, not sure if I got it right. Currently, |
chop2 would help giving the same implementation to atomic vectors and lists. But you could also make S3 lists follow the same code path as for bare lists if you prefer? S3 lists should be unpacked the same way as bare lists right? |
|
- `as_tibble_row()` supports arbitrary vectors (#797).
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary. |
In tibble 3.0.1,
as_tibble_row(x)
returns an error when x is a vector of dates.The expected behavior would be to return a tibble row containing dates, exactly like
as_tibble_col()
ortibble()
are doing.1. Example of the issue with
as_tibble_row
:This returns the following error:
2. On the contrary, the same code is working fine with
as_tibble_col
:Result:
The same is also obtained with
tibble(x)
instead ofas_tibble_col(x)
.3. Also a code for creating a 2-column tibble of dates is handled perfectly by
tibble()
:Result:
Conclusion
The behavior of
as_tibble_row()
with date vectors seems to be a bug, or at least is very inconsistent and unexpected. It's also really a pity when the code of theas_tibble_row(x)
function (new_tibble(as.list(x), nrow = 1)
) would work just fine if there wasn't, in the function, an unfortunate test making this unnecessary error... This test was introduced recently (6f572f5#diff-cb6cd20025e0469d14d1ce6271cbf679) in order to try to fix #739 but the fix is much worse than the problem (which in my opinion was not even a real problem, basically it was said that an error message, which was "All elements must be size one, uselist()
to wrap.", was not clear enough but I think it was rather clear, and better and clearer at least than now getting this unexpected error "x
must be a bare vector inas_tibble_row()
, not Date" for something which should work).The text was updated successfully, but these errors were encountered: