Skip to content

Commit

Permalink
apacheGH-38376 [R]: Add dimnames method to Dataset class (apache#…
Browse files Browse the repository at this point in the history
…38377)

### Rationale for this change

Add `dimnames` method so that things like `colnames(ds)` work out of the box on datasets. Inspired by dbplyr's lazy tbl implementation

### What changes are included in this PR?

An additional `dimnames` method for Dataset objects

### Are these changes tested?

Yes

### Are there any user-facing changes?

Yes
* Closes: apache#38376

Authored-by: Jonathan Keane <[email protected]>
Signed-off-by: Nic Crane <[email protected]>
  • Loading branch information
jonkeane authored and JerAguilon committed Oct 25, 2023
1 parent d4a64ce commit a5e5508
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ S3method(dim,RecordBatchReader)
S3method(dim,StructArray)
S3method(dim,arrow_dplyr_query)
S3method(dimnames,ArrowTabular)
S3method(dimnames,Dataset)
S3method(head,ArrowDatum)
S3method(head,ArrowTabular)
S3method(head,Dataset)
Expand Down
3 changes: 3 additions & 0 deletions r/R/dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ names.Dataset <- function(x) names(x$schema)
#' @export
dim.Dataset <- function(x) c(x$num_rows, x$num_cols)

#' @export
dimnames.Dataset <- function(x) list(NULL, names(x))

#' @export
c.Dataset <- function(...) Dataset$create(list(...))

Expand Down
6 changes: 6 additions & 0 deletions r/tests/testthat/test-dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ test_that("dim method returns the correct number of rows and columns", {
expect_identical(dim(ds), c(20L, 7L))
})

test_that("dimnames, colnames on Dataset objects", {
ds <- open_dataset(dataset_dir, partitioning = schema(part = uint8()))
col_names <- c("int", "dbl", "lgl", "chr", "fct", "ts", "part")
expect_identical(dimnames(ds), list(NULL, col_names))
expect_identical(colnames(ds), col_names)
})

test_that("dim() correctly determine numbers of rows and columns on arrow_dplyr_query object", {
ds <- open_dataset(dataset_dir, partitioning = schema(part = uint8()))
Expand Down

0 comments on commit a5e5508

Please sign in to comment.