thisisnic commented on code in PR #13786:
URL: https://github.com/apache/arrow/pull/13786#discussion_r955295593


##########
r/R/dplyr-mutate.R:
##########
@@ -151,3 +153,95 @@ ensure_named_exprs <- function(exprs) {
   names(exprs)[unnamed] <- map_chr(exprs[unnamed], format_expr)
   exprs
 }
+
+# Take the input quos and unfold any instances of across()
+# into individual quosures
+unfold_across <- function(.data, quos_in) {
+  quos_out <- list()
+  # Check for any expressions starting with across
+  for (quo_i in seq_along(quos_in)) {
+    quo_in <- quos_in[quo_i]
+    quo_expr <- quo_get_expr(quo_in[[1]])
+
+    if (is_call(quo_expr, "across")) {
+      new_quos <- list()
+      across_call <- match.call(dplyr::across, quo_expr)
+
+      if (!all(names(across_call[-1]) %in% c(".cols", ".fns", ".names"))) {
+        abort("`...` argument to `across()` is deprecated in dplyr and not 
supported in Arrow")
+      }
+
+      # ARROW-17364: add support for .names argument
+      if (!is.null(across_call[[".names"]])) {
+        abort("`.names` argument to `across()` not yet supported in Arrow")
+      }
+
+      # use select() to get the column names so we can take advantage of 
tidyselect
+      cols <- names(select(.data, !!across_call[[".cols"]]))
+      funcs <- as.character(across_call[[".fns"]])
+
+      # calling across() with .fns = NULL returns all columns unchanged
+      if (is_empty(funcs)) {
+        return()
+      }
+
+      if (funcs[[1]] == "~") {
+        abort(
+          paste(
+            "purrr-style lambda functions as `.fns` argument to `across()`",
+            "not yet supported in Arrow"
+          )
+        )
+      }
+
+      # if only 1 function, we overwrite the old columns with the new values
+      if (length(funcs) == 1) {
+        # work out the quosures from the call
+        col_syms <- syms(cols)
+        new_quos <- map(col_syms, ~ quo(!!call2(funcs, .x)))
+        new_quos <- set_names(new_quos, cols)
+      } else {
+        # remove `c()` and `list()` which have been used to specify functions
+        extracted_funcs <- funcs[map_lgl(funcs, ~ !.x %in% c("c", "list"))]
+
+        func_list <- ensure_named_funcs(extracted_funcs)
+        new_quos <- quosures_from_func_list(func_list, cols)
+      }
+
+      quos_out <- append(quos_out, new_quos)
+    } else {
+      quos_out <- append(quos_out, quo_in)
+    }
+  }
+
+  quos_out
+}
+
+# if the function is unnamed (an empty character), use the index instead
+ensure_named_funcs <- function(funcs) {
+  func_list <- as.list(funcs)
+  func_names <- names(funcs) %||% rep("", length(funcs))
+  func_indices <- seq_along(funcs)
+  names(func_list) <- map2_chr(func_names, func_indices, max)
+  func_list
+}
+
+# given a named list of functions and column names, create a list of new 
quosures
+quosures_from_func_list <- function(func_list, cols) {
+  func_list_full <- rep(func_list, length(cols))
+  cols_list_full <- rep(cols, each = length(func_list))
+
+  # get names of new quosures
+  new_quo_names <- map2_chr(
+    names(func_list_full), cols_list_full,
+    ~ paste(.y, .x, sep = "_")
+  )

Review Comment:
   Now implemented here



-- 
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