nealrichardson commented on a change in pull request #10269:
URL: https://github.com/apache/arrow/pull/10269#discussion_r647851002
##########
File path: r/R/util.R
##########
@@ -139,3 +139,42 @@ attr(is_writable_table, "fail") <- function(call, env){
)
}
+#' Take an object of length 1 and repeat it.
+#'
+#' @param object Object of length 1 to be repeated - vector, `Scalar`,
`Array`, or `ChunkedArray`
+#' @param n Number of repetitions
+#'
+#' @return `Array` of length `n`
+#'
+#' @keywords internal
+repeat_value_as_array <- function(object, n) {
+ if (inherits(object, "ChunkedArray")) {
+ return(Scalar$create(object$chunks[[1]])$as_array(n))
+ }
+ return(Scalar$create(object)$as_array(n))
+}
+
+#' Recycle scalar values in a list of arrays
+#'
+#' @param arrays List of arrays
+#' @return List of arrays with any vector/Scalar/Array/ChunkedArray values of
length 1 recycled
+#' @keywords internal
+recycle_scalars <- function(arrays){
+ # Get lengths of items in arrays
+ arr_lens <- map_int(arrays, NROW)
+
Review comment:
NBD but you call `arr_lens == 1` 4 times below so you could store that
up here, and then I think the code becomes a little more readable (in a
prose-like way).
```suggestion
is_scalar <- arr_lens == 1
```
##########
File path: r/R/util.R
##########
@@ -139,3 +139,42 @@ attr(is_writable_table, "fail") <- function(call, env){
)
}
+#' Take an object of length 1 and repeat it.
+#'
+#' @param object Object of length 1 to be repeated - vector, `Scalar`,
`Array`, or `ChunkedArray`
+#' @param n Number of repetitions
+#'
+#' @return `Array` of length `n`
+#'
+#' @keywords internal
+repeat_value_as_array <- function(object, n) {
+ if (inherits(object, "ChunkedArray")) {
+ return(Scalar$create(object$chunks[[1]])$as_array(n))
+ }
+ return(Scalar$create(object)$as_array(n))
+}
+
+#' Recycle scalar values in a list of arrays
+#'
+#' @param arrays List of arrays
+#' @return List of arrays with any vector/Scalar/Array/ChunkedArray values of
length 1 recycled
+#' @keywords internal
+recycle_scalars <- function(arrays){
+ # Get lengths of items in arrays
+ arr_lens <- map_int(arrays, NROW)
+
+ if (length(arrays) > 1 && any(arr_lens == 1) && !all(arr_lens==1)) {
+
+ # Recycling not supported for tibbles and data.frames
+ if(all(map_lgl(arrays, ~inherits(.x, "data.frame")))){
+ abort(c(
+ "All input tibbles or data.frames must have the same number of rows",
+ x = paste("Number of rows in inputs:",oxford_paste(map_int(arrays,
~nrow(.x))))
Review comment:
right?
```suggestion
x = paste("Number of rows in inputs:", oxford_paste(arr_lens)
```
Also, are we worried that `arr_lens` could be large (and thus this error
message would be huge)?
##########
File path: r/R/util.R
##########
@@ -139,3 +139,42 @@ attr(is_writable_table, "fail") <- function(call, env){
)
}
+#' Take an object of length 1 and repeat it.
+#'
+#' @param object Object of length 1 to be repeated - vector, `Scalar`,
`Array`, or `ChunkedArray`
+#' @param n Number of repetitions
+#'
+#' @return `Array` of length `n`
+#'
+#' @keywords internal
+repeat_value_as_array <- function(object, n) {
+ if (inherits(object, "ChunkedArray")) {
+ return(Scalar$create(object$chunks[[1]])$as_array(n))
+ }
+ return(Scalar$create(object)$as_array(n))
+}
+
+#' Recycle scalar values in a list of arrays
+#'
+#' @param arrays List of arrays
+#' @return List of arrays with any vector/Scalar/Array/ChunkedArray values of
length 1 recycled
+#' @keywords internal
+recycle_scalars <- function(arrays){
+ # Get lengths of items in arrays
+ arr_lens <- map_int(arrays, NROW)
+
+ if (length(arrays) > 1 && any(arr_lens == 1) && !all(arr_lens==1)) {
+
+ # Recycling not supported for tibbles and data.frames
+ if(all(map_lgl(arrays, ~inherits(.x, "data.frame")))){
Review comment:
You added whitespace throughout the package but not here :)
```suggestion
if (all(map_lgl(arrays, ~inherits(.x, "data.frame")))) {
```
##########
File path: r/R/util.R
##########
@@ -139,3 +139,42 @@ attr(is_writable_table, "fail") <- function(call, env){
)
}
+#' Take an object of length 1 and repeat it.
Review comment:
Minor suggestion: I'd put this function definition after
`recycle_scalars`. It's only interesting in the context of that function.
--
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.
For queries about this service, please contact Infrastructure at:
[email protected]