Description Objects from the Class Slots Methods S3 Methods Implementation Author(s) See Also
Each “dbframe” object references a particular table inside an sql database. The class and its methods are designed to query and manipulate the table easily inside R.
Objects can be created by calls of the form
new("dbframe", ...)
, but I recommend that you use the
constructor dbframe.
The dbframe{\char95}sqlite class creates a link to a table in an sqlite database, and dbframe acts as a superclass. dbframe{\char95}sqlite{\char95}temporary implements temporary sqlite databases. I'd like to add other classes that store links to other databases (i.e. Mysql and Postgresql) as well. Until then, dbframe also works as an experimental class that tries to guess how to open a connection to the right database—please let me know how well it works, or if it works at all.
dbframe
dbConnect.arguments
:Object of class "list"
, a
list of arguments to pass to dbConnect when it's time to
connect to the database.
readonly
:Object of class "logical"
that
indicates whether the user is allowed to write to the database.
Be careful! This is implemented only as a safeguard against
carelessness; it is still very easy to write to the table.
table
:Object of class "character"
, the name
of the table associated with this dbframe object.
dbframe_sqlite
dbname
:Object of class "character"
, the filename
of the associated SQLite database.
rowid
:Object of class "integer"
, the rowid of the
last value inserted into the table.
dbframe
signature(... = "dbframe")
: Remove the table
corresponding to the dbframe object from its database.
Please see the clear documentation for details.
signature(x = "dbframe_sqlite")
: Returns the
filename of the sqlite database associated with the object.
<<Define "dbname" method>>= setMethod("dbname", signature = c("dbframe"), function(x) x@dbname)
signature(drv = "dbframe")
:
Establishes a connection with the database associated with the
object.
<<Define "dbConnect" method for "dbframe">>= setMethod("dbConnect", signature = "dbframe", definition = function(drv,...) do.call("dbConnect", drv@dbConnect.arguments))
signature(x = "dbframe")
: Insert records
into the dbframe table. Please see individual
documentation for details.
signature(x = "dbframe")
: Determine whether
the dbframe object is read-only.
<<Define "readonly" method>>= setMethod("readonly", signature = c("dbframe"), function(x) x@readonly)
Query the dbframe table. Please see individual documentation for details.
signature(x = "dbframe")
: Returns the table
name associated with the object.
<<Define "tablename" method>>= setMethod("tablename", signature = c("dbframe"), function(x) x@table)
signature(x = "dbframe")
: Check whether
the table associated with the dbframe exists.
<<Define "is.linked" method>>= setMethod("is.linked", signature = c("dbframe"), function(x,...) { dbc <- dbConnect(x,...) answer <- tablename(x) %in% dbListTables(dbc) dbDisconnect(dbc) return(answer) })
dbframe_sqlite
signature(drv = "dbframe_sqlite")
:
Establishes a connection with the database associated with the
object.
<<Define "dbConnect" method for "dbframe_sqlite">>= setMethod("dbConnect", signature = "dbframe_sqlite", definition = function(drv,...) return(do.call("dbConnect", c(drv = "SQLite", dbname = dbname(drv), list(...), dbConnect.arguments = drv@dbConnect.arguments))))
signature(x = "dbframe_sqlite")
: Returns the
rowid associated with the last insert.
<<Define "rowid" method>>= setMethod("rowid", signature = c("dbframe_sqlite"), function(x,...) x@rowid)
signature(x = "dbframe_sqlite")
: Assigns the
rowid associated with the last insert.
<<Define "rowid<-" method>>= setMethod("rowid<-", signature = c("dbframe_sqlite"), function(x,...,value) { x@rowid <- as.integer(value) return(x)})
as.data.frame.dbframe
: Coerce a
dbframe object to a data frame.
<<Define "as.data.frame" method>>= as.data.frame.dbframe <- function(x,...) select(x,...)
dim.dbframe
: Determine the number of
rows and columns in a dbframe.
<<Define "dim" method>>= dim.dbframe <- function(x) { nrows <- select(x, "count(*)")[[1]] ncols <- length(select(x, limit = 0)) c(nrows, ncols) }
The basic definition of these classes is straightforward. Individual methods are defined above; I'll define the generic functions here as necessary; note that the generic function for dbConnect is defined in the DBI package.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <<*>>=
setClass("dbframe", representation(table = "character",
readonly = "logical",
dbConnect.arguments = "list"))
setClass("dbframe_sqlite", contains = "dbframe",
representation(rowid = "integer", dbname = "character"))
<<Define "dbConnect" method for "dbframe">>
<<Define "dbConnect" method for "dbframe_sqlite">>
<<Define "as.data.frame" method>>
<<Define "dim" method>>
setGeneric("tablename", function(x) standardGeneric("tablename"))
<<Define "tablename" method>>
setGeneric("dbname", function(x) standardGeneric("dbname"))
<<Define "dbname" method>>
setGeneric("readonly", function(x) standardGeneric("readonly"))
<<Define "readonly" method>>
setGeneric("is.linked", function(x,...) standardGeneric("is.linked"))
<<Define "is.linked" method>>
setGeneric("rowid", function(x,...) standardGeneric("rowid"))
<<Define "rowid" method>>
setGeneric("rowid<-", function(x,...,value) standardGeneric("rowid<-"))
<<Define "rowid<-" method>>
|
Gray Calhoun gcalhoun@iastate.edu
dbframe
, clear
,
insert<-
, select
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.