Skip to content

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

Closed
@earcanal

Description

@earcanal
Contributor

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)

Activity

cderv

cderv commented on Jul 30, 2019

@cderv
Contributor

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)
added a commit that references this issue on Nov 16, 2019
2302685
billdenney

billdenney commented on Nov 16, 2019

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

cderv commented on Nov 17, 2019

@cderv
Contributor

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

added a commit that references this issue on Nov 25, 2019
5b32cc6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @earcanal@cderv@billdenney

      Issue actions

        Can't pass additional arguments to group_modify() function · Issue #4509 · tidyverse/dplyr