Recall that {{
is shorthand for !!enquo()
. Since you want to quote either cols
or .vars
, depending on which one is missing, I suggest doing !!
and enquo()
separately:
my_fun = function(df, cols, .vars = deprecated()){
if (quo_is_missing(enquo(cols)) && !quo_is_missing(enquo(.vars))) {
deprecate_warn("0.1.6", "my_fun(.vars=)", "my_fun(cols=)")
cols <- enquo(.vars) # Quote .vars, if cols is missing
}
else cols <- enquo(cols) # Quote cols, if cols is not missing
select(df, !!cols) # Unquote with !!, instead of {{, which is !!enquo()
}
my_fun(iris, cols=Sepal.Length) %>% head() # Works
my_fun(iris, .vars=Sepal.Length) %>% head() # Also works
If you absolutely must use {{
, the only way to modify the expression that gets captured is to change how the function is called. This can be done with a little bit of recursion (i.e., having my_fun
call itself):
my_fun = function(df, cols, .vars = deprecated()){
if (quo_is_missing(enquo(cols)) && !quo_is_missing(enquo(.vars))) {
deprecate_warn("0.1.6", "my_fun(.vars=)", "my_fun(cols=)")
return( my_fun(df, {{.vars}}) ) # .vars will be captured as cols
}
select(df, {{cols}})
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…