Pass an equation as an argument of a sub-function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Pass an equation as an argument of a sub-function

Frank S.
Dear all,

I have defined an R function g(y) wich in turn is inside other R function f(x). The function g(y) depends on an
equation, and I would like to know if such an equation could be passed as an argument of the main function
(taking into account that we should change the variable "x" to "y").
As an example, I have:

function(x) {         # Main function, called f(x)

  -----  (code)

  function(y) {        # Sub-function, called g(y)
    -----  (code)
    eq <- y^2 -3*y
    ----- (code)
  }

  ----- (code)

}

In summary, I would like to know is there is any way to:
Put the equation "eq" as an argument of the main function f(x).


Thank you very much!

Frank

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Pass an equation as an argument of a sub-function

Bert Gunter-2
See here for almost the same issue:

http://r.789695.n4.nabble.com/Pasting-R-code-lines-into-labels-td4757446.html

Same answer: pass the unevaluated formula (i.e. an R expression) using
substitute/quote. Then evaluate it appropriately using eval. Same
references.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jun 6, 2019 at 11:08 AM Frank S. <[hidden email]> wrote:

> Dear all,
>
> I have defined an R function g(y) wich in turn is inside other R function
> f(x). The function g(y) depends on an
> equation, and I would like to know if such an equation could be passed as
> an argument of the main function
> (taking into account that we should change the variable "x" to "y").
> As an example, I have:
>
> function(x) {         # Main function, called f(x)
>
>   -----  (code)
>
>   function(y) {        # Sub-function, called g(y)
>     -----  (code)
>     eq <- y^2 -3*y
>     ----- (code)
>   }
>
>   ----- (code)
>
> }
>
> In summary, I would like to know is there is any way to:
> Put the equation "eq" as an argument of the main function f(x).
>
>
> Thank you very much!
>
> Frank
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Pass an equation as an argument of a sub-function

Bert Gunter-2
It is perhaps worth saying that my prior suggestion may not be the best
strategy for doing what you want. The previous poster needed the actual
expression to deparse as a label. If you only need the expression as a
function to be evaluated, it may be better to pass the argument as a
function -- a feature of functional programming languages like R. For
example:

top <- function(x, fun)fun(x)
f <- function(x)x^2 + sin(x)
top(1:4, f)

Again, appropriate sections of the R Language Reference or web tutorials
provide details.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jun 6, 2019 at 11:24 AM Bert Gunter <[hidden email]> wrote:

> See here for almost the same issue:
>
>
> http://r.789695.n4.nabble.com/Pasting-R-code-lines-into-labels-td4757446.html
>
> Same answer: pass the unevaluated formula (i.e. an R expression) using
> substitute/quote. Then evaluate it appropriately using eval. Same
> references.
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Thu, Jun 6, 2019 at 11:08 AM Frank S. <[hidden email]> wrote:
>
>> Dear all,
>>
>> I have defined an R function g(y) wich in turn is inside other R function
>> f(x). The function g(y) depends on an
>> equation, and I would like to know if such an equation could be passed as
>> an argument of the main function
>> (taking into account that we should change the variable "x" to "y").
>> As an example, I have:
>>
>> function(x) {         # Main function, called f(x)
>>
>>   -----  (code)
>>
>>   function(y) {        # Sub-function, called g(y)
>>     -----  (code)
>>     eq <- y^2 -3*y
>>     ----- (code)
>>   }
>>
>>   ----- (code)
>>
>> }
>>
>> In summary, I would like to know is there is any way to:
>> Put the equation "eq" as an argument of the main function f(x).
>>
>>
>> Thank you very much!
>>
>> Frank
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.