Skip to content

Commit

Permalink
GH-38376 [R]: Add dimnames method to Dataset class (#38377)
Browse files Browse the repository at this point in the history
### 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: #38376

Authored-by: Jonathan Keane <jkeane@gmail.com>
Signed-off-by: Nic Crane <thisisnic@gmail.com>
  • Loading branch information
jonkeane authored Oct 22, 2023
1 parent cb7996f commit f489996
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 f489996

Please sign in to comment.