Skip to content

Commit 5cbef8f

Browse files
author
Phuong Quan
committed
more docs
1 parent 0b9923d commit 5cbef8f

11 files changed

Lines changed: 360 additions & 259 deletions

R/alert.R

Lines changed: 81 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# =============================================================================
2+
# EXPORTED FUNCTIONS
3+
14
# -----------------------------------------------------------------------------
25
#' Generate a data frame containing alert results
36
#'
@@ -125,14 +128,6 @@ alertspec <- function(alert_rules,
125128
class = "mantis_alertspec")
126129
}
127130

128-
# -----------------------------------------------------------------------------
129-
#' Test if object is an alertspec object
130-
#'
131-
#' @param x object to test
132-
#' @return Logical
133-
#' @noRd
134-
is_alertspec <- function(x) inherits(x, "mantis_alertspec")
135-
136131

137132
# -----------------------------------------------------------------------------
138133
#' Create set of alert rules
@@ -201,63 +196,6 @@ alert_rules <- function(...) {
201196
structure(ars, class = "mantis_alert_rules")
202197
}
203198

204-
# -----------------------------------------------------------------------------
205-
#' Test if object is an alert_rules object
206-
#'
207-
#' @param x object to test
208-
#' @return Logical
209-
#' @noRd
210-
is_alert_rules <- function(x) inherits(x, "mantis_alert_rules")
211-
212-
213-
# -----------------------------------------------------------------------------
214-
#' Constructor for an alert_rule
215-
#'
216-
#' @param type type of rule
217-
#' @param function_call expression to pass to `eval()`, that returns either `TRUE` or `FALSE`.
218-
#' Return value of `TRUE` means alert result is "FAIL"
219-
#' @param short_name a short computer-friendly name to uniquely identify the rule
220-
#' @param description brief but user-friendly explanation of why the rule result is "FAIL"
221-
#' @param items Named list with names corresponding to members of `item_cols`. List members are
222-
#' character vectors of values contained in the named column that the rule should be applied to.
223-
#' If `items = NULL` the rule will be applied to all items. See Details.
224-
#'
225-
#' @section Details: Use `items` to restrict the rule to be applied only to specified items.
226-
#' `items` can either be NULL or a named list of character vectors. If `NULL`, the rule will be
227-
#' applied to all items. If a named list, the names must match members of the `item_cols`
228-
#' parameter in the `inputspec`, (as well as column names in the `df`), though can be a subset.
229-
#' If an `item_col` is not named in the list, the rule will apply to all its members. If an
230-
#' `item_col` is named in the list, the rule will only be applied when the `item_col`'s value is
231-
#' contained in the corresponding character vector. When multiple `item_col`s are
232-
#' specified, the rule will be applied only to items that satisfy all the conditions.
233-
#'
234-
#' @noRd
235-
alert_rule <- function(type,
236-
function_call,
237-
short_name,
238-
description,
239-
items = NULL) {
240-
241-
structure(
242-
list(
243-
type = type,
244-
function_call = function_call,
245-
short_name = short_name,
246-
description = description,
247-
items = items
248-
),
249-
class = c(paste0("mantis_alert_rule_", type), "mantis_alert_rule")
250-
)
251-
}
252-
253-
# -----------------------------------------------------------------------------
254-
#' Test if object is an alert_rule object
255-
#'
256-
#' @param x object to test
257-
#' @return Logical
258-
#' @noRd
259-
is_alert_rule <- function(x) inherits(x, "mantis_alert_rule")
260-
261199

262200
# -----------------------------------------------------------------------------
263201
#' Built-in alert rules
@@ -285,6 +223,7 @@ is_alert_rule <- function(x) inherits(x, "mantis_alert_rule")
285223
#'
286224
NULL
287225

226+
288227
# -----------------------------------------------------------------------------
289228
#' Test for missing values
290229
#'
@@ -351,6 +290,7 @@ alert_missing <- function(extent_type = "all",
351290
)
352291
}
353292

293+
354294
# -----------------------------------------------------------------------------
355295
#' Test for specific values
356296
#'
@@ -412,6 +352,7 @@ alert_equals <- function(extent_type = "all",
412352
)
413353
}
414354

355+
415356
# -----------------------------------------------------------------------------
416357
#' Test for values greater than a specific value
417358
#'
@@ -473,6 +414,7 @@ alert_above <- function(extent_type = "all",
473414
)
474415
}
475416

417+
476418
# -----------------------------------------------------------------------------
477419
#' Test for values less than a specific value
478420
#'
@@ -533,6 +475,7 @@ alert_below <- function(extent_type = "all",
533475
)
534476
}
535477

478+
536479
# -----------------------------------------------------------------------------
537480
#' Test for when there is a percentage increase in latest values
538481
#'
@@ -596,6 +539,7 @@ alert_difference_above_perc <- function(current_period,
596539
)
597540
}
598541

542+
599543
# -----------------------------------------------------------------------------
600544
#' Test for when there is a percentage drop in latest values
601545
#'
@@ -659,6 +603,7 @@ alert_difference_below_perc <- function(current_period,
659603
)
660604
}
661605

606+
662607
# -----------------------------------------------------------------------------
663608
#' Create a custom alert rule
664609
#'
@@ -722,6 +667,76 @@ alert_custom <- function(short_name,
722667
}
723668

724669

670+
# =============================================================================
671+
# INTERNAL FUNCTIONS
672+
673+
# -----------------------------------------------------------------------------
674+
#' Test if object is an alertspec object
675+
#'
676+
#' @param x object to test
677+
#' @return Logical
678+
#' @noRd
679+
is_alertspec <- function(x) inherits(x, "mantis_alertspec")
680+
681+
682+
# -----------------------------------------------------------------------------
683+
#' Test if object is an alert_rules object
684+
#'
685+
#' @param x object to test
686+
#' @return Logical
687+
#' @noRd
688+
is_alert_rules <- function(x) inherits(x, "mantis_alert_rules")
689+
690+
691+
# -----------------------------------------------------------------------------
692+
#' Constructor for an alert_rule
693+
#'
694+
#' @param type type of rule
695+
#' @param function_call expression to pass to `eval()`, that returns either `TRUE` or `FALSE`.
696+
#' Return value of `TRUE` means alert result is "FAIL"
697+
#' @param short_name a short computer-friendly name to uniquely identify the rule
698+
#' @param description brief but user-friendly explanation of why the rule result is "FAIL"
699+
#' @param items Named list with names corresponding to members of `item_cols`. List members are
700+
#' character vectors of values contained in the named column that the rule should be applied to.
701+
#' If `items = NULL` the rule will be applied to all items. See Details.
702+
#'
703+
#' @section Details: Use `items` to restrict the rule to be applied only to specified items.
704+
#' `items` can either be NULL or a named list of character vectors. If `NULL`, the rule will be
705+
#' applied to all items. If a named list, the names must match members of the `item_cols`
706+
#' parameter in the `inputspec`, (as well as column names in the `df`), though can be a subset.
707+
#' If an `item_col` is not named in the list, the rule will apply to all its members. If an
708+
#' `item_col` is named in the list, the rule will only be applied when the `item_col`'s value is
709+
#' contained in the corresponding character vector. When multiple `item_col`s are
710+
#' specified, the rule will be applied only to items that satisfy all the conditions.
711+
#'
712+
#' @noRd
713+
alert_rule <- function(type,
714+
function_call,
715+
short_name,
716+
description,
717+
items = NULL) {
718+
719+
structure(
720+
list(
721+
type = type,
722+
function_call = function_call,
723+
short_name = short_name,
724+
description = description,
725+
items = items
726+
),
727+
class = c(paste0("mantis_alert_rule_", type), "mantis_alert_rule")
728+
)
729+
}
730+
731+
# -----------------------------------------------------------------------------
732+
#' Test if object is an alert_rule object
733+
#'
734+
#' @param x object to test
735+
#' @return Logical
736+
#' @noRd
737+
is_alert_rule <- function(x) inherits(x, "mantis_alert_rule")
738+
739+
725740
# -----------------------------------------------------------------------------
726741
#' Run all alert rules and return results
727742
#'
@@ -761,6 +776,7 @@ run_alerts <- function(prepared_df,
761776
dplyr::filter(alert_result %in% filter_results)
762777
}
763778

779+
764780
# -----------------------------------------------------------------------------
765781
#' Run single alert rule and return results
766782
#'

R/construct.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ bespoke_rmd_output <- function(df,
141141
tab_name = tab_name,
142142
tab_level = tab_level
143143
)
144+
145+
invisible(df)
144146
}
145147

146148

@@ -245,6 +247,8 @@ bespoke_rmd_alert_results <- function(df,
245247
alert_results = alert_results,
246248
inputspec = inputspec
247249
)
250+
251+
invisible(df)
248252
}
249253

250254

@@ -427,6 +431,7 @@ construct_rmd_tab <- function(df,
427431
#' @param inputspec Specification of data in `df`
428432
#' @param outputspec Specification for display of tab contents
429433
#' @param alert_results_subset alert_results filtered for the tab
434+
#' @return (invisibly) the supplied `df`
430435
#'
431436
#' @noRd
432437
construct_tab_content <- function(prepared_df_subset,
@@ -466,16 +471,17 @@ construct_tab_content <- function(prepared_df_subset,
466471

467472
cat("\n")
468473

474+
invisible(prepared_df_subset)
469475
}
470476

477+
471478
# -----------------------------------------------------------------------------
472479
#' Generate an interactive table containing alert results using side-effects
473480
#'
474-
#' @param prepared_df_subset prepared_df filtered for the tab
475-
#'
476481
#' @param alert_results data frame returned from `run_alerts`
477482
#' @param inputspec Specification of data in original `df`
478483
#' @param filter_results only return rows where the alert result is in this vector of values. Alert results can be "PASS", "FAIL", or "NA".
484+
#' @return (invisibly) the supplied `df`
479485
#' @noRd
480486
construct_alert_results_content <- function(alert_results,
481487
inputspec,
@@ -509,7 +515,6 @@ construct_alert_results_content <- function(alert_results,
509515
#' @param has_child_tabs will there be more tabs underneath
510516
#' @param alert set to `TRUE` to append an alert icon
511517
#'
512-
#' @return markdown-formatted string
513518
#' @noRd
514519
construct_tab_label <- function(tab_name, tab_level, has_child_tabs = FALSE, alert = FALSE){
515520
if (!is.null(tab_name)) {
@@ -523,6 +528,7 @@ construct_tab_label <- function(tab_name, tab_level, has_child_tabs = FALSE, ale
523528
}
524529
}
525530

531+
526532
# -----------------------------------------------------------------------------
527533
#' Calculate appropriate fig.height for the chunks
528534
#'
@@ -535,7 +541,7 @@ construct_tab_label <- function(tab_name, tab_level, has_child_tabs = FALSE, ale
535541
#' @param inputspec `inputspec()` object
536542
#' @param outputspec `outputspec()` object
537543
#'
538-
#' @return height in inches or NULL
544+
#' @return numeric height in inches or NULL
539545
#' @noRd
540546
rmd_fig_height <- function(df, inputspec, outputspec){
541547

R/data.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#' * tab - Labels to group related time series into tabs
1111
"example_data"
1212

13+
14+
# -----------------------------------------------------------------------------
1315
#' Example data frame containing numbers of antibiotic prescriptions in long format
1416
#'
1517
#' Simulated data to demonstrate package usage

R/mantis-package.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
## usethis namespace: start
5+
## usethis namespace: end
6+
NULL

R/plots-static.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ plot_heatmap_static <- function(prepared_df,
9292
g
9393
}
9494

95+
9596
# -----------------------------------------------------------------------------
9697
#' Create a column of plots showing value across all items
9798
#'
@@ -126,7 +127,6 @@ plot_multipanel_static <- function(prepared_df,
126127
}
127128
}
128129
data <- prepared_df |>
129-
# dplyr::filter(item.Location == "SITE1") |>
130130
# combine item_cols into single variable
131131
tidyr::unite(col = "item", dplyr::all_of(item_cols_prefix(item_cols_plot)), sep = " - ") |>
132132
dplyr::mutate(item = factor(item, levels = unique(item)))
@@ -188,6 +188,7 @@ plot_multipanel_static <- function(prepared_df,
188188
}
189189

190190

191+
# -----------------------------------------------------------------------------
191192
#' Create an empty gglot object
192193
#'
193194
#' Use when there is no data but you still want to display a ggplot (for consistency)

0 commit comments

Comments
 (0)