I am using the walk function to iterate over my list of lists and append a list element to every sub-list.
.insideFunction <- function(sublistName, arg2){
newListElement <- "Hello"
newListElement <- as.list(newListElement)
names(newListElement) <- "newListElement"
myList[[sublistName]] <- append(myList[[sublistName]], newListElement)
}
walk(names(myList), .insideFunction, someTable)
The problem is that the list myList
, which is defined globally doesn't change.
I am currently using the global assignment operator inside of the .insideFunction
to force R to overwrite the sublist.
myList[[sublistName]] <<- append(myList[[sublistName]], newListElement)
How can I avoid using the global assignment operator, but still append the globally defined list from inside a function?
Use
map
instead ofwalk
to create a modified version of a list by applying a function to every element e.g. add 2 to each sub list:Created on 2022-04-22 by the reprex package (v2.0.0)