How do I prevent source() R code being included more than once?
-
17-04-2021 - |
سؤال
I have lots of R source files. For example, in both A.R
and C.R
files, B.R
is loaded via source()
. Now I'd like to use the functions in both A.R
and C.R
, how can I avoid sourcing B.R
repeatedly? Is there any include guard mechanisms similar in C/C++?
المحلول
If B.R already has a function or data set in it with a rather unique name, you could use it as a guard. Otherwise, just define one:
B.R
B_is_loaded <- TRUE
# The normal B stuff
A.R:
if(!exists("B_is_loaded")) source("B.R")
# Now you can call the stuff in B
لا تنتمي إلى StackOverflow