有條理的思考

在 R 中使用 S3 與 R6 的物件導向程式設計

Richie Cotton

Data Evangelist at DataCamp

第 2 章:有條理的思考示意圖

在 R 中使用 S3 與 R6 的物件導向程式設計
methods
在 R 中使用 S3 與 R6 的物件導向程式設計
methods("mean") # or methods(mean)
mean.Date     mean.default  mean.difftime mean.POSIXct 
mean.POSIXlt 
see '?methods' for accessing help and source code
在 R 中使用 S3 與 R6 的物件導向程式設計

類別範例圖

在 R 中使用 S3 與 R6 的物件導向程式設計
methods(class = "glm") # or methods(class = glm)
add1           anova          coerce        
confint        cooks.distance deviance      
drop1          effects        extractAIC    
family         formula        influence     
initialize     logLik         model.frame   
nobs           predict        print         
residuals      rstandard      rstudent      
show           slotsFromS3    summary       
vcov           weights       
see '?methods' for accessing help and source code
在 R 中使用 S3 與 R6 的物件導向程式設計

 

 

 

methods() 會回傳 S3S4 方法。

在 R 中使用 S3 與 R6 的物件導向程式設計
.S3methods(class = "glm")
add1           anova          confint       
cooks.distance deviance       drop1         
effects        extractAIC     family        
formula        influence      logLik        
model.frame    nobs           predict       
print          residuals      rstandard     
rstudent       summary        vcov          
weights       
see '?methods' for accessing help and source code
在 R 中使用 S3 與 R6 的物件導向程式設計
.S4methods(class = "glm")
coerce      initialize  show        slotsFromS3
see '?methods' for accessing help and source code
在 R 中使用 S3 與 R6 的物件導向程式設計

重點整理

  • methods() 可為泛型函式找到方法
  • 或為類別找到方法
  • .S3methods() 只找出 S3 方法
在 R 中使用 S3 與 R6 的物件導向程式設計

一起來練習吧!

在 R 中使用 S3 與 R6 的物件導向程式設計

Preparing Video For Download...