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 8026177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:24:18+00:00 2026-06-04T23:24:18+00:00

How do I test if an object is a instance of an S4 class

  • 0

How do I test if an object is a instance of an S4 class (not sure if this is the right terminology)? I know about the function isS4 however, this also returns true for class definitions, etc. E.g.:

traj <- setClass(
  Class="Trajectories",
  representation=representation(
    times = "numeric",
    traj = "matrix"
  )
)

trajclass <- getClass("Trajectories")
trajobject <- new(Class="Trajectories",times=c(1,3),traj=matrix(1:4,ncol=2))

isS4(traj)
isS4(trajclass)
isS4(trajobject)

I am only interested in objects containing data, trajobject in this case; not so much in methods or class definitions. Is there a native function that I can use to test if an S4 object is an actual object? I.e. when using print(object) the output starts with:

An object of class “foo”…..

  • 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-04T23:24:19+00:00Added an answer on June 4, 2026 at 11:24 pm

    S4 classes (and reference classes, for that matter and perhaps to the chagrin of some) are defined using S4 classes (cool (?) eh?)

    > class(trajclass)
    [1] "classRepresentation"
    attr(,"package")
    [1] "methods"
    > getClass(class(trajclass))
    Class "classRepresentation" [package "methods"]
    
    Slots:
    
    Name:             slots         contains          virtual        prototype
    Class:             list             list          logical              ANY
    
    Name:          validity           access        className          package
    Class: OptionalFunction             list        character        character
    
    Name:        subclasses       versionKey           sealed
    Class:             list      externalptr          logical
    
    Known Subclasses: "ClassUnionRepresentation", "refClassRepresentation"
    

    I think the best you could do is along the lines of

    isS4(trajclass) && !is(trajclass, "classRepresentation")
    

    S4 uses show as a synonym (sort of) for print; there is an S4 show method for classRepresentation that is

    > selectMethod(show, class(traj))
    Method Definition:
    
    function (object) 
    {
        cat(gettextf("Class generator function for class \"%s\" from package \"%s\"\n", 
            object@className, object@package))
        show(as(object, "function"))
    }
    <bytecode: 0x1522d40>
    <environment: 0xf1d808>
    
    Signatures:
            object                  
    target  "classGeneratorFunction"
    defined "classGeneratorFunction"
    

    whereas the show method for trajobject is the default, which is

    > selectMethod(show, class(trajobject))
    Method Definition (Class "derivedDefaultMethod"):
    
    function (object) 
    showDefault(object, FALSE)
    <bytecode: 0x1516d08>
    <environment: namespace:methods>
    
    Signatures:
            object        
    target  "Trajectories"
    defined "ANY"         
    

    with the meat in methods::showDefault. I guess you could implement

    setGeneric("isNotS4ClassDef",
        function(object) standardGeneric("isNotS4ClassDef"))
    
    setMethod(isNotS4ClassDef, "ANY",
        function(object) TRUE)
    
    setMethod(isNotS4ClassDef, "classRepresentation",
        function(object) FALSE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error object has no attribute 'im_func' with this class Test(object): def
I'm not sure if this is a trivial questions but in a PHP class:
Hello i have this code class Test(object): def start_conn(self): pass def __init__(self): self.conn =
i have a method as follows. test(Object obj){ } now within this method i
Why isn't my code around the test function in Object RegExp working the same
I don't understand why can't i access values like this: object = { test:{
I'm not sure if this is possible in Delphi. I've looked around and can't
I'm new to Moq and not quite sure why this won't run. Repository Interface
I apologize if this was asked somewhere else, but I do not know how
I know in Java we can create an instance of a Class by new

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.