R脚本找不到函数。

15

我需要通过bash shell运行多个脚本,使用Rscript和一些我使用的函数需要函数isGeneric。然而,在这种情况下,进程会以这样的方式结束(例如):

Error in .getLogLik() : could not 
find function "isGeneric"
Calls: main -> dredge -> .getLik -> .getLogLik
Execution halted

以下是复现方法:

# in the bash shell
echo "isGeneric('apply')" > /tmp/test.R
Rscript /tmp/test.R

结果:

Error: could not find function "isGeneric"
Execution halted

但是,如果我们打开一个R会话并输入以下内容,它可以正常工作:

# in the R shell
isGeneric('apply')
[1] FALSE

你知道这个问题来自哪里以及如何解决吗?

1个回答

24
根据help(Rscript)Rscript默认不加载methods包,因为这会耗费时间。所以你需要在命令行上指定它:
Rscript --default-packages=methods file.R

或者在你调用的文件顶部加上 library(methods)


谢谢Joshua,现在它完美地工作了。对不起,我没有看到isGenericmethods包之间的链接。 - Ludovic Duvaux
@LudoDuvaux:别担心。这不是一个显而易见的链接,除非你之前遇到过这个问题。 - Joshua Ulrich
3
我刚遇到了这个问题:Error: could not find function "is"。连接性甚至更不明显! - DavidC
根据help(Rscript)的说明,正确的命令应该是:Rscript --default-packages=methods,datasets,utils,grDevices,graphics,stats,这样才能与R具有相同的默认包。如果只包含methods,则会缺少许多其他内容。 - Kem Mason

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接