Skip to content
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

cols() does not accept a named list #401

Closed
andrewheiss opened this issue May 10, 2016 · 6 comments
Closed

cols() does not accept a named list #401

andrewheiss opened this issue May 10, 2016 · 6 comments
Assignees
Labels
feature a feature request or enhancement

Comments

@andrewheiss
Copy link

andrewheiss commented May 10, 2016

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
@hadley
Copy link
Member

hadley commented Jun 1, 2016

This implies we need as_cols() and as_cols_only()

@hadley hadley added the ready label Jun 1, 2016
@hadley hadley added the feature a feature request or enhancement label Jun 2, 2016
@hadley hadley self-assigned this Jun 15, 2016
@antoine-lizee
Copy link
Contributor

antoine-lizee commented Jun 21, 2016

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

@jimhester jimhester self-assigned this Jul 5, 2016
jimhester added a commit to jimhester/readr that referenced this issue Jul 6, 2016
@jimhester jimhester removed the ready label Jul 6, 2016
@andypohl
Copy link

andypohl commented Nov 2, 2017

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?

@hadley
Copy link
Member

hadley commented Nov 2, 2017

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

@andypohl
Copy link

andypohl commented Nov 2, 2017

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

@lock
Copy link

lock bot commented Sep 25, 2018

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/

@lock lock bot locked and limited conversation to collaborators Sep 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants