Hi @leobarlach! Welcome!
(updating my answer because sometimes you forget obvious things!
)
The the col_types parameter (to any of the read_delim() variants) is more flexible than calling cols() directly, and is probably what you're going to actually use in practice. It takes either a list (ideally named) of string shorthand references, or a single string, so you'll need to as.list() your vector or str_flatten() / paste0() the strings together:
library(tidyverse)
aa <- c('c','n')
spec_csv(
"chr1,num1
a,1
b,2",
col_types = as.list(aa)
)
#> cols(
#> chr1 = col_character(),
#> num1 = col_number()
#> )
spec_csv(
"chr1,num1
a,1
b,2",
col_types = str_flatten(aa)
)
#> cols(
#> chr1 = col_character(),
#> num1 = col_number()
#> )
spec_csv(
"chr1,num1
a,1
b,2",
col_types = paste0(aa, collapse = "")
)
#> cols(
#> chr1 = col_character(),
#> num1 = col_number()
#> )
Created on 2018-11-19 by the reprex package (v0.2.1)