How to pass an argument included in '…' through to a recursive call in R












1















I am having trouble passing arguments through a recursive call to the integrate function. Here is a simple example that illustrates my conundrum.



Consider a function, foo1, that requires a parameter x and returns some transformation of x. For instance:



foo1 = function(x, alpha = 1.5) {
return(x^alpha)
}


Now consider a function foo2 that also requires x and returns some different transformation:



foo2 = function(x, y, z) {
return(x * y * z)
}


Now consider a function bar that returns the finite integral (from x to xMax) of any foo-like function, but accepts a vector of multiple values for x:



bar = function(xVals, xMax, ..., fun) {
return(
sapply(
xVals,
function(xVal) integrate(f = fun, lower = xVal, upper = xMax, ...)$value)
)
}


Note that foo1's parameter alpha would be passed as part of the ...:



bar(10:20, 100, alpha = 1.5, fun = foo1)  


and foo2's parameters y and z would also be passed as part of the ...:



bar(10:20, 100, y = 2, z = 4, fun = foo2)


I like this arrangement because other required parameters for integrate can also be passed as part of .... For instance, to change the default value of the subdivisions parameter of the integrate function called by bar, one can simply specify:



bar(10:20, 100, y = 2, z = 4, fun = foo2, subdivisions = 200L)


Now, consider a function that integrates bar from xStart to xEnd, but accepts vectors for multiple values for xStart and xEnd. It might look like this:



integrateBar = function(xStart, xEnd, xMax, ..., fun) {
aMatrix = cbind(xStart, xEnd)
result =
apply(
aMatrix,
1,
function(xRange) integrate(bar, xRange[1], xRange[2], xMax, ..., fun = fun)$value
)
return(result)
}


So this is fine and pretty straightforward, until I want to call integrateBar but change the value of the subdivisions argument for the integrate function called within bar. If I use:



integrateBar(10:20, 40, 100, alpha = 1.5, subdivisions = 200L, fun = foo1)


the subdivisions argument is grabbed and used by the integrate function in integrateBar and is not passed through to bar. This is desirable behavior - sometimes. But sometimes I also want to be able to leave subdivisions set at the default for the integrate call in integrateBar but change it for the integrate call in bar. Can anyone tell me an elegant way to do the latter?



I'm willing to modify the formals for bar, but I'm trying not to make it too clunky because I want to call bar directly, too. It would be really nice if there were some R trick so that a subdivisions argument would be ignored by the first integrate function encountered, but available to the second.



The best solution I have is to write a wrapper for integrate that uses a parameter SUBDIVISIONS and call the wrapper from bar. If I don't get a better answer, I will post that solution.










share|improve this question





























    1















    I am having trouble passing arguments through a recursive call to the integrate function. Here is a simple example that illustrates my conundrum.



    Consider a function, foo1, that requires a parameter x and returns some transformation of x. For instance:



    foo1 = function(x, alpha = 1.5) {
    return(x^alpha)
    }


    Now consider a function foo2 that also requires x and returns some different transformation:



    foo2 = function(x, y, z) {
    return(x * y * z)
    }


    Now consider a function bar that returns the finite integral (from x to xMax) of any foo-like function, but accepts a vector of multiple values for x:



    bar = function(xVals, xMax, ..., fun) {
    return(
    sapply(
    xVals,
    function(xVal) integrate(f = fun, lower = xVal, upper = xMax, ...)$value)
    )
    }


    Note that foo1's parameter alpha would be passed as part of the ...:



    bar(10:20, 100, alpha = 1.5, fun = foo1)  


    and foo2's parameters y and z would also be passed as part of the ...:



    bar(10:20, 100, y = 2, z = 4, fun = foo2)


    I like this arrangement because other required parameters for integrate can also be passed as part of .... For instance, to change the default value of the subdivisions parameter of the integrate function called by bar, one can simply specify:



    bar(10:20, 100, y = 2, z = 4, fun = foo2, subdivisions = 200L)


    Now, consider a function that integrates bar from xStart to xEnd, but accepts vectors for multiple values for xStart and xEnd. It might look like this:



    integrateBar = function(xStart, xEnd, xMax, ..., fun) {
    aMatrix = cbind(xStart, xEnd)
    result =
    apply(
    aMatrix,
    1,
    function(xRange) integrate(bar, xRange[1], xRange[2], xMax, ..., fun = fun)$value
    )
    return(result)
    }


    So this is fine and pretty straightforward, until I want to call integrateBar but change the value of the subdivisions argument for the integrate function called within bar. If I use:



    integrateBar(10:20, 40, 100, alpha = 1.5, subdivisions = 200L, fun = foo1)


    the subdivisions argument is grabbed and used by the integrate function in integrateBar and is not passed through to bar. This is desirable behavior - sometimes. But sometimes I also want to be able to leave subdivisions set at the default for the integrate call in integrateBar but change it for the integrate call in bar. Can anyone tell me an elegant way to do the latter?



    I'm willing to modify the formals for bar, but I'm trying not to make it too clunky because I want to call bar directly, too. It would be really nice if there were some R trick so that a subdivisions argument would be ignored by the first integrate function encountered, but available to the second.



    The best solution I have is to write a wrapper for integrate that uses a parameter SUBDIVISIONS and call the wrapper from bar. If I don't get a better answer, I will post that solution.










    share|improve this question



























      1












      1








      1








      I am having trouble passing arguments through a recursive call to the integrate function. Here is a simple example that illustrates my conundrum.



      Consider a function, foo1, that requires a parameter x and returns some transformation of x. For instance:



      foo1 = function(x, alpha = 1.5) {
      return(x^alpha)
      }


      Now consider a function foo2 that also requires x and returns some different transformation:



      foo2 = function(x, y, z) {
      return(x * y * z)
      }


      Now consider a function bar that returns the finite integral (from x to xMax) of any foo-like function, but accepts a vector of multiple values for x:



      bar = function(xVals, xMax, ..., fun) {
      return(
      sapply(
      xVals,
      function(xVal) integrate(f = fun, lower = xVal, upper = xMax, ...)$value)
      )
      }


      Note that foo1's parameter alpha would be passed as part of the ...:



      bar(10:20, 100, alpha = 1.5, fun = foo1)  


      and foo2's parameters y and z would also be passed as part of the ...:



      bar(10:20, 100, y = 2, z = 4, fun = foo2)


      I like this arrangement because other required parameters for integrate can also be passed as part of .... For instance, to change the default value of the subdivisions parameter of the integrate function called by bar, one can simply specify:



      bar(10:20, 100, y = 2, z = 4, fun = foo2, subdivisions = 200L)


      Now, consider a function that integrates bar from xStart to xEnd, but accepts vectors for multiple values for xStart and xEnd. It might look like this:



      integrateBar = function(xStart, xEnd, xMax, ..., fun) {
      aMatrix = cbind(xStart, xEnd)
      result =
      apply(
      aMatrix,
      1,
      function(xRange) integrate(bar, xRange[1], xRange[2], xMax, ..., fun = fun)$value
      )
      return(result)
      }


      So this is fine and pretty straightforward, until I want to call integrateBar but change the value of the subdivisions argument for the integrate function called within bar. If I use:



      integrateBar(10:20, 40, 100, alpha = 1.5, subdivisions = 200L, fun = foo1)


      the subdivisions argument is grabbed and used by the integrate function in integrateBar and is not passed through to bar. This is desirable behavior - sometimes. But sometimes I also want to be able to leave subdivisions set at the default for the integrate call in integrateBar but change it for the integrate call in bar. Can anyone tell me an elegant way to do the latter?



      I'm willing to modify the formals for bar, but I'm trying not to make it too clunky because I want to call bar directly, too. It would be really nice if there were some R trick so that a subdivisions argument would be ignored by the first integrate function encountered, but available to the second.



      The best solution I have is to write a wrapper for integrate that uses a parameter SUBDIVISIONS and call the wrapper from bar. If I don't get a better answer, I will post that solution.










      share|improve this question
















      I am having trouble passing arguments through a recursive call to the integrate function. Here is a simple example that illustrates my conundrum.



      Consider a function, foo1, that requires a parameter x and returns some transformation of x. For instance:



      foo1 = function(x, alpha = 1.5) {
      return(x^alpha)
      }


      Now consider a function foo2 that also requires x and returns some different transformation:



      foo2 = function(x, y, z) {
      return(x * y * z)
      }


      Now consider a function bar that returns the finite integral (from x to xMax) of any foo-like function, but accepts a vector of multiple values for x:



      bar = function(xVals, xMax, ..., fun) {
      return(
      sapply(
      xVals,
      function(xVal) integrate(f = fun, lower = xVal, upper = xMax, ...)$value)
      )
      }


      Note that foo1's parameter alpha would be passed as part of the ...:



      bar(10:20, 100, alpha = 1.5, fun = foo1)  


      and foo2's parameters y and z would also be passed as part of the ...:



      bar(10:20, 100, y = 2, z = 4, fun = foo2)


      I like this arrangement because other required parameters for integrate can also be passed as part of .... For instance, to change the default value of the subdivisions parameter of the integrate function called by bar, one can simply specify:



      bar(10:20, 100, y = 2, z = 4, fun = foo2, subdivisions = 200L)


      Now, consider a function that integrates bar from xStart to xEnd, but accepts vectors for multiple values for xStart and xEnd. It might look like this:



      integrateBar = function(xStart, xEnd, xMax, ..., fun) {
      aMatrix = cbind(xStart, xEnd)
      result =
      apply(
      aMatrix,
      1,
      function(xRange) integrate(bar, xRange[1], xRange[2], xMax, ..., fun = fun)$value
      )
      return(result)
      }


      So this is fine and pretty straightforward, until I want to call integrateBar but change the value of the subdivisions argument for the integrate function called within bar. If I use:



      integrateBar(10:20, 40, 100, alpha = 1.5, subdivisions = 200L, fun = foo1)


      the subdivisions argument is grabbed and used by the integrate function in integrateBar and is not passed through to bar. This is desirable behavior - sometimes. But sometimes I also want to be able to leave subdivisions set at the default for the integrate call in integrateBar but change it for the integrate call in bar. Can anyone tell me an elegant way to do the latter?



      I'm willing to modify the formals for bar, but I'm trying not to make it too clunky because I want to call bar directly, too. It would be really nice if there were some R trick so that a subdivisions argument would be ignored by the first integrate function encountered, but available to the second.



      The best solution I have is to write a wrapper for integrate that uses a parameter SUBDIVISIONS and call the wrapper from bar. If I don't get a better answer, I will post that solution.







      r function arguments






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 5:06







      Geoffrey Poole

















      asked Nov 22 '18 at 5:00









      Geoffrey PooleGeoffrey Poole

      27126




      27126
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The argument subdivisions is not passed further from integrate, because it is an actual named argument of integrate (only arguments in ...) will be passed to f:



          > integrate
          # function (f, lower, upper, ..., subdivisions = 100L, rel.tol = .Machine$double.eps^0.25,
          # abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE,
          # aux = NULL)


          One possible solution that does not require changing bar would be to write a wrapper for bar inside integrateBar. I have two variants:



          Variant 1:
          If you want to pass the arguments in ... to both, the outer and the inner call to integrate, this would be it:



          integrateBar = function(xStart, xEnd, xMax, ..., fun) {

          argsExtra <- list(...)

          # A wrapper function for bar:
          # Note that the argument ... is necessary, although the function does not use
          # it. This is because, we still pass ... to the call to integrate below.
          CallBarOnFun <- function(xVals, xMax, ...) {
          do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
          }

          aMatrix = cbind(xStart, xEnd)
          result =
          apply(
          aMatrix,
          1,
          function(xRange) {
          integrate(CallBarOnFun, xRange[1], xRange[2], xMax, ...)$value
          }
          )
          return(result)
          }


          Variant 2: If you want to pass the arguments in ... only to the inner call:



          integrateBar = function(xStart, xEnd, xMax, ..., fun) {
          argsExtra <- list(...)

          CallBarOnFun <- function(xVals, xMax) {
          do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
          }

          aMatrix = cbind(xStart, xEnd)
          result =
          apply(
          aMatrix,
          1,
          function(xRange) {
          integrate(CallBarOnFun, xRange[1], xRange[2], xMax)$value
          }
          )
          return(result)
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424161%2fhow-to-pass-an-argument-included-in-through-to-a-recursive-call-in-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The argument subdivisions is not passed further from integrate, because it is an actual named argument of integrate (only arguments in ...) will be passed to f:



            > integrate
            # function (f, lower, upper, ..., subdivisions = 100L, rel.tol = .Machine$double.eps^0.25,
            # abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE,
            # aux = NULL)


            One possible solution that does not require changing bar would be to write a wrapper for bar inside integrateBar. I have two variants:



            Variant 1:
            If you want to pass the arguments in ... to both, the outer and the inner call to integrate, this would be it:



            integrateBar = function(xStart, xEnd, xMax, ..., fun) {

            argsExtra <- list(...)

            # A wrapper function for bar:
            # Note that the argument ... is necessary, although the function does not use
            # it. This is because, we still pass ... to the call to integrate below.
            CallBarOnFun <- function(xVals, xMax, ...) {
            do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
            }

            aMatrix = cbind(xStart, xEnd)
            result =
            apply(
            aMatrix,
            1,
            function(xRange) {
            integrate(CallBarOnFun, xRange[1], xRange[2], xMax, ...)$value
            }
            )
            return(result)
            }


            Variant 2: If you want to pass the arguments in ... only to the inner call:



            integrateBar = function(xStart, xEnd, xMax, ..., fun) {
            argsExtra <- list(...)

            CallBarOnFun <- function(xVals, xMax) {
            do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
            }

            aMatrix = cbind(xStart, xEnd)
            result =
            apply(
            aMatrix,
            1,
            function(xRange) {
            integrate(CallBarOnFun, xRange[1], xRange[2], xMax)$value
            }
            )
            return(result)
            }





            share|improve this answer




























              1














              The argument subdivisions is not passed further from integrate, because it is an actual named argument of integrate (only arguments in ...) will be passed to f:



              > integrate
              # function (f, lower, upper, ..., subdivisions = 100L, rel.tol = .Machine$double.eps^0.25,
              # abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE,
              # aux = NULL)


              One possible solution that does not require changing bar would be to write a wrapper for bar inside integrateBar. I have two variants:



              Variant 1:
              If you want to pass the arguments in ... to both, the outer and the inner call to integrate, this would be it:



              integrateBar = function(xStart, xEnd, xMax, ..., fun) {

              argsExtra <- list(...)

              # A wrapper function for bar:
              # Note that the argument ... is necessary, although the function does not use
              # it. This is because, we still pass ... to the call to integrate below.
              CallBarOnFun <- function(xVals, xMax, ...) {
              do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
              }

              aMatrix = cbind(xStart, xEnd)
              result =
              apply(
              aMatrix,
              1,
              function(xRange) {
              integrate(CallBarOnFun, xRange[1], xRange[2], xMax, ...)$value
              }
              )
              return(result)
              }


              Variant 2: If you want to pass the arguments in ... only to the inner call:



              integrateBar = function(xStart, xEnd, xMax, ..., fun) {
              argsExtra <- list(...)

              CallBarOnFun <- function(xVals, xMax) {
              do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
              }

              aMatrix = cbind(xStart, xEnd)
              result =
              apply(
              aMatrix,
              1,
              function(xRange) {
              integrate(CallBarOnFun, xRange[1], xRange[2], xMax)$value
              }
              )
              return(result)
              }





              share|improve this answer


























                1












                1








                1







                The argument subdivisions is not passed further from integrate, because it is an actual named argument of integrate (only arguments in ...) will be passed to f:



                > integrate
                # function (f, lower, upper, ..., subdivisions = 100L, rel.tol = .Machine$double.eps^0.25,
                # abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE,
                # aux = NULL)


                One possible solution that does not require changing bar would be to write a wrapper for bar inside integrateBar. I have two variants:



                Variant 1:
                If you want to pass the arguments in ... to both, the outer and the inner call to integrate, this would be it:



                integrateBar = function(xStart, xEnd, xMax, ..., fun) {

                argsExtra <- list(...)

                # A wrapper function for bar:
                # Note that the argument ... is necessary, although the function does not use
                # it. This is because, we still pass ... to the call to integrate below.
                CallBarOnFun <- function(xVals, xMax, ...) {
                do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
                }

                aMatrix = cbind(xStart, xEnd)
                result =
                apply(
                aMatrix,
                1,
                function(xRange) {
                integrate(CallBarOnFun, xRange[1], xRange[2], xMax, ...)$value
                }
                )
                return(result)
                }


                Variant 2: If you want to pass the arguments in ... only to the inner call:



                integrateBar = function(xStart, xEnd, xMax, ..., fun) {
                argsExtra <- list(...)

                CallBarOnFun <- function(xVals, xMax) {
                do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
                }

                aMatrix = cbind(xStart, xEnd)
                result =
                apply(
                aMatrix,
                1,
                function(xRange) {
                integrate(CallBarOnFun, xRange[1], xRange[2], xMax)$value
                }
                )
                return(result)
                }





                share|improve this answer













                The argument subdivisions is not passed further from integrate, because it is an actual named argument of integrate (only arguments in ...) will be passed to f:



                > integrate
                # function (f, lower, upper, ..., subdivisions = 100L, rel.tol = .Machine$double.eps^0.25,
                # abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE,
                # aux = NULL)


                One possible solution that does not require changing bar would be to write a wrapper for bar inside integrateBar. I have two variants:



                Variant 1:
                If you want to pass the arguments in ... to both, the outer and the inner call to integrate, this would be it:



                integrateBar = function(xStart, xEnd, xMax, ..., fun) {

                argsExtra <- list(...)

                # A wrapper function for bar:
                # Note that the argument ... is necessary, although the function does not use
                # it. This is because, we still pass ... to the call to integrate below.
                CallBarOnFun <- function(xVals, xMax, ...) {
                do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
                }

                aMatrix = cbind(xStart, xEnd)
                result =
                apply(
                aMatrix,
                1,
                function(xRange) {
                integrate(CallBarOnFun, xRange[1], xRange[2], xMax, ...)$value
                }
                )
                return(result)
                }


                Variant 2: If you want to pass the arguments in ... only to the inner call:



                integrateBar = function(xStart, xEnd, xMax, ..., fun) {
                argsExtra <- list(...)

                CallBarOnFun <- function(xVals, xMax) {
                do.call(bar, c(list(xVals, xMax), argsExtra, list(fun=fun)))
                }

                aMatrix = cbind(xStart, xEnd)
                result =
                apply(
                aMatrix,
                1,
                function(xRange) {
                integrate(CallBarOnFun, xRange[1], xRange[2], xMax)$value
                }
                )
                return(result)
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 7:29









                Thriving For PerfectionThriving For Perfection

                517




                517
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424161%2fhow-to-pass-an-argument-included-in-through-to-a-recursive-call-in-r%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    'app-layout' is not a known element: how to share Component with different Modules