Skip to content
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

lazy evaluation cannot be disabled partial inside grouped tibble #1073

Closed
idavydov opened this issue Apr 20, 2023 · 1 comment
Closed

lazy evaluation cannot be disabled partial inside grouped tibble #1073

idavydov opened this issue Apr 20, 2023 · 1 comment

Comments

@idavydov
Copy link

I'm trying to create a list-column with functions. Each function should depend on arguments from that tibble row, so I'm using purrr::partial().

It seems that it's impossible to reproduce the .lazy = FALSE behavior with the !! syntax.

# works, but deprecated
tibble::tibble(x = 1:3) |> 
  dplyr::rowwise() |> 
  dplyr::mutate(square_root = list(purrr::partial(sqrt, x, .lazy = FALSE)))
#> Warning: There were 3 warnings in `dplyr::mutate()`.
#> The first warning was:
#> ℹ In argument: `square_root = list(purrr::partial(sqrt, x, .lazy = FALSE))`.
#> ℹ In row 1.
#> Caused by warning:
#> ! The `.lazy` argument of `partial()` is deprecated as of purrr 0.3.0.
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 2 remaining warnings.
#> # A tibble: 3 × 2
#> # Rowwise: 
#>       x square_root
#>   <int> <list>     
#> 1     1 <prrr_fn_> 
#> 2     2 <prrr_fn_> 
#> 3     3 <prrr_fn_>

# fails
tibble::tibble(x = 1:3) |> 
  dplyr::rowwise() |> 
  dplyr::mutate(square_root = list(purrr::partial(sqrt, !!x)))
#> Error in quos(..., .ignore_empty = "all"): object 'x' not found

# fails
tibble::tibble(x = 1:3) |> 
  dplyr::rowwise() |> 
  dplyr::mutate(square_root = list(purrr::partial(sqrt, !!.data$x)))
#> Error in quos(..., .ignore_empty = "all"): object '.data' not found

Created on 2023-04-20 with reprex v2.0.2

CC @klmr

@hadley
Copy link
Member

hadley commented Jul 26, 2023

The problem is that the !! is getting evaluated by mutate instead of by partial(). I think the easiest way to work around this is to just construct the function factory yourself:

library(tidyverse)

my_sqrt <- function(x) {
  force(x)
  function() sqrt(x)
}

tibble(x = 1:3) |> 
  rowwise() |> 
  mutate(square_root = list(my_sqrt(x)))
#> # A tibble: 3 × 2
#> # Rowwise: 
#>       x square_root
#>   <int> <list>     
#> 1     1 <fn>       
#> 2     2 <fn>       
#> 3     3 <fn>

Created on 2023-07-26 with reprex v2.0.2

@hadley hadley closed this as completed Jul 26, 2023
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