考虑这种形式的源文件:
# initialize the function
f = function() {
# ...
}
# call the function
f()
在python中,import
函数将加载并且执行该文件;但是在R中,source
命令执行初始化源文件定义的函数,但不调用函数,如果在文件中调用它们。
是否有任何R命令(或选项)导入和执行文件?
感谢您的帮助。
答案 0 :(得分:7)
?source
州:
Description:
‘source’ causes R to accept its input from the named file or URL
or connection. Input is read and ‘parse’d from that file until
the end of the file is reached, then the parsed expressions are
evaluated sequentially in the chosen environment.
因此,
source
您正在寻找的功能source
根据文档如果您不看到记录在案的行为,那么必定会有一个您没有告诉我们的其他问题。
您如何推断source
未执行f
?
我可以想到你可能不知道的一种情况,?source
中记载了这种情况,即:
Details:
Note that running code via ‘source’ differs in a few respects from
entering it at the R command line. Since expressions are not
executed at the top level, auto-printing is not done. So you will
need to include explicit ‘print’ calls for things you want to be
printed (and remember that this includes plotting by ‘lattice’,
FAQ Q7.22).
未看到名称调用的对象或未显示基于网格图形的图表的输出是自动打印的症状未激活,因为评估未在顶级进行。您需要明确print()
个对象,包括基于网格的图形,如Lattice和ggplot2数字。
另请注意print.eval
参数,默认为
> getOption("verbose")
[1] FALSE
也可能对您有用。