I am trying to search a database and then label the ouput with a name derived from the original search, "derived_name"
in the reproducible example below. I am using a dplyr
pipe %>%
, and I am having trouble with quasiquotation and/or non-standard evaluation. Specifically, using count_colname
, a character object derived from "derived_name"
, in the final top_n()
function fails to subset the dataframe.
search_name <- "derived_name"
set.seed(1)
letrs <- letters[rnorm(52, 13.5, 5)]
letrs_count.df <- letrs %>%
table() %>%
as.data.frame()
count_colname <- paste0(search_name, "_letr_count")
colnames(letrs_count.df) <- c("letr", count_colname)
letrs_top.df <- letrs_count.df %>%
top_n(5, count_colname)
identical(letrs_top.df, letrs_count.df)
# [1] TRUE
Based on this discussion I thought the code above would work. And this post lead me to try top_n_()
, which does not seem to exist.
I am studying vignette("programming")
which is a little over my head. This post led me to try the !! sym()
syntax, which works, but I have no idea why! Help understanding why the below code works would be much appreciated. Thanks.
colnames(letrs_count.df) <- c("letr", count_colname)
letrs_top.df <- letrs_count.df %>%
top_n(5, (!! sym(count_colname)))
letrs_top.df
# letr derived_name_letr_count
# 1 l 5
# 2 m 6
# 3 o 7
# 4 p 5
# 5 q 6
Additional confusing examples based on @lionel and @Tung's questions and comments below. What is confusing me here is that the help fils say that sym()
"take strings as input and turn them into symbols" and !!
"unquotes its argument". However, in the examples below, sym(count_colname)
appears to unquote to derived_name_letr_count
. I do not understand why the !!
is needed in !! sym(count_colname)
, since sym(count_colname)
and qq_show(!! sym(count_colname))
give the same value.
count_colname
# [1] "derived_name_letr_count"
sym(count_colname)
# derived_name_letr_count
qq_show(count_colname)
# count_colname
qq_show(sym(count_colname))
# sym(count_colname)
qq_show(!! sym(count_colname))
# derived_name_letr_count
qq_show(!! count_colname)
# "derived_name_letr_count"
dplyr
automatically quotes its inputs. Here is the source code oftop_n
: it usesenquo
&!!
to quote and unquote the inputs as well. Runqq_show(!!quo(sym(count_colname)))
to see why you need to unquotesym(count_colname)
with!!
first before supplying totop_n
– Castroprauxel