dplyr()中的非标准评估和准引用无法正常运行(天真) [英] Non-standard evaluation and quasiquotation in dplyr() not working as (naively) expected

查看:108
本文介绍了dplyr()中的非标准评估和准引用无法正常运行(天真)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索数据库,然后使用以下原始示例中的原始搜索"derived_name"的名称标记输出.我正在使用dplyr管道%>%,并且在准报价和/或非标准评估方面遇到问题.具体来说,使用count_colname(在最后一个top_n()函数中从"derived_name"派生的字符对象)无法对数据帧进行子集化.

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

基于此讨论我认为上面的代码可以工作.而这篇文章使我尝试了top_n_(),这似乎并没有存在.

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.

我正在学习vignette("programming"),这有点烦人. 这篇文章使我尝试了!! sym()语法,该语法有效,但是我有不知道为什么!帮助理解下面的代码为什么起作用,将不胜感激.谢谢.

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

以下基于@lionel和@Tung的问题和评论的其他令人困惑的示例.这里让我感到困惑的是,帮助文件说sym()将字符串作为输入并将其转换为符号"和!!取消引用其参数".但是,在下面的示例中,sym(count_colname)似乎取消了对derived_name_letr_count的引用.我不明白为什么!! sym(count_colname)需要!!,因为sym(count_colname)qq_show(!! sym(count_colname))给出相同的值.

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"

推荐答案

因此,我意识到我在这个问题(以及许多其他问题)中苦苦挣扎的并不是真正的准报价和/或非标准评估,而是将字符串转换为对象名称.这是我的新解决方案:

So, I've realized that what I was struggling with in this question (and many other probelms) is not really quasiquotation and/or non-standard evaluation, but rather converting character strings into object names. Here is my new solution:

letrs_top.df <- letrs_count.df %>%
    top_n(5, get(count_colname))

这篇关于dplyr()中的非标准评估和准引用无法正常运行(天真)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆