问题
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