I am using the zoo
class in my own packages. I would like to set a generic method with type zoo
:
setMethod(
"doWork",
signature = c("zoo"),
definition = function(x) {
# Do work...
})
However, this gives me the error:
in method for ‘dowork’ with signature ‘"zoo"’: no definition for class “zoo”
How should I set the signature so that it refers to zoo::zoo
?
This is because the zoo class from the zoo
package is not a formal S4 class. In order to use it with S4 methods you can use the setOldClass
function which will set a S3 class as a formally defined class. Once you do this you should be able to use the class however you wish with the methods. Starting a new package (which I just call 'test') with the following file (please note the 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 the function
library(test) # if not already 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!!!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With