Skip to content

data masking doesn't work in anonymous functions used as mutate(data, y = map(x, function(x){}) #1328

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
archavan opened this issue Dec 8, 2021 · 1 comment

Comments

@archavan
Copy link

archavan commented Dec 8, 2021

I am not sure whether this issue falls under the purview of rlang or dplyr. Happy to post to another repo as appropriate.

Data masking doesn’t seem to be working when I use {{ }} in an anonymous function within a purrr::map() or lapply() call inside of a dplyr::mutate() call. It works when I use a predefined function or in a bare purrr::map() or lapply() call outside of dplyr::mutate(), though. Using the .data pronoun method also shows the same behaviour. Please see the reprex below.

library(tidyverse)
iris_nested <- nest(iris, data = -Species)

This doesn’t work

mutate(iris_nested, newcol = map(data,
                                 function(x) {
                                   select_col <- names(x)[2]
                                   select(x, {{ select_col }})
                                 }))
#> Error in (function (arg) : object 'select_col' not found

This works

select_2 <- function(x) {
  select_col <- names(x)[2]
  select(x, {{ select_col }})
}

mutate(iris_nested, newcol = map(data, select_2))
#> # A tibble: 3 × 3
#>   Species    data              newcol           
#>   <fct>      <list>            <list>           
#> 1 setosa     <tibble [50 × 4]> <tibble [50 × 1]>
#> 2 versicolor <tibble [50 × 4]> <tibble [50 × 1]>
#> 3 virginica  <tibble [50 × 4]> <tibble [50 × 1]>

This works too

map(iris_nested$data,  
    function(x) {
      select_col <- names(x)[2]
      select(x, {{ select_col }})
    }) %>% 
  map(head, 1)
#> [[1]]
#> # A tibble: 1 × 1
#>   Sepal.Width
#>         <dbl>
#> 1         3.5
#> 
#> [[2]]
#> # A tibble: 1 × 1
#>   Sepal.Width
#>         <dbl>
#> 1         3.2
#> 
#> [[3]]
#> # A tibble: 1 × 1
#>   Sepal.Width
#>         <dbl>
#> 1         3.3

Created on 2021-12-08 by the reprex package (v2.0.1)

@lionel-
Copy link
Member

lionel- commented Dec 8, 2021

This is an injection timing issue. I have plans to fix this for rlang 1.1 (so not the next release). However this fix will be a behaviour change that might cause too much backward incompatibility, so there is no guarantee that this will ever happen. You can track this at the original issue #845.

In the meantime, you can work around by storing your lambda function as a named function outside of the mutate, as in your select_2() example.

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

No branches or pull requests

2 participants