schemas <- relation_schema(
list(
a = list(c("a", "b"), list("a")),
b = list(c("b", "c"), list("b", "c"))
),
attrs_order = c("a", "b", "c", "d")
)
print(schemas)
attrs(schemas)
keys(schemas)
attrs_order(schemas)
names(schemas)
# vector operations
schemas2 <- relation_schema(
list(
e = list(c("a", "e"), list("e"))
),
attrs_order = c("a", "e")
)
c(schemas, schemas2) # attrs_order attributes are merged
unique(c(schemas, schemas))
# subsetting
schemas[1]
schemas[c(1, 2, 1)]
stopifnot(identical(schemas[[1]], schemas[1]))
# reassignment
schemas3 <- schemas
schemas3[2] <- relation_schema(
list(d = list(c("d", "c"), list("d"))),
attrs_order(schemas3)
)
print(schemas3) # note the schema's name doesn't change
# names(schemas3)[2] <- "d" # this would change the name
keys(schemas3)[[2]] <- list(character()) # removing keys first...
attrs(schemas3)[[2]] <- c("b", "c") # so we can change the attrs legally
keys(schemas3)[[2]] <- list("b", "c") # add the new keys
stopifnot(identical(schemas3, schemas))
# changing appearance priority for attributes
attrs_order(schemas3) <- c("d", "c", "b", "a")
print(schemas3)
# reconstructing from components
schemas_recon <- relation_schema(
Map(list, attrs(schemas), keys(schemas)),
attrs_order(schemas)
)
stopifnot(identical(schemas_recon, schemas))
# can be a data frame column
data.frame(id = 1:2, schema = schemas)
Run the code above in your browser using DataLab