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

Problem with cur_column() #5892

Closed
olivroy opened this issue May 21, 2021 · 2 comments
Closed

Problem with cur_column() #5892

olivroy opened this issue May 21, 2021 · 2 comments

Comments

@olivroy
Copy link

olivroy commented May 21, 2021

The cur_column() function does not detect the values, using !!!. I read that in across, some parts may be evaluated too early.
Is there a way to replace the !!! call in that case?

The code works outside across.


Brief description of the problem

library(dplyr, warn.conflicts = FALSE)

d <- tibble(x = 1:2, y = 7:8)
values <- list(x = c("1" = "yes",  "2" = "no"),
                      y = c("7" = "YEs",  "8" = "No")
      )

d %>% 
    mutate(across(any_of(names(values)),
                  .fn = ~ recode(., !!!(values[[(cur_column())]])))
    )
#> Error: `cur_column()` must only be used inside `across()`.
d %>% mutate(x = recode(x, !!!(values[["x"]])))
#> # A tibble: 2 x 2
#>   x         y
#>   <chr> <int>
#> 1 yes       7
#> 2 no        8

Created on 2021-05-21 by the reprex package (v2.0.0)

@romainfrancois
Copy link
Member

!!! is spliced too early. You can work around this by defining the function upfront:

library(dplyr, warn.conflicts = FALSE)

d <- tibble(x = 1:2, y = 7:8)
values <- list(x = c("1" = "yes",  "2" = "no"),
               y = c("7" = "YEs",  "8" = "No")
)

f <- function(x) {
  recode(x, !!!(values[[(cur_column())]]))
}

d %>% 
  mutate(across(any_of(names(values)), f))
#> # A tibble: 2 x 2
#>   x     y    
#>   <chr> <chr>
#> 1 yes   YEs  
#> 2 no    No

Created on 2021-05-26 by the reprex package (v2.0.0)

@netique
Copy link

netique commented Feb 22, 2025

I've just stumbled upon a similar issue, but with a different error message that is not helpful at all. Is it possible to somehow inform the user where the culprit is? In this case, there is nocur_collumn(), though:

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
library(forcats)

# fails
tibble(a = factor(c("A1", "A2", "A3"))) |>
  mutate(
    across(
      a,
      \(x) {
        rename_vec <- c("a" = "A1", "b" = "A2", "c" = "A3")
        fct_recode(x, !!!rename_vec)
      }
    )
  )
#> Error: object 'rename_vec' not found

# works
rename_fun <- function(x) {
  rename_vec <- c("a" = "A1", "b" = "A2", "c" = "A3")
  fct_recode(x, !!!rename_vec)
}

tibble(a = factor(c("A1", "A2", "A3"))) |>
  mutate(
    across(
      a,
      rename_fun
    )
  )
#> # A tibble: 3 × 1
#>   a    
#>   <fct>
#> 1 a    
#> 2 b    
#> 3 c

Created on 2025-02-22 with reprex v2.1.1

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

3 participants