Rscript does not recognize setGeneric function

烈酒焚心 提交于 2020-01-14 13:46:07

问题


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"))

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?


回答1:


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.

So you need to add a require(methods) before calling setGeneric in your script.

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"


来源:https://stackoverflow.com/questions/17423869/rscript-does-not-recognize-setgeneric-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!