[Rd] S4 coerce

From: Paul Gilbert <pgilbert_at_bank-banque-canada.ca>
Date: Mon, 16 Jul 2007 10:57:53 -0400


(I am not sure if this is a bug or a request for a more understandable warning, or possible something obvious I should be posting on r-help.)

I am trying to coerce an new class object to be a DBIConnection and it does not work the way I think it should:

R version 2.5.1 (2007-06-27) ...
> require("RMySQL") # or require("RSQLite")
Loading required package: RMySQL
Loading required package: DBI
[1] TRUE
> m <- dbDriver("MySQL") # or m <- dbDriver("SQLite")
> con <- dbConnect(m, dbname="test")
> dbGetQuery(con, "create table zzz (

+    vintage     VARCHAR(20) NOT NULL,
+    alias       VARCHAR(20) default NULL,
+    Documentation     TEXT,
+    PRIMARY KEY (vintage)
+    );")

NULL
> dbListTables(con)

  [1] "zzz"
> setClass("TSconnection", representation(con="DBIConnection",
+    vintage = "logical",
+    panel   = "logical")
+    )

[1] "TSconnection"
> setAs("TSconnection", "DBIConnection", def = function(from) from_at_con)
> setIs("TSconnection", "DBIConnection", coerce = function(x) x_at_con)
Warning messages:
1: there is no automatic definition for as(object, "DBIConnection") <- value when object has class "TSconnection" and no 'replace' argument was supplied; replacement will be an error in: makeExtends(class1, class2, coerce, test, replace, by, classDef1 = classDef, 2: methods currently exist for coercing from "TSconnection" to "DBIConnection"; they will be replaced. in: ..removePreviousCoerce(class1, class2, where, prevIs)

# (warning may be important, but I don't understand it)

> setAs("TSconnection", "MySQLConnection", def = function(from) from_at_con)
> setIs("TSconnection", "MySQLConnection", coerce = function(x) x_at_con)
Warning messages:
1: there is no automatic definition for as(object, "MySQLConnection") <- value when object has class "TSconnection" and no 'replace' argument was supplied; replacement will be an error in: makeExtends(class1, class2, coerce, test, replace, by, classDef1 = classDef, 2: methods currently exist for coercing from "TSconnection" to "MySQLConnection"; they will be replaced. in: ..removePreviousCoerce(class1, class2, where, prevIs)

> # or
> # setAs("TSconnection", "SQLiteConnection", def = function(from)
from_at_con)
> # setIs("TSconnection", "SQLiteConnection", coerce = function(x) x_at_con)

> Tcon <- new("TSconnection", con=dbConnect(m, dbname="test"),
vintage=FALSE, panel=FALSE)
> is(Tcon, "DBIConnection")

[1] TRUE
> is(Tcon, "MySQLConnection")

[1] TRUE
> # or is(Tcon, "SQLiteConnection")

> # This fails but I think it should work
> dbListTables(Tcon)

Error in slot(from, "Id") : no slot of name "Id" for this object of class "TSconnection"

#these all work
> dbListTables(Tcon_at_con)

  [1] "zzz"
> dbListTables(as(Tcon, "MySQLConnection") )
  [1] "zzz"
> dbListTables(as(Tcon, "DBIConnection") )
  [1] "zzz"
 >

Is this a bug or am I doing something wrong?

Thanks,
Paul Gilbert


La version franšaise suit le texte anglais.


This email may contain privileged and/or confidential inform...{{dropped}}



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 16 Jul 2007 - 16:07:21 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 Tue 17 Jul 2007 - 06:36:46 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.