Skip to content

Can't pass additional arguments to group_modify() function #4509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
earcanal opened this issue Jul 29, 2019 · 3 comments · Fixed by #4620
Closed

Can't pass additional arguments to group_modify() function #4509

earcanal opened this issue Jul 29, 2019 · 3 comments · Fixed by #4620

Comments

@earcanal
Copy link
Contributor

earcanal commented Jul 29, 2019

I have some code which seems to have broken since group_map() became group_modify(). Converting to group_modify() only partially resolves this as my function also requires additional arguments. Under the old group_map() my function received args passed in ..., but I get an error for the same function under the new group_modify().

The docs indicate I should be able to do this:

... Additional arguments passed on to .f

Is this a regression, or am I doing something wrong?

# dplyr_0.8.3
library(tidyverse)

m <- function(x, y, a, b) {
  tibble(z = z)  
}

mtcars %>%
  group_by(cyl) %>%
  group_modify(m, 1, 2) %>%
  ungroup

Error in (function (.x, .y)  : unused arguments (1, 2)
@cderv
Copy link
Contributor

cderv commented Jul 30, 2019

I think there is indeed an issue in group_modify

dplyr/R/group_map.R

Lines 153 to 166 in ec09492

fun <- function(.x, .y){
res <- .f(.x, .y, ...)
if (!inherits(res, "data.frame")) {
abort("The result of .f should be a data frame")
}
if (any(bad <- names(res) %in% tbl_group_vars)) {
abort(sprintf(
"The returned data frame cannot contain the original grouping variables : ",
paste(names(res)[bad], collapse = ", ")
))
}
bind_cols(.y[rep(1L, nrow(res)), , drop = FALSE], res)
}
chunks <- group_map(.tbl, fun, ..., keep = keep)

the ... are passed to a new fun of two arguments .x and .y but then a re-pass in group_map applied on fun and this is where the error comes from because fun does not expect more than its two arguments.

I don't think the dots need to be passed in the second case and it should be

chunks <- group_map(.tbl, fun, keep = keep)

@billdenney
Copy link
Contributor

I experienced the same issue just now. @cderv, I came up with the same fix before coming to the issue, so I included both of our names in the NEWS file.

@cderv
Copy link
Contributor

cderv commented Nov 17, 2019

Oh thanks ! I forgot about that one and forgot to do a PR 🤦‍♂
Thank @billdenney ! it was not necessary to mention me in NEWS but I appreciate it. 👍

romainfrancois pushed a commit that referenced this issue Nov 25, 2019
* Allow group_modify() to work with ... args (Fix #4509)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants