Following from the answer on join datasets using a quosure as the by argument which suggests using: quo_name
in order to join tables using quosures; I would like to arrive at identical result using as_name
/ as_label
as quo_name
's is currently within questioning lifecycle stage
These functions are in the questioning life cycle stage.
as_label()
andas_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.
Example
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
Error in
is_quosure(x)
: object'col_ltr'
not found
Desired results
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
)
An option would to convert to
sym
bol and then to string withas_string
-testing