Refactor S3 methods for fixed designs #482
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a proof-of-concept PR related to our discussion of the classes in #457
Instead of using conditional statements via
if
/else
/switch
to mimic OOP, I convertedsummary.fixed_design()
,as_gt.fixed_design()
, andas_rtf.fixed_design()
to instead use hierarchical S3 classes to dispatch the correct function for the given "method", eg "ahr".I'll explain how this works using
as_gt()
. When callingas_gt()
on the result of asummary()
of a design object, it will first call its method-specificas_gt()
. In the case of data produced byfixed_design_ahr()
, this will beas_gt.design_fixed_ahr_summary()
. This function sets the method-specific title and footnote, and then passes this information viaNextMethod()
to the more generalas_gt.design_fixed_summary()
, which contains the actual {gt} code to produce the table.summary()
andas_rtf()
work in similar ways, defining the method-specific data and then passing this to the generic function used by all methods.Here is an example with ahr:
Pros:
Cons:
as_gt()
, we'd have to add it multiple placesOther notes:
to_integer()
and thegs_design
S3 generics. I started with thefixed_design
S3 generics since they were the simplest.as_rtf()
so that the superscript"{a}"
is always added, even for custom titles and footnotes. In the current version, the superscript is lost when providing a custom title or footnote. I think my update is an improvement, and thus propose we update the snapshot testssummary()
. It doesn't make sense for both the input and output object ofsummary()
to have the same classes when they are fundamentally different (ie we have designedas_gt()
to only work on the output fromsummary()
, and it shouldn't be applied to the output fromfixed_design_ahr()
)