diff --git a/NEWS.md b/NEWS.md index b1789928..a6c72acb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ * Jenny Bryan is now the maintainer. +* `read_table()` and edition 1 parsers gain support for `show_col_types()` (#1331) * Fix buffer overflow when trying to parse an integer from a field that is over 64 characters long (#1326) # readr 2.1.0 diff --git a/R/read_delim.R b/R/read_delim.R index 014030b2..df0c82cf 100644 --- a/R/read_delim.R +++ b/R/read_delim.R @@ -169,7 +169,8 @@ read_delim <- function(file, delim = NULL, quote = '"', return(read_delimited(file, tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, - comment = comment, n_max = n_max, guess_max = guess_max, progress = progress + comment = comment, n_max = n_max, guess_max = guess_max, progress = progress, + show_col_types = show_col_types )) } if (!missing(quoted_na)) { @@ -230,7 +231,8 @@ read_csv <- function(file, read_delimited(file, tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, - comment = comment, n_max = n_max, guess_max = guess_max, progress = progress + comment = comment, n_max = n_max, guess_max = guess_max, progress = progress, + show_col_types = show_col_types ) ) } @@ -300,7 +302,8 @@ read_csv2 <- function(file, return(read_delimited(file, tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, - comment = comment, n_max = n_max, guess_max = guess_max, progress = progress + comment = comment, n_max = n_max, guess_max = guess_max, progress = progress, + show_col_types = show_col_types )) } vroom::vroom(file, @@ -349,7 +352,8 @@ read_tsv <- function(file, col_names = TRUE, col_types = NULL, return(read_delimited(file, tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, - comment = comment, n_max = n_max, guess_max = guess_max, progress = progress + comment = comment, n_max = n_max, guess_max = guess_max, progress = progress, + show_col_types = show_col_types )) } @@ -386,9 +390,17 @@ read_tokens <- function(data, tokenizer, col_specs, col_names, locale_, n_max, p read_tokens_(data, tokenizer, col_specs, col_names, locale_, n_max, progress) } +should_show_col_types <- function(has_col_types, show_col_types) { + if (is.null(show_col_types)) { + return(isTRUE(!has_col_types)) + } + isTRUE(show_col_types) +} + read_delimited <- function(file, tokenizer, col_names = TRUE, col_types = NULL, locale = default_locale(), skip = 0, skip_empty_rows = TRUE, skip_quote = TRUE, - comment = "", n_max = Inf, guess_max = min(1000, n_max), progress = show_progress()) { + comment = "", n_max = Inf, guess_max = min(1000, n_max), progress = show_progress(), + show_col_types = should_show_col_types()) { name <- source_name(file) # If connection needed, read once. file <- standardise_path(file) @@ -420,7 +432,9 @@ read_delimited <- function(file, tokenizer, col_names = TRUE, col_types = NULL, ds <- datasource(data, skip = spec$skip, skip_empty_rows = skip_empty_rows, comment = comment, skip_quote = skip_quote) - if (is.null(col_types) && !inherits(ds, "source_string") && !is_testing()) { + has_col_types <- !is.null(col_types) + + if (((is.null(show_col_types) && !has_col_types) || isTRUE(show_col_types)) && !inherits(ds, "source_string") && !is_testing()) { show_cols_spec(spec) } diff --git a/R/read_delim_chunked.R b/R/read_delim_chunked.R index 63b2ce59..eed19895 100644 --- a/R/read_delim_chunked.R +++ b/R/read_delim_chunked.R @@ -68,6 +68,7 @@ read_delim_chunked <- function(file, callback, delim = NULL, chunk_size = 10000, comment = "", trim_ws = FALSE, skip = 0, guess_max = chunk_size, progress = show_progress(), + show_col_types = should_show_types(), skip_empty_rows = TRUE) { tokenizer <- tokenizer_delim(delim, quote = quote, @@ -79,7 +80,8 @@ read_delim_chunked <- function(file, callback, delim = NULL, chunk_size = 10000, callback = callback, chunk_size = chunk_size, tokenizer = tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, comment = comment, guess_max = guess_max, - progress = progress + progress = progress, + show_col_types = show_col_types ) } @@ -89,7 +91,9 @@ read_csv_chunked <- function(file, callback, chunk_size = 10000, col_names = TRU locale = default_locale(), na = c("", "NA"), quoted_na = TRUE, quote = "\"", comment = "", trim_ws = TRUE, skip = 0, guess_max = chunk_size, - progress = show_progress(), skip_empty_rows = TRUE) { + progress = show_progress(), + show_col_types = should_show_types(), + skip_empty_rows = TRUE) { tokenizer <- tokenizer_csv( na = na, quoted_na = quoted_na, quote = quote, comment = comment, trim_ws = trim_ws, skip_empty_rows = skip_empty_rows @@ -98,7 +102,8 @@ read_csv_chunked <- function(file, callback, chunk_size = 10000, col_names = TRU callback = callback, chunk_size = chunk_size, tokenizer = tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, comment = comment, - guess_max = guess_max, progress = progress + guess_max = guess_max, progress = progress, + show_col_types = show_col_types ) } @@ -108,7 +113,9 @@ read_csv2_chunked <- function(file, callback, chunk_size = 10000, col_names = TR locale = default_locale(), na = c("", "NA"), quoted_na = TRUE, quote = "\"", comment = "", trim_ws = TRUE, skip = 0, guess_max = chunk_size, - progress = show_progress(), skip_empty_rows = TRUE) { + progress = show_progress(), + show_col_types = should_show_types(), + skip_empty_rows = TRUE) { tokenizer <- tokenizer_delim( delim = ";", na = na, quoted_na = quoted_na, quote = quote, comment = comment, trim_ws = trim_ws, @@ -118,7 +125,8 @@ read_csv2_chunked <- function(file, callback, chunk_size = 10000, col_names = TR callback = callback, chunk_size = chunk_size, tokenizer = tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, comment = comment, - guess_max = guess_max, progress = progress + guess_max = guess_max, progress = progress, + show_col_types = show_col_types ) } @@ -128,7 +136,9 @@ read_tsv_chunked <- function(file, callback, chunk_size = 10000, col_names = TRU locale = default_locale(), na = c("", "NA"), quoted_na = TRUE, quote = "\"", comment = "", trim_ws = TRUE, skip = 0, guess_max = chunk_size, - progress = show_progress(), skip_empty_rows = TRUE) { + progress = show_progress(), + show_col_types = should_show_types(), + skip_empty_rows = TRUE) { tokenizer <- tokenizer_tsv( na = na, quoted_na = quoted_na, quote = quote, comment = comment, trim_ws = trim_ws, skip_empty_rows = skip_empty_rows @@ -137,6 +147,7 @@ read_tsv_chunked <- function(file, callback, chunk_size = 10000, col_names = TRU callback = callback, chunk_size = chunk_size, tokenizer = tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, comment = comment, - guess_max = guess_max, progress = progress + guess_max = guess_max, progress = progress, + show_col_types = show_col_types ) } diff --git a/R/read_log.R b/R/read_log.R index 691f5506..c75364e8 100644 --- a/R/read_log.R +++ b/R/read_log.R @@ -10,10 +10,13 @@ #' read_log(readr_example("example.log")) read_log <- function(file, col_names = FALSE, col_types = NULL, trim_ws = TRUE, - skip = 0, n_max = Inf, progress = show_progress()) { + skip = 0, n_max = Inf, + show_col_types = should_show_types(), + progress = show_progress()) { tokenizer <- tokenizer_log(trim_ws = trim_ws) read_delimited(file, tokenizer, col_names = col_names, col_types = col_types, - skip = skip, n_max = n_max, progress = progress + skip = skip, n_max = n_max, progress = progress, + show_col_types = show_col_types ) } diff --git a/R/read_table.R b/R/read_table.R index ca546b55..078417cd 100644 --- a/R/read_table.R +++ b/R/read_table.R @@ -34,6 +34,7 @@ read_table <- function(file, col_names = TRUE, col_types = NULL, locale = default_locale(), na = "NA", skip = 0, n_max = Inf, guess_max = min(n_max, 1000), progress = show_progress(), comment = "", + show_col_types = should_show_types(), skip_empty_rows = TRUE) { tokenizer <- tokenizer_ws( na = na, comment = comment, @@ -42,7 +43,8 @@ read_table <- function(file, col_names = TRUE, col_types = NULL, read_delimited(file, tokenizer, col_names = col_names, col_types = col_types, locale = locale, skip = skip, skip_empty_rows = skip_empty_rows, - skip_quote = FALSE, comment = comment, n_max = n_max, guess_max = guess_max, progress = progress + skip_quote = FALSE, comment = comment, n_max = n_max, guess_max = guess_max, progress = progress, + show_col_types = show_col_types ) }