Edit the elements of a list by name

list_edit(.l, ..., .add = TRUE)

Arguments

.l

List to edit

...

Named elements to add or delete

.add

Should named elements not existing in the original list be added?

Details

list_edit is similar to purrr::list_modify in purrr, but unlike list_modify, does not operate recursively on a list. It only edits elements at the top level of hierarchy. Elements can be removed by setting them equal to NULL.

Examples

list_edit(list(x = 1, y = 2), x = 10, z = 20)
#> $x
#> [1] 10
#> 
#> $y
#> [1] 2
#> 
#> $z
#> [1] 20
#> 
list_edit(list(x = 1, y = 2), x = NULL, y = 5)
#> $y
#> [1] 5
#> 
list_edit(list(x = 1, y = 2), !!!list(x = NULL, z = 100))
#> $y
#> [1] 2
#> 
#> $z
#> [1] 100
#> 
list_edit(list(x = 1, y = 2), x = 10, z = 20, .add = FALSE)
#> $x
#> [1] 10
#> 
#> $y
#> [1] 2
#>