rels <- relation(
list(
a = list(
df = data.frame(a = logical(), b = logical()),
keys = list("a")
),
b = list(
df = data.frame(b = logical(), c = logical()),
keys = list("b", "c")
)
),
attrs_order = c("a", "b", "c", "d")
)
print(rels)
records(rels)
attrs(rels)
stopifnot(identical(
attrs(rels),
lapply(records(rels), names)
))
keys(rels)
attrs_order(rels)
names(rels)
# inserting data
insert(rels, data.frame(a = 1L, b = 2L, c = 3L, d = 4L))
# data is only inserted into relations where all columns are given...
insert(rels, data.frame(a = 1L, b = 2L, c = 3L))
# and that are listed in relations argument
insert(
rels,
data.frame(a = 1L, b = 2L, c = 3L, d = 4L),
relations = "a"
)
# vector operations
rels2 <- relation(
list(
e = list(
df = data.frame(a = logical(), e = logical()),
keys = list("e")
)
),
attrs_order = c("a", "e")
)
c(rels, rels2) # attrs_order attributes are merged
unique(c(rels, rels))
# subsetting
rels[1]
rels[c(1, 2, 1)]
stopifnot(identical(rels[[1]], rels[1]))
# reassignment
rels3 <- rels
rels3[2] <- relation(
list(
d = list(
df = data.frame(d = logical(), c = logical()),
keys = list("d")
)
),
attrs_order(rels3)
)
print(rels3) # note the relation's name doesn't change
# names(rels3)[2] <- "d" # this would change the name
keys(rels3)[[2]] <- list(character()) # removing keys first...
# for a relation_schema, we could then change the attrs for
# the second relation. For a created relation, this is not
# allowed.
if (FALSE) {
attrs(rels3)[[2]] <- c("b", "c")
names(records(rels3)[[2]]) <- c("b", "c")
}
# changing appearance priority for attributes
rels4 <- rels
attrs_order(rels4) <- c("d", "c", "b", "a")
print(rels4)
# reconstructing from components
rels_recon <- relation(
Map(list, df = records(rels), keys = keys(rels)),
attrs_order(rels)
)
stopifnot(identical(rels_recon, rels))
# can be a data frame column
data.frame(id = 1:2, relation = rels)
Run the code above in your browser using DataLab