Home > Blockchain >  Replace cur_data() with something else but not pick()
Replace cur_data() with something else but not pick()

Time:01-05

I get a warning (see below) for cur_data() but I am not sure how to use pick() instead. I don't use it to access columns but to refer to the current dataset in the pipe.

You can use tidylog::filter() to see that 5 rows are dropped.

library(dplyr)

VARS <- c("mpg", "disp")

mtcars |> 
  filter(if_all(all_of(VARS), 
   ~ .x >= min( filter(cur_data(), as.logical(am)) |> pull(.x) ))) 
# Warning message:
#   There was 1 warning in `filter()`.
# ℹ In argument `..1 = ... & ...`.
# Caused by warning:
#   ! `cur_data()` was deprecated in dplyr 1.1.0.
# ℹ Please use `pick()` instead.

mtcars |> 
  filter(if_all(all_of(VARS), 
     ~ .x >= min( filter(.data, as.logical(am)) |> pull(.x) ))) 
# fails


sessionInfo()
# R version 4.2.0 (2022-04-22)
# Platform: aarch64-apple-darwin20 (64-bit)
# Running under: macOS 13.1
# 
# Matrix products: default
# LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] dplyr_1.0.99.9000

CodePudding user response:

If it is a single column, then we don't need cur_data(), just convert am to logical to subset ([) the column, get the minmum can check whether the column value is greater than or equal to the column value

library(dplyr)
out2 <- mtcars %>% 
    filter(if_all(all_of(VARS), ~ .x >= min(.x[as.logical(am)] )))

-checking with OP's output

> out1 <- mtcars |> 
    filter(if_all(all_of(VARS), 
     ~ .x >= min( filter(cur_data(), as.logical(am)) |> pull(.x) ))) 
> all.equal(out1, out2)
[1] TRUE

If we need to use pick (from the devel version of dplyr)

out3 <- mtcars |> 
  filter(if_all(all_of(VARS), 
   ~ .x >= min( filter(pick(everything()), 
     as.logical(am)) |> pull(.x) )))

-checking

> all.equal(out2, out3)
[1] TRUE
  • Related