Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic printing (show method) not working for S7 classes #516

Closed
atsyplenkov opened this issue Dec 24, 2024 · 1 comment
Closed

Automatic printing (show method) not working for S7 classes #516

atsyplenkov opened this issue Dec 24, 2024 · 1 comment

Comments

@atsyplenkov
Copy link

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 class
Track <- new_class(
  "Track",
  properties = list(
    x = class_numeric,
    y = class_numeric
  )
)

# Create an instance
t1 <- Track(
  x = 1:3,
  y = c(1, 4, 9)
)

# Current behavior: prints internal representation
t1
#> <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 show
show <- new_generic("show", "x")
method(show, Track) <- function(x) {
  print(c(
    x = max(x@x),
    y = min(x@y)
  ))
}

#  prints internal representation
t1
#> <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 S4
setClass("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 behavior
t1
#> <Track>
#>  @ x: int [1:3] 1 2 3
#>  @ y: num [1:3] 1 4 9

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 with show methods).

setClass("Track", slots = c(x = "numeric", y = "numeric"))

setMethod("show", "Track",
  function(object) {
    print(c(
      x = max(object@x),
      y = min(object@y)
    ))
  }
)

t1 <- new("Track", x=1:20, y=(1:20)^2)
t1
#>  x  y 
#> 20  1

Created on 2024-12-24 with reprex v2.1.0

Show Session Info
library(S7)
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       Pacific/Auckland
#>  date     2024-12-24
#>  pandoc   3.2 @ c:\\scoop\\apps\\positron\\2025.01.0-39\\resources\\app\\quarto\\bin\\tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.3   2024-06-21 [1] RSPM
#>  digest        0.6.35  2024-03-11 [1] CRAN (R 4.4.1)
#>  evaluate      1.0.1   2024-10-10 [1] RSPM (R 4.4.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.4.1)
#>  fs            1.6.4   2024-04-25 [1] CRAN (R 4.4.1)
#>  glue          1.8.0   2024-09-30 [1] RSPM
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.1)
#>  knitr         1.49    2024-11-08 [1] RSPM
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.1)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.4.1)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.4.1)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.4.1)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.4.0)
#>  R.oo          1.26.0  2024-01-24 [1] CRAN (R 4.4.0)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.4.1)
#>  reprex        2.1.0   2024-01-11 [1] CRAN (R 4.4.1)
#>  rlang         1.1.4   2024-06-04 [1] CRAN (R 4.4.1)
#>  rmarkdown     2.29    2024-11-04 [1] RSPM
#>  S7          * 0.2.0   2024-11-07 [1] RSPM
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.1)
#>  styler        1.10.3  2024-04-07 [1] CRAN (R 4.4.1)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.4.1)
#>  withr         3.0.2   2024-10-28 [1] RSPM
#>  xfun          0.49    2024-10-31 [1] RSPM (R 4.4.0)
#>  yaml          2.3.10  2024-07-26 [1] RSPM (R 4.4.0)
#> 
#>  [1] C:/Users/TsyplenkovA/AppData/Local/R/win-library/4.4
#>  [2] C:/Program Files/R/R-4.4.2/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@hadley
Copy link
Member

hadley commented Dec 24, 2024

Try providing a method for the S3 print generic.

@hadley hadley closed this as completed Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants