Navigation Menu

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

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

Closed
richierocks opened this issue Feb 16, 2018 · 3 comments
Closed

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

richierocks opened this issue Feb 16, 2018 · 3 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@richierocks
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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

@jimhester jimhester added the bug an unexpected problem or unintended behavior label May 4, 2018
@lock
Copy link

lock bot commented May 13, 2019

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 May 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants