From fd5bcbf9dbb6d59da2f9fe56cf85ec74d6808a24 Mon Sep 17 00:00:00 2001 From: Joseph Bulbulia Date: Sun, 25 Aug 2024 21:03:16 +1200 Subject: [PATCH] [24-08-2024] boilerplate 0.0.1.1-alpha * `boilerplate_merge_databases()`: merges databases, currently implemented for measures_data. * fixed helper functions on the `boilerplate_report_methods()` function. --- DESCRIPTION | 2 +- NEWS.md | 7 ++ R/boilerplate_merge_database.R | 89 ++++++++++++++ R/boilerplate_report_methods.R | 8 +- _pkgdown.yml | 1 + docs/404.html | 2 +- docs/LICENSE.html | 2 +- docs/authors.html | 6 +- docs/index.html | 2 +- docs/news/index.html | 11 +- docs/pkgdown.yml | 2 +- .../boilerplate_manage_measures.html | 2 +- .../boilerplate_merge_databases.html | 115 ++++++++++++++++++ ...oilerplate_report_additional_sections.html | 2 +- ...ilerplate_report_causal_interventions.html | 2 +- ...oilerplate_report_confounding_control.html | 2 +- ...ilerplate_report_eligibility_criteria.html | 2 +- ...ate_report_identification_assumptions.html | 2 +- .../boilerplate_report_measures.html | 2 +- .../reference/boilerplate_report_methods.html | 2 +- .../boilerplate_report_missing_data.html | 2 +- docs/reference/boilerplate_report_sample.html | 2 +- ...lerplate_report_statistical_estimator.html | 2 +- .../boilerplate_report_target_population.html | 2 +- .../boilerplate_report_variables.html | 2 +- docs/reference/index.html | 8 +- docs/search.json | 2 +- docs/sitemap.xml | 1 + inst/CITATION | 2 +- man/boilerplate_merge_databases.Rd | 48 ++++++++ 30 files changed, 305 insertions(+), 29 deletions(-) create mode 100644 R/boilerplate_merge_database.R create mode 100644 docs/reference/boilerplate_merge_databases.html create mode 100644 man/boilerplate_merge_databases.Rd diff --git a/DESCRIPTION b/DESCRIPTION index e37ed9a..46508c3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: boilerplate Title: Tools for Managing and Compiling Manuscript Templates -Version: 0.0.1.0 +Version: 0.0.1.1 Authors@R: person("Joseph", "Bulbulia", email = "joseph.bulbulia@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-5861-2056")) diff --git a/NEWS.md b/NEWS.md index d77d7f4..edc3b7a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,12 @@ ## boilerplate (development version) +## [24-08-2024] boilerplate 0.0.1.1-alpha + +### New + +* `boilerplate_merge_databases()`: merges databases, currently implemented for measures_data. +* fixed helper functions on the `boilerplate_report_methods()` function. + ## [24-08-2024] boilerplate 0.0.1.0-alpha * alpha release diff --git a/R/boilerplate_merge_database.R b/R/boilerplate_merge_database.R new file mode 100644 index 0000000..4e96314 --- /dev/null +++ b/R/boilerplate_merge_database.R @@ -0,0 +1,89 @@ +#' Merge Two Measure Databases +#' +#' This function merges two measure databases, allowing the user to resolve conflicts +#' when the same measure exists in both databases with different content. +#' +#' @param db1 A list representing the first measure database. +#' @param db2 A list representing the second measure database. +#' @param db1_name Character string. The name of the first database (default: "Database 1"). +#' @param db2_name Character string. The name of the second database (default: "Database 2"). +#' +#' @return A list representing the merged measure database. +#' +#' @details +#' The function iterates through all measures in both databases. When a measure exists +#' in both databases: +#' \itemize{ +#' \item If the entries are identical, it keeps one copy. +#' \item If the entries differ, it prompts the user to choose which entry to keep. +#' } +#' Measures that exist in only one database are automatically added to the merged database. +#' +#' @examples +#' \dontrun{ +#' # Merge two databases with default names +#' merged_db <- boilerplate_merge_databases(test_a, test_b) +#' +#' # Merge two databases with custom names +#' merged_db <- boilerplate_merge_database(test_a, test_b, "NZAVS 2009", "NZAVS 2020") +#' } +#' +#' @importFrom cli cli_h1 cli_h2 cli_text cli_code cli_progress_bar cli_progress_update +#' @importFrom cli cli_progress_done cli_alert_success cli_alert_info +#' +#' @export +boilerplate_merge_databases <- function(db1, db2, db1_name = "Database 1", db2_name = "Database 2") { + merged_db <- list() + + # Helper function to get user choice + get_user_choice <- function(name, db1_entry, db2_entry) { + cli::cli_h2("Conflict found for measure: {.val {name}}") + cli::cli_text("Entry from {.strong {db1_name}}:") + cli::cli_code(capture.output(print(db1_entry))) + cli::cli_text("Entry from {.strong {db2_name}}:") + cli::cli_code(capture.output(print(db2_entry))) + + prompt <- cli::cli_text("Which entry do you want to keep? ({.val 1} for {db1_name}, {.val 2} for {db2_name}): ") + choice <- readline(prompt) + while (!(choice %in% c("1", "2"))) { + choice <- readline(cli::cli_text("Invalid input. Please enter {.val 1} or {.val 2}: ")) + } + return(as.integer(choice)) + } + + # Start merging process + cli::cli_h1("Starting database merge") + cli::cli_progress_bar(total = length(unique(c(names(db1), names(db2)))), + format = "{cli::pb_spin} Merging databases... [{cli::pb_current}/{cli::pb_total}] [{cli::pb_percent}] [{cli::pb_bar}]") + + # Merge entries from both databases + all_names <- unique(c(names(db1), names(db2))) + for (name in all_names) { + cli::cli_progress_update() + + if (name %in% names(db1) && name %in% names(db2)) { + # Entry exists in both databases + if (identical(db1[[name]], db2[[name]])) { + merged_db[[name]] <- db1[[name]] + cli::cli_alert_success("Measure {.val {name}} is identical in both databases. Keeping it.") + } else { + choice <- get_user_choice(name, db1[[name]], db2[[name]]) + merged_db[[name]] <- if (choice == 1) db1[[name]] else db2[[name]] + cli::cli_alert_info("Kept entry from {.strong {if(choice == 1) db1_name else db2_name}} for measure {.val {name}}") + } + } else if (name %in% names(db1)) { + # Entry only in database 1 + merged_db[[name]] <- db1[[name]] + cli::cli_alert_info("Measure {.val {name}} only found in {.strong {db1_name}}. Adding it to merged database.") + } else { + # Entry only in database 2 + merged_db[[name]] <- db2[[name]] + cli::cli_alert_info("Measure {.val {name}} only found in {.strong {db2_name}}. Adding it to merged database.") + } + } + + cli::cli_progress_done() + cli::cli_alert_success("Merge completed. Total measures in merged database: {.val {length(merged_db)}}") + + return(merged_db) +} diff --git a/R/boilerplate_report_methods.R b/R/boilerplate_report_methods.R index 524aad8..5dd75ee 100644 --- a/R/boilerplate_report_methods.R +++ b/R/boilerplate_report_methods.R @@ -104,7 +104,7 @@ boilerplate_report_methods <- function(exposure_var, outcome_vars, n_total, base # Define sections based on sections_to_include parameter sections <- if(identical(sections_to_include, 'all')) all_sections else sections_to_include - cli::cli_alert_info("Starting boilerplate_methods function") + cli::cli_alert_info("Starting boilerplate_report_methods function") # initialise an empty list to store all sections methods_sections <- list() @@ -144,7 +144,7 @@ boilerplate_report_methods <- function(exposure_var, outcome_vars, n_total, base for (section in sections) { cli::cli_h1("Processing section: {section}") section_name <- section - func_name <- paste0("boilerplate_methods_", section) + func_name <- paste0("boilerplate_report_", section) # Prepare base arguments for all sections args <- list( @@ -180,7 +180,7 @@ boilerplate_report_methods <- function(exposure_var, outcome_vars, n_total, base # Execute the function for each section if (section_name == "variables") { - result <- do.call(boilerplate_methods_variables, args) + result <- do.call(boilerplate_report_variables, args) } else { result <- safe_execute(func_name, args) } @@ -198,7 +198,7 @@ boilerplate_report_methods <- function(exposure_var, outcome_vars, n_total, base cli::cli_alert_info("Combining all sections") markdown_output <- paste(unlist(methods_sections), collapse = "\n\n") - cli::cli_alert_success("Finished boilerplate_methods function \U0001F44D") + cli::cli_alert_success("Finished boilerplate_report_methods function \U0001F44D") return(markdown_output) } diff --git a/_pkgdown.yml b/_pkgdown.yml index 67ff552..e4413eb 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -23,6 +23,7 @@ reference: - title: "Create templates" contents: - boilerplate_manage_measures + - boilerplate_merge_databases - title: "Generate Markdown" diff --git a/docs/404.html b/docs/404.html index 962cb65..1a9b454 100644 --- a/docs/404.html +++ b/docs/404.html @@ -23,7 +23,7 @@ boilerplate - 0.0.1.0 + 0.0.1.1 + + + + + +
+
+
+ +
+

This function merges two measure databases, allowing the user to resolve conflicts +when the same measure exists in both databases with different content.

+
+ +
+

Usage

+
boilerplate_merge_databases(
+  db1,
+  db2,
+  db1_name = "Database 1",
+  db2_name = "Database 2"
+)
+
+ +
+

Arguments

+ + +
db1
+

A list representing the first measure database.

+ + +
db2
+

A list representing the second measure database.

+ + +
db1_name
+

Character string. The name of the first database (default: "Database 1").

+ + +
db2_name
+

Character string. The name of the second database (default: "Database 2").

+ +
+
+

Value

+

A list representing the merged measure database.

+
+
+

Details

+

The function iterates through all measures in both databases. When a measure exists +in both databases:

  • If the entries are identical, it keeps one copy.

  • +
  • If the entries differ, it prompts the user to choose which entry to keep.

  • +

Measures that exist in only one database are automatically added to the merged database.

+
+ +
+

Examples

+
if (FALSE) { # \dontrun{
+# Merge two databases with default names
+merged_db <- boilerplate_merge_databases(test_a, test_b)
+
+# Merge two databases with custom names
+merged_db <- boilerplate_merge_database(test_a, test_b, "NZAVS 2009", "NZAVS 2020")
+} # }
+
+
+
+
+ + +
+ + + +
+ + + + + + + diff --git a/docs/reference/boilerplate_report_additional_sections.html b/docs/reference/boilerplate_report_additional_sections.html index 244d15d..3ecf5e0 100644 --- a/docs/reference/boilerplate_report_additional_sections.html +++ b/docs/reference/boilerplate_report_additional_sections.html @@ -13,7 +13,7 @@ boilerplate - 0.0.1.0 + 0.0.1.1