如何防止 source() R 代码被多次包含?

发布于 2024-12-27 18:26:17 字数 197 浏览 0 评论 0原文

我有很多 R 源文件。例如,在 ARCR 文件中,BR 都是通过 source() 加载的。现在我想同时使用ARCR中的功能,如何避免重复采购BR? C/C++ 中是否有类似的包含保护机制?

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++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凑诗 2025-01-03 18:26:17

如果 BR 中已经有一个具有相当唯一名称的函数或数据集,您可以将其用作防护。否则,只需定义一个:

BR

B_is_loaded <- TRUE
# The normal B stuff

AR:

if(!exists("B_is_loaded")) source("B.R")
# Now you can call the stuff in B

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文