2

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.

Atirag
  • 1,518
  • 5
  • 30
  • 55
  • Can you give some more context? Maybe the code for the entire function? When I run `list – histelheim Sep 07 '13 at 19:33
  • 1
    Maybe you want `list – Ferdinand.kraft Sep 07 '13 at 19:43
  • 2
    It should be noted that relying on globals isn't best practice. If you explain what you're doing we can probably find a better way... – Dason Sep 07 '13 at 19:53
  • @Dason Yeah I know but I needed to do a quick program and I'm not really familiar with R so I used global variables for simplicity. I was programming a hanged man game so I needed global lists for the letters chosen by the player to compare them with the word chosen by the executioner. – Atirag Sep 07 '13 at 19:59
  • So is this an exercise to learn R? If so I would think that taking the extra time to learn how to do things 'the R way' would be more beneficial than just plowing through relying on bad practice. – Dason Sep 07 '13 at 20:03
  • Not really is for a friend who needs to do some experiments but knows nothing about programming. I can't spend too much on this though since I need to do stuff of my own that's why is kind of sloppy but functional... if I have time later I'll fix it. – Atirag Sep 07 '13 at 20:06

1 Answers1

6

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)
Drew Steen
  • 15,225
  • 12
  • 59
  • 90