You may use this function at the last line of your script to indicate that
this script has already been loaded. This information is stored in the R
option "kwb.utils.loaded" from which you may read the information back with
isLoaded
.
setLoaded(scriptName, isLoaded = TRUE)
scriptName | name of the script for which we want to set the "loaded" state |
---|---|
isLoaded | logical. Use |
# If you have a script with the main part on top and the required functions # defined below (as recommended by Robert C. Martin, the author of "Clean # Code") your script may look like this: # Main part ----- # Check if the script has already been loaded (i.e. if setLoaded() has been # called, see end of script). If yes, we can enter the main section. Otherwise # we have to skip the main section since the function sayHello() is not yet # defined. if (isLoaded("welcome")) { sayHello(who = "Hauke") }#> Error in sayHello(who = "Hauke"): konnte Funktion "sayHello" nicht finden# Functions ----- sayHello <- function(who) { clearConsole() cat("***\n***\n*** Hello", who, "how are you?\n***\n***\n") } # At the end of your script, call setLoaded() to indicate that your script is # loaded now. If you "source" the script a second time, isLoaded("welcome") # will return TRUE and thus the main section will be entered... setLoaded("welcome")