Skip to content

as_tibble_row() doesn't work with date vectors #797

@FrancoisR95

Description

@FrancoisR95

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() or tibble() are doing.

1. Example of the issue with as_tibble_row:

      x <- c(col1 = as.Date("2012-04-04"), col2 = as.Date("2012-05-04"))
      as_tibble_row(x)

This returns the following error:

Error: `x` must be a bare vector in `as_tibble_row()`, not Date.

2. On the contrary, the same code is working fine with as_tibble_col:

      x <- c(col1 = as.Date("2012-04-04"), col2 = as.Date("2012-05-04"))
      as_tibble_col(x)

Result:

# A tibble: 2 x 1
  value     
  <date>    
1 2012-04-04
2 2012-05-04

The same is also obtained with tibble(x) instead of as_tibble_col(x).

3. Also a code for creating a 2-column tibble of dates is handled perfectly by tibble():

tibble(col1 = as.Date("2012-02-12"), col2 = as.Date(as.Date("2012-04-01"):as.Date("2012-04-05"), origin="1970-01-01"))
tibble(x)

Result:

# A tibble: 5 x 2
  col1       col2      
  <date>     <date>    
1 2012-02-12 2012-04-01
2 2012-02-12 2012-04-02
3 2012-02-12 2012-04-03
4 2012-02-12 2012-04-04
5 2012-02-12 2012-04-05

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 the as_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, use list() 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 in as_tibble_row(), not Date" for something which should work).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions