[Rd] S4 generic for lowess

Oleg Sklyar osklyar at ebi.ac.uk
Tue May 27 21:18:15 CEST 2008


One can redefine range and similar ones (c, cbind, rbind etc) as S4 methods:

:: R version 2.8.0 Under development (unstable) (2008-04-22 r45454)
 > setGeneric("range")
[1] "range"
 > setClass("MyClass", representation("numeric",comment="character"))
[1] "MyClass"
 > setMethod("range", "MyClass", function(x, ..., na.rm = FALSE) {
+ print(x at comment)
+ range(x at .Data)
+ })
[1] "range"
 > a = new("MyClass", runif(5,1,25), comment="Hello world")
 > range(a)
[1] "Hello world"
[1]  4.953273 21.792185

This is a trivial example, but handling of ... in the function body is 
also possible thus making it less trivial in general.

Best,
Oleg

Iago Mosqueira wrote:
> Hi,
> 
> Henrik Bengtsson wrote:
>> A quick comment on design:
>>
>> On Tue, May 20, 2008 at 1:41 AM, Iago Mosqueira
>> <iago.mosqueira at gmail.com> wrote:
>>> Hi,
>>>
>>> A lowess method is defined in our package for one of our S4 classes. To
>>> explicitely define the generic this is being used
>>>
>>> if (!isGeneric("lowess"))
>>>  setGeneric("lowess", useAsDefault = lowess)
>>>
>>> This works fine for many other methods inherited from various R
>>> packages. In this case a warning is issued in R CMD check:
>>>
>>> * checking R code for possible problems ... NOTE
>>> lowess: no visible binding for global variable 'xy'
>>> lowess: no visible binding for global variable 'o'
>>>
>>> which I assume originates from the formals of lowess
>>>
>>> lowess(x, y = NULL, f = 2/3, iter = 3, delta = 0.01 * diff(range(xy$x[o])))
>>>
>>> where 'xy' and 'o' are present in the argument list but are really
>>> defined inside the function itself.
>> To my opinion, this could have been implemented as (or with some other
>> default value on 'delta'):
>>
>> lowess <- function(x, y=NULL, f=2/3, iter=3, delta=NULL) {
>>   [...]
>>   if (is.null(delta))
>>     delta <- 0.01 * diff(range(xy$x[o])));
>>   [...]
>> }
>>
>> and then document in the help pages what 'delta' defaults to.
>>
>> My $.02
>>
>> /Henrik
> 
> That would be extremely useful for our code. I have some other examples
> of functions or S3 methods that could benefit from changes in default
> arguments to make them more S4 friendly. range, for example, cannot be
> overload as its generic needs to be based on
> 
> function (..., na.rm = FALSE)  .Primitive("range"
> 
> Is there interest in the R Core Team in identifying these?
> 
> Regards,
> 
> 
> Iago
> 
> 
> 
>>> I suppose I can safely ignore this warning, and the new methods seems to
>>> work fine, but I was curious to know if there is a better way to deal
>>> with this.
>>>
>>> Many thanks,
>>>
>>>
>>> Iago
>>>
>>> --
>>> Iago Mosqueira
>>> Cefas
>>> Systems Modelling
>>>
>>> Pakefield Rd.
>>> Lowestoft NR33 0HT
>>> U.K.
>>> +44 (0)1502 558003
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466



More information about the R-devel mailing list