Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-13888: [R] Rephrase docs for schema()'s ellipses argument and rephrase error message #11645

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ importFrom(rlang,is_character)
importFrom(rlang,is_false)
importFrom(rlang,is_integerish)
importFrom(rlang,is_interactive)
importFrom(rlang,is_list)
importFrom(rlang,is_quosure)
importFrom(rlang,list2)
importFrom(rlang,new_data_mask)
Expand Down
1 change: 1 addition & 0 deletions r/R/arrow-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#' @importFrom rlang eval_tidy new_data_mask syms env new_environment env_bind set_names exec
#' @importFrom rlang is_bare_character quo_get_expr quo_get_env quo_set_expr .data seq2 is_interactive
#' @importFrom rlang expr caller_env is_character quo_name is_quosure enexpr enexprs as_quosure
#' @importFrom rlang is_list
#' @importFrom tidyselect vars_pull vars_rename vars_select eval_select
#' @useDynLib arrow, .registration = TRUE
#' @keywords internal
Expand Down
19 changes: 16 additions & 3 deletions r/R/schema.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @include arrow-package.R
#' @title Schema class
#'
#' @description A `Schema` is a list of [Field]s, which map names to
#' @description A `Schema` is an Arrow object containing [Field]s, which map names to
#' Arrow [data types][data-type]. Create a `Schema` when you
#' want to convert an R `data.frame` to Arrow but don't want to rely on the
#' default mapping of R types to Arrow types, such as when you want to choose a
Expand Down Expand Up @@ -78,6 +78,14 @@
#' @rdname Schema
#' @name Schema
#' @examplesIf arrow_available()
#' schema(a = int32(), b = float64())
#'
#' schema(
#' field("b", double()),
#' field("c", bool(), nullable = FALSE),
#' field("d", string())
#' )
#'
#' df <- data.frame(col1 = 2:4, col2 = c(0.1, 0.3, 0.5))
#' tab1 <- arrow_table(df)
#' tab1$schema
Expand Down Expand Up @@ -155,6 +163,12 @@ Schema <- R6Class("Schema",
)
Schema$create <- function(...) {
.list <- list2(...)

# if we were provided only a list of types or fields, use that
if (length(.list) == 1 && is_list(.list[[1]])) {
thisisnic marked this conversation as resolved.
Show resolved Hide resolved
.list <- .list[[1]]
}

if (all(map_lgl(.list, ~ inherits(., "Field")))) {
schema_(.list)
} else {
Expand Down Expand Up @@ -185,8 +199,7 @@ print_schema_fields <- function(s) {
paste(map_chr(s$fields, ~ .$ToString()), collapse = "\n")
}

#' @param ... named list containing [data types][data-type] or
#' a list of [fields][field] containing the fields for the schema
#' @param ... [fields][field] or field name/[data type][data-type] pairs
#' @export
#' @rdname Schema
schema <- Schema$create
Expand Down
13 changes: 10 additions & 3 deletions r/man/Schema.Rd

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

26 changes: 23 additions & 3 deletions r/tests/testthat/test-schema.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ test_that("Schema print method", {

test_that("Schema with non-nullable fields", {
expect_output(
print(schema(field("b", double()),
field("c", bool(), nullable = FALSE),
field("d", string()))),
print(
schema(
field("b", double()),
field("c", bool(), nullable = FALSE),
field("d", string())
)
),
paste(
"Schema",
"b: double",
Expand Down Expand Up @@ -218,3 +222,19 @@ test_that("Schema to C-interface", {
# must clean up the pointer or we leak
delete_arrow_schema(ptr)
})

test_that("Schemas from lists", {
name_list_schema <- schema(list(b = double(), c = string(), d = int8()))


field_list_schema <- schema(
list(
field("b", double()),
field("c", bool()),
field("d", string())
)
)

expect_equal(name_list_schema, schema(b = double(), c = string(), d = int8()))
expect_equal(field_list_schema, schema(b = double(), c = bool(), d = string()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

})