Вопрос

Okay, it took me a while to create a snippet of code that replicates my problem. Here it is. Notice that if you run the command new("FirstSet", id = "Input", multiplier = 2) you will get the correct answer. However, if you try to create a class that contains both you will get the following: Error in .local(.Object, ...) : argument "id" is missing, with no default. This is literally the best I can do to explain/show the problem.

What in the world am I doing wrong?

setClass("Details",
     representation(
      ID = "character",
      Anumber = "numeric"))

 Input <- new("Details",
         ID = "Input",
         Anumber = 2)

 setClass("FirstSet",
     representation(
     Anothernumber = "numeric"))

 setGeneric(
 name = "FirstSet",
 def = function(object){standardGeneric("FirstSet")}
 )

 setMethod("initialize",
      signature(.Object = "FirstSet"),
      function (.Object, id, multiplier) 
      { x = id@Anumber
        y = x * multiplier

        .Object@Anothernumber = y
        return(.Object)
      }
 )

setClass("Super", contains = c("Details", "FirstSet"))  

Corrected Code now gives a new error. I followed the instruction in the post and solved my problem. I also created a generic and a method for "Super", see code below,. Now, I get a new error. Error in .local(.Object, ...) : trying to get slot "Anumber" from an object of a basic class ("character") with no slots. Man, this is exhausting, I thought I had it.

The goal for details is, there will be many files that are serialized and methods are called depending on characteristics of the data in the file. Is this even possible in R or am I trying to do something that R cannot do?

New Code

setClass("Details",
representation(
    ID = "character",
    Anumber = "numeric"))

setGeneric("Details",
def = function(object){standardGeneric("Details")})

setMethod("initialize",
signature(.Object = "Details"),
function(.Object, ID = character(), Anumber = numeric()){
    .Object@ID = ID
    .Object@Anumber = 2
    return(.Object)
 })

setClass("FirstSet",
representation(
    Anothernumber = "numeric"))

setGeneric(
name = "FirstSet",
def = function(object){standardGeneric("FirstSet")}
)

 setMethod("initialize",
signature(.Object = "FirstSet"),
function (.Object, id = character(), multiplier = numeric()) 
{ x = id@Anumber
    y = x * multiplier

    .Object@Anothernumber = y
    return(.Object)
}
)

setClass("Super", contains = c("Details", "FirstSet"))

setGeneric("Super",
def = function(object){standardGeneric("Super")})

  setMethod("initialize",
  signature(.Object = "Super"),
  function(.Object, id = character(), Anumber = numeric()){
    Details <- new("Details", ID = id, Anumber = Anumber)
    FirstSet <- new("FirstSet", Anothernumber = Anothernumber)
    Super <- new("Super", Details, FirstSet)
    return(.Object)
 })
Это было полезно?

Решение

The basic rule is that new("FirstSet") (or any non-virtual class) needs to work. Yours doesn't (because the intiailize arguments don't have default values). See this answer for some more guidelines.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top