Skip to content

Commit

Permalink
reduce and rename functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksander Chlebowski committed Dec 4, 2022
1 parent 0d93eec commit 794777f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 173 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ RoxygenNote: 7.2.2
Collate:
'dummy_functions.R'
'example_module.R'
'gather_fails.R'
'get_rcode.R'
'get_rcode_utils.R'
'include_css_js.R'
Expand All @@ -94,5 +93,6 @@ Collate:
'tdata.R'
'teal.R'
'utils.R'
'validate_inputs.R'
'validations.R'
'zzz.R'
5 changes: 2 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ S3method(ui_nested_tabs,teal_module)
S3method(ui_nested_tabs,teal_modules)
export("%>%")
export(example_module)
export(gather_fails)
export(gather_fails_com)
export(gather_fails_grp)
export(get_code_tdata)
export(get_join_keys)
export(get_metadata)
Expand All @@ -43,6 +40,8 @@ export(validate_has_data)
export(validate_has_elements)
export(validate_has_variable)
export(validate_in)
export(validate_inputs)
export(validate_inputs_segregated)
export(validate_n_levels)
export(validate_no_intersection)
export(validate_one_row_per_id)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### New features

* Added the `gather_fails` function that produces informative error messages in app output.
* Added the `validate_inputs` function that produces informative error messages in app output.

### Major breaking changes

Expand Down
46 changes: 15 additions & 31 deletions R/gather_fails.R → R/validate_inputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@
#' validation message that is passed to a `validate`/`need` call.
#' This way the input validator messages are repeated in the output.
#'
#' \code{gather_fails} accepts one `InputValidator`
#' and can add a header to its validation messages.
#' \code{gather_fails_com} accepts an arbitrary number of `InputValidator`s
#' and prints all messages together under one header.
#' \code{gather_fails_grp} accepts a \strong{list} of `InputValidator`s
#' and prints messages in groups. If elements of \code{validators} are named,
#' \code{validate_inputs} accepts an arbitrary number of `InputValidator`s
#' and prints all messages together, adding one (optional) header.
#' \code{validate_inputs_segregated} accepts a list of `InputValidator`s
#' and prints messages grouped by validator. If elements of \code{validators} are named,
#' the names are used as headers for their respective message groups.
#'
#'
#' @name gather_fails
#' @name validate_inputs
#'
#' @param iv object of class `InputValidator`
#' @param ... for \code{validate_inputs} any number of `InputValidator` objects \cr
#' for \code{validate_inputs_segregated} arguments passed to `shiny::validate`
#' @param header `character(1)` optional generic validation message
#' @param ... for \code{gather_fails} and \code{gather_fails_grp} arguments passed to `shiny::validate`\cr
#' for \code{gather_fails_com} any number of `InputValidator` objects
#' @param validators optionally named `list` of `InputValidator` objects, see \code{Details}
#'
#' @return
Expand Down Expand Up @@ -82,11 +79,11 @@
#' # validate output
#' switch(input[["method"]],
#' "hierarchical" = {
#' gather_fails(iv)
#' gather_fails(iv_par, "Set proper graphical parameters")
#' validate_inputs(iv)
#' validate_inputs(iv_par, "Set proper graphical parameters")
#' },
#' "combined" = gather_fails_com(iv, iv_par),
#' "grouped" = gather_fails_grp(list(
#' "combined" = validate_inputs(iv, iv_par),
#' "grouped" = validate_inputs_segregated(list(
#' "Some inputs require attention" = iv,
#' "Set proper graphical parameters" = iv_par
#' ))
Expand All @@ -103,22 +100,9 @@
#' shinyApp(ui, server)
#' }

#' @rdname gather_fails
#' @rdname validate_inputs
#' @export
gather_fails <- function(iv, header = "Some inputs require attention", ...) {
checkmate::assert_class(iv, "InputValidator")
checkmate::assert_string(header, null.ok = TRUE)

fail_messages <- gather_messages(iv)
failings <- add_header(fail_messages, header)

shiny::validate(shiny::need(is.null(failings), failings), ...)
}


#' @rdname gather_fails
#' @export
gather_fails_com <- function(..., header = "Some inputs require attention") {
validate_inputs <- function(..., header = "Some inputs require attention") {
vals <- list(...)
lapply(vals, checkmate::assert_class, "InputValidator")
checkmate::assert_string(header, null.ok = TRUE)
Expand All @@ -130,9 +114,9 @@ gather_fails_com <- function(..., header = "Some inputs require attention") {
}


#' @rdname gather_fails
#' @rdname validate_inputs
#' @export
gather_fails_grp <- function(validators, ...) {
validate_inputs_segregated <- function(validators, ...) {
checkmate::assert_list(validators, types = "InputValidator")

# Since some or all names may be NULL, mapply cannot be used here, a loop is required.
Expand Down
39 changes: 16 additions & 23 deletions man/gather_fails.Rd → man/validate_inputs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 794777f

Please sign in to comment.