默认值

数组和集合 / 字典

dic <- list("1" = "A", "2" = "B")

# NULL is a missing key: %||% gives the default
value <- dic[["3"]] %||% "-"
# value = -

# Store the default as well
dic[["3"]] <- dic[["3"]] %||% "-"

# Counting: a missing key starts from 0
scores <- list()
scores[["ann"]] <- (scores[["ann"]] %||% 0) + 1

cat("value =", value, "\n")
str(dic)
str(scores)