【问题标题】:(How) is it possible in R to include external files with source code(如何)在 R 中是否可以在源代码中包含外部文件
【发布时间】:2015-07-18 02:07:34
【问题描述】:

是否有可能在 R 中包含一个包含 R 源代码的文本文件并在包含它的位置执行?

在 PHP 中我会使用命令 include (http://php.net/manual/en/function.include.php)

我有一个文件,我首先在其中定义函数(约 200 行),然后通过处理大量文件并使用定义的函数(约 1500 行)来创建和设置大量变量,最后我使用用于计算和绘图的变量(约 700 行)。

# functions
readfile <- function (...) {
     ...
}


# reading files, general plots,...
dataFolder1="..."
pdf("param01_Set01.pdf")
    param01_Set01_SV = readfile(dataFolder1, ...)
    param01_Set01_KP = readfile(dataFolder1, ...)
    param01_Set01_NK = readfile(dataFolder1, ...)
dev.off()

dataFolder2="..."
pdf("param01_Set01.pdf")
    param01_Set02_SV = readfile(dataFolder2, ...)
    param01_Set02_KP = readfile(dataFolder2, ...)
    param01_Set02_NK = readfile(dataFolder2, ...)
dev.off()

...


# dooing specific calculations + plotting
result1 = (param01_Set01_SV$xyz + 123) * param02_Set08$xyz
plot(...)

我喜欢将中间部分(变量定义、文件读取、一般绘图)“外包”到一个单独的文件中,只是因为它非常大而且一旦我输入了所有文件名就没有兴趣了,...。这里有像 R 中的 include 这样的命令吗?

【问题讨论】:

    标签: r include


    【解决方案1】:

    您要查找的命令是source() - 查看?source 了解更多信息。另一种选择是sys.source()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2011-06-04
      • 2020-09-16
      • 2022-08-04
      • 1970-01-01
      • 2012-08-26
      • 2018-02-07
      相关资源
      最近更新 更多