如何在 R 函数中转换用户输入?

How can I convert a user-input in a function in R?

我正在尝试在 R 中开发一个 UDF 函数,它应该像这样工作:

所以,myfunction(2*x+4) 必须解决这个问题并显示 5 作为输出。

我试过的:

myfunction <- function(x, low, up) {

        user_input <- function(x){}
        res <- integrate(user_input, lower = low, upper = up)
        
return(res)
   }

myfunction(2*x+4, low = 0, up = 1)

错误:

Error in integrate(user_input, lower = low, upper = up) : evaluation of function gave a result of wrong length


当然,我可以执行以下操作:

integrand <- function(x){2*x+4}
integrate(integrand, lower = 0, upper = 1)

但这不是我要找的。

我会很高兴得到一些提示。

我们可以做到

myfunction <- function(y, low, up) {

        user_input <- as.function(c(alist(x = ), list(substitute(y))))
        res <- integrate(user_input, lower = low, upper = up)
        
return(res)

   }

-测试

myfunction(2*x+4, low = 0, up = 1)
5 with absolute error < 5.6e-14