You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by th...@apache.org on 2023/06/23 15:02:19 UTC
[arrow] branch main updated: GH-36264: [R] Add scalar() function (#36265)
This is an automated email from the ASF dual-hosted git repository.
thisisnic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new ed99693319 GH-36264: [R] Add scalar() function (#36265)
ed99693319 is described below
commit ed99693319036f0b8fb6585858541cec7a1268ae
Author: Nic Crane <th...@gmail.com>
AuthorDate: Fri Jun 23 16:02:12 2023 +0100
GH-36264: [R] Add scalar() function (#36265)
Authored-by: Nic Crane <th...@gmail.com>
Signed-off-by: Nic Crane <th...@gmail.com>
---
r/NAMESPACE | 1 +
r/R/scalar.R | 20 +++++++++++++++++++-
r/_pkgdown.yml | 3 ++-
r/man/{Scalar.Rd => Scalar-class.Rd} | 0
r/man/scalar.Rd | 25 +++++++++++++++++++++++++
r/tests/testthat/test-scalar.R | 24 ++++++++++++------------
6 files changed, 59 insertions(+), 14 deletions(-)
diff --git a/r/NAMESPACE b/r/NAMESPACE
index e2078a1290..a60c15fbbf 100644
--- a/r/NAMESPACE
+++ b/r/NAMESPACE
@@ -379,6 +379,7 @@ export(register_extension_type)
export(register_scalar_function)
export(reregister_extension_type)
export(s3_bucket)
+export(scalar)
export(schema)
export(set_cpu_count)
export(set_io_thread_count)
diff --git a/r/R/scalar.R b/r/R/scalar.R
index aff2444208..1ada5a9f4e 100644
--- a/r/R/scalar.R
+++ b/r/R/scalar.R
@@ -52,7 +52,7 @@
#' to a different type
#'
#' @name Scalar
-#' @rdname Scalar
+#' @rdname Scalar-class
#' @examples
#' Scalar$create(pi)
#' Scalar$create(404)
@@ -104,6 +104,24 @@ Scalar$create <- function(x, type = NULL) {
Array__GetScalar(Array$create(x, type = type), 0)
}
+#' @title Create an Arrow Scalar
+#'
+#' @name scalar
+#' @rdname scalar
+#'
+#' @param x An R vector, list, or `data.frame`
+#' @param type An optional [data type][data-type] for `x`. If omitted, the type will be inferred from the data.
+#' @examples
+#' scalar(pi)
+#' scalar(404)
+#' # If you pass a vector into scalar(), you get a list containing your items
+#' scalar(c(1, 2, 3))
+#'
+#' scalar(9) == scalar(10)
+#'
+#' @export
+scalar <- Scalar$create
+
#' @rdname array
#' @usage NULL
#' @format NULL
diff --git a/r/_pkgdown.yml b/r/_pkgdown.yml
index 029debc772..6902ae2746 100644
--- a/r/_pkgdown.yml
+++ b/r/_pkgdown.yml
@@ -187,9 +187,10 @@ reference:
- title: Arrow data containers
contents:
+ - scalar
- array
- ChunkedArray
- - Scalar
+ - Scalar-class
- RecordBatch
- Table
- ArrayData
diff --git a/r/man/Scalar.Rd b/r/man/Scalar-class.Rd
similarity index 100%
rename from r/man/Scalar.Rd
rename to r/man/Scalar-class.Rd
diff --git a/r/man/scalar.Rd b/r/man/scalar.Rd
new file mode 100644
index 0000000000..5b35104c16
--- /dev/null
+++ b/r/man/scalar.Rd
@@ -0,0 +1,25 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/scalar.R
+\name{scalar}
+\alias{scalar}
+\title{Create an Arrow Scalar}
+\usage{
+scalar(x, type = NULL)
+}
+\arguments{
+\item{x}{An R vector, list, or \code{data.frame}}
+
+\item{type}{An optional \link[=data-type]{data type} for \code{x}. If omitted, the type will be inferred from the data.}
+}
+\description{
+Create an Arrow Scalar
+}
+\examples{
+scalar(pi)
+scalar(404)
+# If you pass a vector into scalar(), you get a list containing your items
+scalar(c(1, 2, 3))
+
+scalar(9) == scalar(10)
+
+}
diff --git a/r/tests/testthat/test-scalar.R b/r/tests/testthat/test-scalar.R
index f6b0c4c14a..06f9565043 100644
--- a/r/tests/testthat/test-scalar.R
+++ b/r/tests/testthat/test-scalar.R
@@ -17,7 +17,7 @@
expect_scalar_roundtrip <- function(x, type) {
- s <- Scalar$create(x)
+ s <- scalar(x)
expect_r6_class(s, "Scalar")
expect_equal(s$type, type)
expect_identical(length(s), 1L)
@@ -40,12 +40,12 @@ test_that("Scalar object roundtrip", {
})
test_that("Scalar print", {
- expect_output(print(Scalar$create(4)), "Scalar\n4")
+ expect_output(print(scalar(4)), "Scalar\n4")
})
test_that("ExtensionType scalar behaviour", {
ext_array <- vctrs_extension_array(4)
- ext_scalar <- Scalar$create(ext_array)
+ ext_scalar <- scalar(ext_array)
expect_equal(ext_scalar$as_array(), ext_array)
expect_identical(ext_scalar$as_vector(), 4)
expect_identical(ext_scalar$as_vector(10), rep(4, 10))
@@ -53,21 +53,21 @@ test_that("ExtensionType scalar behaviour", {
})
test_that("Creating Scalars of a different type and casting them", {
- expect_equal(Scalar$create(4L, int8())$type, int8())
- expect_equal(Scalar$create(4L)$cast(float32())$type, float32())
+ expect_equal(scalar(4L, int8())$type, int8())
+ expect_equal(scalar(4L)$cast(float32())$type, float32())
})
test_that("Scalar to Array", {
- a <- Scalar$create(42)
+ a <- scalar(42)
expect_equal(a$as_array(), Array$create(42))
expect_equal(Array$create(a), Array$create(42))
})
test_that("Scalar$Equals", {
- a <- Scalar$create(42)
+ a <- scalar(42)
aa <- Array$create(42)
- b <- Scalar$create(42)
- d <- Scalar$create(43)
+ b <- scalar(42)
+ d <- scalar(43)
expect_equal(a, b)
expect_true(a$Equals(b))
expect_false(a$Equals(d))
@@ -75,9 +75,9 @@ test_that("Scalar$Equals", {
})
test_that("Scalar$ApproxEquals", {
- a <- Scalar$create(1.0000000000001)
+ a <- scalar(1.0000000000001)
aa <- Array$create(1.0000000000001)
- b <- Scalar$create(1.0)
+ b <- scalar(1.0)
d <- 2.400000000000001
expect_false(a$Equals(b))
expect_true(a$ApproxEquals(b))
@@ -92,7 +92,7 @@ test_that("Handling string data with embedded nuls", {
"embedded nul in string: 'ma\\0n'", # See?
fixed = TRUE
)
- scalar_with_nul <- Scalar$create(raws, binary())$cast(utf8())
+ scalar_with_nul <- scalar(raws, binary())$cast(utf8())
# The behavior of the warnings/errors is slightly different with and without
# altrep. Without it (i.e. 3.5.0 and below, the error would trigger immediately