R 如何在另一个函数中将函数作为字符串传递 [英] R How to Pass a function as a String Inside another Function

查看:33
本文介绍了R 如何在另一个函数中将函数作为字符串传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对这个小难题的任何帮助将不胜感激,谢谢.

Any assistance on this little conundrum would be mightily appreciated thanks.

我正在尝试将参数传递给 tidyquant 包中的 tq_transmute 函数;参数的值是一个函数,但是我想将它作为字符串传递(在下面示例的范围之外,我将通过 Shiny selectInput 传递它).

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).

我已经尝试了所有我能想到的方法将字符串 'apply.quarterly' 转换为 mutate_fun 参数接受的对象 apply.quarterly.注释行是我失败的尝试.

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.

最终,我想将此概念扩展到其他参数,即 FUN = maxFUN = ‘max’.

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

myfun <- "apply.quarterly"
bquote(FANG %>%
  group_by(symbol) %>%
  tq_transmute(select     = adjusted, 
               mutate_fun = .(as.name(myfun)),
               FUN        = max, 
               col_rename = "max.close")) %>% 
  eval()

或者如果您更喜欢 rlang 语法

or if you prefer rlang syntax

myfun <- "apply.quarterly"
quo(FANG %>%
         group_by(symbol) %>%
         tq_transmute(select     = adjusted, 
                      mutate_fun = !!sym(myfun),
                      FUN        = max, 
                      col_rename = "max.close")) %>% 
  eval_tidy()

请注意,我们必须将整个表达式视为 rlang quosure.除非 tq_transmute 函数是专门为处理像 !! 这样的 rlang 特性而编写的,否则它们在默认情况下不会工作.

Note that we have to treat the entire expression as rlang quosure. Unless the tq_transmute function was specifically written to handle rlang features like !! then they won't work by default.

这篇关于R 如何在另一个函数中将函数作为字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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