How can a method for a new class refer to the basic function it's replacing?












1















Suppose I want to create a method for a class I've created, but I don't have access to the code of the original function - I just want to build on top of it. Just to give a simple example that doesn't actually do anything:



x1<-1
class(x1)<-c("myclass",class(x1))
print.myclass<-function(x) {
x<-paste0(x,"foobar")
print(x)
}
print(x1)


If I try to run the last line, it throws the function into a loop and R eventually crashes. The solution I found was to add a line to the function that strips the new class name from x before passing it to the original function:



print.myclass<-function(x) {x<-paste0(x,"foobar"); class(x)<-class(x)[-1]; print(x)}


Is there a better/best practice way to do it?










share|improve this question





























    1















    Suppose I want to create a method for a class I've created, but I don't have access to the code of the original function - I just want to build on top of it. Just to give a simple example that doesn't actually do anything:



    x1<-1
    class(x1)<-c("myclass",class(x1))
    print.myclass<-function(x) {
    x<-paste0(x,"foobar")
    print(x)
    }
    print(x1)


    If I try to run the last line, it throws the function into a loop and R eventually crashes. The solution I found was to add a line to the function that strips the new class name from x before passing it to the original function:



    print.myclass<-function(x) {x<-paste0(x,"foobar"); class(x)<-class(x)[-1]; print(x)}


    Is there a better/best practice way to do it?










    share|improve this question



























      1












      1








      1








      Suppose I want to create a method for a class I've created, but I don't have access to the code of the original function - I just want to build on top of it. Just to give a simple example that doesn't actually do anything:



      x1<-1
      class(x1)<-c("myclass",class(x1))
      print.myclass<-function(x) {
      x<-paste0(x,"foobar")
      print(x)
      }
      print(x1)


      If I try to run the last line, it throws the function into a loop and R eventually crashes. The solution I found was to add a line to the function that strips the new class name from x before passing it to the original function:



      print.myclass<-function(x) {x<-paste0(x,"foobar"); class(x)<-class(x)[-1]; print(x)}


      Is there a better/best practice way to do it?










      share|improve this question
















      Suppose I want to create a method for a class I've created, but I don't have access to the code of the original function - I just want to build on top of it. Just to give a simple example that doesn't actually do anything:



      x1<-1
      class(x1)<-c("myclass",class(x1))
      print.myclass<-function(x) {
      x<-paste0(x,"foobar")
      print(x)
      }
      print(x1)


      If I try to run the last line, it throws the function into a loop and R eventually crashes. The solution I found was to add a line to the function that strips the new class name from x before passing it to the original function:



      print.myclass<-function(x) {x<-paste0(x,"foobar"); class(x)<-class(x)[-1]; print(x)}


      Is there a better/best practice way to do it?







      r function class methods






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 18:56







      iod

















      asked Nov 22 '18 at 15:50









      iodiod

      4,2172723




      4,2172723
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I think your problem is that you create an infinite loop: print(print(...).

          I don't know what you want to achieve but this might be what you are looking for:



          x1 <- 1
          class(x1) <- c("myclass",class(x1))
          print.myclass <- function(x) print.default(x)
          print(x1)


          Perhaps you might want to look here



          BTW: I don't think your solution really solves the problem. You just delete your new class entry which causes print not to use print.myclass.



          For details see Hadley






          share|improve this answer


























          • Ah! I didn't realize function.default is a thing. Does that work for any function with methods? If so, that will resolve my issue.

            – iod
            Nov 22 '18 at 18:48











          • I think this is a general concept. See also the comment here

            – Christoph
            Nov 22 '18 at 18:51











          • To your BTW - my actual function makes some calculations on my object, but ultimately I want to run it through the default function (imagine if before passing x1 to print default, it does paste0(x,"foobar")).

            – iod
            Nov 22 '18 at 18:54











          • Ok. I think, this is really the concept. See my edit and the link...

            – Christoph
            Nov 22 '18 at 18:55











          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%2f53434483%2fhow-can-a-method-for-a-new-class-refer-to-the-basic-function-its-replacing%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














          I think your problem is that you create an infinite loop: print(print(...).

          I don't know what you want to achieve but this might be what you are looking for:



          x1 <- 1
          class(x1) <- c("myclass",class(x1))
          print.myclass <- function(x) print.default(x)
          print(x1)


          Perhaps you might want to look here



          BTW: I don't think your solution really solves the problem. You just delete your new class entry which causes print not to use print.myclass.



          For details see Hadley






          share|improve this answer


























          • Ah! I didn't realize function.default is a thing. Does that work for any function with methods? If so, that will resolve my issue.

            – iod
            Nov 22 '18 at 18:48











          • I think this is a general concept. See also the comment here

            – Christoph
            Nov 22 '18 at 18:51











          • To your BTW - my actual function makes some calculations on my object, but ultimately I want to run it through the default function (imagine if before passing x1 to print default, it does paste0(x,"foobar")).

            – iod
            Nov 22 '18 at 18:54











          • Ok. I think, this is really the concept. See my edit and the link...

            – Christoph
            Nov 22 '18 at 18:55
















          1














          I think your problem is that you create an infinite loop: print(print(...).

          I don't know what you want to achieve but this might be what you are looking for:



          x1 <- 1
          class(x1) <- c("myclass",class(x1))
          print.myclass <- function(x) print.default(x)
          print(x1)


          Perhaps you might want to look here



          BTW: I don't think your solution really solves the problem. You just delete your new class entry which causes print not to use print.myclass.



          For details see Hadley






          share|improve this answer


























          • Ah! I didn't realize function.default is a thing. Does that work for any function with methods? If so, that will resolve my issue.

            – iod
            Nov 22 '18 at 18:48











          • I think this is a general concept. See also the comment here

            – Christoph
            Nov 22 '18 at 18:51











          • To your BTW - my actual function makes some calculations on my object, but ultimately I want to run it through the default function (imagine if before passing x1 to print default, it does paste0(x,"foobar")).

            – iod
            Nov 22 '18 at 18:54











          • Ok. I think, this is really the concept. See my edit and the link...

            – Christoph
            Nov 22 '18 at 18:55














          1












          1








          1







          I think your problem is that you create an infinite loop: print(print(...).

          I don't know what you want to achieve but this might be what you are looking for:



          x1 <- 1
          class(x1) <- c("myclass",class(x1))
          print.myclass <- function(x) print.default(x)
          print(x1)


          Perhaps you might want to look here



          BTW: I don't think your solution really solves the problem. You just delete your new class entry which causes print not to use print.myclass.



          For details see Hadley






          share|improve this answer















          I think your problem is that you create an infinite loop: print(print(...).

          I don't know what you want to achieve but this might be what you are looking for:



          x1 <- 1
          class(x1) <- c("myclass",class(x1))
          print.myclass <- function(x) print.default(x)
          print(x1)


          Perhaps you might want to look here



          BTW: I don't think your solution really solves the problem. You just delete your new class entry which causes print not to use print.myclass.



          For details see Hadley







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 18:54

























          answered Nov 22 '18 at 18:46









          ChristophChristoph

          3,02222044




          3,02222044













          • Ah! I didn't realize function.default is a thing. Does that work for any function with methods? If so, that will resolve my issue.

            – iod
            Nov 22 '18 at 18:48











          • I think this is a general concept. See also the comment here

            – Christoph
            Nov 22 '18 at 18:51











          • To your BTW - my actual function makes some calculations on my object, but ultimately I want to run it through the default function (imagine if before passing x1 to print default, it does paste0(x,"foobar")).

            – iod
            Nov 22 '18 at 18:54











          • Ok. I think, this is really the concept. See my edit and the link...

            – Christoph
            Nov 22 '18 at 18:55



















          • Ah! I didn't realize function.default is a thing. Does that work for any function with methods? If so, that will resolve my issue.

            – iod
            Nov 22 '18 at 18:48











          • I think this is a general concept. See also the comment here

            – Christoph
            Nov 22 '18 at 18:51











          • To your BTW - my actual function makes some calculations on my object, but ultimately I want to run it through the default function (imagine if before passing x1 to print default, it does paste0(x,"foobar")).

            – iod
            Nov 22 '18 at 18:54











          • Ok. I think, this is really the concept. See my edit and the link...

            – Christoph
            Nov 22 '18 at 18:55

















          Ah! I didn't realize function.default is a thing. Does that work for any function with methods? If so, that will resolve my issue.

          – iod
          Nov 22 '18 at 18:48





          Ah! I didn't realize function.default is a thing. Does that work for any function with methods? If so, that will resolve my issue.

          – iod
          Nov 22 '18 at 18:48













          I think this is a general concept. See also the comment here

          – Christoph
          Nov 22 '18 at 18:51





          I think this is a general concept. See also the comment here

          – Christoph
          Nov 22 '18 at 18:51













          To your BTW - my actual function makes some calculations on my object, but ultimately I want to run it through the default function (imagine if before passing x1 to print default, it does paste0(x,"foobar")).

          – iod
          Nov 22 '18 at 18:54





          To your BTW - my actual function makes some calculations on my object, but ultimately I want to run it through the default function (imagine if before passing x1 to print default, it does paste0(x,"foobar")).

          – iod
          Nov 22 '18 at 18:54













          Ok. I think, this is really the concept. See my edit and the link...

          – Christoph
          Nov 22 '18 at 18:55





          Ok. I think, this is really the concept. See my edit and the link...

          – Christoph
          Nov 22 '18 at 18:55




















          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%2f53434483%2fhow-can-a-method-for-a-new-class-refer-to-the-basic-function-its-replacing%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()