readr icon indicating copy to clipboard operation
readr copied to clipboard

For col_types, using a named vector should error

Open ginolhac opened this issue 2 years ago • 0 comments

Dear Jenny,

I am using readr CRAN version 2.1.2 and playing around with col_types(). If we specify the columns and their types in the dedicated cols() function it works. The document of cols() says:

In general you can substitute list() for cols() without changing the behavior.

It works for using list() but using c() issued no warnings and the wrong output. The col_type specified is ignored.

I suggest col_types() should only accept cols() and cols_only() as arguments for robustness.

Best wishes

see reprex below:

library(readr)
# be default will be read as character
read_csv("A\n1\na", show_col_types = FALSE)
#> # A tibble: 2 × 1
#>   A    
#>   <chr>
#> 1 1    
#> 2 a

# forcing it to be double, got expected NA
read_csv("A\n1\na", show_col_types = FALSE,
         col_types = cols(A = col_double()))
#> Warning: One or more parsing issues, see `problems()` for details
#> # A tibble: 2 × 1
#>       A
#>   <dbl>
#> 1     1
#> 2    NA

# cols() doc https://readr.tidyverse.org/reference/cols.html
# says that we can 'In general substitute list() for cols()'
# works as expected
read_csv("A\n1\na", show_col_types = FALSE,
         col_types = list(A = col_double()))
#> Warning: One or more parsing issues, see `problems()` for details
#> # A tibble: 2 × 1
#>       A
#>   <dbl>
#> 1     1
#> 2    NA

# substitute list() for c() and we got no warning
# and the wrong column type back: a character
read_csv("A\n1\na", show_col_types = FALSE,
         col_types = c(A = col_double()))
#> # A tibble: 2 × 1
#>   A    
#>   <chr>
#> 1 1    
#> 2 a

Created on 2022-03-24 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.0 (2021-05-18)
#>  os       Ubuntu 21.10
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Zurich
#>  date     2022-03-24
#>  pandoc   2.17.1.1 @ /usr/lib/rstudio/bin/quarto/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package     * version date (UTC) lib source
#>  P cli           3.2.0   2022-02-14 [?] CRAN (R 4.1.0)
#>  P digest        0.6.29  2021-12-01 [?] CRAN (R 4.1.0)
#>  P evaluate      0.15    2022-02-18 [?] CRAN (R 4.1.0)
#>  P fastmap       1.1.0   2021-01-25 [?] CRAN (R 4.1.0)
#>  P fs            1.5.2   2021-12-08 [?] CRAN (R 4.1.0)
#>  P glue          1.6.2   2022-02-24 [?] CRAN (R 4.1.0)
#>  P highr         0.9     2021-04-16 [?] CRAN (R 4.1.0)
#>  P htmltools     0.5.2   2021-08-25 [?] CRAN (R 4.1.0)
#>  P knitr         1.37    2021-12-16 [?] CRAN (R 4.1.0)
#>  P magrittr      2.0.2   2022-01-26 [?] CRAN (R 4.1.0)
#>  P reprex        2.0.1   2021-08-05 [?] CRAN (R 4.1.0)
#>  P rlang         1.0.2   2022-03-04 [?] CRAN (R 4.1.0)
#>  P rmarkdown     2.13    2022-03-10 [?] CRAN (R 4.1.0)
#>  P readr    2.1.2    2022-01-30 [?] CRAN (R 4.1.0)
#>  P sessioninfo   1.2.2   2021-12-06 [?] CRAN (R 4.1.0)
#>  P vroom   1.5.7   2021-11-29 [?] CRAN (R 4.1.0)
#>  P withr         2.5.0   2022-03-03 [?] CRAN (R 4.1.0)
#>  P xfun          0.30    2022-03-02 [?] CRAN (R 4.1.0)
#>  P yaml          2.3.5   2022-02-21 [?] CRAN (R 4.1.0)
#> 
#>  [1] /home/ginolhac/Projects/basv53/renv/library/R-4.1/x86_64-pc-linux-gnu
#>  [2] /usr/lib/R/library
#> 
#>  P ── Loaded and on-disk path mismatch.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

ginolhac avatar Mar 24 '22 08:03 ginolhac