Skip to content

Running initial code only once in R script

(Last Updated On: December 10, 2018)

This script helped me a lot once I figured it out. When I work in R I usually split different functions in different script files and then import them to the “main” script, but if you do this you don’t want  to import all functions each time you run the script. So how can you solve this and still be able to run the entire script every time?

It’s really super easy. Just create an IF statement where you check if a variable exists. Since it’s the first time you run the script the variable obviously won’t exist. When the code inside the IF statement runs it will create the varaible on the last line. This ensures that the IF statement won’t run again until you clear the environment or the specific variable.

 

 

Share this article
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
Published inR

Leave a Reply

avatar
  Subscribe  
Notify of
© Copyright 2017-2019 - Linus Larsson