Skip to content

Validate chart_type early and improve the error message#217

Merged
HorridTom merged 8 commits intoHorridTom:masterfrom
ahmadmkhatib:fix-chart-type-issue-151
Jan 21, 2026
Merged

Validate chart_type early and improve the error message#217
HorridTom merged 8 commits intoHorridTom:masterfrom
ahmadmkhatib:fix-chart-type-issue-151

Conversation

@ahmadmkhatib
Copy link
Copy Markdown
Collaborator

I added early validation for chart_type in autospc(), replacing the old error with a clear, user-facing message listing supported chart types.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@HorridTom
Copy link
Copy Markdown
Owner

Thanks @ahmadmkhatib this looks good. A couple of suggestions:

  1. I think we need to handle cases where the user passes chart_type = NULL or where length(chart_type) > 1. See the 3rd code block in this StackOverflow answer for an example: https://stackoverflow.com/a/63788053/6180841.

  2. Over time I'd like to add argument checks for many more arguments, and as this happens, if the code for these checks sits in autospc() it will quickly become too much. So instead, could we put this check in a function (called e.g. validate_chart_type() that sits in a new .R file (called e.g. argument_validation.R) and then call this function from near the top of autospc()? I think this will end up being neater. There should be no need for validate_chart_type() to return anything, because its purpose is to error if the argument is not valid.

I don't think these changes should take much work from what you have already, but let me know if anything not clear or if I can help in any way.

@ahmadmkhatib
Copy link
Copy Markdown
Collaborator Author

ahmadmkhatib commented Jan 14, 2026

Yes @HorridTom, agree and will work on these suggestions

@ahmadmkhatib
Copy link
Copy Markdown
Collaborator Author

@HorridTom, I added the function in an R file now called argument_validation. Check it out.

Comment thread R/argument_validation.R Outdated
# 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."

Comment thread R/argument_validation.R Outdated
#'
#' @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?

Comment thread R/autospc.R Outdated
#' 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"

@HorridTom
Copy link
Copy Markdown
Owner

@HorridTom, I added the function in an R file now called argument_validation. Check it out.

Great, thanks @ahmadmkhatib - just a couple of minor comments now. Plus you might want to add a few more tests to cover the other checks you've added? (i.e. chart_type NULL, of length greater than 1, or non-character)

@ahmadmkhatib
Copy link
Copy Markdown
Collaborator Author

Hi @HorridTom - I enhanced the presentation and added more tests

1) added @nord back into roxygen skeleton for validate_chart_type() - since this is an internal only function there should not be a .Rd man file for this
2) There was already a check for chart_type NULL in preprocess_inputs(), with a deprecation warning. I have moved this into validate_chart_type() and elevated from warning to error
3) Adjusted test to account for this change (2)
4) Added a missing space in the error message for length != 1
@HorridTom
Copy link
Copy Markdown
Owner

Hi @ahmadmkhatib - this looks great now. I made a few tweaks, see c8fb2bc above. If you're happy with these I'll go ahead and merge.

@ahmadmkhatib
Copy link
Copy Markdown
Collaborator Author

Great, yes, happy, let's merge

@HorridTom HorridTom merged commit 5696433 into HorridTom:master Jan 21, 2026
10 checks passed
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