2015-06-20 75 views
3

比方說,我有一個看起來像這樣寫作方法類

obj <- list() 
obj$title <- "Hello" 
obj$data <- data.frame(c(1,2,3,4,5),c(5,4,3,2,1)) 
class(obj) <- "myclass" 

我知道我可以寫在類方法的結構:

myfunction <- function(obj,...) { 
    UseMethod("myfunction") 
} 

myfunction.myclass <- function(obj,...) { 
    # magic 
} 

但我會怎麼寫根據對象的類,包含在對象中的數據的方法。我的意思是不必把它全部寫在同一個函數中。像那種...

myfunction.myclass.data.frame <- function(obj,...) { 
    # do something if class(obj$data) == "data.frame" 
} 

myfunction.myclass.character <- function(obj,...) { 
    # do something if class(obj$data) == "character" 
} 

myfunction.myclass.numeric <- function(obj,...) { 
    # do something if class(obj$data) == "numeric" 
} 

回答

3

你可以做的是使myfunction.myclass()第二級通用(其中myfunction()將是第一級通用)通過調用它UseMethod(),和而不是省略第二個參數(這通常是完成的,默認爲封閉函數的第一個參數),您可以將obj$data傳遞給它。這將派遣到指定的功能基於類的obj$data

## define first-level obj generic 
myfunction <- function(obj,...) UseMethod('myfunction'); 

## define second-level obj$data generic 
myfunction.myclass <- function(obj,...) UseMethod('myfunction.myclass',obj$data); 

## define obj$data specifics 
myfunction.myclass.data.frame <- function(obj,...) { cat('----- class -----\ndata.frame\n----- obj -----\n'); print(obj); cat('----- args -----\n'); print(list(...)); }; 
myfunction.myclass.character <- function(obj,...) { cat('----- class -----\ncharacter\n----- obj -----\n'); print(obj); cat('----- args -----\n'); print(list(...)); }; 
myfunction.myclass.numeric <- function(obj,...) { cat('----- class -----\nnumeric\n----- obj -----\n'); print(obj); cat('----- args -----\n'); print(list(...)); }; 
myfunction.myclass.default <- function(obj,...) { cat('----- class -----\ndefault\n----- obj -----\n'); print(obj); cat('----- args -----\n'); print(list(...)); }; 

## create test obj 
obj <- list(); 
obj$title <- 'Hello'; 
class(obj) <- 'myclass'; 

## demo 1: data.frame 
obj$data <- data.frame(a=c(1,2,3,4,5),b=c(5,4,3,2,1)); 
myfunction(obj,1,'a',T); 
## ----- class ----- 
## data.frame 
## ----- obj ----- 
## $title 
## [1] "Hello" 
## 
## $data 
## a b 
## 1 1 5 
## 2 2 4 
## 3 3 3 
## 4 4 2 
## 5 5 1 
## 
## attr(,"class") 
## [1] "myclass" 
## ----- args ----- 
## [[1]] 
## [1] 1 
## 
## [[2]] 
## [1] "a" 
## 
## [[3]] 
## [1] TRUE 

## demo 2: character 
obj$data <- letters; 
myfunction(obj,2,'b',F); 
## ----- class ----- 
## character 
## ----- obj ----- 
## $title 
## [1] "Hello" 
## 
## $data 
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" 
## 
## attr(,"class") 
## [1] "myclass" 
## ----- args ----- 
## [[1]] 
## [1] 2 
## 
## [[2]] 
## [1] "b" 
## 
## [[3]] 
## [1] FALSE 

## demo 3: numeric 
obj$data <- 1:10; 
myfunction(obj,3,'c',T); 
## ----- class ----- 
## numeric 
## ----- obj ----- 
## $title 
## [1] "Hello" 
## 
## $data 
## [1] 1 2 3 4 5 6 7 8 9 10 
## 
## attr(,"class") 
## [1] "myclass" 
## ----- args ----- 
## [[1]] 
## [1] 3 
## 
## [[2]] 
## [1] "c" 
## 
## [[3]] 
## [1] TRUE 

## demo 4: default (logical) 
obj$data <- T; 
myfunction(obj,4,'d',F); 
## ----- class ----- 
## default 
## ----- obj ----- 
## $title 
## [1] "Hello" 
## 
## $data 
## [1] TRUE 
## 
## attr(,"class") 
## [1] "myclass" 
## ----- args ----- 
## [[1]] 
## [1] 4 
## 
## [[2]] 
## [1] "d" 
## 
## [[3]] 
## [1] FALSE 
+0

你能保持鏈接的類以同樣的方式,如果你已經被以類似的方式對待那些需要其他對象? 'myfunction.myclass.myotherclass < - function(obj,...)UseMethod('myfunction.myclass.myotherclass,obj $ something_else)' –

+1

對此的回答是肯定的。 –