You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When creating a class with S7, there doesn't seem to be a way to implement automatic printing behavior when the object is typed in the console, unlike S4 classes.
Is it a limitation of S7 or is there any workaround I am missing?
Minimal Reproducible Example
library(S7)
# Define a simple classTrack<- new_class(
"Track",
properties=list(
x=class_numeric,
y=class_numeric
)
)
# Create an instancet1<- Track(
x=1:3,
y= c(1, 4, 9)
)
# Current behavior: prints internal representationt1#> <Track>#> @ x: int [1:3] 1 2 3#> @ y: num [1:3] 1 4 9# Attempted solutions that didn't work:# 1. Using S7 method for showshow<- new_generic("show", "x")
method(show, Track) <-function(x) {
print(c(
x= max(x@x),
y= min(x@y)
))
}
# prints internal representationt1#> <Track>#> @ x: int [1:3] 1 2 3#> @ y: num [1:3] 1 4 9# Prints desired representation
show(t1)
#> x y #> 3 1#2. Trying to bridge with S4setClass("Track",
slots= c(x="numeric", y="numeric"),
contains="S7_object")
setMethod("show", "Track",
function(object) {
print(c(
x= max(object@x),
y= min(object@y)
))
}
)
# None of these approaches achieve the desired automatic printing behaviort1#> <Track>#> @ x: int [1:3] 1 2 3#> @ y: num [1:3] 1 4 9
When creating a class with S7, there doesn't seem to be a way to implement automatic printing behavior when the object is typed in the console, unlike S4 classes.
Is it a limitation of S7 or is there any workaround I am missing?
Minimal Reproducible Example
Created on 2024-12-24 with reprex v2.1.0
Expected Behavior
When typing
t1
in the console, it should display a custom formatted output (similar to how S4 classes work withshow
methods).Created on 2024-12-24 with reprex v2.1.0
Show Session Info
The text was updated successfully, but these errors were encountered: