S4中的SetMethod - 如何定义从流水线函数获取其参数的方法?

时间:2016-12-10 23:30:10

标签: r oop pipeline s4

我应该在R中实现一个S4代码来获取此查询的结果:

out <- subject (x, 14) %>% summary

我也为主题(x,14)和摘要编写了我的方法。后者是现有摘要函数的扩展,具有我定义的类的签名。

单独输入时,这两个功能完美运行。但是,我很难弄清楚如何使用S4中所需的setMethod语法将第一个函数的值作为输入传递给第二个函数。 你们中的任何人都可以提出解决方案吗?

这是我的代码:

    #Class definitions:
    setClass("LongitudinalData_S4", slots = c(DataFile = "data.frame"))
    setClass("Subject", slots= c(id = "numeric",value = "numeric",
                         timepoint = "numeric"), contains =       "LongitudinalData_S4") 
    setClass("Visit", slots = c(visit = "numeric"), contains = "Subject")
    setClass("Room", slots = c(room= "character"),contains = "Subject")

    #Set generics:
    setGeneric("summary")
    setGeneric(name="make_LD",def=function(x,...)
                   {standardGeneric("make_LD")
                   }
                   )
    setGeneric(name="SetDataFile", def=function(x,...)
                   {standardGeneric("SetDataFile")
                   }
                   )
    setGeneric(name="subject",def=function(s_ID,...)
                   {standardGeneric("subject")
                   }
                   )
    setGeneric(name="visit",def=function(v_ID, ...)
                   {standardGeneric("visit")
                   }
                   )
    setGeneric(name="room",def=function(r_name, ...)
                  {standardGeneric("room")
                  }
                  )
    #Set methods:
    setMethod(f="SetDataFile",signature="data.frame",definition=function(x)
                   {LD@DataFile<<-x
                   }
                   )
    setMethod(f="make_LD",signature="data.frame",definition=function(x)
                  {structure(list(data=x))
                   class<-"LongitudinalData_S4"
                   SetDataFile(data)
                   return(data)
                  }
                  )
    setMethod("[",  signature(x="LongitudinalData_S4",i="ANY"), definition=function(x, i)
      {f<-x@id[which(x@id==i)]
       return(d<-f[1])
      }
      )
    setMethod(summary, signature(x="numeric", y="character"), function(x,y)
      {baba<<-subset(LD@DataFile, LD@DataFile[[y]]==x)
      class(baba)=="data.frame"
      f<-(with(baba, tapply(value, list(visit, room), FUN=mean)))
      return(print(f))
      }
      )

     setMethod(subject,"numeric", definition = function(s_ID)
     {if(!(s_ID %in% LD_Subject@id))
      {cat("No such ID in the dataframe.")}
      else
      { cat(sprintf("Subject ID: %d",LD_Subject[s_ID], "\n"))
        cat("   ", "\n")
      }
     }
     )

    #Create objects:
    LD<-new("LongitudinalData_S4")
    LD_Visit <-new("Visit", visit = data$visit)
    LD_Room <-new("Room", room = as.character(data$room))
    LD_Subject <-new("Subject", id = data$ID, value =data$value, timepoint = data$timepoint)

通过调用subject(14),我得到了我期望的结果:

    > subject(14)
    Subject ID: 14 

通过调用summary(14,“ID”),我得到了我期望的结果:

    > summary(14, "ID")
   bedroom den dining room family  room hall kitchen living room office study room tv room
    0 NA  4.786592  NA          NA           NA   NA      NA     2.75000     NA         NA      NA
    1 NA  3.401442  NA          NA     8.426549   NA      NA          NA     NA         NA      NA
    2 NA 18.583635  NA          NA           NA   NA      NA    22.55069     NA         NA      NA

但是,我应该拥有的是上述结果,通过调用subject(14)%&gt;%摘要。在这里,我找不到方法。

0 个答案:

没有答案