Re: [Rd] S4 coercion

From: Martin Morgan <mtmorgan_at_fhcrc.org>
Date: Wed, 27 Aug 2008 08:46:36 -0700

Hi Paul -- does this make the problem more explicit?

> ## class hierarchy
> setClass("A", representation=representation(a="numeric"))
[1] "A"
> setClass("B", representation=representation(b="numeric"))
[1] "B"
> setClass("C", contains=c("A", "B"))

[1] "C"

> ## coerce methods -- in some instances implicitly created
> setAs("A", "numeric", function(from) slot(from, "a"))
> setAs("B", "numeric", function(from) slot(from, "b"))

> ## trouble: C equidistant from A, B so no unambiguous nearest 'coerce'
> ## method
> as(new("C"), "numeric")

numeric(0)
Warning message:
Ambiguous method selection for "coerce", target "C#numeric" (the first of the signatures shown will be used)

    A#numeric
    B#numeric

The solution is to define a method to resolve the conflict, e.g.,

setAs("C", "numeric", function(from) {

    ## avoid breaking the class abstraction     as(as(from, "A"), "numeric") *

            as(as(from, "B"), "numeric") })

I don't know how you want to break your particular tie; that has to do with the classes you're extending.

Martin

Paul Gilbert <pgilbert_at_bank-banque-canada.ca> writes:

> I am extending a DBI connection by
>
> setClass("TSPostgreSQLConnection",
> contains=c("PostgreSQLConnection","TSdbOptions"))
>
> but the first time I use this I am getting a warning when it tries to
> coerce the TSPostgreSQLConnection to a PostgreSQLConnection. After
> the first use the warning stops, but the first warning is causing me
> problems when I do automatic checks and set my tests to stop on
> warnings. (I think there should be a correct way to do this that does
> not produce a warning.) I can trace it back by setting
> options(warn=2) as below. Do I need to be more specific about how the
> coercion happens? If so, what is the correct way to coerce the
> TSPostgreSQLConnection into a PostgreSQLConnection? If not, how to a
> get rid of the warning?
>
> Paul Gilbert
>
>
> > TSdelete("vec", con)
> Error: (converted from warning) Ambiguous method selection for
> "coerce", target "TSPostgreSQLConnection#integer" (the first of the
> signatures shown will be used)
> PostgreSQLConnection#integer
> dbObjectId#integer
> > traceback()
> 15: doWithOneRestart(return(expr), restart)
> 14: withOneRestart(expr, restarts[[1]])
> 13: withRestarts({
> .Internal(.signalCondition(simpleWarning(msg, call), msg,
> call))
> .Internal(.dfltWarn(msg, call))
> }, muffleWarning = function() NULL)
> 12: .signalSimpleWarning("Ambiguous method selection for \"coerce\",
> target \"TSPostgreSQLConnection#integer\" (the first of the signatures
> shown will be used)\n PostgreSQLConnection#integer\n
> dbObjectId#integer\n",
> quote(NULL))
> 11: warning(gettextf(paste("Ambiguous method selection for \"%s\",
> target \"%s\"",
> "(the first of the signatures shown will be used)\n%s\n"),
> fdef_at_generic, .sigLabel(classes), paste(" ", names(methods),
> collapse = "\n")), domain = NA, call. = FALSE)
> 10: .findInheritedMethods(signature, fdef, mtable = allmethods, table
> = mlist,
> useInherited = useInherited, verbose = verbose)
> 9: selectMethod("coerce", sig, optional = TRUE, c(from = TRUE, to = FALSE),
> fdef = coerceFun, mlist = coerceMethods)
> 8: as(obj, "integer")
> 7: isIdCurrent(con)
> 6: postgresqlQuickSQL(conn, statement, ...)
> 5: dbGetQuery(con, paste("SELECT tbl FROM Meta ", where, ";"))
> 4: dbGetQuery(con, paste("SELECT tbl FROM Meta ", where, ";"))
> 3: TSdbi:::TSdeleteSQL(serIDs = serIDs, con = con, ...)
> 2: TSdelete("vec", con)
> 1: TSdelete("vec", con)
> > str(con)
> Formal class 'TSPostgreSQLConnection' [package "TSPostgreSQL"] with 4 slots
> ..@ Id : int [1:2] 21795 1
> ..@ dbname : chr "test"
> ..@ vintage: logi FALSE
> ..@ panel : logi FALSE
> ====================================================================================
>
> La version franšaise suit le texte anglais.
>
> ------------------------------------------------------------------------------------
>
> This email may contain privileged and/or confidential in...{{dropped:26}}
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 27 Aug 2008 - 15:50:13 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Aug 2008 - 16:37:45 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive