ThomUK / SPCreporter

Creates Metric Reports using Statistical Process Control in the NHS style
https://thomuk.github.io/SPCreporter/
Other
6 stars 2 forks source link

spcr_make_data_bundle() errors if empty a_data is supplied #95

Open francisbarton opened 10 months ago

francisbarton commented 10 months ago

Reprex:

library(lubridate, warn.conflicts = FALSE)

test_data <- list(
  month = tibble::tribble(
    ~ref, ~measure_name, ~comment
  ),
  events = tibble::tribble(
    ~ref, ~measure_name, ~comment, ~event_date_or_datetime,
    "1", "Test data", "Reprex for duplicate date", as_date("2023-12-18"),
    "1", "Test data", "Reprex for duplicate date", as_date("2023-12-18")
  )
)

test_rc <- tibble::tribble(
  ~ref, ~measure_name, ~domain, ~spc_chart_type, ~aggregation, ~report_comment,
  "1", "Test data", NA_character_, "t", "none", NA_character_
)

test_mc <- tibble::tribble(
  ~ref, ~measure_name, ~unit, ~data_source, ~data_owner, ~accountable_person, ~improvement_direction, ~target, ~target_set_by, ~data_quality, ~rebase_dates, ~rebase_comment,
  "1", "Test data", "integer", "My brain", "Me", "nobody", "decrease", 0, "Me", "GGGG", "", ""
)

test_data_bundle <- SPCreporter::spcr_make_data_bundle(
  test_data, test_rc, test_mc
)
#> ℹ check_for_optional_columns: Optional column 'allowable_days_lag' is missing.
#> Adding it.
#> Error in `tidyr::pivot_longer()` at SPCreporter/R/helper_functions.R:38:3:
#> ! `cols` must select at least one column.
#> Backtrace:
#>      ▆
#>   1. └─SPCreporter::spcr_make_data_bundle(test_data, test_rc, test_mc)
#>   2.   ├─dplyr::bind_rows(lengthen_measure_data(a_data_df), e_data_time_between) at SPCreporter/R/spcr_make_data_bundle.R:51:3
#>   3.   │ └─rlang::list2(...)
#>   4.   └─SPCreporter:::lengthen_measure_data(a_data_df)
#>   5.     ├─dplyr::arrange(...) at SPCreporter/R/helper_functions.R:38:3
#>   6.     ├─dplyr::mutate(...) at SPCreporter/R/helper_functions.R:38:3
#>   7.     ├─tidyr::pivot_longer(...) at SPCreporter/R/helper_functions.R:38:3
#>   8.     └─tidyr:::pivot_longer.data.frame(.data, !any_of(init_cols), names_to = "date", values_drop_na = TRUE)
#>   9.       └─tidyr::build_longer_spec(...)
#>  10.         └─cli::cli_abort(...)
#>  11.           └─rlang::abort(...)

If only a data frame that contains no rows is supplied within measure data, then spcr_make_data_bundle()'s check and shape process will error at the lengthen_measure_data() step.