奇怪的错误:方法定义中省略的形式参数不能出现在签名中

StackOverflow https://stackoverflow.com//questions/22025572

  •  21-12-2019
  •  | 
  •  

我试图在一个 R 文件中定义通用方法并在另一个 R 文件中实现它们。但我收到一个我不明白的错误。

问题:

  1. 错误的根源是什么?
  2. 我该如何修复该错误?

内容 BuyProbabilityGeneric.R:

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

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

内容 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控制台输出:

> 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
有帮助吗?

解决方案

尝试:

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

注意我的改变 tperiod 在你的函数定义形式中。基本上,您试图定义一个形式与签名冲突的函数。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top