[R] S4 method signature - integer matrix
David Winsemius
dwinsemius at comcast.net
Sat Jul 20 00:03:51 CEST 2013
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