Skip to content

Commit

Permalink
Expose col_types from readr (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonysena authored Jun 6, 2024
1 parent 11c1b42 commit 6274b4c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
40 changes: 35 additions & 5 deletions R/CsvHelper.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,43 @@
#' @param file The .csv file to read.
#' @param warnOnCaseMismatch When TRUE, raise a warning if column headings
#' in the .csv are not in snake_case format
#'
#'
#' @param colTypes Corresponds to the `col_types` in the `readr::read_csv` function.
#' One of `NULL`, a [readr::cols()] specification, or
#' a string. See `vignette("readr")` for more details.
#'
#' If `NULL`, all column types will be inferred from `guess_max` rows of the
#' input, interspersed throughout the file. This is convenient (and fast),
#' but not robust. If the guessed types are wrong, you'll need to increase
#' `guess_max` or supply the correct types yourself.
#'
#' Column specifications created by [list()] or [cols()] must contain
#' one column specification for each column.
#'
#' Alternatively, you can use a compact string representation where each
#' character represents one column:
#' - c = character
#' - i = integer
#' - n = number
#' - d = double
#' - l = logical
#' - f = factor
#' - D = date
#' - T = date time
#' - t = time
#' - ? = guess
#' - _ or - = skip
#'
#' By default, reading a file without a column specification will print a
#' message showing what `readr` guessed they were. To remove this message,
#' set `show_col_types = FALSE` or set `options(readr.show_col_types = FALSE)`.
#'
#' @return
#' A tibble with the .csv contents
#'
#' @export
readCsv <- function(file, warnOnCaseMismatch = TRUE) {
fileContents <- .readCsv(file = file)
readCsv <- function(file, warnOnCaseMismatch = TRUE, colTypes = readr::cols()) {
fileContents <- .readCsv(file = file, colTypes = colTypes)
columnNames <- colnames(fileContents)
columnNamesInSnakeCaseFormat <- isSnakeCase(columnNames)
if (!all(columnNamesInSnakeCaseFormat) && warnOnCaseMismatch) {
Expand All @@ -58,10 +88,10 @@ readCsv <- function(file, warnOnCaseMismatch = TRUE) {
#'
#' @noRd
#' @keywords internal
.readCsv <- function(file) {
.readCsv <- function(file, colTypes = readr::cols()) {
invisible(readr::read_csv(
file = file,
col_types = readr::cols(),
col_types = colTypes,
lazy = FALSE
))
}
Expand Down
32 changes: 31 additions & 1 deletion man/readCsv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6274b4c

Please sign in to comment.