diff --git a/r/NAMESPACE b/r/NAMESPACE index 572e5e24c9a1c..31fd9cf6a872e 100644 --- a/r/NAMESPACE +++ b/r/NAMESPACE @@ -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) diff --git a/r/R/arrow-package.R b/r/R/arrow-package.R index edc2652b6b63d..a72483a0d6df7 100644 --- a/r/R/arrow-package.R +++ b/r/R/arrow-package.R @@ -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 diff --git a/r/R/schema.R b/r/R/schema.R index c3dfee5f9fe35..0d66aeb83c4b9 100644 --- a/r/R/schema.R +++ b/r/R/schema.R @@ -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 @@ -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 @@ -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]])) { + .list <- .list[[1]] + } + if (all(map_lgl(.list, ~ inherits(., "Field")))) { schema_(.list) } else { @@ -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 diff --git a/r/man/Schema.Rd b/r/man/Schema.Rd index 7322c70f2c774..7a2d255190887 100644 --- a/r/man/Schema.Rd +++ b/r/man/Schema.Rd @@ -9,11 +9,10 @@ schema(...) } \arguments{ -\item{...}{named list containing \link[=data-type]{data types} or -a list of \link[=field]{fields} containing the fields for the schema} +\item{...}{\link[=field]{fields} or field name/\link[=data-type]{data type} pairs} } \description{ -A \code{Schema} is a list of \link{Field}s, which map names to +A \code{Schema} is an Arrow object containing \link{Field}s, which map names to Arrow \link[=data-type]{data types}. Create a \code{Schema} when you want to convert an R \code{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 @@ -77,6 +76,14 @@ the metadata is dropped. \examples{ \dontshow{if (arrow_available()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +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 diff --git a/r/tests/testthat/test-schema.R b/r/tests/testthat/test-schema.R index 8473550df652d..b7209a3b1c7aa 100644 --- a/r/tests/testthat/test-schema.R +++ b/r/tests/testthat/test-schema.R @@ -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", @@ -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())) +})