Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rscript does not recognize setGeneric function

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?

like image 990
Branko K. Avatar asked Dec 01 '25 09:12

Branko K.


1 Answers

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"
like image 86
dickoa Avatar answered Dec 04 '25 01:12

dickoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!