In R, how can I set a generic method on a class from another package? -
i using zoo class in own packages. set generic method type zoo:
setmethod( "dowork", signature = c("zoo"), definition = function(x) { # work... }) however, gives me error:
in method ‘dowork’ signature ‘"zoo"’: no definition class “zoo”
how should set signature refers zoo::zoo?
this because zoo class zoo package not formal s4 class. in order use s4 methods can use setoldclass function set s3 class formally defined class. once should able use class wish methods. starting new package (which call 'test') following file (please note use of roxygen2).
methods.r
#' @import zoo setoldclass("zoo") setgeneric("dowork", function(x){ standardgeneric("dowork") }) #' @export setmethod( "dowork", signature = c("zoo"), definition = function(x) { print("it works!!!") } ) test function
library(test) # if not loaded library(zoo) x.date <- as.date("2003-02-01") + c(1, 3, 7, 9, 14) - 1 x <- zoo(rnorm(5), x.date) dowork(x) [1] "it works!!!"
Comments
Post a Comment