diff --git a/r/R/schema.R b/r/R/schema.R index 9ff38487c4b6f..dc0b4ba81fbbc 100644 --- a/r/R/schema.R +++ b/r/R/schema.R @@ -229,9 +229,13 @@ prepare_key_value_metadata <- function(metadata) { call. = FALSE ) } + + metadata <- as.list(metadata) + if (!is_empty(metadata) && is.list(metadata[["r"]])) { metadata[["r"]] <- .serialize_arrow_r_metadata(metadata[["r"]]) } + map_chr(metadata, as.character) } diff --git a/r/tests/testthat/test-schema.R b/r/tests/testthat/test-schema.R index 24776e6d0c199..a6a0555eaca0e 100644 --- a/r/tests/testthat/test-schema.R +++ b/r/tests/testthat/test-schema.R @@ -140,6 +140,22 @@ test_that("Schema modification", { expect_error(schm[[c(2, 4)]] <- int32(), "length(i) not equal to 1", fixed = TRUE) }) +test_that("Metadata can be reassigned as a whole", { + schm <- schema(b = double(), c = string(), d = int8()) + + # Check named character vector + schm$metadata <- c("foo" = "bar") + expect_identical(schm$metadata, list(foo = "bar")) + + # Check list() + schm$metadata <- list("foo" = "bar") + expect_identical(schm$metadata, list(foo = "bar")) + + # Check NULL for removal + schm$metadata <- NULL + expect_identical(schm$metadata, set_names(list(), character())) +}) + test_that("Metadata is preserved when modifying Schema", { schm <- schema(b = double(), c = string(), d = int8()) schm$metadata$foo <- "bar"