In Tidy Evaluation the examples show that, in order to use a variable on the LHS of an assignment, we have to use as_label() and enquo().
Yet, I'm finding that the following seems to work ok:
addSma = function( data, period ) {
colName = paste0("smaCl_", period)
ts2 = data %>%
dplyr::mutate( !!colName := SMA(close, n=period ) )
return( ts2 )
}
I'm failing to understand quite what as_label() and enquo() do, and when they are actually required.
Besides support for unquoting (and now for glue strings), the LHS of
:=works the same way as=: it expects symbols or strings.There is no requirement to use
enquo(), you can unquote any string on the LHS of:=.When you do use
enquo()on a function argument, the result can be any sort of R object. It is usually a symbol or a call, but it can also be a number or a string like1or"foo". And if the user called!!it really could be anything. In this case, if you need a string to represent this argument that you've captured and which could be anything, you can useas_label(). It is well defined for any R objects and is guaranteed to return a single string representing the object informatively.