【问题标题】:R: Turn content of string into body of functionR:将字符串的内容转换为函数体
【发布时间】:2016-06-19 19:14:29
【问题描述】:

我写了一个函数,它根据某些属性返回一个函数。 问题是我不得不使用“粘贴”,这使得返回的对象成为一个字符串。这里是返回对象的示例:

  the_problem <- "beta['v_p'] * 0.1 * ((3.99 * exp(-0.144 * time)) +   
                 (4.78 *  exp(-0.0111 * time))) + 0.1 * beta['ktrans_1'] * (3.99 * (exp(-
     beta['kep_1']* time) - exp(- 0.144 * time)) * (0.144 - 
     beta['kep_1'])**(-1) + (4.78 * (exp(- beta['kep_1'] * time)- exp(- 
     0.0111 * time)) * (0.0111 - beta['kep_1'])**(-1)))"

我想操纵对象“the_problem”以使其可用作函数。比如:

dcemri_func <- function(beta){
  return(get.rid.of.string(the_problem))}

我已经尝试过“as.function”,但这不起作用。 有什么想法吗?

谢谢,祝你好运,

克里斯

【问题讨论】:

  • body(dcemri_func) &lt;- parse(text = the_problem) ?

标签: r string function


【解决方案1】:

您可以尝试使用eval(parse(text = ..)),即:

 the_problem <- paste0('function(beta) ', the_problem)
 dcemri_func <- eval(parse(text = the_problem))

【讨论】:

  • 这就是我想要的!谢谢!
  • 感谢您的来信!如果问题解决了,请考虑通过点击投票旁边的勾号来接受解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多