Creating default print methods for class sr_model#114
Creating default print methods for class sr_model#114sschildhauer wants to merge 84 commits intomainfrom
Conversation
Merge commit '4ee4b99f9f601b2f95fab79fb25a7d781b37e1a7' #Conflicts: # R/Run_Mod.R
Codecov Report❌ Patch coverage is
|
|
📖 https://ucd-serg.github.io/serodynamics/preview/pr114 |
|
I am a little confused with the warning/error I am getting here. It looks like there is a scoping issue relating to the base dataset when using dplyr, but when I fix the issue the tests are producing a warning. It seems like the solution might be in conflicting with each other. Grateful for any help! |
|
Hi Ezra and Kwan. I checked the snaps and having a hard time finding a solution. It looks like the test for print.sr_model in the github review is producing different median values than when I run the function locally. There also seems to be something going on with autoplot.case_data documention -- this is a new issue and has me stumped as well. Thank you so much for review. These should almost be there! |
There was a problem hiding this comment.
@sschildhauer re: the document error: ggplot2 recently updated to v4.0 and changed a lot of stuff, including documentation, which the documentation for autoplot.case_data() depends on.
Please update ggplot2 and its dependencies on your local machine and re-run document(); once you do that, the changes you introduced in autoplot.case_data.Rd will hopefully go away.
|
@sschildhauer PS - I bet updating ggplot2 will also resolve the snapshot discrepancies; feel free to re-ping me if you're still stuck after that. |
Merge commit '783e92bef4e57e1fa0a000041fe2906346d81e1b'
|
@Kwan-Jenny , can you also take a quick look at this when you get a chance? I recently created an option in the print to make the output similar to that of print.tbl_df. |
Co-authored-by: Kwan-Jenny <68584166+Kwan-Jenny@users.noreply.github.com>
Merge commit 'fcdeafb1bf63c03cdeb86504ad1235ecf179ffb7' #Conflicts: # DESCRIPTION # NEWS.md # R/expect_snapshot_data.R # inst/WORDLIST
There was a problem hiding this comment.
Pull request overview
Adds an S3 print() method for sr_model objects (the primary output type of run_mod()), with snapshot tests and documentation, and surfaces the method in pkgdown.
Changes:
- Introduces
print.sr_model()with an option to print either a median-summary table or the underlying tibble. - Adds snapshot tests + snapshot outputs for the new print behavior.
- Updates documentation/indexing (NAMESPACE, Rd, pkgdown reference) and project metadata (NEWS, WORDLIST).
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
R/print.sr_model.R |
New S3 print method for sr_model including a summary view and a “print as tibble” option. |
tests/testthat/test-print.sr_model.R |
Adds snapshot tests covering printed output for sr_model. |
tests/testthat/_snaps/print.sr_model.md |
Snapshot expectations for the new print outputs. |
man/print.sr_model.Rd |
Roxygen-generated documentation for print.sr_model. |
NAMESPACE |
Registers print.sr_model as an S3 method. |
pkgdown/_pkgdown.yml |
Adds print.sr_model to the reference index. |
NEWS.md |
Changelog entry for the new print method. |
inst/WORDLIST |
Adds new spelling whitelist terms. |
R/Run_Mod.R |
Minor formatting-only change at end of file. |
| test_that( | ||
| desc = "results are consistent with printed output for sr_model class", | ||
| code = { | ||
| testthat::expect_snapshot(nepal_sees_jags_output) | ||
| } | ||
| ) | ||
|
|
||
| test_that( | ||
| desc = "results are consistent with printed output for sr_model class as tbl", | ||
| code = { | ||
| testthat::expect_snapshot(print(nepal_sees_jags_output, print_tbl = TRUE)) | ||
| } | ||
| ) |
There was a problem hiding this comment.
The print method has logic to drop the Stratification column when it’s missing or all "None", but the added snapshot tests only cover stratified data (typhi/paratyphi). Add a test case for an sr_model with Stratification == "None" to exercise this branch and prevent regressions.
| x <- x |> | ||
| dplyr::summarise(.by = c(.data$Stratification, .data$Iso_type, | ||
| .data$Parameter), | ||
| median_val = stats::median(.data$value)) |> | ||
| tidyr::pivot_wider(names_from = .data$Parameter, | ||
| values_from = .data$median_val) |> | ||
| dplyr::arrange(.data$Iso_type) |> |
There was a problem hiding this comment.
The printed summary is currently computed across all Subject values. Since sr_model contains subject-specific parameter draws, aggregating without filtering (e.g., to Subject == "newperson") produces a mixture that depends on how many subjects are in the object and is inconsistent with post_summ() / diagnostic helpers that summarise only newperson draws.
| cat("An sr_model with the following median values:") | ||
| cat("\n") | ||
| cat("\n") | ||
| x <- x |> | ||
| dplyr::summarise(.by = c(.data$Stratification, .data$Iso_type, | ||
| .data$Parameter), | ||
| median_val = stats::median(.data$value)) |> |
There was a problem hiding this comment.
The PR description says the default print shows the mean of the posterior distribution, but the implementation/message and docs use the median (stats::median() and "median values"). Please align the implementation, printed wording, and documentation with the intended statistic.
| #' @param print_tbl A [logical] indicator to print in style of [dplyr::tbl_df]. | ||
| #' @param ... Additional arguments affecting the summary produced. | ||
| #' [serodynamics::run_mod()] function. |
There was a problem hiding this comment.
... is documented as affecting the summary produced by run_mod(), but this is a print method and ... is not used/forwarded. Consider documenting ... as additional arguments passed to print() (and actually pass ... through), or remove it if intentionally unused.
| if (print_tbl) { | ||
| x <- dplyr::as_tibble(x) | ||
| print(x) | ||
| } else { |
There was a problem hiding this comment.
In the print_tbl = TRUE branch, the method converts x to a tibble and returns the result of print(x) (visible) without invisible(). For S3 print() methods, it’s conventional to return the original object invisibly; also consider not overwriting/dropping the sr_model class just for printing.
I am creating a default print methods for sr_model using the S3 object system. The default print provides the mean value of the posterior distribution for each parameter and all stratification iso_type combinations.