使用 dplyr 和条件管道运算符评估提取列索引时出错

Error when extracting column indices with dplyr and conditional pipe operator evaluation

以下两个操作应该都导致 mtcars 数据集的所有数值变量的索引。然而,使用管道运算符对数据集进行条件评估的操作会产生错误。谁能解释一下为什么?

MWE :

data(mtcars)
require(dplyr)
require(stringr)
mtcars %>% {grep(pattern = . %>% select_if(.predicate = is.numeric ) %>% 
                              colnames %>%
                              str_flatten('|'),x = colnames(.))}

输出:

Error in as.vector(x, "character") : cannot coerce type 'closure' to vector of type 'character'

data(mtcars)
require(dplyr)
require(stringr)
grep(pattern = mtcars %>% select_if(.predicate = is.numeric ) %>% 
                                  colnames %>%
                                  str_flatten('|'),x = colnames(mtcars))

提供预期的输出:[1] 1 2 3 4 5 6 7 8 9 10 11

Session 信息

> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringr_1.4.0 dplyr_1.0.7  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7        rstudioapi_0.13   magrittr_2.0.1    tidyselect_1.1.1  munsell_0.5.0     colorspace_1.4-1  R6_2.5.1          rlang_0.4.11.9001 fansi_0.5.0       tools_4.0.5       grid_4.0.5       
[12] gtable_0.3.0      GauPro_0.2.4      utf8_1.2.2        cli_3.0.1         DBI_1.1.0         ellipsis_0.3.2    assertthat_0.2.1  tibble_3.1.5      lifecycle_1.0.1   crayon_1.4.1      purrr_0.3.4      
[23] ggplot2_3.3.5     vctrs_0.3.8       rpart_4.1-15      glue_1.4.2        stringi_1.4.6     compiler_4.0.5    pillar_1.6.4      generics_0.1.0    scales_1.1.1      pkgconfig_2.0.3 

我们可以在 select 中使用带有 where 的管道,因为 _if/_at 已弃用

library(dplyr)
library(stringr)
mtcars %>% 
 select(where(is.numeric)) %>% 
 names %>%
 str_flatten('|') %>%
 grep(x = colnames(mtcars))

-输出

[1]  1  2  3  4  5  6  7  8  9 10 11

因为我们使用 stringrstr_which 会给出与 grep

相同的索引输出
mtcars %>% 
  select(where(is.numeric)) %>% 
  names %>%
  str_flatten('|') %>%
  str_which(string = names(mtcars))

-输出

[1]  1  2  3  4  5  6  7  8  9 10 11

在 OP 的代码中,. 可以包含在 {}

mtcars %>%
   {grep(pattern = {.} %>% 
           select_if(.predicate = is.numeric ) %>% 
           colnames %>%
           str_flatten('|'),x = colnames(.))
    }

-输出

[1]  1  2  3  4  5  6  7  8  9 10 11