0

I'm currently working on S3-classes trying to generate a write-function for my class. Just a minimal example:

testclass <- function() {
    value  <- list(c(1,2))
    attr(value, "class") <- "testclass"
    value
}
print.testclass <- function(obj) {
    cat("testtest")
}
summary.testclass <- function(obj) {
    cat("testtest2")
}
write.testclass <- function(obj) {
    cat("testtest3")
}


##### Testing:
> a <- testclass()
> print(a)
testtest
> a
testtest
> summary(a)
testtest2
> write(a)
Error in file(file, ifelse(append, "a", "w")) : 
    cannot open the connection
In addition: Warning message:
    In file(file, ifelse(append, "a", "w")) :
    cannot open file 'data': Permission denied

So, why does the write not use the right write?

Solution:

write <- function(x, ...) UseMethod("write")
write.default <- base::write
write.testclass <- function(obj) {
    cat("testtest3")
}

> write(a)
testtest3
groebsgr
  • 131
  • 1
  • 14
  • 5
    write is not a generic function, you need to define that and a default method at least before you create methods for other classes. see [here](http://stackoverflow.com/questions/4728342/using-sd-as-a-generic-function-in-r?rq=1) – rawr Apr 25 '16 at 12:36
  • very nice, solves it, thank you very much! If you want you can add this as answer - or how would I close this question now? – groebsgr Apr 25 '16 at 12:41

0 Answers0