How to modify nested list values in R
modify_in_maybe <- function(.x, .where, .f, ...) { .where <- as.list(.where) .f <- rlang::as_function(.f) current_value <- purrr::pluck(.x, !!!.where) replacement <- .f(current_value, ...) if (is.null(current_value)) return(.x) purrr::assign_in(.x, .where, replacement) } list(root = list(nesting = list(leaf = 2))) %>% modify_in_maybe(c("root", "nesting", "leaf"), ~.x + .x) list(list(root = list(nesting = list(leaf = 2))), list(root = list(nesting = list()))) %>% map(modify_in_maybe, c("root", "nesting", "leaf"), ~.x + .x)











