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?
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
No difference between as.name
and sym
for several examples I run.
iris %>%
group_by(!!as.name(varName))
iris %>%
group_by(!!sym(varName))
Attempts to change encoding
:
var1 <- `Encoding<-`(varName, "unknown")
Both as.name
and sym
still worked.
Original answer:
From the docs?sym
:
These functions take strings as input and turn them into symbols.
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()
).
It therefore seems that using sym
and related functions eliminates the trouble to do with encoding.