Skip to content

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

Description

@richierocks

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions