From deepayan.sarkar at gmail.com  Wed Oct 19 19:45:50 2005
From: deepayan.sarkar at gmail.com (Deepayan Sarkar)
Date: Wed, 19 Oct 2005 12:45:50 -0500
Subject: [Rd] extending lattice to S4 classes
In-Reply-To: <4354D388.9000209@ipimar.pt>
References: <434F88B9.1020600@ipimar.pt> <434FD8C2.205@ipimar.pt>
	<Pine.LNX.4.61.0510150758480.27345@gannet.stats>
	<4354D388.9000209@ipimar.pt>
Message-ID: <eb555e660510191045s5de87a89p714e5fa8929a053b@mail.gmail.com>

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



[Rd] extending lattice to S4 classes
Deepayan Sarkar