[Rd] S4 coerce
Paul Gilbert
pgilbert at bank-banque-canada.ca
Mon Jul 16 16:57:53 CEST 2007
(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}}
More information about the R-devel
mailing list