在 dplyr 的 left_join 中使用 as_label / as_name 而不是 quo_name 加入两个数据集

Joining two data sets using as_label / as_name instead of quo_name within dplyr's left_join

根据 的答案,建议使用:quo_name 以便使用 quosures 连接表;我想使用 as_name / as_label 得到相同的结果,因为 quo_name 目前处于质疑生命周期阶段

These functions are in the questioning life cycle stage.

as_label() and as_name() should be used instead of quo_name(). as_label() transforms any R object to a string but should only be used to create a default name. Labelisation is not a well defined operation and no assumption should be made about the label. On the other hand, as_name() only works with (possibly quosured) symbols, but is a well defined and deterministic operation.

例子

library("tidyverse")

data_a <- tibble(col_ltr = letters, col_nums = seq_along(letters))
data_b <- tibble(col_ltr = letters, col_nums = seq_along(letters) * -1)


clean_and_join <-
    function(data_one,
             data_two,
             column_id_one,
             column_id_two,
             col_nums_one,
             col_nums_two) {

        clean_data_one <- filter(data_one, {{col_nums_one}} %% 2 == 0)
        clean_data_two <- filter(data_two, {{col_nums_two}} %% 2 != 0)

        by_cols <- set_names(as_label({{column_id_one}}), as_label({{column_id_two}}))

        left_join(
            x = clean_data_one,
            y = clean_data_two,
            by = by_cols
        )
    }

clean_and_join(data_one = data_a, data_two = data_b, column_id_one = col_ltr,
               column_id_two = col_ltr, col_nums_one = col_nums,
               col_nums_two = col_nums)

错误

Error in is_quosure(x) : object 'col_ltr' not found

想要的结果

left_join(
    x = clean_data_one,
    y = clean_data_two,
    by = c("col_ltr" = "col_ltr") # Or by = "col_ltr" in case of identical name
)

一个选项将转换为 symbol,然后使用 as_string

转换为字符串
clean_and_join <-
    function(data_one,
             data_two,
             column_id_one,
             column_id_two,
             col_nums_one,
             col_nums_two) {

        clean_data_one <- filter(data_one, {{col_nums_one}} %% 2 == 0)
        clean_data_two <- filter(data_two, {{col_nums_two}} %% 2 != 0)

        by_cols <- set_names(rlang::as_string(rlang::ensym(column_id_one)), 
                 rlang::as_string(rlang::ensym(column_id_two)))

        left_join(
            x = clean_data_one,
            y = clean_data_two,
            by = by_cols
        )
    }
  

-测试

clean_and_join(data_one = data_a, data_two = data_b, column_id_one = col_ltr,
                column_id_two = col_ltr, col_nums_one = col_nums,
                col_nums_two = col_nums)
# A tibble: 13 x 3
   col_ltr col_nums.x col_nums.y
#   <chr>        <int>      <dbl>
# 1 b                2         NA
# 2 d                4         NA
# 3 f                6         NA
# 4 h                8         NA
# 5 j               10         NA
# 6 l               12         NA
# 7 n               14         NA
# 8 p               16         NA
# 9 r               18         NA
#10 t               20         NA
#11 v               22         NA
#12 x               24         NA
#13 z               26         NA