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

fwf_positions() should enforce col_names to be a character #797

Closed
richierocks opened this Issue Feb 16, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@richierocks

richierocks commented Feb 16, 2018

If the col_names argument to fwf_positions() is a factor, then the return value of that function has a column named col_names that is also a factor, which causes read_fwf() to throw the error

Error: `col_names` must be TRUE, FALSE or a character vector

Reproducible example

# 1. Create a tibble with col_names as a factor
library(readr)
start_posns <- c(1, 2, 4, 8)
end_posns <- c(1, 3, 7, 15)
col_names <- factor(letters[1:4])
fwf_col_info <- fwf_positions(start_posns, end_posns, col_names)

glimpse(fwf_col_info)
## Observations: 4
## Variables: 3
## $ begin     <dbl> 0, 1, 3, 7
## $ end       <dbl> 1, 3, 7, 15
## $ col_names <fct> a, b, c, d

# 2. Write some data to file
tfile <- tempfile()
writeLines(
  c("1AA1111aaaaaaaa", "2BB2222bbbbbbbb", "3CC3333cccccccc", "4DD4444dddddddd"),
  tfile
)

# 3. Try to import it
read_fwf(tfile, fwf_col_info)
## Error: `col_names` must be TRUE, FALSE or a character vector

Thoughts on fixes

Either fwf_positions() should convert factor col_names to character, or read_fwf() should allow factors and do the conversion itself, or both.

@jimhester

This comment has been minimized.

Member

jimhester commented Feb 16, 2018

This looks like a legitimate bug, but unfortunately the reproducible example is not actually reproducible.

The best way to ensure it is reproducible is to use the reprex package from GitHub devtools::install_github("tidyverse/reprex").

@richierocks

This comment has been minimized.

richierocks commented Feb 19, 2018

@jimhester Whoops! Good spot. I've updated the comment to add the missing line.

@jimhester jimhester added the bug label May 4, 2018

@jimhester jimhester closed this in 80a6d8f Nov 14, 2018

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