R/transform.R

Defines functions stone_transform

Documented in stone_transform

#' Transform touchstone-relevant extracted data into tables ready for
#' database import. This should be called from the transform code of montagu-import,
#' calling `stoner::stone_transform(extracted_data)` with the same argument provided
#' to the montagu-import transform function.
#'
#' For specific information on what steps the transform function does.
#' see the vignette.
#' @title Transform extracted_data into transformed_data
#' @export
#' @param extracted_data The extracted data that was generated by the previous extract call
#' @return A list of data frames, one for each table, with names matching the
#'   database table names, and containing rows to be added to that table.

stone_transform <- function(extracted_data) {
  t <- c(transform_touchstone(extracted_data),
         transform_scenario_type(extracted_data),
         transform_scenario_description(extracted_data),
         transform_touchstone_demographic_dataset(extracted_data),
         transform_touchstone_country(extracted_data)
  )

  t <- c(t, transform_responsibilities(extracted_data, t))
  t <- c(t, transform_fast_forward(extracted_data))
  t <- c(t, transform_prune(extracted_data))

  # Remove all rows that shouldn't be added/edited. (ie, database
  # already contains identical rows).

  for (table in names(t)) {
    if (("already_exists_db") %in% names(t[[table]])) {
      t[[table]] <- t[[table]][!t[[table]]$already_exists_db, ]
      t[[table]]$already_exists_db <- NULL
    }
  }

  t

}
vimc/stoner documentation built on May 16, 2024, 11:09 a.m.