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

Unnest no longer handles lists with date vectors #407

Closed
thays42 opened this Issue Feb 8, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@thays42

thays42 commented Feb 8, 2018

Reproducible example:

library(dplyr)
library(tidyr)
library(lubridate)
df1 <- seq(from = as.Date('2017-01-01'), to = as.Date('2017-01-10'), by = "1 day") %>%
  list() %>%
  data_frame(x = .)
df2 <- seq(from = as.Date('2017-01-01'), to = as.Date('2017-01-10'), by = "1 day") %>%
  as.integer() %>%
  list() %>%
  data_frame(x = .)

Using the above, I am unable to unnest df1 but can get around it by coercing dates to integers and then back to dates.

> unnest(df1)
Error: Each column must either be a list of vectors or a list of data frames [x]
> unnest(df2) %>% mutate(x = as_date(x))
# A tibble: 10 x 1
   x         
   <date>    
 1 2017-01-01
 2 2017-01-02
 3 2017-01-03
 4 2017-01-04
 5 2017-01-05
 6 2017-01-06
 7 2017-01-07
 8 2017-01-08
 9 2017-01-09
10 2017-01-10

Below is my sessionInfo():

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux

Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2    lubridate_1.7.2 tidyr_0.8.0     dplyr_0.7.4    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15     utf8_1.1.3       crayon_1.3.4     assertthat_0.2.0 R6_2.2.2         magrittr_1.5    
 [7] pillar_1.1.0     cli_1.0.0        stringi_1.1.6    rlang_0.1.6      rstudioapi_0.7   tools_3.4.3     
[13] stringr_1.2.0    glue_1.2.0       purrr_0.2.4      yaml_2.1.16      compiler_3.4.3   pkgconfig_2.0.1 
[19] tidyselect_0.2.3 bindr_0.1        tibble_1.4.2
@cderv

This comment has been minimized.

cderv commented Feb 10, 2018

I think the change happened in commit f418817 to make unnest handles list of lists.

tidyr/R/unnest.R

Lines 144 to 155 in f418817

list_col_type <- function(x) {
is_data_frame <- map_lgl(x, is.data.frame)
is_atomic <- map_lgl(x, function(x) !is.object(x) && is_vector(x))
if (all(is_data_frame)) {
"dataframe"
} else if (all(is_atomic)) {
"atomic"
} else {
"mixed"
}
}

There is an is.object test activated because x is of class Date. Then, df1 is seen as type "mixted" that throws the error in unnest.

And it seems to mean than unnest cannot handles any typed element because if I understand well is.object will return true (so will get an error) for any object with a class. That is verified:

library(dplyr, warn.conflicts = F)
library(tidyr)
as.factor(month.name) %>%
  list() %>%
  data_frame(x = .) %>%
  unnest()
#> Error: Each column must either be a list of vectors or a list of data frames [x]

hms::hms(hours = 1:10) %>%
  list() %>%
  data_frame(x = .) %>%
  unnest()
#> Error: Each column must either be a list of vectors or a list of data frames [x]

Created on 2018-02-10 by the reprex package (v0.2.0).

This is an issue with more than dates vectors...
Not sure how to handle that yet. I just wanted to help identify the error.

@hadley

This comment has been minimized.

Member

hadley commented Feb 15, 2018

Minimal reprex:

library(tidyr)

df <- tibble::tibble(x = as.list(as.factor(letters[1:3])))
unnest(df)
#> Error: Each column must either be a list of vectors or a list of data frames [x]

@thays42 in the future, I'd appreciate it if you didn't include session info unless it's explicitly asked for, or you've used reprex::reprex(..., si = TRUE) to hide it away. Otherwise it clutters the discussion without adding significant value.

@hadley hadley added the bug label Feb 15, 2018

zeehio added a commit to zeehio/tidyr that referenced this issue Feb 16, 2018

Fix tidyverse#407. unnest works with S3 vectors.
The type coercion of S3 vectors relies on dplyr::combine.

dplyr does not offer a way to know if dplyr::combine will succeed based on the types of the vector given, so we use a tryCatch for now.
@hadley

This comment has been minimized.

Member

hadley commented Feb 19, 2018

Fixed in 96ded3f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment