Reputation: 35
I am trying to develop a UDF function in R, which should work like this:
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.
Upvotes: 1
Views: 80
Reputation: 887501
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
Upvotes: 2