rlang:在 NSE 函数中使用冒号快捷方式从...获取名称

标签 r dplyr rlang nse

我正在编写一个函数包,用于制作人口统计数据表。我有一个函数(缩写如下),我需要在其中获取几列 (...),并在其中收集 一个数据框。诀窍是我想保持这些列的名称按顺序排列,因为我需要在收集后按该顺序放置一列。在本例中,这些列是 estimatemoesharesharemoe

library(tidyverse)
library(rlang)

race <- structure(list(region = c("New Haven", "New Haven", "New Haven", "New Haven", "Outer Ring", "Outer Ring", "Outer Ring", "Outer Ring"), 
    variable = c("white", "black", "asian", "latino", "white", "black", "asian", "latino"), 
    estimate = c(40164, 42970, 6042, 37231, 164150, 3471, 9565, 8518), 
    moe = c(1395, 1383, 697, 1688, 1603, 677, 896, 1052), 
    share = c(0.308, 0.33, 0.046, 0.286, 0.87, 0.018, 0.051, 0.045), 
    sharemoe = c(0.011, 0.011, 0.005, 0.013, 0.008, 0.004, 0.005, 0.006)), 
    class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -8L))

race
#> # A tibble: 8 x 6
#>   region     variable estimate   moe share sharemoe
#>   <chr>      <chr>       <dbl> <dbl> <dbl>    <dbl>
#> 1 New Haven  white       40164  1395 0.308    0.011
#> 2 New Haven  black       42970  1383 0.33     0.011
#> 3 New Haven  asian        6042   697 0.046    0.005
#> 4 New Haven  latino      37231  1688 0.286    0.013
#> 5 Outer Ring white      164150  1603 0.87     0.008
#> 6 Outer Ring black        3471   677 0.018    0.004
#> 7 Outer Ring asian        9565   896 0.051    0.005
#> 8 Outer Ring latino       8518  1052 0.045    0.006

在函数 gather_arrange 中,我通过映射 rlang::exprs(...)< 来获取 ... 列的名称 并转换为字符。将这些列的名称提取为字符串是一项艰巨的任务,因此这可能是一个需要改进或重写的地方。但这按照我想要的方式工作,使列type作为一个因素,其级别为estimatemoesharesharemoe 按此顺序。

gather_arrange <- function(df, ..., group = variable) {
  gather_cols <- rlang::quos(...)
  grp_var <- rlang::enquo(group)
  gather_names <- purrr::map_chr(rlang::exprs(...), as.character)

  df %>%
    tidyr::gather(key = type, value = value, !!!gather_cols) %>%
    dplyr::mutate(!!rlang::quo_name(grp_var) := !!grp_var %>% 
                  forcats::fct_inorder() %>% forcats::fct_rev()) %>%
    dplyr::mutate(type = as.factor(type) %>% forcats::fct_relevel(gather_names)) %>%
    arrange(type)
}

race %>% gather_arrange(estimate, moe, share, sharemoe)
#> # A tibble: 32 x 4
#>    region     variable type      value
#>    <chr>      <fct>    <fct>     <dbl>
#>  1 New Haven  white    estimate  40164
#>  2 New Haven  black    estimate  42970
#>  3 New Haven  asian    estimate   6042
#>  4 New Haven  latino   estimate  37231
#>  5 Outer Ring white    estimate 164150
#>  6 Outer Ring black    estimate   3471
#>  7 Outer Ring asian    estimate   9565
#>  8 Outer Ring latino   estimate   8518
#>  9 New Haven  white    moe        1395
#> 10 New Haven  black    moe        1383
#> # ... with 22 more rows

但我希望还可以使用冒号表示法来选择列,即 estimate:sharemoe 来执行与输入所有这些列名称相同的操作。

race %>% gather_arrange(estimate:sharemoe)
#> Error: Result 1 is not a length 1 atomic vector

此操作失败,因为它无法从 rlang::exprs(...) 中提取列名称。如何使用此表示法获取列名称?提前致谢!

最佳答案

我认为您正在寻找的功能是 tidyselect::vars_select() ,它由 select 和 rename 在内部使用来完成此任务。它返回变量名称的字符向量。例如:

> tidyselect::vars_select(letters, g:j)
  g   h   i   j 
"g" "h" "i" "j"

这允许您使用对 dplyr::select 有效的所有相同语法。

关于rlang:在 NSE 函数中使用冒号快捷方式从...获取名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50555526/

相关文章:

r - 向分组 fiddle 图添加均值

r - 如何在回收值时将数据帧的每一行除以数据帧中相应列的行

java - 如何添加到 HashMap 中的强制列表?

r - 从 rlang 0.3.0 和 mutate_impl 起已失效

mysql - 如何在MySql或R中获取天数桶的计数

r - 如何在 R 中交换部分字符串?

r - 贬低 R data.table : list of columns

R ggtree : How to label single tree tip with ggtree similar to labeling nodes with geom_cladelabel

r - dplyr/rlang : parse_expr with multiple expressions

r - 用于编写函数的rlang运算符的解释