RSCRIPT不承认setGeneric功能 [英] Rscript does not recognize setGeneric function
问题描述
我尝试的R脚本转换到的东西,客户可以在批处理模式下运行。我的code使用泛型函数和一个片段是附近的开始是这样:
I am trying to convert an R script into something that a client can run in batch mode. My code uses generic functions and one snippet which is near the beginning goes like:
setGeneric("testInput", function(inputData, params = list())
standardGeneric("testInput"))
我一直在使用
研究CMD批并能正常工作。但是我无法找到一个简单的方法,使我的剧本打印控制台上的输出,从而基于(和建议,Rscript.exe是正确的方式来运行R.批处理文件)我决定改用<强> RSCRIPT 。运行与RSCRIPT非常相同.R文件然而,当我得到以下内容:
I've been using R CMD BATCH and it works fine. However I couldn't find an easy way to make my script print the output on the console, so based on that (and suggestion that Rscript.exe is the "proper" way to run R batch files) I decided to switch to Rscript. However when running the very same .R file with Rscript I get the following:
Error: could not find function "setGeneric"
Execution halted
我知道有可能是这背后一个微不足道的原因,但我只是不明白。可有人请点我哪里错是什么?结果有什么建议?
I know there is probably a trivial reason behind this but I just cannot figure it out. Can someone please point me to where the mistake is?
Any suggestions?
推荐答案
setGeneric
是方法的一部分
包当你在一个交互式会话在非交互式会话使用开始ř但不是它通常装 RSCRIPT
或利特勒
。
setGeneric
is part of the methods
package which is usually loaded when you start R in an interactive session but not in non interactive session using Rscript
or littler
.
所以,你需要添加一个要求(方法)
之前调用 setGeneric
在你的脚本。
So you need to add a require(methods)
before calling setGeneric
in your script.
例如,这code将无法正常工作
For example, this code will not work
Rscript -e "setGeneric('mean', function(x) standardGeneric('mean'))"
Error: could not find function "setGeneric"
Execution halted
但是这一次将工作
But this one will work
Rscript -e "require(methods);setGeneric('mean', function(x) standardGeneric('mean'))"
Loading required package: methods
[1] "mean"
这篇关于RSCRIPT不承认setGeneric功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!