Skip to content

Commit

Permalink
GH-37842: [R] Implement infer_schema.data.frame() (#37843)
Browse files Browse the repository at this point in the history
### Rationale for this change

Users will be able to easily see the schema which their `data.frame` object will have when it's converted into an Arrwo table.

### What changes are included in this PR?

Implements `infer_schema()` method for `data.frame` objects.

Before:

``` r
library(arrow)
schema(mtcars)
#> Error in UseMethod("infer_schema"): no applicable method for 'infer_schema' applied to an object of class "data.frame"
```
After:

``` r
library(arrow)
schema(mtcars)
#> Schema
#> mpg: double
#> cyl: double
#> disp: double
#> hp: double
#> drat: double
#> wt: double
#> qsec: double
#> vs: double
#> am: double
#> gear: double
#> carb: double
#> 
#> See $metadata for additional Schema metadata
```

### Are these changes tested?

Yes

### Are there any user-facing changes?

Yes
* Closes: #37842

Authored-by: Nic Crane <[email protected]>
Signed-off-by: Nic Crane <[email protected]>
  • Loading branch information
thisisnic authored Sep 28, 2023
1 parent e9730f5 commit 79abb73
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ S3method(infer_schema,ArrowTabular)
S3method(infer_schema,Dataset)
S3method(infer_schema,RecordBatchReader)
S3method(infer_schema,arrow_dplyr_query)
S3method(infer_schema,data.frame)
S3method(infer_type,ArrowDatum)
S3method(infer_type,Expression)
S3method(infer_type,blob)
Expand Down
3 changes: 3 additions & 0 deletions r/R/schema.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ infer_schema.Dataset <- function(x) x$schema
#' @export
infer_schema.arrow_dplyr_query <- function(x) implicit_schema(x)

#' @export
infer_schema.data.frame <- function(x) schema(!!!lapply(x, infer_type))

#' @export
names.Schema <- function(x) x$names

Expand Down
5 changes: 5 additions & 0 deletions r/tests/testthat/test-schema.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,14 @@ test_that("schema name assignment", {

test_that("schema extraction", {
skip_if_not_available("dataset")

tbl <- arrow_table(example_data)
expect_equal(schema(example_data), tbl$schema)
expect_equal(schema(tbl), tbl$schema)

expect_equal(schema(data.frame(a = 1, a = "x", check.names = FALSE)), schema(a = double(), a = string()))
expect_equal(schema(data.frame()), schema())

ds <- InMemoryDataset$create(example_data)
expect_equal(schema(ds), ds$schema)

Expand Down

0 comments on commit 79abb73

Please sign in to comment.