Programmatically switch package in `::` call in R












8















I would like to be able in a call bar::foo(), to be able to programmatically switch the package bar so that the same syntax calls hello::foo().



An example:




  • Let's say I have three packages, parentPkg, childPkg1 and childPkg2.

  • In parentPkg I have a call to function childPkg1::foo()


  • foo() is also a function in childPkg2


  • I would like to be able, in parentPkg to use the :: operator to call foo() but to programatically switch the package name.
    Something like:



    dummy_pkg_name = ifelse(scenario=="child1", "childPkg1", "childPkg2")
    dummy_pkg_name::foo()



Is it possible? How do I achieve it?



Some context



parentPkg is a function that interacts with a web application, takes some request and data and returns results from different statistical models depending on the scenarios.

Each scenario is quite complex and not everything can be generalised in parentPkg. For this reason, childPkg1 and childPkg2 (actually there are also 3 and 4) are sort of sub-packages that deals with the data cleaning and various alternatives for each scenario but return the same class of value.

The idea is that parentPkg would switch the package to the pertinent child depending on the scenario and call all of the necessary functions without having to write the same sequence for each child but just with a slightly different :: call.










share|improve this question























  • I think if(requireNamespace('childPkg1')) is what you really want

    – MichaelChirico
    Dec 30 '18 at 15:02
















8















I would like to be able in a call bar::foo(), to be able to programmatically switch the package bar so that the same syntax calls hello::foo().



An example:




  • Let's say I have three packages, parentPkg, childPkg1 and childPkg2.

  • In parentPkg I have a call to function childPkg1::foo()


  • foo() is also a function in childPkg2


  • I would like to be able, in parentPkg to use the :: operator to call foo() but to programatically switch the package name.
    Something like:



    dummy_pkg_name = ifelse(scenario=="child1", "childPkg1", "childPkg2")
    dummy_pkg_name::foo()



Is it possible? How do I achieve it?



Some context



parentPkg is a function that interacts with a web application, takes some request and data and returns results from different statistical models depending on the scenarios.

Each scenario is quite complex and not everything can be generalised in parentPkg. For this reason, childPkg1 and childPkg2 (actually there are also 3 and 4) are sort of sub-packages that deals with the data cleaning and various alternatives for each scenario but return the same class of value.

The idea is that parentPkg would switch the package to the pertinent child depending on the scenario and call all of the necessary functions without having to write the same sequence for each child but just with a slightly different :: call.










share|improve this question























  • I think if(requireNamespace('childPkg1')) is what you really want

    – MichaelChirico
    Dec 30 '18 at 15:02














8












8








8


1






I would like to be able in a call bar::foo(), to be able to programmatically switch the package bar so that the same syntax calls hello::foo().



An example:




  • Let's say I have three packages, parentPkg, childPkg1 and childPkg2.

  • In parentPkg I have a call to function childPkg1::foo()


  • foo() is also a function in childPkg2


  • I would like to be able, in parentPkg to use the :: operator to call foo() but to programatically switch the package name.
    Something like:



    dummy_pkg_name = ifelse(scenario=="child1", "childPkg1", "childPkg2")
    dummy_pkg_name::foo()



Is it possible? How do I achieve it?



Some context



parentPkg is a function that interacts with a web application, takes some request and data and returns results from different statistical models depending on the scenarios.

Each scenario is quite complex and not everything can be generalised in parentPkg. For this reason, childPkg1 and childPkg2 (actually there are also 3 and 4) are sort of sub-packages that deals with the data cleaning and various alternatives for each scenario but return the same class of value.

The idea is that parentPkg would switch the package to the pertinent child depending on the scenario and call all of the necessary functions without having to write the same sequence for each child but just with a slightly different :: call.










share|improve this question














I would like to be able in a call bar::foo(), to be able to programmatically switch the package bar so that the same syntax calls hello::foo().



An example:




  • Let's say I have three packages, parentPkg, childPkg1 and childPkg2.

  • In parentPkg I have a call to function childPkg1::foo()


  • foo() is also a function in childPkg2


  • I would like to be able, in parentPkg to use the :: operator to call foo() but to programatically switch the package name.
    Something like:



    dummy_pkg_name = ifelse(scenario=="child1", "childPkg1", "childPkg2")
    dummy_pkg_name::foo()



Is it possible? How do I achieve it?



Some context



parentPkg is a function that interacts with a web application, takes some request and data and returns results from different statistical models depending on the scenarios.

Each scenario is quite complex and not everything can be generalised in parentPkg. For this reason, childPkg1 and childPkg2 (actually there are also 3 and 4) are sort of sub-packages that deals with the data cleaning and various alternatives for each scenario but return the same class of value.

The idea is that parentPkg would switch the package to the pertinent child depending on the scenario and call all of the necessary functions without having to write the same sequence for each child but just with a slightly different :: call.







r package






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 14:51









Duccio ADuccio A

414313




414313













  • I think if(requireNamespace('childPkg1')) is what you really want

    – MichaelChirico
    Dec 30 '18 at 15:02



















  • I think if(requireNamespace('childPkg1')) is what you really want

    – MichaelChirico
    Dec 30 '18 at 15:02

















I think if(requireNamespace('childPkg1')) is what you really want

– MichaelChirico
Dec 30 '18 at 15:02





I think if(requireNamespace('childPkg1')) is what you really want

– MichaelChirico
Dec 30 '18 at 15:02












3 Answers
3






active

oldest

votes


















12














Since :: can be seen as a function, it looks like



`::`(dummy_pkg_name, foo)()


is what you want. Alternatively,



getFromNamespace("foo", ns = dummy_pkg_name)()




For instance,



`::`(stats, t.test)
# function (x, ...)
# UseMethod("t.test")
# <bytecode: 0x102fd4b00>
# <environment: namespace:stats>

getFromNamespace("t.test", ns = "stats")
# function (x, ...)
# UseMethod("t.test")
# <bytecode: 0x102fd4b00>
# <environment: namespace:stats>





share|improve this answer

































    2














    To adhere to KISS, simply re-assign to new named functions in global environment. Be sure to leave out () since you are not requesting to run the function.



    parent_foo <- parentPkg::foo
    child1_foo <- childPkg1::foo
    child2_foo <- childPkg2::foo
    child3_foo <- childPkg3::foo


    Then, conditionally apply them as needed:



    if (scenario=="child1") {
    obj <- child1_foo(...)
    }
    else if (scenario=="child2") {
    obj <- child2_foo(...)
    }
    ...





    share|improve this answer































      2














      You could also create a call() that could then be evaluated.



      call("::", quote(bar), quote(foo()))
      # bar::foo()


      Put into use:



      c <- call("::", quote(stats), quote(t.test))
      eval(c)
      # function (x, ...)
      # UseMethod("t.test")
      # <bytecode: 0x4340988>
      # <environment: namespace:stats>


      Wrapped up in a function using setdiff as our default function:



      f <- function(pkg, fn = setdiff) {
      pkg <- substitute(pkg)
      fn <- substitute(fn)
      eval(call("::", pkg, fn))
      }

      f(base)
      # function (x, y)
      # {
      # x <- as.vector(x)
      # y <- as.vector(y)
      # unique(if (length(x) || length(y))
      # x[match(x, y, 0L) == 0L]
      # else x)
      # }
      # <bytecode: 0x30f1ea8>
      # <environment: namespace:base>

      f(dplyr)
      # function (x, y, ...)
      # UseMethod("setdiff")
      # <environment: namespace:dplyr>





      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%2f53978610%2fprogrammatically-switch-package-in-call-in-r%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        12














        Since :: can be seen as a function, it looks like



        `::`(dummy_pkg_name, foo)()


        is what you want. Alternatively,



        getFromNamespace("foo", ns = dummy_pkg_name)()




        For instance,



        `::`(stats, t.test)
        # function (x, ...)
        # UseMethod("t.test")
        # <bytecode: 0x102fd4b00>
        # <environment: namespace:stats>

        getFromNamespace("t.test", ns = "stats")
        # function (x, ...)
        # UseMethod("t.test")
        # <bytecode: 0x102fd4b00>
        # <environment: namespace:stats>





        share|improve this answer






























          12














          Since :: can be seen as a function, it looks like



          `::`(dummy_pkg_name, foo)()


          is what you want. Alternatively,



          getFromNamespace("foo", ns = dummy_pkg_name)()




          For instance,



          `::`(stats, t.test)
          # function (x, ...)
          # UseMethod("t.test")
          # <bytecode: 0x102fd4b00>
          # <environment: namespace:stats>

          getFromNamespace("t.test", ns = "stats")
          # function (x, ...)
          # UseMethod("t.test")
          # <bytecode: 0x102fd4b00>
          # <environment: namespace:stats>





          share|improve this answer




























            12












            12








            12







            Since :: can be seen as a function, it looks like



            `::`(dummy_pkg_name, foo)()


            is what you want. Alternatively,



            getFromNamespace("foo", ns = dummy_pkg_name)()




            For instance,



            `::`(stats, t.test)
            # function (x, ...)
            # UseMethod("t.test")
            # <bytecode: 0x102fd4b00>
            # <environment: namespace:stats>

            getFromNamespace("t.test", ns = "stats")
            # function (x, ...)
            # UseMethod("t.test")
            # <bytecode: 0x102fd4b00>
            # <environment: namespace:stats>





            share|improve this answer















            Since :: can be seen as a function, it looks like



            `::`(dummy_pkg_name, foo)()


            is what you want. Alternatively,



            getFromNamespace("foo", ns = dummy_pkg_name)()




            For instance,



            `::`(stats, t.test)
            # function (x, ...)
            # UseMethod("t.test")
            # <bytecode: 0x102fd4b00>
            # <environment: namespace:stats>

            getFromNamespace("t.test", ns = "stats")
            # function (x, ...)
            # UseMethod("t.test")
            # <bytecode: 0x102fd4b00>
            # <environment: namespace:stats>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 30 '18 at 15:01

























            answered Dec 30 '18 at 14:56









            Julius VainoraJulius Vainora

            35.9k76380




            35.9k76380

























                2














                To adhere to KISS, simply re-assign to new named functions in global environment. Be sure to leave out () since you are not requesting to run the function.



                parent_foo <- parentPkg::foo
                child1_foo <- childPkg1::foo
                child2_foo <- childPkg2::foo
                child3_foo <- childPkg3::foo


                Then, conditionally apply them as needed:



                if (scenario=="child1") {
                obj <- child1_foo(...)
                }
                else if (scenario=="child2") {
                obj <- child2_foo(...)
                }
                ...





                share|improve this answer




























                  2














                  To adhere to KISS, simply re-assign to new named functions in global environment. Be sure to leave out () since you are not requesting to run the function.



                  parent_foo <- parentPkg::foo
                  child1_foo <- childPkg1::foo
                  child2_foo <- childPkg2::foo
                  child3_foo <- childPkg3::foo


                  Then, conditionally apply them as needed:



                  if (scenario=="child1") {
                  obj <- child1_foo(...)
                  }
                  else if (scenario=="child2") {
                  obj <- child2_foo(...)
                  }
                  ...





                  share|improve this answer


























                    2












                    2








                    2







                    To adhere to KISS, simply re-assign to new named functions in global environment. Be sure to leave out () since you are not requesting to run the function.



                    parent_foo <- parentPkg::foo
                    child1_foo <- childPkg1::foo
                    child2_foo <- childPkg2::foo
                    child3_foo <- childPkg3::foo


                    Then, conditionally apply them as needed:



                    if (scenario=="child1") {
                    obj <- child1_foo(...)
                    }
                    else if (scenario=="child2") {
                    obj <- child2_foo(...)
                    }
                    ...





                    share|improve this answer













                    To adhere to KISS, simply re-assign to new named functions in global environment. Be sure to leave out () since you are not requesting to run the function.



                    parent_foo <- parentPkg::foo
                    child1_foo <- childPkg1::foo
                    child2_foo <- childPkg2::foo
                    child3_foo <- childPkg3::foo


                    Then, conditionally apply them as needed:



                    if (scenario=="child1") {
                    obj <- child1_foo(...)
                    }
                    else if (scenario=="child2") {
                    obj <- child2_foo(...)
                    }
                    ...






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 30 '18 at 17:09









                    ParfaitParfait

                    51k84270




                    51k84270























                        2














                        You could also create a call() that could then be evaluated.



                        call("::", quote(bar), quote(foo()))
                        # bar::foo()


                        Put into use:



                        c <- call("::", quote(stats), quote(t.test))
                        eval(c)
                        # function (x, ...)
                        # UseMethod("t.test")
                        # <bytecode: 0x4340988>
                        # <environment: namespace:stats>


                        Wrapped up in a function using setdiff as our default function:



                        f <- function(pkg, fn = setdiff) {
                        pkg <- substitute(pkg)
                        fn <- substitute(fn)
                        eval(call("::", pkg, fn))
                        }

                        f(base)
                        # function (x, y)
                        # {
                        # x <- as.vector(x)
                        # y <- as.vector(y)
                        # unique(if (length(x) || length(y))
                        # x[match(x, y, 0L) == 0L]
                        # else x)
                        # }
                        # <bytecode: 0x30f1ea8>
                        # <environment: namespace:base>

                        f(dplyr)
                        # function (x, y, ...)
                        # UseMethod("setdiff")
                        # <environment: namespace:dplyr>





                        share|improve this answer






























                          2














                          You could also create a call() that could then be evaluated.



                          call("::", quote(bar), quote(foo()))
                          # bar::foo()


                          Put into use:



                          c <- call("::", quote(stats), quote(t.test))
                          eval(c)
                          # function (x, ...)
                          # UseMethod("t.test")
                          # <bytecode: 0x4340988>
                          # <environment: namespace:stats>


                          Wrapped up in a function using setdiff as our default function:



                          f <- function(pkg, fn = setdiff) {
                          pkg <- substitute(pkg)
                          fn <- substitute(fn)
                          eval(call("::", pkg, fn))
                          }

                          f(base)
                          # function (x, y)
                          # {
                          # x <- as.vector(x)
                          # y <- as.vector(y)
                          # unique(if (length(x) || length(y))
                          # x[match(x, y, 0L) == 0L]
                          # else x)
                          # }
                          # <bytecode: 0x30f1ea8>
                          # <environment: namespace:base>

                          f(dplyr)
                          # function (x, y, ...)
                          # UseMethod("setdiff")
                          # <environment: namespace:dplyr>





                          share|improve this answer




























                            2












                            2








                            2







                            You could also create a call() that could then be evaluated.



                            call("::", quote(bar), quote(foo()))
                            # bar::foo()


                            Put into use:



                            c <- call("::", quote(stats), quote(t.test))
                            eval(c)
                            # function (x, ...)
                            # UseMethod("t.test")
                            # <bytecode: 0x4340988>
                            # <environment: namespace:stats>


                            Wrapped up in a function using setdiff as our default function:



                            f <- function(pkg, fn = setdiff) {
                            pkg <- substitute(pkg)
                            fn <- substitute(fn)
                            eval(call("::", pkg, fn))
                            }

                            f(base)
                            # function (x, y)
                            # {
                            # x <- as.vector(x)
                            # y <- as.vector(y)
                            # unique(if (length(x) || length(y))
                            # x[match(x, y, 0L) == 0L]
                            # else x)
                            # }
                            # <bytecode: 0x30f1ea8>
                            # <environment: namespace:base>

                            f(dplyr)
                            # function (x, y, ...)
                            # UseMethod("setdiff")
                            # <environment: namespace:dplyr>





                            share|improve this answer















                            You could also create a call() that could then be evaluated.



                            call("::", quote(bar), quote(foo()))
                            # bar::foo()


                            Put into use:



                            c <- call("::", quote(stats), quote(t.test))
                            eval(c)
                            # function (x, ...)
                            # UseMethod("t.test")
                            # <bytecode: 0x4340988>
                            # <environment: namespace:stats>


                            Wrapped up in a function using setdiff as our default function:



                            f <- function(pkg, fn = setdiff) {
                            pkg <- substitute(pkg)
                            fn <- substitute(fn)
                            eval(call("::", pkg, fn))
                            }

                            f(base)
                            # function (x, y)
                            # {
                            # x <- as.vector(x)
                            # y <- as.vector(y)
                            # unique(if (length(x) || length(y))
                            # x[match(x, y, 0L) == 0L]
                            # else x)
                            # }
                            # <bytecode: 0x30f1ea8>
                            # <environment: namespace:base>

                            f(dplyr)
                            # function (x, y, ...)
                            # UseMethod("setdiff")
                            # <environment: namespace:dplyr>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 30 '18 at 22:10

























                            answered Dec 30 '18 at 15:49









                            Rich ScrivenRich Scriven

                            76.2k8100171




                            76.2k8100171






























                                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%2f53978610%2fprogrammatically-switch-package-in-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

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas