paleolimbot commented on code in PR #14922:
URL: https://github.com/apache/arrow/pull/14922#discussion_r1062549323


##########
r/R/array.R:
##########
@@ -450,6 +439,44 @@ StructArray <- R6Class("StructArray",
   )
 )
 
+StructArray$create <- function(...) {
+  dots <- list2(...)
+
+  stopifnot(length(dots) > 0)
+
+  if (length(dots) == 1 && is.data.frame(dots[[1]])) {
+    return(Array$create(dots[[1]]))
+  }
+
+  # if we can convert items in dots to Arrays, then do
+  if (!all(map_lgl(dots, ~ inherits(.x, "Array")))) {
+    dots <- map(dots, Array$create)
+  }
+
+  if (all(map_lgl(dots, ~ inherits(.x, "Array")))) {
+    # Check for Array length equality
+    if (!length(unique(lengths(dots))) == 1) {
+      abort(
+        message = c(
+          "All input Arrays must be equal lengths",
+          x = paste("Array lengths:", paste(lengths(dots), collapse = ", "))
+        )
+      )
+    }

Review Comment:
   Maybe we also have to consider what happens with Scalars/R vectors that are 
length 1 here?
   
   ``` r
   # pr_fetch(14922)
   library(arrow, warn.conflicts = FALSE)
   #> Some features are not enabled in this build of Arrow. Run `arrow_info()` 
for more information.
   StructArray$create(a = 5L, b = 1:10)
   #> Error in eval(expr, envir, enclos): attempt to apply non-function
   record_batch(a = 5L, b = 1:10)
   #> RecordBatch
   #> 10 rows x 2 columns
   #> $a <int32>
   #> $b <int32>
   str(tibble::tibble(a = 5L, b = 1:10))
   #> tibble [10 × 2] (S3: tbl_df/tbl/data.frame)
   #>  $ a: int [1:10] 5 5 5 5 5 5 5 5 5 5
   #>  $ b: int [1:10] 1 2 3 4 5 6 7 8 9 10
   ```



##########
r/R/array.R:
##########
@@ -450,6 +439,44 @@ StructArray <- R6Class("StructArray",
   )
 )
 
+StructArray$create <- function(...) {
+  dots <- list2(...)
+
+  stopifnot(length(dots) > 0)
+
+  if (length(dots) == 1 && is.data.frame(dots[[1]])) {
+    return(Array$create(dots[[1]]))
+  }
+
+  # if we can convert items in dots to Arrays, then do
+  if (!all(map_lgl(dots, ~ inherits(.x, "Array")))) {
+    dots <- map(dots, Array$create)
+  }
+
+  if (all(map_lgl(dots, ~ inherits(.x, "Array")))) {
+    # Check for Array length equality
+    if (!length(unique(lengths(dots))) == 1) {
+      abort(
+        message = c(
+          "All input Arrays must be equal lengths",
+          x = paste("Array lengths:", paste(lengths(dots), collapse = ", "))
+        )
+      )
+    }

Review Comment:
   Can `cbind_check_length()` be reused here? It has a pretty good error 
message for arguments of different lengths.



##########
r/R/array.R:
##########
@@ -450,6 +439,44 @@ StructArray <- R6Class("StructArray",
   )
 )
 
+StructArray$create <- function(...) {
+  dots <- list2(...)
+
+  stopifnot(length(dots) > 0)
+
+  if (length(dots) == 1 && is.data.frame(dots[[1]])) {
+    return(Array$create(dots[[1]]))
+  }
+
+  # if we can convert items in dots to Arrays, then do
+  if (!all(map_lgl(dots, ~ inherits(.x, "Array")))) {
+    dots <- map(dots, Array$create)
+  }

Review Comment:
   I wonder if `dots <- lapply(dots, as_arrow_array)` is more concise?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to