Skip to content

cols() does not accept a named list #401

Closed

Description

@andrewheiss

Specifying column types with a named list inside cols() and cols_only() only works when the list is explicitly defined within cols() and cols_only(). Passing a named list created separately (e.g. loaded from a separate CSV of metadata) raises an error about collector objects.

mtcars_csv <- system.file("extdata", "mtcars.csv", package="readr")

# The named list of column types can be moved into a separate list, both with
# column objects or abbreviations:
col_types_list <- cols_only(
  mpg = col_double(),
  cyl = col_integer(),
  disp = col_double(),
  hp = col_integer()
)

col_types_list_abbr <- cols_only(
  mpg = "d",
  cyl = "i",
  disp = "d",
  hp = "i"
)

# And then it can be used in read_csv:
df1 <- read_csv(mtcars_csv, col_types=col_types_list)
df2 <- read_csv(mtcars_csv, col_types=col_types_list_abbr)

# However, passing a named list object to cols() or cols_only() will not work,
# raising an error about collector objects:
col_list <- list(mpg = col_double(),
                 cyl = col_integer(),
                 disp = col_double(),
                 hp = col_integer())

col_list_abbr <- list(mpg = "d",
                      cyl = "i",
                      disp = "d",
                      hp = "i")

df3 <- read_csv(mtcars_csv, col_types=cols_only(col_list))
#> Error: Some `col_types` are not S3 collector objects: 1
df4 <- read_csv(mtcars_csv, col_types=cols_only(col_list_abbr))
#> Error: Some `col_types` are not S3 collector objects: 1

Activity

  1. hadley commented on Jun 1, 2016

    @hadley
    Member

    This implies we need as_cols() and as_cols_only()

  2. self-assigned this
    on Jun 15, 2016
  3. antoine-lizee commented on Jun 21, 2016

    @antoine-lizee
    Contributor

    or something like a .dots argument to cols[_only]().

  4. self-assigned this
    on Jul 5, 2016
  5. andypohl commented on Nov 2, 2017

    @andypohl

    I know it's a closed issue, but I got this "Error: Some col_types are not S3 collector objects: 1" error in a similar way. Is as_cols_only() in the readr library?

  6. hadley commented on Nov 2, 2017

    @hadley
    Member

    If you have a question, you're better off asking on community.rstudio.com

  7. andypohl commented on Nov 2, 2017

    @andypohl

    Thanks, I was pretty sure this wasn't the right place to ask.

  8. lock commented on Sep 25, 2018

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  9. locked and limited conversation to collaborators on Sep 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

featurea feature request or enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions