I’ve created a utility R script, util.R, which I want to use from other scripts in my project.
What is the proper way to ensure that the function this script defines are available to function in my other scripts?
I’m looking for something similar to the require function, that loads a package only if it has not been loaded yet. I don’t want to call source("util.R") because that will load the script every time it is called.
I know that I will get some answers telling me to create a package, as in Organizing R Source Code 🙂
But I’m not creating something that will be used elsewhere, it is just a standalone project.
Here is one possible way. Use the
existsfunction to check for something unique in yourutil.Rcode.For example:
(Edited to include
mode="function", as Gavin Simpson pointed out)