Skip to content

Commit 79abb73

Browse files
authored
GH-37842: [R] Implement infer_schema.data.frame() (#37843)
### 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]>
1 parent e9730f5 commit 79abb73

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

r/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ S3method(infer_schema,ArrowTabular)
112112
S3method(infer_schema,Dataset)
113113
S3method(infer_schema,RecordBatchReader)
114114
S3method(infer_schema,arrow_dplyr_query)
115+
S3method(infer_schema,data.frame)
115116
S3method(infer_type,ArrowDatum)
116117
S3method(infer_type,Expression)
117118
S3method(infer_type,blob)

r/R/schema.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ infer_schema.Dataset <- function(x) x$schema
285285
#' @export
286286
infer_schema.arrow_dplyr_query <- function(x) implicit_schema(x)
287287

288+
#' @export
289+
infer_schema.data.frame <- function(x) schema(!!!lapply(x, infer_type))
290+
288291
#' @export
289292
names.Schema <- function(x) x$names
290293

r/tests/testthat/test-schema.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,14 @@ test_that("schema name assignment", {
295295

296296
test_that("schema extraction", {
297297
skip_if_not_available("dataset")
298+
298299
tbl <- arrow_table(example_data)
300+
expect_equal(schema(example_data), tbl$schema)
299301
expect_equal(schema(tbl), tbl$schema)
300302

303+
expect_equal(schema(data.frame(a = 1, a = "x", check.names = FALSE)), schema(a = double(), a = string()))
304+
expect_equal(schema(data.frame()), schema())
305+
301306
ds <- InMemoryDataset$create(example_data)
302307
expect_equal(schema(ds), ds$schema)
303308

0 commit comments

Comments
 (0)