Hi folks, I am trying to learn a bit of how to extend R using object oriented programming. I set myself a little task to extend numerics so that if the value equals 7 "little bunny foo foo" will be printed. In other cases, the actual value will be printed. My code is
new_nursery_Song <- function(x = double()){
stopifnot(is.double(x))
structure(x,class = c("nursery_Song","numeric"))
}
print.nursery_Song <- function(x){
if (x == 7)
print("little bunny foo foo")
else
print(x)
}
print(new_nursery_Song(7))
print(new_nursery_Song(3))
which gives
[1] "little bunny foo foo"
Error: C stack usage 15927136 is too close to the limit
(My guess is that print() is being called recursively rather than going to the generic print(), but it's just a guess.)
Any help will be greatly appreciated, including telling me that I'm thinking about this entirely in the wrong way. By way of background,
- It's been a long time since I've done any object oriented programming.
- I've never done oop in R (which you've probably already guessed).
I have read through the relevant parts of both Hands on Programming with R (Grolemund) and Advanced R (Wickham). Both quite helpful.