考虑这种形式的源文件:
# initialize the function
f = function() {
# ...
}
# call the function
f()
Run Code Online (Sandbox Code Playgroud)
在python中,该import函数将加载并执行该文件; 但是在R中,该source命令会初始化由源文件定义的函数,但如果在文件中调用它们则不会调用这些函数.
是否有任何R命令(或选项)来导入和执行文件?
谢谢你的帮助.
?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.
Run Code Online (Sandbox Code Playgroud)
因此,
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).
Run Code Online (Sandbox Code Playgroud)
没有看到由名称或基于网格图形的图表调用的对象的输出未显示自动打印的症状未激活,因为评估未在顶级进行.您需要明确的print()对象,包括基于网格的图形,如莱迪思和ggplot2数字.
还要注意print.eval参数,默认为
> getOption("verbose")
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
这也可能对你有用.