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

发布于 2025-02-01 19:41:27 字数 721 浏览 1 评论 0原文

我正在尝试在R中开发一个UDF函数,该功能应该像这样起作用:

  • 用户编写数学方程(例如 2*x + 4
  • 我的函数应在R功能中转换输入并集成它具有下限/上限( 0和1 ),

因此,我的功能(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)

错误:

集成中的错误(user_input,下=低,upper = up): 功能的评估给出了错误长度

的结果

,我可以做以下操作:

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

但这不是我想要的。

我会为一些提示感到非常高兴。

I am trying to develop a UDF function in R, which should work like this:

  • user write a mathematical equation (eg 2*x + 4)
  • My function should convert the input in a R-function and integrate it with lower/upper limits (0 and 1)

so, myfunction(2*x+4) have to solve this problem and show 5 as output.

What I've tried:

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:

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


sure, I could do the following:

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

but thats not what I am looking for.

I would be very happy about some hints.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2025-02-08 19:41:27

我们可以进行

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

We can do

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)

   }

-testing

myfunction(2*x+4, low = 0, up = 1)
5 with absolute error < 5.6e-14
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文