Here is one possible solution:
ifun <-
function(a, b, FUN){
evala <- FUN(a)
evalb <- FUN(b)
if (evala > evalb) return(evala) else return(evalb)
}
ifun(1,2,function(x) (x*x) - 2)
On 7/24/07, Anup Nandialath <anup_nandialath at yahoo.com>
wrote:> Friends,
>
> I'm trying to pass an equation as an argument to a function. The idea
is as follows. Let us say i write an independent function
>
> Ideal Situation:
>
> ifunc <- function(x)
> {
> return((x*x)-2)
> }
>
> mainfunc <- function(a,b)
> {
> evala <- ifunc(a)
> evalb <- ifunc(b)
> if (evala>evalb){return(evala)}
> else
> return(evalb)
> }
>
> Now I want to try and write this entire program in a single function with
the user specifying the equation as an argument to the function.
>
> myfunc <- function(a, b, eqn)
> {
> func1 <- function (x) ??????????????????
> {
> return(eqn in terms of x) ??????????????????
> }
>
> Further arguments to check
>
> The ???????? imply that this does not seem to be correct. The idea is how
to assign the equation expression from the main equation into the inner
function. Is there anyway to do that within this set up?
>
>
> Thanks in advance
> Regards
>
> Anup
>
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?