Edit the elements of a list by name
list_edit(.l, ..., .add = TRUE)
List to edit
Named elements to add or delete
Should named elements not existing in the original list be added?
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
.
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
#>