Skip to content

Instantly share code, notes, and snippets.

@maurolepore
Created March 12, 2018 18:50
Show Gist options
  • Save maurolepore/a8fb18caefd10f397b4eb7c0df5d357f to your computer and use it in GitHub Desktop.
Save maurolepore/a8fb18caefd10f397b4eb7c0df5d357f to your computer and use it in GitHub Desktop.
Help to create a list for the argument `col_types` of [readr::read_csv()].
#' Help to create a list for the argument `col_types` of [readr::read_csv()].
#' @noRd
cat_col_types <- function(x) {
  types <- purrr::map(x, class) %>%
    tibble::enframe() %>%
    tidyr::unnest() %>%
    dplyr::mutate(
      type = dplyr::case_when(
        value == "character" ~ "c",
        value == "integer" ~ "i",
        value == "numeric" ~ "d"
      ),
      type = paste0(name, " = '", type, "',")
    ) %>%
    dplyr::pull(type)
  
  cat(types)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment