im7
im7

Reputation: 673

setMethod for a Class in R

I am trying to create a method for the class "subject" using the Generic method "summary". However, I get an error message. Could you help me understand what I am doing wrong and how to correct it? Thank you.

      setGeneric("summary")

      setMethod("summary",
      c(x = "subject"),
      function(x){"This is summary for subject class"})

The error message is the following:

      Error in match.call(definition, call, expand.dots, envir) : 
      unused argument (x = c("subject", ""))

I have used the setMethod to create a method for the class "subject" using the Generic method "print" as follows:

      setMethod("print",
            c(x = "subject"),
            function(x){
                 if (length(x$id) > 0){
                       paste0("Subject ID: ", unique(x$id))}
                 else {"NULL"}
             })

The aforementioned code is executed without errors. I can not understand what is the difference between the two cases.

Upvotes: 0

Views: 1942

Answers (1)

im7
im7

Reputation: 673

The code works if x is replaced with "object". I.e.:

     setMethod("summary",
     c(object = "subject"),
     function(object){"This is summary for subject class"})

There is some related information here: Is 'show' a normal S4 generic function?

Hope that helps.

Upvotes: 5

Related Questions