as.name和sym的区别是什么? [英] What's the difference between `as.name` and `sym`?

查看:61
本文介绍了as.name和sym的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力围绕标准,非标准评估,担保等问题进行探讨.在许多示例中,我看到传递的字符串变量通过 dplyr -可用形式转换为 dplyr -usable> as.name 或带有 rlang 包的 sym .

I'm trying to wrap my head around standard, non-standard evaluation, quosures, etc. In many examples, I see passed string variable being transformed into dplyr-usable form either with as.name or with sym of rlang package.

它们在管道中可以互换吗?在哪些情况下会失败?

Are they interchangeable in pipes? What are the cases that will fail for one or the other?

推荐答案

编辑:我无法轻易创建 as.name 无法正常工作的情况.哈德利似乎同意 as.name 可能有效.尽管这使用了确实很强大的 select ,但尝试使用 group_by summarise 似乎没有什么区别.

EDIT: I could not readily create a scenario where as.name fails to work. Hadley seems to agree that as.name might work. Although this is using select which really is robust, trying with group_by and summarise appears to have no difference.

严格选择 :

Robust select:

 varName <- "Sepal.Length"
 select(iris, varName) #This works

在我运行的几个示例中, as.name sym 之间没有区别.

No difference between as.name and sym for several examples I run.

iris %>%
    group_by(!!as.name(varName))
iris %>% 
    group_by(!!sym(varName))

尝试更改编码:

var1 <- `Encoding<-`(varName, "unknown") 

as.name sym 仍然有效.

原始答案:

从文档?sym :

这些函数将字符串作为输入并将其转换为符号.

These functions take strings as input and turn them into symbols.

as.name()相反,它们会事先将字符串转换为本地编码.这是必需的,因为符号会默默地删除字符串的编码标记(请参阅 set_str_encoding()).

Contrarily to as.name(), they convert the strings to the native encoding beforehand.This is necessary because symbols remove silently the encoding mark of strings (see set_str_encoding()).

因此,似乎使用 sym 和相关功能消除了编码方面的麻烦.

It therefore seems that using sym and related functions eliminates the trouble to do with encoding.

这篇关于as.name和sym的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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