tidyverse / purrr

A functional programming toolkit for R
https://purrr.tidyverse.org/
Other
1.27k stars 274 forks source link

FR: make `list_modify()` aware of pipe's LHS variables #1052

Closed DanChaltiel closed 1 year ago

DanChaltiel commented 1 year ago

Hi,

list_modify() is a wonderful tool, but it does not keep a reference to the LHS of the pipe.

Therefore, to use it like dplyr::mutate(), you should use the . placeholder with magrittr pipes.

library(tidyverse)
lst(x = 1) %>% 
  list_modify(x2 = .$x+1)
#> $x
#> [1] 1
#> 
#> $x2
#> [1] 2

Created on 2023-01-31 with reprex v2.0.2

However, this is not compatible with native pipe for R versions <4.2 (which have no placeholder). A purist would even go further and advocate against placeholders per se. Note that many people are stuck with R v4.1 due to external limitations such as with rstan.

It would be great if you could write it lst(x = 1) %>% list_modify(x2 = x+1), just like you would do with dplyr::mutate().

Do you think that is doable?

hadley commented 1 year ago

That was basically update_list() which we just deprecated. It just doesn't feel like a good fit for purrr to me.