根据关于使用 quosure 作为 by 参数连接数据集的答案,建议使用:quo_name
为了使用 quosures 连接表;我想使用as_name
/ as_label
as得到相同quo_name
的结果,目前处于质疑生命周期阶段
这些功能处于质疑生命周期阶段。
as_label()
并且as_name()
应该用来代替 quo_name()。as_label()
将任何 R 对象转换为字符串,但只能用于创建默认名称。标签化不是一个定义明确的操作,不应对标签做出任何假设。另一方面, as_name() 仅适用于(可能是quosured)符号,但它是一个定义明确且确定性的操作。
例子
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)
错误
错误
is_quosure(x)
:'col_ltr'
找不到对象
期望的结果
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
)