Skip to content

Commit

Permalink
GH-35949: [R] CSV File reader options class objects should print the …
Browse files Browse the repository at this point in the history
…selected values (#35955)

Fixes #35949
* Closes: #35949

Authored-by: Nic Crane <thisisnic@gmail.com>
Signed-off-by: Nic Crane <thisisnic@gmail.com>
  • Loading branch information
thisisnic committed Jun 11, 2023
1 parent 7887605 commit fe60932
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
20 changes: 20 additions & 0 deletions r/R/arrowExports.R

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

20 changes: 18 additions & 2 deletions r/R/csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,25 @@ CsvTableReader$create <- function(file,
CsvReadOptions <- R6Class("CsvReadOptions",
inherit = ArrowObject,
public = list(
encoding = NULL
encoding = NULL,
print = function(...) {
cat("CsvReadOptions\n")
for (attr in c(
"column_names", "block_size", "skip_rows", "autogenerate_column_names",
"use_threads", "skip_rows_after_names", "encoding"
)) {
cat(sprintf("%s: %s\n", attr, self[[attr]]))
}
invisible(self)
}
),
active = list(
column_names = function() csv___ReadOptions__column_names(self)
column_names = function() csv___ReadOptions__column_names(self),
block_size = function() csv___ReadOptions__block_size(self),
skip_rows = function() csv___ReadOptions__skip_rows(self),
autogenerate_column_names = function() csv___ReadOptions__autogenerate_column_names(self),
use_threads = function() csv___ReadOptions__use_threads(self),
skip_rows_after_names = function() csv___ReadOptions__skip_rows_after_names(self)
)
)
CsvReadOptions$create <- function(use_threads = option_use_threads(),
Expand All @@ -491,6 +506,7 @@ CsvReadOptions$create <- function(use_threads = option_use_threads(),
)

options$encoding <- encoding

options
}

Expand Down
45 changes: 45 additions & 0 deletions r/src/arrowExports.cpp

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

30 changes: 30 additions & 0 deletions r/src/csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,36 @@ SEXP csv___ReadOptions__column_names(
return cpp11::as_sexp(options->column_names);
}

// [[arrow::export]]
SEXP csv___ReadOptions__block_size(
const std::shared_ptr<arrow::csv::ReadOptions>& options) {
return cpp11::as_sexp(options->block_size);
}

// [[arrow::export]]
SEXP csv___ReadOptions__skip_rows(
const std::shared_ptr<arrow::csv::ReadOptions>& options) {
return cpp11::as_sexp(options->skip_rows);
}

// [[arrow::export]]
SEXP csv___ReadOptions__autogenerate_column_names(
const std::shared_ptr<arrow::csv::ReadOptions>& options) {
return cpp11::as_sexp(options->autogenerate_column_names);
}

// [[arrow::export]]
SEXP csv___ReadOptions__use_threads(
const std::shared_ptr<arrow::csv::ReadOptions>& options) {
return cpp11::as_sexp(options->use_threads);
}

// [[arrow::export]]
SEXP csv___ReadOptions__skip_rows_after_names(
const std::shared_ptr<arrow::csv::ReadOptions>& options) {
return cpp11::as_sexp(options->skip_rows_after_names);
}

// [[arrow::export]]
std::shared_ptr<arrow::csv::ConvertOptions> csv___ConvertOptions__initialize(
cpp11::list options) {
Expand Down
19 changes: 19 additions & 0 deletions r/tests/testthat/test-dataset-csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,22 @@ test_that("open_delim_dataset params passed through to open_dataset", {

expect_equal(ds$time, "16-01-2023")
})

test_that("CSVReadOptions printing", {
default_read_options <- CsvReadOptions$create()
custom_read_options <- CsvReadOptions$create(skip_rows = 102)

expect_output(print(default_read_options), "skip_rows: 0")
expect_output(print(custom_read_options), "skip_rows: 102")
})

test_that("CSVReadOptions field access", {
options <- CsvReadOptions$create()
expect_equal(options$skip_rows, 0)
expect_equal(options$autogenerate_column_names, FALSE)
expect_equal(options$skip_rows_after_names, 0)
expect_equal(options$use_threads, option_use_threads())
expect_equal(options$column_names, character(0))
expect_equal(options$block_size, 1048576L)
expect_equal(options$encoding, "UTF-8")
})

0 comments on commit fe60932

Please sign in to comment.