I am trying to convert a Tibble to a parameter list for a function call. The reason I am doing this is because I want to create a simple file specification Tibble for reading in multiple fixed width files with varying columns. This way I only need to specify what columns are in a file using pull and select and then I can automatically have the file loaded and parsed. However, I am running into problems using the cols object to specify column formats.
For this example lets assume I have a Tibble of the format:
> (filespec <- tibble(ID = c("Title", "Date", "ATTR"), Length = c(23, 8, 6), Type = c("col_character()", "col_date()", "col_factor(levels=c(123456,654321)")))
# A tibble: 3 x 3
ID Length Type
<chr> <dbl> <chr>
1 Title 23 col_character()
2 Date 8 col_date()
3 ATTR 6 col_factor(levels=c(123456,654321)
I want to end up with a cols object of the format:
> (cols(Title = col_character(), Date = col_date(), ATTR=col_factor(levels=c(123456,654321))))
cols(
Title = col_character(),
Date = col_date(format = ""),
ATTR = col_factor(levels = c(123456, 654321), ordered = FALSE)
)
From other questions I have read I know this can be done with do.call. But I can not figure out how to convert the columns ID and Type to a cols object in an automated manner. Here is an example of what I tried...
> do.call(cols, select(filespec,ID, Type))
Error in switch(x, `_` = , `-` = col_skip(), `?` = col_guess(), c = col_character(), :
EXPR must be a length 1 vector
I am assuming the select needs to be wrapped with another function that performs the row to parameter mapping, how is this done?