Skip to content

S4 method signature - integer matrix

3 messages · Simon Zehnder, David Winsemius

#
Dear R-Users and R-Devels,

I am programming a package with S4 classes and I search for a solution of the following problem: 

If you want an S4 method to await an integer argument you set the signature like this

setMethod("myfunction", signature(object = "myClass", y = "integer"), function(object, y) {//Do sth})

Now, if you want the method to await an integer matrix or array there is only one way how to define it

setMethod("myfunction", signature(object = "myClass", y = "matrix"), function(object, y) {//Do sth}) or
setMethod("myfunction", signature(object = "myClass", y = "array"), function(object, y) {//Do sth}) 

am I right? WIth this you can also pass a numeric matrix.

How would you check for an integer matrix in an S4 method (in the index function of R I think it is just coerced to integer)? Furthermore: How would you check for an integer matrix with values 
bigger one (so the typical R indices)? Is there a way how it is usually done in R (I think probably with apply())? Or is it usually better to throw an exception?


Best

Simon
#
On Jul 19, 2013, at 9:54 AM, Simon Zehnder wrote:

            
I don't think that is the only way. You could also test for mode being 'numeric' at the signature level and then within the validation check to see whether it has dimensions. See ?is.numeric and ?validObject

There is also the option of using a prototype.

As I understand the S4 checks they will apply an is.<mode> or is.<class> test as long as there is an correspond function that returns a logical. see ... ?is
It would need be both integer mode...   ?is.integer and have dimensions...  ?dim
Just add a test in the 'validity' code. See ... ?setClass
I don't see a reason to use apply.
#
Hi David,

thanks for the reply! 

The prototype solution is I think not the appropriate solution, when the method adds the integer matrix next to the class object into the argument list. Also the .validObject method can only be applied to the object for which the method is called or I have to write an integer matrix class that has its own .validObject method.