R/reorder_fields.R

Defines functions reorder_fields

Documented in reorder_fields

#' @title
#'   Reorder the processed timber's fields
#'
#' @description
#'   \code{reorder_fields()} organizes the left-to-right placement of the
#'   fields (columns) to match the order in which they are arranged in the
#'   iAM.AMR Analytica model nodes. It also deletes the fields
#'   \code{exclude_sawmill} and \code{exclude_sawmill_reason} from the final
#'   output, as these are only needed internally, to filter out unusable
#'   factors into the \code{scrap_pile}.
#'
#' @param timber
#'    a tibble of timber, returned from \code{\link{add_ident}}.
#'
#' @param cedar_version
#'    numeric [1,2]: the version of CEDAR that created the export.
#'
#' @return
#'    The passed tibble of processed timber with reorganized columns, ready for
#'    Analytica.
#'
#' @importFrom dplyr select
#'
#' @export


reorder_fields <- function(timber, cedar_version) {
  # Only reorder v2 timber
  if (cedar_version == 1) {
    return(timber)
  }

  timber_del <- timber

  ## CHANGES:
  ## - Removed "pval", "insensible_prev_table" due to throwing NA errors
  ## - Refactored "ID_meta" to "meta_ID" in all R script occurrences in package

  ## TO DO:
  ## - Include "risk_ratio", "risk_ratio_lo", "risk_ratio_up",
  ## "risk_ratio_sig", "risk_ratio_confidence"

  # Only include the logOR field if a meta-analysis was done
  if (exists("ma_results")) {
    field_names <- c(
      "ID", "RWID", "identifier", "factor_title",
      "factor_description", "ref_key", "html_link",
      "group_exposed", "group_referent",
      "odds_ratio", "se_log_or", "logOR",
      "meta_ID", "meta_amr", "meta_type",
      "AMR", "host_01", "host_02", "microbe_01",
      "microbe_02", "stage_allocate", "stage_observe",
      "res_unit", "res_format", "grain", "A", "B", "C", "D",
      "P", "R", "Q", "S", "nexp", "nref", "odds",
      "oddslo", "oddsup", "oddsig", "oddsci",
      "low_cell_count", "null_comparison",
      "doi", "pmid", "url"
    )
  } else {
    field_names <- c(
      "ID", "RWID", "identifier", "factor_title",
      "factor_description", "ref_key", "html_link",
      "group_exposed", "group_referent",
      "odds_ratio", "se_log_or",
      "meta_ID", "meta_amr", "meta_type",
      "AMR", "host_01", "host_02", "microbe_01",
      "microbe_02", "stage_allocate", "stage_observe",
      "res_unit", "res_format", "grain", "A", "B", "C", "D",
      "P", "R", "Q", "S", "nexp", "nref", "odds",
      "oddslo", "oddsup", "oddsig", "oddsci",
      "low_cell_count", "null_comparison",
      "doi", "pmid", "url"
    )
  }

  # Reorder the fields
  timber_reordered <- timber_del[field_names]

  # Update 'sawmill_status'
  status <- "OK: Processed timber is ready for Analytica."
  timber_reordered[, "sawmill_status"] <- status

  return(timber_reordered)
}
iAM-AMR/sawmill documentation built on June 30, 2024, 2:25 a.m.