From a992982cef1292cf0493c95e3850d4950fd4146e Mon Sep 17 00:00:00 2001 From: Fran Barton Date: Fri, 22 Sep 2023 10:14:57 +0100 Subject: [PATCH 1/8] Adapt make_report to use label-limits version of plotthedots --- R/spcr_make_report.R | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/R/spcr_make_report.R b/R/spcr_make_report.R index 927fae8..6e200c2 100644 --- a/R/spcr_make_report.R +++ b/R/spcr_make_report.R @@ -18,6 +18,7 @@ #' @param fresh_colour string. Customise the date lozenge to indicate that data is up to date, using a hex code, or CSS colour name #' @param output_directory string. The name of the directory in which to save the resulting report #' @param include_dq_icon logical. Whether to include the data quality icon on the final report +#' @param annotate_spc logical. Whether to add annotations to a secondary y axis for #' @param export_csv logical. Whether to export a CSV file of the source data as well as the HTML report. Default TRUE. #' #' @export @@ -40,6 +41,7 @@ spcr_make_report <- function( fresh_colour = "white", output_directory = ".", include_dq_icon = TRUE, + annotate_spc = TRUE, export_csv = TRUE) { start_time <- Sys.time() @@ -63,6 +65,7 @@ spcr_make_report <- function( "aggregation" ))) |> dplyr::mutate(spc_data = spc_data) |> + dplyr::mutate(label_limits = annotate_spc) |> purrr::pmap(make_spc_chart, .progress = "SPC charts") @@ -71,9 +74,8 @@ spcr_make_report <- function( purrr::pmap_chr(\(x, y) paste0("tmp_", x, "_", y, "_")) |> tempfile(fileext = ".png") - tmp_files |> - purrr::walk2(spc_charts, ggplot2::ggsave, width = 1800, height = 900, units = "px", dpi = 144) + purrr::walk2(spc_charts, write_chart_to_img) spc_chart_uris <- tmp_files |> purrr::map_chr(knitr::image_uri) @@ -134,6 +136,7 @@ spcr_make_report <- function( toc = FALSE, self_contained = TRUE, fig_caption = FALSE, + highlight = NULL, mathjax = NULL ), output_dir = file.path(getwd(), output_directory), @@ -147,19 +150,34 @@ spcr_make_report <- function( path <- file.path("file://", wd, output_directory, output_file_name) usethis::ui_info(paste0("Full path: ", path)) + # open the result in the browser + utils::browseURL(path) + beepr::beep() + process_duration <- lubridate::as.period(Sys.time() - start_time) |> round() |> tolower() - usethis::ui_done("Process completed in {process_duration}.") - beepr::beep() + usethis::ui_done("Report generated in {process_duration}.") invisible(TRUE) } - +#' Write a ggplot2 chart to a temporary png file (wrapper round `ggsave()`) +#' @noRd +write_chart_to_img <- function(img_file, chart) { + ggplot2::ggsave( + filename = img_file, + plot = chart, + device = ragg::agg_png, + width = 1500, + height = 750, + units = "px", + dpi = 125 + ) +} #' Create a 'plot the dots' SPC data parcel from data bundle columns @@ -186,7 +204,8 @@ make_spc_chart <- function( data_source, unit, aggregation, - spc_data) { + spc_data, + label_limits = TRUE) { spc_data |> NHSRplotthedots::ptd_create_ggplot( point_size = 4, # default is 2.5, orig in this package was 5 @@ -196,7 +215,8 @@ make_spc_chart <- function( y_axis_label = NULL, x_axis_date_format = dplyr::if_else(aggregation == "week", "%d-%b-%Y", "%b '%y"), icons_position = "none", - break_lines = "limits" + break_lines = "limits", + label_limits = label_limits ) + ggplot2::labs( caption = paste0("Data source: ", data_source) From d72fbff62edb1e49a3d8f10be9548b0a57bd787b Mon Sep 17 00:00:00 2001 From: Fran Barton Date: Mon, 6 Nov 2023 14:21:06 +0000 Subject: [PATCH 2/8] Follow function argument order --- R/spcr_make_report.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/spcr_make_report.R b/R/spcr_make_report.R index 6e200c2..3c74c39 100644 --- a/R/spcr_make_report.R +++ b/R/spcr_make_report.R @@ -214,9 +214,9 @@ make_spc_chart <- function( x_axis_label = NULL, y_axis_label = NULL, x_axis_date_format = dplyr::if_else(aggregation == "week", "%d-%b-%Y", "%b '%y"), + label_limits = label_limits, icons_position = "none", - break_lines = "limits", - label_limits = label_limits + break_lines = "limits" ) + ggplot2::labs( caption = paste0("Data source: ", data_source) From 52593ac50697aa6790608073ab658983d5e8f4f9 Mon Sep 17 00:00:00 2001 From: Tom Smith Date: Mon, 6 Nov 2023 22:29:32 +0000 Subject: [PATCH 3/8] change argument name to annotate_limits --- R/spcr_make_report.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/spcr_make_report.R b/R/spcr_make_report.R index 3c74c39..7fbe5ab 100644 --- a/R/spcr_make_report.R +++ b/R/spcr_make_report.R @@ -18,8 +18,8 @@ #' @param fresh_colour string. Customise the date lozenge to indicate that data is up to date, using a hex code, or CSS colour name #' @param output_directory string. The name of the directory in which to save the resulting report #' @param include_dq_icon logical. Whether to include the data quality icon on the final report -#' @param annotate_spc logical. Whether to add annotations to a secondary y axis for -#' @param export_csv logical. Whether to export a CSV file of the source data as well as the HTML report. Default TRUE. +#' @param annotate_limits logical. Whether to add annotations to a secondary y axis for process limits and mean +#' @param export_csv logical. Whether to export a CSV file of the source data as well as the HTML report. Default TRUE #' #' @export spcr_make_report <- function( @@ -41,7 +41,7 @@ spcr_make_report <- function( fresh_colour = "white", output_directory = ".", include_dq_icon = TRUE, - annotate_spc = TRUE, + annotate_limits = TRUE, export_csv = TRUE) { start_time <- Sys.time() @@ -65,7 +65,7 @@ spcr_make_report <- function( "aggregation" ))) |> dplyr::mutate(spc_data = spc_data) |> - dplyr::mutate(label_limits = annotate_spc) |> + dplyr::mutate(label_limits = annotate_limits) |> purrr::pmap(make_spc_chart, .progress = "SPC charts") From d6e3b2dea026629b324f7bb94400ce8f38e8eda8 Mon Sep 17 00:00:00 2001 From: Tom Smith Date: Mon, 6 Nov 2023 22:30:06 +0000 Subject: [PATCH 4/8] requires the development version of NHSRplotthedots --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index d707573..c9b4afd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,7 +28,7 @@ Imports: ggplot2, knitr, lubridate, - NHSRplotthedots (== 0.1.0), + NHSRplotthedots, purrr, readr, rlang, From c8b590f2aaab22d01b8e17207ffbf75b3b0c15ac Mon Sep 17 00:00:00 2001 From: Tom Smith Date: Mon, 6 Nov 2023 22:30:50 +0000 Subject: [PATCH 5/8] update docs --- NAMESPACE | 1 + man/spcr_make_report.Rd | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/NAMESPACE b/NAMESPACE index 00b6214..f8ae789 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(spcr_make_report) importFrom(dplyr,across) importFrom(dplyr,if_any) importFrom(dplyr,pick) +importFrom(glue,glue) importFrom(rlang,":=") importFrom(rlang,.data) importFrom(tidyselect,all_of) diff --git a/man/spcr_make_report.Rd b/man/spcr_make_report.Rd index 2b7bfea..3a25c23 100644 --- a/man/spcr_make_report.Rd +++ b/man/spcr_make_report.Rd @@ -23,6 +23,7 @@ spcr_make_report( fresh_colour = "white", output_directory = ".", include_dq_icon = TRUE, + annotate_limits = TRUE, export_csv = TRUE ) } @@ -63,7 +64,9 @@ spcr_make_report( \item{include_dq_icon}{logical. Whether to include the data quality icon on the final report} -\item{export_csv}{logical. Whether to export a CSV file of the source data as well as the HTML report. Default TRUE.} +\item{annotate_limits}{logical. Whether to add annotations to a secondary y axis for process limits and mean} + +\item{export_csv}{logical. Whether to export a CSV file of the source data as well as the HTML report. Default TRUE} } \description{ Make the SPC Report From e267c2f598ccc4ff5edf7385700a99383f1b6c0b Mon Sep 17 00:00:00 2001 From: Tom Smith Date: Mon, 6 Nov 2023 22:56:22 +0000 Subject: [PATCH 6/8] fix unrelated README error --- README.Rmd | 5 ++--- README.md | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.Rmd b/README.Rmd index e03d0a7..ddc87dd 100644 --- a/README.Rmd +++ b/README.Rmd @@ -109,7 +109,7 @@ data_bundle <- spcr_make_data_bundle( # make the report, including information for the six mandatory arguments. spcr_make_report( data_bundle = data_bundle, - title = "My Example Report", + report_title = "My Example Report", report_ref = "EG.001", data_cutoff_dttm = as.POSIXct("2022-09-30 23:59:59"), author_name = "Anne Author", @@ -128,7 +128,7 @@ spcr_make_report( # example where we map over several reports, creating them in one go all_my_reports <- tibble::tibble( - title = c("Report 1", "Report 2", "Report 3"), + report_title = c("Report 1", "Report 2", "Report 3"), report_ref = c("ID.1", "ID.2", "ID.3"), author_name = "Anne Author", author_email = "a.author@example.com", @@ -142,7 +142,6 @@ all_my_reports <- tibble::tibble( purrr::pwalk(all_my_reports, spcr_make_report, data_bundle = data_bundle) # ... but you may want to pass different data to each: - all_my_reports |> dplyr::mutate(data_bundle = list(data_bundle1, data_bundle2, data_bundle3)) |> purrr::pwalk(spcr_make_report) diff --git a/README.md b/README.md index 366bf47..da43835 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ data_bundle <- spcr_make_data_bundle( # make the report, including information for the six mandatory arguments. spcr_make_report( data_bundle = data_bundle, - title = "My Example Report", + report_title = "My Example Report", report_ref = "EG.001", data_cutoff_dttm = as.POSIXct("2022-09-30 23:59:59"), author_name = "Anne Author", @@ -116,7 +116,7 @@ spcr_make_report( # example where we map over several reports, creating them in one go all_my_reports <- tibble::tibble( - title = c("Report 1", "Report 2", "Report 3"), + report_title = c("Report 1", "Report 2", "Report 3"), report_ref = c("ID.1", "ID.2", "ID.3"), author_name = "Anne Author", author_email = "a.author@example.com", @@ -130,7 +130,6 @@ all_my_reports <- tibble::tibble( purrr::pwalk(all_my_reports, spcr_make_report, data_bundle = data_bundle) # ... but you may want to pass different data to each: - all_my_reports |> dplyr::mutate(data_bundle = list(data_bundle1, data_bundle2, data_bundle3)) |> purrr::pwalk(spcr_make_report) From ab1213640b66bf051342e8dde1ddbd4ea30815fa Mon Sep 17 00:00:00 2001 From: Tom Smith Date: Mon, 6 Nov 2023 23:13:15 +0000 Subject: [PATCH 7/8] update version and NEWS --- DESCRIPTION | 2 +- NEWS.md | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index c9b4afd..f3fa133 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: SPCreporter Title: Creates Metric Reports using Statistical Process Control in the NHS style -Version: 0.1.5.6 +Version: 0.1.6 Authors@R: c( person("Tom", "Smith",, "tomsmith_uk@hotmail.com", role = c("aut", "cre")), person("Fran", "Barton",, "fbarton@alwaysdata.net", role = "ctb")) diff --git a/NEWS.md b/NEWS.md index 220ec89..2f6d5e0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,16 @@ +# SPCreporter 0.1.6 + +## Feature addition + +* There is a new argument "annotate_limits" available for `spcr_make_report()`. By default it is `TRUE`, and adds labels for the numeric values of the control limits and mean to the right of the plot. + +## Bugfix + +* Fixed a bug which caused status messages to be printed in the main report if report generation is done on a slow computer. +* Fixed minor README argument name mistake, which would have thrown an error for new users. + +--- + # SPCreporter 0.1.5.6 This version introduces a large number of changes to the package. From 4cf5e4d9f477651e71ce3175a70c0d6ec23a851b Mon Sep 17 00:00:00 2001 From: Tom Smith Date: Mon, 6 Nov 2023 23:13:40 +0000 Subject: [PATCH 8/8] minor NEW formatting changes --- NEWS.md | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2f6d5e0..17913c1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -43,59 +43,71 @@ Some additional tests have been written, and existing tests amended to match the * Example data already supplied with the package (as xlsx files in the `inst` folder) is now also available as internal package data, which can be used as example data for demonstrating package functions and also for facilitating testing. +--- # SPCreporter 0.1.5 ## Feature addition -* Rebasing of SPC charts is now supported. Pass in a date string in "YYYY-MM-DD" format, or multiple date strings. `'"2020-01-01"'`, or `'"2020-01-01", "2021-05-01"'` -* Stale data is now highlighted in the report. By default data is highlighted as stale if the report cutoff is after the data update date (as defined by the `updated_to` argument for the data, and the `data_cutoff_dttm` for the report). Optionally, a column called `allowable_days_lag` can be included in the measure_config. This can be used to delay the flagging of stale data. For example using a value of 31 in the `allowable_days_lag` column will enable a measure to be reported a month in arrears without being flagged as stale. +* Rebasing of SPC charts is now supported. Pass in a date string in "YYYY-MM-DD" format, or multiple date strings. `'"2020-01-01"'`, or `'"2020-01-01", "2021-05-01"'` + +* Stale data is now highlighted in the report. By default data is highlighted as stale if the report cutoff is after the data update date (as defined by the `updated_to` argument for the data, and the `data_cutoff_dttm` for the report). Optionally, a column called `allowable_days_lag` can be included in the measure_config. This can be used to delay the flagging of stale data. For example using a value of 31 in the `allowable_days_lag` column will enable a measure to be reported a month in arrears without being flagged as stale. ## Useability -* Fixes an issue where if there are mismatches in measure_name across any of the 3 supplied dataframes (measure_data, report_config, measure_config) the error message throw reported missing data. `Error: spcr_check_dataset_is_complete: Data is missing for 1 report items. The first is ref 1, M1, monthly.` The correct and more helpful error in this case is to complain of the name mismatch: `Error: spcr_check_measure_names: There is a name mismatch for measure ref: 1. Check for typos or mismatching refs or data.` +* Fixes an issue where if there are mismatches in measure_name across any of the 3 supplied dataframes (measure_data, report_config, measure_config) the error message throw reported missing data. `Error: spcr_check_dataset_is_complete: Data is missing for 1 report items. The first is ref 1, M1, monthly.` The correct and more helpful error in this case is to complain of the name mismatch: `Error: spcr_check_measure_names: There is a name mismatch for measure ref: 1. Check for typos or mismatching refs or data.` * Improved progress messages during `spcr_make_data_bundle()` to support debugging of incoming data. ## Bugfix -* Fix a bug relating to NAs in the dataset which was causing printed warnings in the final report, as well as blank space in the charts. The new behaviour strips NAs from all data, meaning that the chart axis starts at the first data point, and ends at the last. +* Fix a bug relating to NAs in the dataset which was causing printed warnings in the final report, as well as blank space in the charts. The new behaviour strips NAs from all data, meaning that the chart axis starts at the first data point, and ends at the last. + +--- # SPCreporter 0.1.4 ## Feature addition -* Add two new optional columns to the measure_config: "Reviewed At", and "Escalated To". If present the text in these fields is rendered into the final report below each chart. +* Add two new optional columns to the measure_config: "Reviewed At", and "Escalated To". If present the text in these fields is rendered into the final report below each chart. ## Bugfix -* Fixed an error caused when measure_data contained NAs. The package will now tolerate NAs in the data. +* Fixed an error caused when measure_data contained NAs. The package will now tolerate NAs in the data. + +--- # SPCreporter 0.1.3 ## Useability improvements -* Significant improvements to error messages relating to measure_data, report_config, and measure_config. Error messages now explicitly name any required columns which the user has not supplied. This is intended to make troubleshooting simpler to do. -* The data source for metrics is now named in charts as a caption. -* Other minor useability improvements intended to make working with real-world config documents easier to do. +* Significant improvements to error messages relating to measure_data, report_config, and measure_config. Error messages now explicitly name any required columns which the user has not supplied. This is intended to make troubleshooting simpler to do. +* The data source for metrics is now named in charts as a caption. +* Other minor useability improvements intended to make working with real-world config documents easier to do. + +--- # SPCreporter 0.1.2 ## Useability improvements -* measure_data requires data for only one (or more) of "week" or "month". -* measure_data tolerates capitalised list (worksheet) names. +* `measure_data` requires data for only one (or more) of "week" or "month". +* `measure_data` tolerates capitalised list (worksheet) names. * Use the base pipe |> in place of %>%. * Throw a helpful error if insufficient data items have been provided for a given report. +--- + # SPCreporter 0.1.1 ## Bugfix * Fixes an error occurring when targets are not set for any of the reported measures +--- # SPCreporter 0.1.0 + ## Initial Release -* spcr_make_data_bundle() -* spcr_make_report() +* `spcr_make_data_bundle()` +* `spcr_make_report()`