knlist: Create a List With Unique Named Keys

View source: R/knlist.r

knlistR Documentation

Create a List With Unique Named Keys

Description

Create a list with unique names/keys, erroring if any duplicate names/keys are found.

Usage

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(...)

Arguments

...

for knlist: named objects, for as.knlist: passed to other methods.

x

object to be coerced or tested.

.recursive

boolean indicating whether to recursively validate nested lists. If TRUE then all nested lists will be validated and converted to knlists, if FALSE then only the top level list will be validated and converted to a knlist.

value

a character vector the same length as x. Resulting names must be unique.

Details

⁠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.

Value

A list object of class knlist. For is.knlist() a boolean.

Note

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.

See Also

klist and keylist.

Examples

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)))

keylist documentation built on April 27, 2026, 5:11 p.m.