Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9180127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:58:11+00:00 2026-06-17T17:58:11+00:00

I want to subclass an S4 class and add a special method to that

  • 0

I want to subclass an S4 class and add a special method to that subclass.
The method should work only for the subclass, it does not make sense for any other class in my application.

    setClass("MySpecies", contains="Species", ##Species is an S4 class
         representation(x="numeric"))

setMethod("initialize", "MySpecies", function(.Object, x, ...){
  .Object@x <- x
  args <- list(...)
  for(i in seq_len(length(args))){
    attr(.Object, names(args)[i]) <- args[[i]]
  }
  .Object
})

##CalcMatrix <- function(.Object, y){
##  x <- .Object@x
##  matrix(x, x*2, y*3)
##}

setGeneric("CalcMatrix", function(object, y){standardGeneric("CalcMatrix")})
setMethod("CalcMatrix", "MySpecies",function(object, y){
  x <- object@x
  matrix(x, x*2, y*3)
})

With the setGeneric it works, but do I really have to define a generic function although it will be used only with this object? The commented out part works, but then there is no check if the function is called with the right arguments. What is the correct way to do this?
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:58:12+00:00Added an answer on June 17, 2026 at 5:58 pm

    You’re wanting to use method dispatch, and every method needs to be associated with a generic, so yes, setGeneric is required.

    And for a little unasked-for advice… It’s a bit weird to use a formal class system (presumably because the well-defined classes help in writing more complicated programs) and then to subvert the structure by adding arbitrary attributes; these should really be additional, well-defined slots in your class.

    Let’s make your example reproducible by defining Species

    setClass("Species", representation(y="numeric"))
    setClass("MySpecies", contains="Species", ##Species is an S4 class
             representation(x="numeric"))
    

    An implicit requirement for S4 classes is that new("MySpecies") works; your initialize method fails this test (because x does not have a default value). In addition, it’s common practice to expect that initializing MySpecies calls the initialize methods for the classes it contains. One could write

    setMethod("initialize", "MySpecies", function(.Object, ..., x=numeric()) {
        callNextMethod(.Object, x=x, ...)
    })
    

    Note callNextMethod, so that the base class gets initialized properly. Using ... and passing it to callNextMethod means that slots that might be defined in Species would also be initialized correctly. Also, x needs to be after ..., because initialize is defined to take unnamed arguments that represent contained classes — new("MySpecies", new("Species")) is required to work, even if it is a way of constructing arguments that you do not use directly. The initialize method above doesn’t actually do anything more than the default initialize method, so in reality (and this is often the case) it makes sense not to write an initialize method at all.

    And then in more recent R, setClass returns a default constructor so

    MySpecies <- setClass("MySpecies", contains="Species", ##Species is an S4 class
                          representation(x="numeric"))
    

    and then

    > MySpecies(x=1:5, y=5:1)
    An object of class "MySpecies"
    Slot "x":
    [1] 1 2 3 4 5
    
    Slot "y":
    [1] 5 4 3 2 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some sprites that I do not know what class to subclass from.
I want to subclass the built-in DropDownList in ASP.NET so that I can add
I want to add a custom assert method to a TestCase subclass. I tried
I want my UIView subclass to behave so that when it is fitted in
I want my AbstracttableModel subclass data() method to return html i.e. PreText<b>Text</b>PostText And this
I want a non-editable TextField (or a subclass) that doesn't even have the caret
I want to have a UIView subclass that has a border image, but I
I want to add some behavior to a certain class at runtime. I know
I want to add methods (more specifically: method aliases) automatically to Python subclasses. If
I want to create a generic class, whose builder would not return an instance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.