Here is a R list:
list1 <- list(var1=1,var2=list(var21=1,var22=2,var23=list(var231=1,var232=0)))
Here is another R list:
list2 <- list(var1=3,var2=list(var22=0,var23=list(var232=1,var233=2)),var3=list(var31=1))
Now I want to update list1
by list2
, which should include updating existing values and introducing new values. As a result var1
, var22
, var232
should be updated to the value specified in list2
, and var233
, var3
, var31
should be introduced as new entries. Therefore the updated list should be like:
list(var1=3,var2=list(var21=1,var22=0,var23=list(var231=1,var232=2,var233=2)),var3=list(var31=1))
It is very similar with default settings and user-specific settings. The default settings should be loaded and updated by user-specific settings. In my use, I just create a R list from a JSON file (default.json
) as default settings and want to update the default settings by another list produced by another JSON file (user.json
), like many programs do.
Is there an existing package or some simple/decent way to do this?
There's a modifyList
for these purposes. Note that I corrected list3
definition, there's a mistake in your desired output.
list4 <- modifyList(list1, list2)
list3 <- list(var1=3,var2=list(var21=1,var22=0,var23=list(var231=1,var232=1,var233=2)),var3=list(var31=1))
all.equal(list3, list4)
[1] TRUE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With