Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange error: formal arguments omitted in the method definition cannot be in the signature

Tags:

r

s4

I am trying to define generic methods in one R file and implement them in another R file. But I get an error which I don't understand.

Questions:

  1. What is the source of the error?
  2. How do I fix the error?

Contents of BuyProbabilityGeneric.R:

setGeneric("FirstTimeBuyProbability",
        valueClass = "numeric",
        function(object, period, price) {
    standardGeneric("FirstTimeBuyProbability")
})

setGeneric("ReturningBuyProbability",
        valueClass = "numeric",
        function(object, period1, price1, period2, price2) {
    standardGeneric("ReturningBuyProbability")
})

Contents of BuyProbabilityBasic.R:

setClass("BuyProbabilityBasic", representation(
        full.price.probability = "numeric",
        elasticity = "numeric"))

setMethod("FirstTimeBuyProbability",
        signature(object = "BuyProbabilityBasic", 
                period = "numeric", price = "numeric"), 
        function(object, t, price) {
    full.price.probability * price^elasticity
})

setMethod("ReturningBuyProbability",
        signature(object = "BuyProbabilityBasic", 
                period1 = "numeric", price1 = "numeric",
                period2 = "numeric", price2 = "numeric"), 
        function(object, period1, price1, period2, price2) {
    max(0, FirstTimeBuyProbability(object, period2, price2) -
        FirstTimeBuyProbability(object, period1, price1))
})

R console output:

> source('BuyProbabilityGeneric.R')
> source('BuyProbabilityBasic.R')
Error in conformMethod(signature, mnames, fnames, f, fdef, definition) : 
  in method for ‘FirstTimeBuyProbability’ with signature 
‘object="BuyProbabilityBasic",period="numeric",price="numeric"’: 
formal arguments (period = "numeric") omitted in the method definition
cannot be in the signature
like image 240
I Like to Code Avatar asked Feb 25 '14 20:02

I Like to Code


1 Answers

try:

setMethod("FirstTimeBuyProbability",
          signature(object = "BuyProbabilityBasic", 
                    period = "numeric", price = "numeric"), 
          function(object, period, price) {
            full.price.probability * price^elasticity
          })

Notice how I changed t to period in your function definition formals. Basically, you were trying to define a function where the formals conflicted with the signature.

like image 166
BrodieG Avatar answered Nov 18 '22 22:11

BrodieG