[R] S4 method signature - integer matrix

Simon Zehnder szehnder at uni-bonn.de
Sat Jul 20 14:06:51 CEST 2013


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.

From your suggestions I had a direction to search more precisely and I found this: 

Set the signature to "matrix": checks for matrix when method is called.
Then check for typeof(mymatrix) == "integer": Return an exception if FALSE.
In case of an index I can add then an all(apply(mymatrix, c(1, 2), ">", 0)): Return an exception if FALSE

Thank you very much for your advice. It helped me to find a good solution.


Best 

Simon


On Jul 20, 2013, at 12:03 AM, David Winsemius <dwinsemius at comcast.net> wrote:

> 
> On Jul 19, 2013, at 9:54 AM, Simon Zehnder wrote:
> 
>> 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?
> 
> 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
> 
> 
>> 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)?
> 
> It would need be both integer mode...   ?is.integer and have dimensions...  ?dim
> 
>> Furthermore: How would you check for an integer matrix with values 
>> bigger one (so the typical R indices)?
> 
> Just add a test in the 'validity' code. See ... ?setClass
> 
>> 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?
> 
> 
> I don't see a reason to use apply.
> 
> -- 
> David Winsemius
> Alameda, CA, USA
> 



More information about the R-help mailing list