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

vctrs support in if_else() and case_when() #6145

Closed
wants to merge 5 commits into from

Conversation

krlmlr
Copy link
Member

@krlmlr krlmlr commented Jan 2, 2022

Useful to replace multiple values at once.

@krlmlr krlmlr requested a review from romainfrancois January 12, 2022 05:54
@romainfrancois
Copy link
Member

This snowballs to allow for a multi column mutate() + case_when() :

# pak::pak("tidyverse/dplyr#6145")
library(dplyr, warn.conflicts = FALSE)

example_data <- tibble(
  col1 = LETTERS[1:3], 
  col2 = 1:3, 
  col3 = c("red", "yellow", "orange")
)

example_data %>% 
  mutate(
    case_when(col1 == "A" ~ tibble(type = "fruit", description = "1 red apple"))
  )
#> # A tibble: 3 × 5
#>   col1   col2 col3   type  description
#>   <chr> <int> <chr>  <chr> <chr>      
#> 1 A         1 red    fruit 1 red apple
#> 2 B         2 yellow <NA>  <NA>       
#> 3 C         3 orange <NA>  <NA>

Created on 2022-01-12 by the reprex package (v2.0.1.9000)

Session info
sessioninfo::session_info()
#> ─ Session info  🏪  🐀  💅🏼   ─────────────────────────────────────────────────
#>  hash: convenience store, rat, nail polish: medium-light skin tone
#> 
#>  setting  value
#>  version  R version 4.0.3 (2020-10-10)
#>  os       macOS Big Sur 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Paris
#>  date     2022-01-12
#>  pandoc   2.9.2.1 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version     date (UTC) lib source
#>  assertthat    0.2.1       2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.4.1       2021-12-13 [1] CRAN (R 4.0.2)
#>  cli           3.1.0.9000  2022-01-10 [1] local
#>  crayon        1.4.2       2021-10-29 [1] CRAN (R 4.0.2)
#>  DBI           1.1.2       2021-12-20 [1] CRAN (R 4.0.2)
#>  digest        0.6.29      2021-12-01 [1] CRAN (R 4.0.3)
#>  dplyr       * 1.0.7.9000  2022-01-12 [1] local
#>  ellipsis      0.3.2       2021-04-29 [1] CRAN (R 4.0.3)
#>  evaluate      0.14        2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.5.0       2021-05-25 [1] CRAN (R 4.0.3)
#>  fastmap       1.1.0       2021-01-25 [1] CRAN (R 4.0.2)
#>  fs            1.5.0       2020-07-31 [1] CRAN (R 4.0.2)
#>  generics      0.1.1       2021-10-25 [1] CRAN (R 4.0.2)
#>  glue          1.6.0       2021-12-17 [1] CRAN (R 4.0.2)
#>  highr         0.9         2021-04-16 [1] CRAN (R 4.0.2)
#>  htmltools     0.5.2       2021-08-25 [1] CRAN (R 4.0.2)
#>  knitr         1.37        2021-12-16 [1] CRAN (R 4.0.2)
#>  lifecycle     1.0.1       2021-09-24 [1] CRAN (R 4.0.2)
#>  magrittr      2.0.1       2020-11-17 [1] CRAN (R 4.0.2)
#>  pillar        1.6.4       2021-10-18 [1] CRAN (R 4.0.2)
#>  pkgconfig     2.0.3       2019-09-22 [1] CRAN (R 4.0.0)
#>  purrr         0.3.4       2020-04-17 [1] CRAN (R 4.0.0)
#>  R.cache       0.15.0      2021-04-30 [1] CRAN (R 4.0.2)
#>  R.methodsS3   1.8.1       2020-08-26 [1] CRAN (R 4.0.2)
#>  R.oo          1.24.0      2020-08-26 [1] CRAN (R 4.0.2)
#>  R.utils       2.11.0      2021-09-26 [1] CRAN (R 4.0.2)
#>  R6            2.5.1       2021-08-19 [1] CRAN (R 4.0.2)
#>  reprex        2.0.1.9000  2021-10-01 [1] Github (tidyverse/reprex@9ca939f)
#>  rlang         0.99.0.9003 2022-01-03 [1] Github (r-lib/rlang@ce4aca5)
#>  rmarkdown     2.11        2021-09-14 [1] CRAN (R 4.0.2)
#>  rstudioapi    0.13        2020-11-12 [1] CRAN (R 4.0.2)
#>  sessioninfo   1.2.1       2021-11-02 [1] CRAN (R 4.0.2)
#>  stringi       1.7.6       2021-11-29 [1] CRAN (R 4.0.3)
#>  stringr       1.4.0       2019-02-10 [1] CRAN (R 4.0.2)
#>  styler        1.6.2       2021-09-23 [1] CRAN (R 4.0.2)
#>  tibble        3.1.6       2021-11-07 [1] CRAN (R 4.0.3)
#>  tidyselect    1.1.1       2021-04-30 [1] CRAN (R 4.0.2)
#>  utf8          1.2.2       2021-07-24 [1] CRAN (R 4.0.2)
#>  vctrs         0.3.8       2021-04-29 [1] CRAN (R 4.0.2)
#>  withr         2.4.3       2021-11-30 [1] CRAN (R 4.0.2)
#>  xfun          0.29        2021-12-14 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1       2020-02-01 [1] CRAN (R 4.0.0)
#> 
#>  [1] /Users/romainfrancois/.R/library/4.0
#>  [2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Copy link
Member

@romainfrancois romainfrancois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will ultimately redesign these in funs:: but the change seems harmless.

@romainfrancois
Copy link
Member

Any objection to include this in 1.0.8 @DavisVaughan ?

Copy link
Member

@DavisVaughan DavisVaughan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be honest, I am slightly nervous about this because I think if_else() and case_when() need to be fully rewritten with vctrs to truly support all types correctly.

I would run revdeps against this and see if it breaks anything. If any new breakage pops up, I feel like it would make more sense to hold off until we rewrite them from scratch in funs - perhaps using this gist of mine as the starting point https://gist.github.com/DavisVaughan/282ea2cfb88e0938bf0b655014d22c55

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

Successfully merging this pull request may close these issues.

3 participants