Any assistance on this little conundrum would be mightily appreciated thanks.
I am trying to pass an argument to the tq_transmute
function from the tidyquant
package; the value for the argument is a function, however I would like to pass it as a string (out with the scope of the example below I’ll be passing it via a Shiny selectInput
).
I have tried every way I can think of to turn the string 'apply.quarterly' into the object apply.quarterly
accepted by the mutate_fun
argument. The commented lines are my failed attempts.
Ultimately, I would like to extend this concept to the other arguments also i.e. FUN = max
to FUN = ‘max’
.
library(tidyverse)
library(tidyquant)
library(rlang)
FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = apply.quarterly,
# mutate_fun = sym('apply.quarterly'),
# mutate_fun = syms('apply.quarterly'),
# mutate_fun = !!sym('apply.quarterly'),
# mutate_fun = !!!sym('apply.quarterly'),
# mutate_fun = eval(parse('apply.quarterly')),
# mutate_fun = eval(substitute('apply.quarterly')),
# mutate_fun = enquo('apply.quarterly'),
# mutate_fun = expr(!!enquo('apply.quarterly')),
# mutate_fun = quo('apply.quarterly'),
# mutate_fun = enquos('apply.quarterly'),
# mutate_fun = enquote('apply.quarterly'),
# mutate_fun = quote('apply.quarterly'),
# mutate_fun = substitute('apply.quarterly'),
# mutate_fun = parse('apply.quarterly'),
# mutate_fun = parse('apply.quarterly'),
# mutate_fun = ensym('apply.quarterly'),
# mutate_fun = rlang::as_function('apply.quarterly'),
# mutate_fun = rlang::as_closure('apply.quarterly'),
# mutate_fun = rlang::as_quosure('apply.quarterly'),
# mutate_fun = rlang::as_quosure('apply.quarterly'),
# mutate_fun = enexpr('apply.quarterly'),
# mutate_fun = enexprs('apply.quarterly'),
# mutate_fun = ensym('apply.quarterly'),
# mutate_fun = ensyms('apply.quarterly'),
# mutate_fun = eval_tidy('apply.quarterly'),
# mutate_fun = exprs('apply.quarterly'),
# mutate_fun = expr_deparse('apply.quarterly'),
# mutate_fun = expr_label('apply.quarterly'),
# mutate_fun = expr_label(substitute('apply.quarterly')),
# mutate_fun = expr_label(quote('apply.quarterly')),
# mutate_fun = parse_expr('apply.quarterly'),
# mutate_fun = quasiquotation('apply.quarterly'),
# mutate_fun = quotation('apply.quarterly'),
# mutate_fun = quotation('apply.quarterly'),
FUN = max,
col_rename = "max.close")
It seems that function is a bit finicky for some reason. One way would be to change the call and then evaulate that. For example
or if you prefer rlang syntax
Note that we have to treat the entire expression as
rlang
quosure. Unless thetq_transmute
function was specifically written to handle rlang features like!!
then they won't work by default.