Reputation: 121
Using lists in R seems to be tricky. I was not aware that referencing list elements with identical first part of name is ambiguous:
opts = list()
opts$value = NULL
opts$valueDefault = c(1,2,3)
print(opts) # note: displaying the whole list does not reveal
# the element "value"
$valueDefault
[1] 1 2 3
print(opts$value) # with this notation I do not get the correct (intended)
# result
[1] 1 2 3
print(opts[["value"]]) # with this notation I do
NULL
Upvotes: 0
Views: 122
Reputation: 81693
If you want a list element to be NULL
, you have to create a list with NULL
.
opts <- list(value = NULL)
> opts
$value
NULL
Upvotes: 1
Reputation: 61933
Note that setting a list element to NULL is the same as removing it from the list. Your list at that point literally only contains a single element called valueDefault. Try setting value to NA instead to see the difference.
Reading over ?"$"
will give some more info on this. And to summarize briefly - one difference between referencing via $
and via [[
is that $
will do partial matching. So since your list only contained valueDefault and you said opts$value
it realized that you must be referring to valueDefault. opts[["value"]]
doesn't try to do partial matching (by default)
Upvotes: 3