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

Adopt S7 in most places #6364

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Mar 17, 2025

This PR aims to fix a large part of #6352.

Briefly it implements the following S7 classes:

  • class_ggplot
  • class_ggplot_built
  • class_mapping
  • class_labels
  • class_theme

I've put in the usual extractors and replacers ([, [[, $ and their <- methods) for the ggplot/ggplot_built classes for backwards compatibility. The code internally now uses @ though, so that we can deprecate the usual extractors/replacers later.


#' @export
print.uneval <- function(x, ...) {
`print.ggplot2::mapping` <- function(x, ...) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we have an S3 generic we need for both S3 and S7 objects, we need to implement methods for the S7 objects in the S3 fashion, because using S7::method() will invalidate the S3 methods.

@teunbrand
Copy link
Collaborator Author

Older versions of R seem to get stuck on dispatch with +. We can get around this by implementing this + as S7 methods only (i.e. converting the +.gg method), but it makes error messages less informative. For example: Cannot use + with a single argument. i Did you accidentally put + on a new line? will become invalid argument to unary operator and also Cannot add <ggproto> objects together. i Did you forget to add this object to a <ggplot> object? becomes non-numeric argument to binary operator.

Comment on lines +2 to +3
class_gg <- S7::new_class("gg", abstract = TRUE)
class_S3_gg <- S7::new_S3_class("gg")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add note to remove at some point

@teunbrand
Copy link
Collaborator Author

teunbrand commented Mar 25, 2025

TODO: Remove class gymnastics
Clearly mark workarounds for R < 4.3.0

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

Successfully merging this pull request may close these issues.

1 participant