For a single key-value pair, this can be done pretty trivially with [<-:
`[<-`(original, "x", 2)
#> $x
#> [1] 2
#>
#> $x
#> [1] 1
And so you could write a function that does this for a series of key-value pairs (e.g. with reduce). However, it still feels like this is a bit involved for quite simple functionality that probably exists within the language anyway.
As a semi-interesting sidebar, I thought I could easily achieve something similar with append or rlang::list2, but apparently I misunderstood some of R's list semantics:
update_list_rlang <- function(original, ...) {
rlang::list2(..., !!!original)
}
update_list_rlang(original, x = 2)
#> $x
#> [1] 2
#>
#> $x
#> [1] 1
#>
#> $y
#> [1] 2
append(original, list(x = 2))
#> $x
#> [1] 1
#>
#> $y
#> [1] 2
#>
#> $x
#> [1] 2
Upon inspection, this makes sense, but I was initially surprised to see that multiple list elements could have the same name.