Create a method for a class I have created

I am in need to create a create a method for a class I have created, I do not have access to the original function code. I just want to build on top of it.
a <-1 class(a)←c("This is my class",class(a)) print.firstclass<-function(b)
{ b<-paste0(b,"foobar") print(b) } print(a)
When I try to run the last line, it throws the function into a loop and R in conclusion crashes. I found a solution to add a line to the function that strips the new class name from b before passing it to the original function:
print.firstclass <-function(b) {b<-paste0(b,"foobar"); class(b)<-class(b)[-1]; print(b)}
Is ther any other good way to do this? Please suggest.
Thanks in advance.



  • I think your problem is that you create an infinite loop: print(print(...).

    This might be what you are looking for:

    a <- 1 class(a) <- c("myclass",class(a)) print.myclass <- function(x) print.default(a) print(a)

Sign In or Register to comment.