2

考虑这种形式的源文件:

# initialize the function
f = function() {
 # ...
}

# call the function
f()

在python中,该import函数将加载执行文件;但是在 R 中,该source命令初始化源文件定义的函数,但如果在文件中调用函数,则不会调用这些函数。

是否有任何 R 命令(或选项)来导入执行文件?

谢谢你的帮助。

4

1 回答 1

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.

所以,

  1. source 您正在寻找的功能,
  2. 我驳斥了您的主张-source根据文档为我工作
  3. 如果您没有看到记录在案的行为,那么一定是您没有告诉我们的其他问题。

    你如何推断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

这也可能对您有用。

于 2013-04-03T20:40:26.217 回答