You can make the printing method S3
sky <- function(){ structure(list(sun = 1, clouds = 4, birds =2, moon = 0), class="mysky") } print.mysky <- function(x, ...) print(x[1:2]) sky()
You can see that this only affects how it is printed.
str(sky()) #List of 4 # $ sun : num 1 # $ clouds: num 4 # $ birds : num 2 # $ moon : num 0 # - attr(*, "class")= chr "mysky" names(sky()) #[1] "sun" "clouds" "birds" "moon"
Here's another way to assign a class to an object
sky <- function(){ out <- list(sun = 1, clouds = 4, birds =2, moon = 0) class(out) <- "mysky" out }
print.mysky
will be sent because the object class is "mysky"
class(sky()) #[1] "mysky"
if you want to send the default print method, you can either call it directly ( print.default(sky())
) or an unclass
object
#print.default(sky()) unclass(sky())
source share