| Title: | Lightweight List Extensions that Enforce Unique Keys |
|---|---|
| Description: | Provides two lightweight keylist S3 classes 'klist' and 'knlist': extensions of list that enforce unique keys, supporting either mixed named/unnamed elements or fully named elements, ensuring predictable key-value access. |
| Authors: | Luke Jenkins [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-7206-7242>) |
| Maintainer: | Luke Jenkins <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0.9000 |
| Built: | 2026-05-31 06:24:19 UTC |
| Source: | https://github.com/lj-jenkins/keylist |
Create a list with unique keys (either fully named, knlist, or a mix of named and unnamed, klist), erroring if duplicate keys are found.
keylist(..., .named = FALSE) is.keylist(x) as.keylist(x, ...) ## Default S3 method: as.keylist(x, ..., .named = FALSE, .recursive = FALSE) keylist.append(klst, values, after = length(klst))keylist(..., .named = FALSE) is.keylist(x) as.keylist(x, ...) ## Default S3 method: as.keylist(x, ..., .named = FALSE, .recursive = FALSE) keylist.append(klst, values, after = length(klst))
... |
for |
.named |
boolean indicating whether the list should be fully named or not.
If |
x |
object to be coerced or tested. |
.recursive |
boolean indicating whether to recursively validate nested lists.
If |
klst |
passed to append: the keylist to which the values are to be appended. |
values |
passed to append: the values to be included in the modified keylist. |
after |
passed to append: a subscript, after which the values are to be appended. |
keylist() creates one of the two keylist objects: klist or knlist,
depending on the value of the .named argument. Those methods are
generally preferred as they are more explicit.
is.keylist() checks if an object inherits from either klist or knlist.
keylist.append() applies append to a keylist and validates the result
depending on whether the input was a klist or knlist.
keylist objects accept any R object as elements, but the keys must be
unique. The key validation is done at the top level, so nested lists
are not validated unless they are also keylists. To recursively turn
a nested list structure into keylists, use
as.keylist(x, .recursive = TRUE) or one of the lower level variants.
A list object of class klist or knlist. For is.keylist() a boolean.
keylists compare names using C's strcmp function.
as.list and as.vector methods for keylist objects remove the class and return a base R list or vector.
keylist(a = 1, 2, b = 3) # default is a klist try(keylist(1, a = 2, a = 1)) # duplicate keys not allowed x <- keylist(a = 1, b = 2, .named = TRUE) # create a knlist try(keylist(1, b = 2, .named = TRUE)) # unnamed keys not allowed try(x[[1]] <- 1) # knlist only accepts character indexing for assignment # objects within a keylist are not subject to validation keylist(1, list(a = 1, a = 2)) try(keylist(1, keylist(a = 1, a = 2))) # but nested keylists are # recursively validate and convert to keylist x <- list(1, list(1, 2)) x <- as.keylist(x, .recursive = TRUE) class(x[[2]]) # nested list is now a keylist is.keylist(klist(1)) && is.keylist(knlist(a = 1)) # TRUE keylist.append(klist(a = 1), list(2, b = 3)) # append to a klist # c() method for keylist objects also validates try(c(keylist(a = 1), list(a = 3)))keylist(a = 1, 2, b = 3) # default is a klist try(keylist(1, a = 2, a = 1)) # duplicate keys not allowed x <- keylist(a = 1, b = 2, .named = TRUE) # create a knlist try(keylist(1, b = 2, .named = TRUE)) # unnamed keys not allowed try(x[[1]] <- 1) # knlist only accepts character indexing for assignment # objects within a keylist are not subject to validation keylist(1, list(a = 1, a = 2)) try(keylist(1, keylist(a = 1, a = 2))) # but nested keylists are # recursively validate and convert to keylist x <- list(1, list(1, 2)) x <- as.keylist(x, .recursive = TRUE) class(x[[2]]) # nested list is now a keylist is.keylist(klist(1)) && is.keylist(knlist(a = 1)) # TRUE keylist.append(klist(a = 1), list(2, b = 3)) # append to a klist # c() method for keylist objects also validates try(c(keylist(a = 1), list(a = 3)))
Create a named/unnamed list with unique keys, erroring if any duplicate keys (names) are found.
klist(...) is.klist(x) as.klist(x, ...) ## Default S3 method: as.klist(x, ..., .recursive = FALSE) ## S3 replacement method for class 'klist' names(x) <- value ## S3 method for class 'klist' c(...)klist(...) is.klist(x) as.klist(x, ...) ## Default S3 method: as.klist(x, ..., .recursive = FALSE) ## S3 replacement method for class 'klist' names(x) <- value ## S3 method for class 'klist' c(...)
... |
for |
x |
object to be coerced or tested. |
.recursive |
boolean indicating whether to recursively validate nested lists.
If |
value |
a character vector of up to the same length as x, or NULL. Resulting names must be unique. |
names<- method for klist objects will apply the names and then validate
that the resulting names are unique.
c.klist method will combine the objects and then validate that the
resulting klist's names are unique.
For a list with unique keys of all names, see knlist.
A list object of class klist. For is.klist() a boolean.
klists compare names using C's strcmp function.
as.list and as.vector methods for klist objects remove the class and return a base R list or vector.
klist(a = 1, 2, b = 3) try(klist(1, a = 2, a = 1)) # duplicate keys not allowed # objects within a klist are not subject to validation klist(1, list(a = 1, a = 2)) try(klist(1, klist(a = 1, a = 2))) # but nested klists are # recursively validate and convert to klist x <- list(1, list(1, 2)) x <- as.klist(x, .recursive = TRUE) class(x[[2]]) # nested list is now a klist is.klist(klist(1)) # TRUE try(names(x) <- c("a", "a")) # names are validated when changed # c() method for klist objects also validates try(c(klist(a = 1), list(a = 3)))klist(a = 1, 2, b = 3) try(klist(1, a = 2, a = 1)) # duplicate keys not allowed # objects within a klist are not subject to validation klist(1, list(a = 1, a = 2)) try(klist(1, klist(a = 1, a = 2))) # but nested klists are # recursively validate and convert to klist x <- list(1, list(1, 2)) x <- as.klist(x, .recursive = TRUE) class(x[[2]]) # nested list is now a klist is.klist(klist(1)) # TRUE try(names(x) <- c("a", "a")) # names are validated when changed # c() method for klist objects also validates try(c(klist(a = 1), list(a = 3)))
Create a list with unique names/keys, erroring if any duplicate names/keys are found.
knlist(...) is.knlist(x) as.knlist(x, ...) ## Default S3 method: as.knlist(x, ..., .recursive = FALSE) ## S3 replacement method for class 'knlist' names(x) <- value ## S3 method for class 'knlist' c(...)knlist(...) is.knlist(x) as.knlist(x, ...) ## Default S3 method: as.knlist(x, ..., .recursive = FALSE) ## S3 replacement method for class 'knlist' names(x) <- value ## S3 method for class 'knlist' c(...)
... |
for |
x |
object to be coerced or tested. |
.recursive |
boolean indicating whether to recursively validate nested lists.
If |
value |
a character vector the same length as x. Resulting names must be unique. |
names<- method for knlist objects will apply the names and then validate
that the resulting names are unique.
c.knlist method will combine the objects and then validate that the
resulting knlist's names are unique.
For a list with unique keys of names and indexes, see klist.
A list object of class knlist. For is.knlist() a boolean.
knlists compare names using C's strcmp function.
as.list and as.vector methods for klist objects remove the class and return a base R list or vector.
x <- knlist(a = 1, b = 2, c = 3) try(knlist(b = 1, a = 2, a = 1)) # duplicate keys not allowed try(x[[1]] <- 1) # knlist only accepts character indexing for assignment # objects within a knlist are not subject to validation knlist(x = 1, y = list(a = 1, a = 2)) try(knlist(x = 1, y = knlist(a = 1, a = 2))) # but nested knlists are # recursively validate and convert to knlist x <- list(a = 1, b = list(x = 1, y = 2)) x <- as.knlist(x, .recursive = TRUE) class(x[[2]]) # nested list is now a knlist is.knlist(knlist(a = 1)) # TRUE try(names(x) <- c("a", "a")) # names are validated when changed # c() method for knlist objects also validates try(c(knlist(a = 1), list(a = 3)))x <- knlist(a = 1, b = 2, c = 3) try(knlist(b = 1, a = 2, a = 1)) # duplicate keys not allowed try(x[[1]] <- 1) # knlist only accepts character indexing for assignment # objects within a knlist are not subject to validation knlist(x = 1, y = list(a = 1, a = 2)) try(knlist(x = 1, y = knlist(a = 1, a = 2))) # but nested knlists are # recursively validate and convert to knlist x <- list(a = 1, b = list(x = 1, y = 2)) x <- as.knlist(x, .recursive = TRUE) class(x[[2]]) # nested list is now a knlist is.knlist(knlist(a = 1)) # TRUE try(names(x) <- c("a", "a")) # names are validated when changed # c() method for knlist objects also validates try(c(knlist(a = 1), list(a = 3)))