Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,15 @@ export(value_counts)
export(vctrs_extension_array)
export(vctrs_extension_type)
export(write_csv_arrow)
export(write_csv_dataset)
export(write_dataset)
export(write_delim_dataset)
export(write_feather)
export(write_ipc_file)
export(write_ipc_stream)
export(write_parquet)
export(write_to_raw)
export(write_tsv_dataset)
importFrom(R6,R6Class)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.string)
Expand Down
40 changes: 32 additions & 8 deletions r/R/csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -510,32 +510,56 @@ CsvReadOptions$create <- function(use_threads = option_use_threads(),
options
}

readr_to_csv_write_options <- function(include_header = TRUE,
readr_to_csv_write_options <- function(col_names = TRUE,
batch_size = 1024L,
na = "") {
delim = ",",
na = "",
eol = "\n",
quote = c("needed", "all", "none")) {
quoting_style_arrow_opts <- c("Needed", "AllValid", "None")
quote <- match(match.arg(quote), c("needed", "all", "none"))
quote <- quoting_style_arrow_opts[quote]

CsvWriteOptions$create(
include_header = include_header,
include_header = col_names,
batch_size = batch_size,
null_string = na
delimiter = delim,
null_string = na,
eol = eol,
quoting_style = quote
)
}

#' @rdname CsvReadOptions
#' @export
CsvWriteOptions <- R6Class("CsvWriteOptions", inherit = ArrowObject)
CsvWriteOptions$create <- function(include_header = TRUE, batch_size = 1024L, null_string = "") {
assert_that(is_integerish(batch_size, n = 1, finite = TRUE), batch_size > 0)
CsvWriteOptions$create <- function(include_header = TRUE,
batch_size = 1024L,
null_string = "",
delimiter = ",",
eol = "\n",
quoting_style = c("Needed", "AllValid", "None")) {
quoting_style <- match.arg(quoting_style)
quoting_style_opts <- c("Needed", "AllValid", "None")
quoting_style <- match(quoting_style, quoting_style_opts) - 1L

assert_that(is.logical(include_header))
assert_that(is_integerish(batch_size, n = 1, finite = TRUE), batch_size > 0)
assert_that(is.character(delimiter))
assert_that(is.character(null_string))
assert_that(!is.na(null_string))
assert_that(length(null_string) == 1)
assert_that(!grepl('"', null_string), msg = "na argument must not contain quote characters.")
assert_that(is.character(eol))
Comment thread
dgreiss marked this conversation as resolved.
Resolved

csv___WriteOptions__initialize(
list(
include_header = include_header,
batch_size = as.integer(batch_size),
null_string = as.character(null_string)
delimiter = delimiter,
null_string = as.character(null_string),
eol = eol,
quoting_style = quoting_style
)
)
}
Expand Down Expand Up @@ -787,7 +811,7 @@ write_csv_arrow <- function(x,

if (is.null(write_options)) {
write_options <- readr_to_csv_write_options(
include_header = include_header,
col_names = include_header,
batch_size = batch_size,
na = na
)
Expand Down
19 changes: 13 additions & 6 deletions r/R/dataset-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ FileFormat <- R6Class("FileFormat",
)
FileFormat$create <- function(format, schema = NULL, ...) {
opt_names <- names(list(...))
if (format %in% c("csv", "text") || any(opt_names %in% c("delim", "delimiter"))) {
if (format %in% c("csv", "text", "txt") || any(opt_names %in% c("delim", "delimiter"))) {
CsvFileFormat$create(schema = schema, ...)
} else if (format == "tsv") {
# This delimiter argument is ignored.
Comment thread
dgreiss marked this conversation as resolved.
Resolved
CsvFileFormat$create(delimiter = "\t", schema = schema, ...)
} else if (format == "parquet") {
ParquetFileFormat$create(...)
Expand Down Expand Up @@ -635,7 +636,7 @@ FileWriteOptions <- R6Class("FileWriteOptions",
"codec",
"null_fallback"
)
} else if (format == "csv") {
} else if (format %in% c("csv", "tsv", "txt", "text")) {
supported_args <- c(
names(formals(CsvWriteOptions$create)),
names(formals(readr_to_csv_write_options))
Expand Down Expand Up @@ -691,14 +692,20 @@ FileWriteOptions <- R6Class("FileWriteOptions",
get_ipc_metadata_version(args$metadata_version)
)
}
} else if (self$type == "csv") {
} else if (self$type %in% c("csv", "tsv", "txt", "text")) {
arrow_opts <- names(formals(CsvWriteOptions$create))
readr_opts <- names(formals(readr_to_csv_write_options))
readr_only_opts <- setdiff(readr_opts, arrow_opts)
arrow_only_opts <- setdiff(arrow_opts, readr_opts)

is_arrow_opt <- !is.na(pmatch(names(args), arrow_opts))
is_readr_opt <- !is.na(pmatch(names(args), readr_opts))
is_readr_only_opt <- !is.na(pmatch(names(args), readr_only_opts))
is_arrow_opt <- !is.na(match(names(args), arrow_opts))
is_readr_opt <- !is.na(match(names(args), readr_opts))
is_arrow_only_opt <- !is.na(match(names(args), arrow_only_opts))
is_readr_only_opt <- !is.na(match(names(args), readr_only_opts))

if (any(is_arrow_only_opt) && any(is_readr_only_opt)) {
stop("Use either Arrow write options or readr write options, not both")
}

# These option names aren't mutually exclusive, so only use readr path
# if we have at least one readr-specific option.
Expand Down
199 changes: 193 additions & 6 deletions r/R/dataset-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
#' @export
write_dataset <- function(dataset,
path,
format = c("parquet", "feather", "arrow", "ipc", "csv"),
format = c("parquet", "feather", "arrow", "ipc", "csv", "tsv", "txt", "text"),
partitioning = dplyr::group_vars(dataset),
basename_template = paste0("part-{i}.", as.character(format)),
hive_style = TRUE,
Expand Down Expand Up @@ -178,12 +178,31 @@ write_dataset <- function(dataset,
}

path_and_fs <- get_path_and_filesystem(path)

dots <- list(...)
if (format %in% c("txt", "text") && !any(c("delimiter", "delim") %in% names(dots))) {
stop("A delimiter must be given for a txt format.")
}
if (format == "tsv" && any(c("delimiter", "delim") %in% names(dots))) {
stop("Can't set a delimiter for the tsv format.")
}

output_schema <- final_node$schema
options <- FileWriteOptions$create(
format,
column_names = names(output_schema),
...
)
# This is a workaround because CsvFileFormat$create defaults the delimiter to ","
if (format == "tsv") {
options <- FileWriteOptions$create(
format,
column_names = names(output_schema),
delimiter = "\t",
...
)
} else {
options <- FileWriteOptions$create(
format,
column_names = names(output_schema),
...
)
}

# TODO(ARROW-16200): expose FileSystemDatasetWriteOptions in R
# and encapsulate this logic better
Expand All @@ -209,6 +228,174 @@ write_dataset <- function(dataset,
)
}

#' Write a dataset into partitioned flat files.
#'
#' The `write_*_dataset()` are a family of wrappers around [write_dataset] to allow for easy switching
#' between functions for writing datasets.
#'
#' @inheritParams write_dataset
#' @param col_names Whether to write an initial header line with column names.
#' @param batch_size Maximum number of rows processed at a time. Default is 1024L.
#' @param delim Delimiter used to separate values. Defaults to `","` for `write_delim_dataset()` and
#' `write_csv_dataset()`, and `"\t` for `write_tsv_dataset()`. Cannot be changed for `write_tsv_dataset()`.
#' @param na a character vector of strings to interpret as missing values. Quotes are not allowed in this string.
#' The default is an empty string `""`.
#' @param eol the end of line character to use for ending rows. The default is `"\n"`.
#' @param quote How to handle fields which contain characters that need to be quoted.
#' - `needed` - Enclose all strings and binary values in quotes which need them, because their CSV rendering can
#' contain quotes itself (the default)
#' - `all` - Enclose all valid values in quotes. Nulls are not quoted. May cause readers to
#' interpret all values as strings if schema is inferred.
#' - `none` - Do not enclose any values in quotes. Prevents values from containing quotes ("),
#' cell delimiters (,) or line endings (\\r, \\n), (following RFC4180). If values
#' contain these characters, an error is caused when attempting to write.
#' @return The input `dataset`, invisibly.
#'
#' @seealso [write_dataset()]
#' @export
write_delim_dataset <- function(dataset,
path,
partitioning = dplyr::group_vars(dataset),
basename_template = "part-{i}.txt",
hive_style = TRUE,
existing_data_behavior = c("overwrite", "error", "delete_matching"),
max_partitions = 1024L,
max_open_files = 900L,
max_rows_per_file = 0L,
min_rows_per_group = 0L,
max_rows_per_group = bitwShiftL(1, 20),
col_names = TRUE,
batch_size = 1024L,
delim = ",",
na = "",
eol = "\n",
quote = c("needed", "all", "none")) {
if (!missing(max_rows_per_file) && missing(max_rows_per_group) && max_rows_per_group > max_rows_per_file) {
max_rows_per_group <- max_rows_per_file
}

quoting_style_arrow_opts <- c("Needed", "AllValid", "None")
quote <- match(match.arg(quote), c("needed", "all", "none"))
quote <- quoting_style_arrow_opts[quote]

write_dataset(
dataset = dataset,
path = path,
format = "txt",
partitioning = partitioning,
basename_template = basename_template,
hive_style = hive_style,
existing_data_behavior = existing_data_behavior,
max_partitions = max_partitions,
max_open_files = max_open_files,
max_rows_per_file = max_rows_per_file,
min_rows_per_group = min_rows_per_group,
max_rows_per_group = max_rows_per_group,
include_header = col_names,
batch_size = batch_size,
delimiter = delim,
null_string = na,
eol = eol,
quoting_style = quote
)
}

#' @rdname write_delim_dataset
#' @export
write_csv_dataset <- function(dataset,
path,
partitioning = dplyr::group_vars(dataset),
basename_template = "part-{i}.csv",
hive_style = TRUE,
existing_data_behavior = c("overwrite", "error", "delete_matching"),
max_partitions = 1024L,
max_open_files = 900L,
max_rows_per_file = 0L,
min_rows_per_group = 0L,
max_rows_per_group = bitwShiftL(1, 20),
col_names = TRUE,
batch_size = 1024L,
delim = ",",
na = "",
eol = "\n",
quote = c("needed", "all", "none")) {
if (!missing(max_rows_per_file) && missing(max_rows_per_group) && max_rows_per_group > max_rows_per_file) {
max_rows_per_group <- max_rows_per_file
}

quoting_style_arrow_opts <- c("Needed", "AllValid", "None")
quote <- match(match.arg(quote), c("needed", "all", "none"))
quote <- quoting_style_arrow_opts[quote]

write_dataset(
dataset = dataset,
path = path,
format = "csv",
partitioning = partitioning,
basename_template = basename_template,
hive_style = hive_style,
existing_data_behavior = existing_data_behavior,
max_partitions = max_partitions,
max_open_files = max_open_files,
max_rows_per_file = max_rows_per_file,
min_rows_per_group = min_rows_per_group,
max_rows_per_group = max_rows_per_group,
include_header = col_names,
batch_size = batch_size,
delimiter = delim,
null_string = na,
eol = eol,
quoting_style = quote
)
}

#' @rdname write_delim_dataset
#' @export
write_tsv_dataset <- function(dataset,
path,
partitioning = dplyr::group_vars(dataset),
basename_template = "part-{i}.tsv",
hive_style = TRUE,
existing_data_behavior = c("overwrite", "error", "delete_matching"),
max_partitions = 1024L,
max_open_files = 900L,
max_rows_per_file = 0L,
min_rows_per_group = 0L,
max_rows_per_group = bitwShiftL(1, 20),
col_names = TRUE,
batch_size = 1024L,
na = "",
eol = "\n",
quote = c("needed", "all", "none")) {
if (!missing(max_rows_per_file) && missing(max_rows_per_group) && max_rows_per_group > max_rows_per_file) {
max_rows_per_group <- max_rows_per_file
}
Comment thread
dgreiss marked this conversation as resolved.
Resolved

quoting_style_arrow_opts <- c("Needed", "AllValid", "None")
quote <- match(match.arg(quote), c("needed", "all", "none"))
quote <- quoting_style_arrow_opts[quote]

write_dataset(
dataset = dataset,
path = path,
format = "tsv",
partitioning = partitioning,
basename_template = basename_template,
hive_style = hive_style,
existing_data_behavior = existing_data_behavior,
max_partitions = max_partitions,
max_open_files = max_open_files,
max_rows_per_file = max_rows_per_file,
min_rows_per_group = min_rows_per_group,
max_rows_per_group = max_rows_per_group,
include_header = col_names,
batch_size = batch_size,
null_string = na,
eol = eol,
quoting_style = quote
)
}

validate_positive_int_value <- function(value, msg) {
if (!is_integerish(value, n = 1) || is.na(value) || value < 0) {
abort(paste(substitute(value), "must be a positive, non-missing integer"))
Expand Down
3 changes: 3 additions & 0 deletions r/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ reference:
Write multi-file datasets to disk.
contents:
- write_dataset
- write_delim_dataset
- write_csv_dataset
- write_tsv_dataset

- title: Read files
desc: >
Expand Down
2 changes: 1 addition & 1 deletion r/man/write_dataset.Rd

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

Loading