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

Order of the newly assigned classes #346

Closed
ddsjoberg opened this issue Mar 25, 2022 · 0 comments · Fixed by #359
Closed

Order of the newly assigned classes #346

ddsjoberg opened this issue Mar 25, 2022 · 0 comments · Fixed by #359
Labels
issue Used for project filtering
Milestone

Comments

@ddsjoberg
Copy link
Collaborator

ddsjoberg commented Mar 25, 2022

Quoting from the Advanced R book:

Objects are not limited to a single class, and can have many classes:
class(x) <- c("A", "B")
class(x) <- LETTERS
As discussed in the next section, R looks for methods in the order in which they appear in the class vector. So in this example, it would be like class A inherits from class B - if a method isn’t defined for A, it will fall back to B. However, if you switched the order of the classes, the opposite would be true!
http://adv-r.had.co.nz/S3.html

In visR, I noticed that some classes are defined in the opposite order: the most generic class is defined first and the specific visR classes are last. In the example below, we see that the ggplot classes are listed first, meaning that if ggplot2 were to ever implement a visr() method for class "ggplot" it would take precedent over the visR-defined method functions.

Generally, you want the most broad class listed last, and the most narrow and specific class should be listed first to ensure the S3 dispatch system selects the most appropriate method. So the classes below should be defined as c("ggsurvfit", "gg", "ggplot")

library(visR)

adtte %>%
  estimate_KM(strata = "TRTP", conf.int = 0.90) %>%
  visr(legend_position = "right", x_unit = "Days") %>%
  class()
#> [1] "gg"        "ggplot"    "ggsurvfit"

adtte %>% 
  ggplot2::ggplot() %>% 
  class()
#> [1] "gg"     "ggplot"

Created on 2022-03-24 by the reprex package (v2.0.1)

As an example, the Hmisc package adds the class "labelled" to labelled objects. The new labelled class is added to the beginning.

Hmisc::label(mtcars$mpg) <- "Miles per Gallon"
class(mtcars$mpg)
#> [1] "labelled" "numeric"

Created on 2022-03-26 by the reprex package (v2.0.1)

@ddsjoberg ddsjoberg added the issue Used for project filtering label Mar 25, 2022
@SHAESEN2 SHAESEN2 added this to the v0.3.0 milestone Mar 28, 2022
@ddsjoberg ddsjoberg mentioned this issue Apr 24, 2022
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue Used for project filtering
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants