[Rd] extending lattice to S4 classes

ernesto ernesto at ipimar.pt
Thu Oct 20 11:00:16 CEST 2005


Deepayan Sarkar wrote:

>On 10/18/05, ernesto <ernesto at ipimar.pt> wrote:
>  
>
>>Prof Brian Ripley wrote:
>>
>>    
>>
>>>I think you are confusing us:  xyplot is an S3 generic with no 'data'
>>>argument.  It is xyplot.formula that you want to add dispatch on its
>>>'data' argument.  I don't really see why you want to mix S3 and S4
>>>systems, but you could make xyplot.formula an S3 or S4 generic and
>>>dispatch on 'data', just as you did earlier with xyplot.
>>>
>>>Other xyplot methods need not (and probably do not) have a 'data'
>>>argument.
>>>
>>>On Fri, 14 Oct 2005, ernesto wrote:
>>>
>>>      
>>>
>>>>ernesto wrote:
>>>>
>>>>        
>>>>
>>>>>Hi,
>>>>>
>>>>>I'm writing methods for a package called FLCore (see
>>>>>http://flr-project.org/) in particular for a class called "FLQuant"
>>>>>which extends array.
>>>>>
>>>>>Previously I was able to write methods based on the class of "data" but
>>>>>now I can not do it due to the single parameter "x". Is there a way to
>>>>>do this ?
>>>>>          
>>>>>
>>>>I found an hack (see below) to make it work but it's terrible and hugly
>>>>... some suggestions are welcome. It simply tests if data belongs to
>>>>class "FLQuant" and transforms it into a data.frame before ploting.
>>>>
>>>>Regards
>>>>
>>>>EJ
>>>>
>>>>if (!isGeneric("xyplot")) {
>>>>   setGeneric("xyplot", useAsDefault = xyplot)
>>>>}
>>>>
>>>>
>>>>setMethod("xyplot", signature("formula"), function(x, ...){
>>>>
>>>>   dots <- list(...)
>>>>   if(class(dots$data)=="FLQuant") dots$data <- as.data.frame(dots$data)
>>>>   call.list <- c(x = x, dots)
>>>>
>>>># needed this to avoid an infinite loop because xyplot is defined only
>>>>for "x"
>>>>   xyplot <- lattice::xyplot
>>>>   ans <- do.call("xyplot", call.list)
>>>>   ans$call <- match.call()
>>>>   ans
>>>>
>>>>})
>>>>        
>>>>
>>>      
>>>
>>Hi,
>>
>>Sorry to get back to this problem so late.
>>
>>I want to use lattice plots for an array. In this specific case this
>>array is of class FLQuant which is defined by package FLCore.
>>
>>This array has 5 dimensions age, year, unit, season and area. My aim is
>>to use formula to define what to plot and what to condition on. So
>>procedures like used for barchart.table are not usefull for me because
>>the user loses the option of defining the model to plot.
>>
>>With the previous version of lattice I was able to use formula and data
>>to dispatch. So I defined S4 generic methods for most high level methods
>>(bwplot, stripplot, dotplot, xyplot, histogram, etc) and simply
>>transform FLQuant objects into dataframes that were passed to the
>>lattice methods. No harm done to other data objects.
>>
>>With the new version only one object is available for dispatching "x" so
>>if I define some xyplot.FLQuant I will miss the formula argument.
>>
>>My question is if there is a way of doing this or not.
>>    
>>
>
>Prof Ripley gave you one (redefine xyplot.formula etc). You could
>always add new arguments to your methods, so you could have (the S4
>equivalent of)
>
>xyplot.FLQuant <- function(x, formula, ...)
>{
>    xyplot(formula, data = as(x, "data.frame"), ...)
>}
>
>and then call
>
>xyplot(flq.obj, formula = y ~ x, [...])
>
>The cleanest solution would be to have a data argument in the xyplot
>generic (with the expectation that it could be non-missing only for
>methods where x is a formula). I'll try to implement that and see if
>it causes any problems.
>
>Deepayan
>  
>
Hi Deepayan,

I see that there are alternatives, I found one my self that works and
it's transparent for the user.

I don't want to implement solutions that force the user to use lattice
methods differently from your implementation.

The cleanest solution as you say is to add a data argument to xyplot but
I can't do it so I would not propose it.

Regards

EJ



More information about the R-devel mailing list