Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions R/argument_validation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

#' Validate chart_type argument
#'
#' Checks that `chart_type` is a single, non-NULL character string corresponding
#' to a supported SPC chart type. Intended for internal use only.
#'
#' @param chart_type Character scalar specifying chart type.
#'
#' @return Invisibly returns TRUE if valid; otherwise errors.
#'
#' @keywords internal
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think with @noRd, @keywords internal is not required? But could you check?

#' @noRd


validate_chart_type <- function(chart_type) {

allowed_chart_types <- c("XMR", "MR", "C", "C'", "P", "P'")

# NULL check
if (is.null(chart_type)) {
stop(
"chart_type must be provided. Available chart types are: ",
paste(allowed_chart_types, collapse = ", "),
".",
call. = FALSE
)
}

# Length check
if (length(chart_type) != 1) {
stop(
"chart_type must be a single value. ",
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think rather than "must be a single value" let's have "must have length one."

"Available chart types are: ",
paste(allowed_chart_types, collapse = ", "),
".",
call. = FALSE
)
}

# Type check (defensive)
if (!is.character(chart_type)) {
stop(
"chart_type must be a character string.",
call. = FALSE
)
}

# Value check
if (!chart_type %in% allowed_chart_types) {
stop(
sprintf(
"Invalid chart_type: '%s'. Available chart types are: %s.",
chart_type,
paste(allowed_chart_types, collapse = ", ")
),
call. = FALSE
)
}

invisible(TRUE)
}
7 changes: 4 additions & 3 deletions R/autospc.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#' }
#' @param n Name of column (passed using tidyselect semantics) to use as
#' denominator for P and P' charts.
#' @param chart_type The type of chart you wish to plot. Available options are:
#' "XMR", "MR", "C", "C'", "P", "P'".
#' @param chart_type The type of chart you wish to plot.Must be a single value.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

As above re. "single value" vs "length one", plus missing space before "Must"

#' Available options are: "XMR", "MR", "C", "C'", "P", "P'".
#'
#' ## Algorithm Parameters
#' Parameters that control behaviour of the algorithm used to re-establish
Expand Down Expand Up @@ -249,7 +249,8 @@ autospc <- function(data,
upper_annotation_sf <- preprocessed_vars$upper_annotation_sf
lower_annotation_sf <- preprocessed_vars$lower_annotation_sf


validate_chart_type(chart_type)

# Get control limits
data <- create_SPC_auto_limits_table(
data,
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-autospc-chart-type.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("autospc errors informatively for invalid chart_type", {

expect_error(
autospc(
ed_attendances_monthly,
chart_type = "XmR'",
x = month_start,
y = att_all
),
"Available chart types are"
)

})