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

  • SEARCH
  • Home
  • 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 8554633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:55:39+00:00 2026-06-11T14:55:39+00:00

Suppose all of your S4 methods associated to a specific S4 generic function/method share

  • 0

Suppose all of your S4 methods associated to a specific S4 generic function/method share a formal argument that is supposed to have a specific default value. Intuitively, I would state such an argument in the definition of the S4 generic (as opposed to stating it in each method definition which would seem somewhat redundant to me).

However, I noticed that this way I’m running into trouble as it seems that the default value of the formal argument is not dispatched to the methods and thus an error is thrown.

Isn’t this somewhat against the idea of having a combination of a generic and methods? Why would I have to state the formal argument in each method separately again when the default value is always the same? Can I explicitly dispatch formal arguments’ default values somehow?


Below you’ll find a short illustration of the behavior

Generic function

setGeneric(
    name="testFoo",
    signature=c("x", "y"),
    def=function(
        x,
        y,
        do.both=FALSE,
        ...
    ) {
    standardGeneric("testFoo")       
    }
)

Method

setMethod(
    f="testFoo", 
    signature=signature(x="numeric", y="numeric"),
    definition=function(
        x,
        y
    ) { 
    if (do.both) {
        out <- list(x=x, y=y)
    } else {
        out <- x
    }
    return(out)
    }
)

Error

> testFoo(x=1, y=2)
Error in .local(x, y, ...) : object 'do.both' not found

Redundant statement of do.both fixes it

setMethod(
    f="testFoo", 
    signature=signature(x="numeric", y="numeric"),
    definition=function(
        x,
        y,
        do.both=FALSE
    ) { 
    if (do.both) {
        out <- list(x=x, y=y)
    } else {
        out <- x
    }
    return(out)
    }
)

> testFoo(x=1, y=2)
[1] 1
  • 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-11T14:55:40+00:00Added an answer on June 11, 2026 at 2:55 pm

    When you call testFoo(x=1, y=2), it is processed first by the S4 generic, which looks for a method, finds it, and dispatches to it a call that looks like this: testFoo(x=1, y=2, do.both=FALSE, ...).

    In the words of ?standardGeneric:

    ‘standardGeneric’ dispatches the method defined for a generic
    function named ‘f’, using the actual arguments in the frame from
    which it is called.

    If the method to which it dispatches that call does not take a do.both argument, the method — just like any other R function — throws an error. No function can process a call containing an argument foo unless it’s function definition contains either (a) a formal argument foo or (b) a “dots” argument, ..., which can absorb arbitrary supplied arguments.

    Basically what you’ve tried is no different than the following, which fails in a similarly but perhaps easier-to-see way:

    testFooGeneric <- function(x=1, y=2, do.both=FALSE, ...) {
        ## The line below does essentially what standardGeneric() does
        if(is.numeric(x) & is.numeric(y)) {
            testFooMethod(x=x, y=y, do.both=do.both)
        }
    }
    
    testFooMethod <- function(x, y) {
        cat("Success!\n")
    }
    
    testFooGeneric(x=1, y=2)
    # Error in testFooMethod(x = x, y = y, do.both = do.both) : 
    #   unused argument(s) (do.both = do.both)
    

    To fix the above, you need to redefine testFooMethod() in one of the following two ways, either of which will also remedy your S4 method:

    ## Option 1
    testFooMethod <- function(x, y, do.both) {
        cat("Success!\n")
    }
    testFooGeneric(x=1, y=2)
    # Success!
    
    ## Option 2
    testFooMethod <- function(x, y, ...) {
        cat("Success!\n")
    }
    testFooGeneric(x=1, y=2)
    ## Success!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a customer object that has all of the standard customer properties
Suppose you have two static libraries A and B such that A references methods
Let's suppose I have all of these already provided: The name of a table
Suppose I have the following code to collect all the possible combinations. abArray =
Suppose we have abstract class A (all examples in C#) public abstract class A
Suppose you take the strings 'a' and 'z' and list all the strings that
Suppose I have a column called fruits I want to select all of the
Suppose you have a file that you are programmatically logging information into with regards
Suppose you have a method: public void Save(Entity data) { this.repositoryIocInstance.EntitySave(data); } Would you
Suppose you have a web site consisting of: A web server serving your various

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.