Skip to content

Adding functions to produce impact, coverage and fvp plots #15

Draft
zegibney wants to merge 2 commits intovimc:developfrom
zegibney:develop_zoe
Draft

Adding functions to produce impact, coverage and fvp plots #15
zegibney wants to merge 2 commits intovimc:developfrom
zegibney:develop_zoe

Conversation

@zegibney
Copy link
Copy Markdown

This PR adds additional functions from [https://github.com/vimc/montagu-reports/tree/main/src/rapid-model-run-impact] a key report for the VIMC Science & Policy team.

Functions added generate central estimate impact plots and coverage and fvp plots per year, country and view.

First attempt at ensuring functions are package-ready/ utilising roxygen etc. so any comment welcome.

TODO's in script fn_plotting_impact.R with specific questions. I understand testing may need to be stronger/ the next step

…ots central impact estimates, coverage and fvps. Associated documentation also added from attempts at roxygen!
@zegibney zegibney marked this pull request as draft April 29, 2026 11:34
@pratikunterwegs
Copy link
Copy Markdown
Contributor

Hi @zegibney - I'm taking a look at the code now. Just a few preliminary points:

  1. I see that some package checks haven't launched. That's because of I've set up the GH Actions config files. Could you change Line 7 on .github/workflows/R-CMD-check.yaml to replace [main, master] with "*"? And the same in .github/workflows/lint-changed-files.yaml? That should allow these checks to run on PRs to any branch.
  2. The {pkgdown} check is failing because the functions you've added don't have a place to live in the online docs. The organisation of the docs is controlled in _pkgdown.yml. Could you add the names of the functions you've added to this file. See the current organisation to get a sense of which section (determined by title) or subsection (subtitle) they should go under. Or add a new (sub)section if needed.

Copy link
Copy Markdown
Contributor

@pratikunterwegs pratikunterwegs left a comment

Choose a reason for hiding this comment

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

I've just added some comments here. We should probably think of a testing strategy as well, but that's a bit bound up with the overall issue of how to provide data for testing as in PR #14.

Comment thread R/fn_plotting_impact.R
@@ -0,0 +1,240 @@
#' Plot central impact estimates by cohort and year.
#' TODO: need to add importFrom ... to avoid package issues with testing?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The way @importFrom is used is to import functions from packages that are used repeatedly, really only to avoid writing out the explicit namespacing each time (i.e., ggplot2::fn()). Tests might fail if you neither import nor explicitly namespace a function, but the tag isn't otherwise linked to test failures.

Comment thread R/fn_plotting_impact.R
Comment on lines +16 to +21
#' plot_impact(
#' data = impact_data,
#' burden_type = "cases",
#' title = "Cases averted",
#' view = "year"
#' )
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This example will fail as impact_data hasn't been defined. Each @example block is standalone, so you'll need to create impact_data here.

Comment thread R/fn_plotting_impact.R
data,
burden_type,
title,
view
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The convention for a small number of fixed argument options is to display them in the function signature (i.e., this bit).

So prefer f = function(arg = c("option_1", "option_2")). This way when users pull up the help page they can immediately see the valid options.

This should be paired with some argument checking which you already have. checkmate::assert_choice() is fine; in {vimcheck} I've gone with rlang::arg_match() whose error messages are a bit more informative.

Comment thread R/fn_plotting_impact.R
view
){
checkmate::assert_tibble(data, min.rows = 1L, min.cols = 1L)
checkmate::assert_character(burden_type, len = 1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Prefer using rlang::arg_match() here too. If keeping assert_choice(), this assert_character() isn't needed. assert_character(..., len = 1) is better replaced with assert_string() (in other use cases, can simply be removed here).

Comment thread R/fn_plotting_impact.R
)

Impact <-
data %>%
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So I would recommend against using pipes in functions, because any errors that occur can only be traced to the top of the pipeline (here, L. 44), and not to the line where they actually are. It's tedious, but it's better to replace each step in the pipeline with an explicit assignment step.

Comment thread R/fn_plotting_impact.R
#' \item{fvps}{A plot of fully vaccinated persons over time.}
#' }
#' @examples
#' plots <- plot_coverage_fvps(fvps)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Need to define the data fvps.

Comment thread R/fn_plotting_impact.R
title,
view
){
checkmate::assert_tibble(data, min.rows = 1L, min.cols = 1L)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might be good to check that data has at least as many cols as are used in this fn; would also be good to add checks on the specific column names. See e.g. fns in R/fn_plotting_prep_bur_diag.R which use checkmate::assert_names() for this.

Comment thread R/fn_plotting_impact.R
Comment on lines +146 to +147
plot_coverage_fvps <- function(fvps){
checkmate::assert_tibble(fvps, min.rows = 1L, min.cols = 1L)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pretty much the same general suggestions as for the preceding fn.

Comment thread R/fn_plotting_impact.R
Comment on lines +158 to +162
dplyr::select(
.data$country,
.data$vaccine_delivery,
.data$year,
.data$coverage_adjusted) %>%
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure how advisable this syntax is. I would define the cols to select as a character vector and pass them to select using all_of() or {{ }}.

Comment thread R/fn_plotting_impact.R
.data$vaccine_delivery,
.data$year,
.data$coverage_adjusted) %>%
dplyr::rename(coverage = .data$coverage_adjusted)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here it might be safer to pass a named vector giving the renaming map. See 2nd example in rename docs: https://dplyr.tidyverse.org/reference/rename.html

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.

2 participants