Description Usage Arguments Value See Also Examples
Return or check the supposed primary and foreign keys in
a DBTABLES
object, or show, traverse,
combine, or split such objects.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | ## S4 method for signature 'DBTABLES,ANY,character,logical'
by(data,
INDICES, FUN, ..., do_map = NULL, do_inline = FALSE, do_quote = '"',
simplify)
## S4 method for signature 'DBTABLES,ANY,character,missing'
by(data,
INDICES, FUN, ..., do_map = NULL, do_inline = FALSE, do_quote = '"',
simplify)
## S4 method for signature 'DBTABLES,ANY,function,logical'
by(data,
INDICES, FUN, ..., do_map = NULL, do_inline = FALSE, do_quote = '"',
simplify)
## S4 method for signature 'DBTABLES,ANY,function,missing'
by(data,
INDICES, FUN, ..., do_map = NULL, do_inline = FALSE, do_quote = '"',
simplify)
## S4 method for signature 'DBTABLES'
c(x, ..., recursive = FALSE)
## S4 method for signature 'DBTABLES'
fkeys(object)
## S4 method for signature 'DBTABLES'
fkeys_valid(object)
## S4 method for signature 'DBTABLES'
head(x)
## S4 method for signature 'DBTABLES'
length(x)
## S4 method for signature 'DBTABLES'
pkeys(object)
## S4 method for signature 'DBTABLES'
pkeys_valid(object)
## S4 method for signature 'DBTABLES'
show(object)
## S4 method for signature 'DBTABLES,logical'
sort(x, decreasing)
## S4 method for signature 'DBTABLES,missing'
sort(x, decreasing)
## S4 method for signature 'DBTABLES,ANY,logical'
split(x, f, drop)
## S4 method for signature 'DBTABLES,ANY,missing'
split(x, f, drop)
## S4 method for signature 'DBTABLES,missing,logical'
split(x, f, drop)
## S4 method for signature 'DBTABLES,missing,missing'
split(x, f, drop)
## S4 method for signature 'DBTABLES'
summary(object)
## S4 method for signature 'DBTABLES'
tail(x)
## S4 method for signature 'DBTABLES'
update(object, start, drop = TRUE)
|
object |
|
x |
|
data |
|
decreasing |
Logical scalar. If missing,
|
start |
Numeric vector or If |
recursive |
Logical scalar passed from |
f |
Missing or (list of) factor(s) that fit(s) to the dimensions of the first table returned by. If missing, the primary key of this table is used. |
drop |
For For |
INDICES |
If |
FUN |
Function to be applied to each table in turn.
If If If |
... |
Objects of the same class as |
do_map |
Optional vector for mapping the slot names
in |
do_inline |
Logical scalar indicating how |
do_quote |
Character scalar or function used for generating SQL identifiers. If a function, used directly; otherwise used as quote character (to be doubled within the character string). |
simplify |
Logical scalar. If |
pkeys
yields a character vector with the
(expected) name of the primary key column for each table.
fkeys
returns a matrix that describes the
(expected) relations between the tables.
fkeys_valid
and pkeys_valid
return
TRUE
if successful and a character vector
describing the problems otherwise. These functions can
thus be used as validity
argument of
setClass
.
summary
creates an S3 object that nicely prints to
the screen (and is used by show
).
length
returns the number of rows of the data
frame in the slot defined by the first entry of
fkeys
.
head
and tail
return the minimum and
maximum available primary key, respectively, for all
contained tables.
sort
sorts all tables by their primary keys and
returns an object of the same class.
update
and c
return an object of the same
class than object
and x
, respectively.
c
runs update
on all objects in ...
to yield overall unique IDs and then runs rbind
on
all tables.
split
uses f
for splitting the first table
returned by pkeys
and then proceeds by accordingly
splitting the tables that refer to it. c
can be
used to get the original object back.
by
returns the result of FUN
or
INDICES
as a list or other kind of object,
depending on simplify
, if do_inline
is
TRUE
.
If do_inline
is FALSE
, by
creates a
novel DBTABLES
object. It starts with passing
INDICES
as indexes (optionally within an
SQL statement) to FUN
, which should
yield a data frame to be inserted as first table. Further
INDICES
arguments are generated using the
information returned by fkeys
to fill the object.
Child classes might need to define a prototype with data
frames that might be empty but already contain the column
naming that defines the cross references.
methods::setClass
Other dbtables: DBTABLES-class
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | # example class, with 'results' referring to 'experiments'
setClass("myclass",
contains = "DBTABLES",
slots = c(experiments = "data.frame", results = "data.frame"))
x <- new("myclass",
experiments = data.frame(id = 3:1, researcher = "Jane Doe"),
results = data.frame(id = 1:5, experiment_id = c(1L, 3L, 2L, 1L, 3L),
type = c("A", "B", "A", "B", "A"), value = runif(5)))
summary(x)
length(x) # not the number of slots
pkeys(x)
fkeys(x) # NA entries are used for table without foreign keys
# conduct some checks
stopifnot(fkeys_valid(x), pkeys_valid(x), length(x) == 3)
stopifnot(anyNA(fkeys(x)), !all(is.na(fkeys(x))))
# originally the primary keys are not in order here
(y <- sort(x))
slot(y, "experiments")
slot(y, "results")
stopifnot(!identical(x, y))
# get first and last primary keys for each table
head(x)
tail(x)
stopifnot(head(x) == 1, tail(x) >= head(x))
# modify the primary keys
start <- 3:4 # one value per table
y <- update(x, start)
head(y)
tail(y)
stopifnot(head(y) == start, tail(y) > start)
stopifnot(fkeys_valid(y), pkeys_valid(y)) # must still be OK
(y <- update(y, NULL))
head(y)
tail(y)
stopifnot(head(y) == 1, tail(y) > 1)
stopifnot(fkeys_valid(y), pkeys_valid(y))
# split the data
(y <- split(x))
stopifnot(sapply(y, length) == 1)
stopifnot(sapply(y, fkeys_valid), sapply(y, pkeys_valid))
# combine data again
(y <- do.call(c, y))
stopifnot(length(y) == length(x), class(x) == class(y))
stopifnot(fkeys_valid(y), pkeys_valid(y))
## ids are not necessarily the same than before but still OK
# traverse the object
(y <- by(x, TRUE, function(a, b) is.data.frame(b), simplify = FALSE))
stopifnot(unlist(y), !is.null(names(y)))
(z <- by(x, 2:1, function(a, b) is.character(a), simplify = FALSE))
stopifnot(unlist(z), names(z) == rev(names(y))) # other order
(z <- by(x, 1:2, function(a, b) a, simplify = FALSE,
do_map = c(experiments = "A", results = "B"))) # with renaming
stopifnot(unlist(z) == c("A", "B")) # new names passed as 2nd argument to FUN
# to illustrate by() in inline mode, we use a function that simply yields
# the already present slots
col_fun <- function(items, tbl, col, idx) {
tmp <- slot(items, tbl)
tmp[tmp[, col] %in% idx, , drop = FALSE]
}
(y <- by(x, slot(x, "experiments")[, "id"], col_fun, items = x,
do_inline = TRUE, simplify = FALSE))
stopifnot(identical(y, x))
# select only a subset of the indexes
(y <- by(x, c(2L, 1L), col_fun, items = x, do_inline = TRUE,
simplify = FALSE))
stopifnot(length(y) == 2)
# try a mapping that does not work
(y <- try(by(x, c(2L, 1L), col_fun, items = x, simplify = FALSE,
do_inline = TRUE, do_map = c(results = "notthere")), silent = TRUE))
stopifnot(inherits(y, "try-error"))
## note that non-matching names would be silently ignored
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.