Skip to content

Commit

Permalink
add examples to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
BatoolMM committed Nov 27, 2024
1 parent ae37766 commit 1826eda
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 3 deletions.
4 changes: 2 additions & 2 deletions R/browse_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ browse_metadata <- function(json_file = NULL, output_dir = NULL) {
# SAVE PLOTS ----

original_wd <- getwd()
setwd(output_dir) # saveWidget has a bug with paths & saving
on.exit(output_dir) # saveWidget has a bug with paths & saving
base_fname <- paste0(gsub(" ", "", dataset_name), "_V", dataset_version)

## Save the table plot to a HTML file
Expand All @@ -168,7 +168,7 @@ browse_metadata <- function(json_file = NULL, output_dir = NULL) {
bar_fname <- paste0("BROWSE_bar_", base_fname, ".csv")
write.csv(count_empty_long, bar_fname, row.names = FALSE)

setwd(original_wd) # saveWidget has a bug with paths & saving
on.exit(original_wd) # saveWidget has a bug with paths & saving

# OUTPUTS ----
cat("\n")
Expand Down
59 changes: 59 additions & 0 deletions R/map_metadata_compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,65 @@
#' @importFrom utils read.csv write.csv
#' @importFrom jsonlite fromJSON
#' @importFrom cli cli_alert_success
#' @examples
#' # Create a temporary directory for the example
#' temp_dir <- tempdir()
#'
#' # Create example LOG_ and OUTPUT_ CSV files for two sessions with primary care data
#' # Session 1 data
#' session1_data <- data.frame(
#' patient_id = c(101, 102),
#' condition_code = c("A01", "B02"),
#' medication = c("Medication1", "Medication2"),
#' visit_date = c("2023-01-15", "2023-02-20")
#' )
#' write.csv(session1_data, file = file.path(temp_dir, "LOG_NationalCommunityChildHealthDatabase(NCCHD)_BLOOD_TEST_2024-07-05-16-07-38.csv"), row.names = FALSE)
#' write.csv(session1_data, file = file.path(temp_dir, "OUTPUT_NationalCommunityChildHealthDatabase(NCCHD)_BLOOD_TEST_2024-07-05-16-07-38.csv"), row.names = FALSE)
#'
#' # Session 2 data (slightly different to simulate mismatch)
#' session2_data <- data.frame(
#' patient_id = c(101, 103),
#' condition_code = c("A01", "A01,C03"),
#' medication = c("Medication1", "Medication3"),
#' visit_date = c("2023-01-15", "2023-03-25")
#' )
#' write.csv(session2_data, file = file.path(temp_dir, "LOG_NationalCommunityChildHealthDatabase(NCCHD)_BLOOD_TEST_2024-07-08-12-03-30.csv"), row.names = FALSE)
#' write.csv(session2_data, file = file.path(temp_dir, "OUTPUT_NationalCommunityChildHealthDatabase(NCCHD)_BLOOD_TEST_2024-07-08-12-03-30.csv"), row.names = FALSE)
#'
#' # Create mock metadata JSON and domain file
#' json_file <- file.path(temp_dir, "metadata.json")
#' domain_file <- file.path(temp_dir, "domain.csv")
#'
#' # Example metadata json (mock structure)
#' meta_data <- list(
#' dataModel = list(
#' label = "NationalCommunityChildHealthDatabase",
#' childDataClasses = list(list(label = "Blood Test Data"))
#' ),
#' data = list()
#' )
#' jsonlite::write_json(meta_data, json_file)
#'
#' # Example domain file (mock domain codes)
#' domain_data <- data.frame(
#' code = c("A01", "B02", "C03", "D04"),
#' description = c("Blood Test A01", "Blood Test B02", "Blood Test C03", "Blood Test D04")
#' )
#' write.csv(domain_data, domain_file, row.names = FALSE)
#'
#' # Use the map_metadata_compare function to compare the sessions and get consensus
#' map_metadata_compare(
#' session_dir = temp_dir,
#' session1_base = "NationalCommunityChildHealthDatabase(NCCHD)_BLOOD_TEST_2024-07-05-16-07-38",
#' session2_base = "NationalCommunityChildHealthDatabase(NCCHD)_BLOOD_TEST_2024-07-08-12-03-30",
#' json_file = json_file,
#' domain_file = domain_file
#' )
#'
#' # Check the consensus output
#' consensus_output <- read.csv(file.path(temp_dir, "CONSENSUS_OUTPUT_NationalCommunityChildHealthDatabase_BloodTestData_2024-07-05-16-07-38.csv"))
#' print(consensus_output)


map_metadata_compare <- function(session_dir, session1_base, session2_base, json_file, domain_file) {
timestamp_now_fname <- format(Sys.time(), "%Y-%m-%d-%H-%M-%S")
Expand Down
18 changes: 17 additions & 1 deletion R/map_metadata_convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@
#' @return The function will return 'L-OUTPUT_' in the same output_dir
#' @export
#' @importFrom utils read.csv write.csv

#' @examples
#' # Create a temporary directory for the example
#' temp_dir <- tempdir()
#'
#' # Create an example OUTPUT_ CSV file with primary care data
#' example_data <- data.frame(
#' patient_id = c(101, 102, 103, 104),
#' condition_code = c("A01", "B02", "A01,C03", "D04"),
#' medication = c("Medication1", "Medication2", "Medication3", "Medication4")
#' )
#' write.csv(example_data, file = file.path(temp_dir, "OUTPUT_example.csv"), row.names = FALSE)
#'
#' # Use the function to process the example file
#' map_metadata_convert("OUTPUT_example.csv", temp_dir)
#'
#' # Check the output
#' read.csv(file.path(temp_dir, "L-OUTPUT_example.csv"))
map_metadata_convert <- function(output_csv, output_dir) {
output <- read.csv(paste0(output_dir, "/", output_csv))
output_long <- output[0, ] # make duplicate
Expand Down
59 changes: 59 additions & 0 deletions man/map_metadata_compare.Rd

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

18 changes: 18 additions & 0 deletions man/map_metadata_convert.Rd

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

0 comments on commit 1826eda

Please sign in to comment.