-
Notifications
You must be signed in to change notification settings - Fork 275
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
pmap, anonymous functions, and same-named variables #280
Comments
Your first example works for me. I think there are some improvements to NSE in the development version of dplyr that come into play here. As a sidebar, it seems like a good idea to use names in your on-the-fly list construction and not rely on resolution by position. That's kind of a tangent, but I did so below and switched argument order, just for kicks. library(dplyr)
library(purrr)
data_frame(cyl = c(4, 6, 8), mpg = c(30, 25, 20)) %>%
mutate(x = pmap(.l = list(cyl, mpg),
function(cc, mm) filter(mtcars, cyl == cc, mpg < mm)))
#> # A tibble: 3 × 3
#> cyl mpg x
#> <dbl> <dbl> <list>
#> 1 4 30 <data.frame [7 × 11]>
#> 2 6 25 <data.frame [7 × 11]>
#> 3 8 20 <data.frame [14 × 11]>
data_frame(cyl = c(4, 6, 8), mpg = c(30, 25, 20)) %>%
mutate(x = pmap(.l = list(cc = cyl, mm = mpg),
function(mm, cc) filter(mtcars, cyl == cc, mpg < mm)))
#> # A tibble: 3 × 3
#> cyl mpg x
#> <dbl> <dbl> <list>
#> 1 4 30 <data.frame [7 × 11]>
#> 2 6 25 <data.frame [7 × 11]>
#> 3 8 20 <data.frame [14 × 11]> |
Thanks for your response. Based on your comment, I rebased my local fork of As to your sidebar, please help me to understand: looking briefly at the code ( Regardless, this problem is resolved, thanks! |
I'm no expert at the C side of purrr, but I believe here is where list names, if they exist, are applied to the |
Gotcha, thanks. |
Using same-named variables in a encompassing data.frame appears to be problematic. As a contrived example:
This can be remedied by using a named function:
Please discard the stylistic preference of using better better variable naming conventions to avoid collisions (there are times when I am using the same original data.frame on the mid-
pmap
filter, so the names will be identical).I know it gets complex, but is there a better way to understand how the internal pipe (
filter(mtcars, ...)
) is confusing the variable names of its data.frame with the names of the encompassing environment? I thought that scope would bias finding the variables more-closely stored.The text was updated successfully, but these errors were encountered: