Search code examples
rlistempty-list

How do I empty a list in R?


I have a file with an R program. I load it interactively on R and then I call the main function. During the execution I fill up some global lists but when I run the main function again I want those lists to be empty. How can I empty a filled up list? I tried doing

list <- NULL

after execution but it didn't work.


Solution

  • Since you are setting them globally, you probably need list <<- NULL, because the <<- operator assigns global variables.

    Edit, per @Dason's comment:

    The <<- operator can in some circumstances fail to change the value of a variable in all possible environments. For that reason, it is better to use

    assign("list", NULL, envir = .GlobalEnv)