R/wrangle_rel_risk_action.R

Defines functions wrangle_rel_risk_action

Documented in wrangle_rel_risk_action

#' @title Wrangle Risks Related to Actions
#'
#' @description Wrangles an input `rel_risk_action` data frame and prepares
#'   it for use by the the reporting functions in this package.
#'
#' @export
#' @param db_rel_risk_action   data frame; A standard `rel_risk_action`
#'                             data frame.
#'
#' @return A formatted data frame suitable for use by the report functions.
#'
#' @examples
#' # Get test data
#' db_rel_risk_action <- rarr::db_rel_risk_action
#'
#' # Format rel_risk_action
#' rel_risk_action <- wrangle_rel_risk_action(db_rel_risk_action)
#'
#' @importFrom dplyr filter relocate
#' @importFrom rlang .data
#' @importFrom magrittr %>%
#'
wrangle_rel_risk_action <- function(db_rel_risk_action) {
  # Check parameters
  if(!is.data.frame(db_rel_risk_action)) {
    stop("db_rel_risk_action must be a data frame")}

  # Remove test records
  rel_risk_action <- rarr::remove_test_records(db_rel_risk_action,
                                               "RISK_NO")
  rel_risk_action <- rarr::remove_test_records(rel_risk_action,
                                               "ACTION_NO")

  # Cleanup id fields for sorting
  rel_risk_action <- rarr::format_id(rel_risk_action, "RISK_NO")
  rel_risk_action <- rarr::format_id(rel_risk_action, "ACTION_NO")

  # Create hyperlink
  rel_risk_action <- rarr::id_link(rel_risk_action, "risk_no")
  rel_risk_action <- rarr::id_link(rel_risk_action, "action_no")

  rel_risk_action <- rel_risk_action %>%
    # Filter for "Active" records
    filter(.data$RISK_ACTIVE == "Yes" & .data$ACTION_ACTIVE == "Yes") %>%

    # Reorder fields
    relocate(.data$ACTION_ACTIVE, .after = .data$action_no_link)

  return(rel_risk_action)
}
MVR-GIS/rarr documentation built on March 4, 2023, 11:47 p.m.