【问题标题】:Rscript does not recognize setGeneric functionRscript 无法识别 setGeneric 函数
【发布时间】:2013-06-29 17:15:40
【问题描述】:

我正在尝试将 R 脚本转换为客户端可以在批处理模式下运行的内容。我的代码使用通用函数和一个接近开头的 sn-p 如下:

setGeneric("testInput", function(inputData, params = list())
    standardGeneric("testInput"))

我一直在使用 R CMD BATCH,效果很好。但是我找不到让我的脚本在控制台上打印输出的简单方法,因此基于此(并建议 Rscript.exe 是运行 R 批处理文件的“正确”方式)我决定切换到 Rscript。但是,当使用 Rscript 运行相同的 .R 文件时,我得到以下信息:

Error: could not find function "setGeneric"
Execution halted

我知道这背后可能有一个微不足道的原因,但我就是想不通。有人可以指出错误在哪里吗?
有什么建议吗?

【问题讨论】:

  • 在脚本开头添加require(methods)
  • 工作就像一个魅力。谢谢!
  • @dickoa,+1。以免这个问题正式“无人回答”,您介意将您的评论转换为答案吗?
  • @gung 你说得对,我将我的评论转换为答案......谢谢

标签: r batch-file rscript


【解决方案1】:

setGenericmethods 包的一部分,通常在您在交互式会话中启动 R 时加载,而不是在使用 Rscriptlittler 的非交互式会话中加载。

因此,您需要在脚本中调用setGeneric 之前添加require(methods)

例如,此代码将不起作用

Rscript -e "setGeneric('mean', function(x) standardGeneric('mean'))"
Error: could not find function "setGeneric"
Execution halted

但是这个可以工作

Rscript -e "require(methods);setGeneric('mean', function(x) standardGeneric('mean'))"
Loading required package: methods
[1] "mean"

【讨论】:

    猜你喜欢
    • 2020-07-26
    • 1970-01-01
    • 2011-07-10
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2015-08-16
    • 2012-07-02
    相关资源
    最近更新 更多