Search code examples
rrlangtidyeval

Unquoting inside of map2 with tidyeval


I'm creating a function that calculates the number of "runs" or missing or complete data - I want this to work with dplyr::group_by, so I have written this as an S3 method - below is a simplified example of this code.

Unfortunately I find that the bare unquoted variable name does not work, but quoting it, this does work, strangely enough.

Below is the example with output

  fun_run <- function(data, var) {

    UseMethod("fun_run")

  }

fun_run.default <- function(data, var) {

  var <- rlang::enquo(var)

  data_pull <- data %>% dplyr::pull(!(!var))

  # find the lengths of the number of missings in a row
  tibble::as_tibble(c(rle(is.na(data_pull))))

}

fun_run.grouped_df <- function(data, var) {

  var <- rlang::enquo(var)

  tidyr::nest(data) %>% dplyr::mutate(data = purrr::map2(.x = data, .y = !(!var), 
                                                         .f = fun_run)) %>% tidyr::unnest()

}

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

airquality %>% fun_run(Ozone)
#> # A tibble: 35 x 2
#> lengths values
#> <int>  <lgl>
#>   4    FALSE
#>   1     TRUE
#>   4    FALSE
#>   1     TRUE
#>  14    FALSE
#>   3     TRUE
#>   4    FALSE
#>   6     TRUE
#>   1    FALSE
#>   1     TRUE
#> ... with 25 more rows

# doesn't work
airquality %>% group_by(Month) %>% fun_run(Ozone)
#> Error in mutate_impl(.data, dots) : Evaluation error: object 'Ozone' not found. 

# does work
airquality %>% group_by(Month) %>% fun_run("Ozone")
#> # A tibble: 37 x 3
#> Month lengths values
#> <int>   <int>  <lgl>
#>     5       4  FALSE
#>     5       1   TRUE
#>     5       4  FALSE
#>     5       1   TRUE
#>     5      14  FALSE
#>     5       3   TRUE
#>     5       4  FALSE
#>     6       6   TRUE
#>     6       1  FALSE
#>     6       1   TRUE
#> # ... with 27 more rows

Solution

  • You don't actually want to use map2, because your second input (var) isn't changing along with the first input (the grouped/nested data). Additionally, the "Ozone" column is hidden in the nested data at that point. You can see this by trying to execute the code without any tidyeval syntax:

    data <- airquality %>% group_by(Month)
    tidyr::nest(data) %>% dplyr::mutate(data = purrr::map2(.x = data, .y = Ozone, 
                                                           .f = fun_run)) %>% tidyr::unnest()
    #>Error in mutate_impl(.data, dots) : 
    #>  Evaluation error: object 'Ozone' not found.
    

    Instead, you want to use standard map:

    tidyr::nest(data) %>% dplyr::mutate(data = purrr::map(.x = data, var = Ozone, 
                                                           .f = fun_run)) %>% tidyr::unnest()
    

    Once rewritten for use in your function:

    fun_run.grouped_df <- function(data, var) {
    
      var <- rlang::enquo(var)
    
      tidyr::nest(data) %>% dplyr::mutate(data = purrr::map(.x = data, var = !!var, 
                                                            .f = fun_run)) %>% tidyr::unnest()
    
    }
    

    This produces the results from your final quoted example.