Skip to content

Commit

Permalink
[24-08-2024] boilerplate 0.0.1.1-alpha
Browse files Browse the repository at this point in the history
* `boilerplate_merge_databases()`: merges databases, currently implemented for measures_data.
* fixed helper functions on the `boilerplate_report_methods()` function.
  • Loading branch information
go-bayes committed Aug 25, 2024
1 parent 906ec0c commit fd5bcbf
Show file tree
Hide file tree
Showing 30 changed files with 305 additions and 29 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
89 changes: 89 additions & 0 deletions R/boilerplate_merge_database.R
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 4 additions & 4 deletions R/boilerplate_report_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ reference:
- title: "Create templates"
contents:
- boilerplate_manage_measures
- boilerplate_merge_databases


- title: "Generate Markdown"
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pandoc: '3.2'
pkgdown: 2.1.0
pkgdown_sha: ~
articles: {}
last_built: 2024-08-25T08:31Z
last_built: 2024-08-25T09:02Z
urls:
reference: https://go-bayes.github.io/boilerplate/reference
article: https://go-bayes.github.io/boilerplate/articles
2 changes: 1 addition & 1 deletion docs/reference/boilerplate_manage_measures.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions docs/reference/boilerplate_merge_databases.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fd5bcbf

Please sign in to comment.