Skip to content

TIMCI report generation R script

Thaliehln edited this page May 19, 2021 · 1 revision

Example of R script for timci_run.R

image

library(timci)
library(readxl)

# Set the root directory for storing results
output_dir <- file.path(getwd(),"timci_exports")
# Import the mapping between the ODK Collect device identifiers and the health facilities where the research assistants are posted
research_facilities <- read_excel(file.path(getwd(),"timci_research_facilities.xlsx"))

# Create the structure of the folder and subfolders that are created everyday to store the reports and exports
subdir <- file.path(output_dir, paste0("export_", Sys.Date()))

pilot_dirname <- paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_pilot")

rctls_dirname <- paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_rct_ls")
spa_dirname <- paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_spa")
qual_dirname <- paste0("03_", Sys.getenv('TIMCI_COUNTRY'), "_qualitative")
cost_dirname <- paste0("04_", Sys.getenv('TIMCI_COUNTRY'), "_cost")
report_dirname <- paste0("05_", Sys.getenv('TIMCI_COUNTRY'), "_reports")
path_dirname <- paste0("06_", Sys.getenv('TIMCI_COUNTRY'), "_path")

dir.create(subdir, showWarnings = FALSE)
dir.create(file.path(subdir, pilot_dirname), showWarnings = FALSE)

# RCT / LS pilot folders
dir.create(file.path(subdir, pilot_dirname, rctls_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, pilot_dirname, rctls_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")), showWarnings = FALSE)
dir.create(file.path(subdir, pilot_dirname, rctls_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_followup")), showWarnings = FALSE)
# SPA / time-flow pilot folders
dir.create(file.path(subdir, pilot_dirname, spa_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, pilot_dirname, spa_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")), showWarnings = FALSE)
# Pilot qualitative folders
dir.create(file.path(subdir, pilot_dirname, qual_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, pilot_dirname, qual_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_caregiver_idis")), showWarnings = FALSE)
dir.create(file.path(subdir, pilot_dirname, qual_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_provider_idis")), showWarnings = FALSE)

dir.create(file.path(subdir, pilot_dirname, cost_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, pilot_dirname, report_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, pilot_dirname, path_dirname), showWarnings = FALSE)

rctls_pilot_pid <- Sys.getenv("TIMCI_PILOT_RCTLS_PID")
rctls_pilot_pp <- Sys.getenv("TIMCI_PILOT_RCTLS_PP")
spa_pilot_pid <- Sys.getenv("TIMCI_PILOT_SPA_PID")
qual_pilot_pid <- Sys.getenv("TIMCI_PILOT_QUAL_PID")
qual_pilot_pp <- Sys.getenv("TIMCI_PILOT_QUAL_PP")

# Run several Rmarkdown files to generate standardised automated reports for the pilot phase
timci::run_rmarkdown(rctls_pilot_pid,
                     rctls_pilot_pp,
                     spa_pilot_pid,
                     qual_pilot_pid,
                     qual_pilot_pp,
                     research_facilities,
                     file.path(subdir, pilot_dirname, report_dirname),
                     file.path(subdir, pilot_dirname, rctls_dirname, "participants.zip"),
                     file.path(subdir, pilot_dirname, rctls_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")),
                     file.path(subdir, pilot_dirname, rctls_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_followup")),
                     file.path(subdir, pilot_dirname, qual_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_caregiver_idis")),
                     file.path(subdir, pilot_dirname, qual_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_provider_idis")),
                     file.path(subdir, pilot_dirname, spa_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")),
                     file.path(subdir, pilot_dirname, path_dirname))

# Send emails
attachment_list <- c(file.path(subdir, pilot_dirname, report_dirname, paste0("timci_rct_monitoring_report", '_',Sys.Date(),'.docx')),
                     file.path(subdir, pilot_dirname, report_dirname, paste0("timci_data_export_report", '_',Sys.Date(),'.docx')),
                     file.path(subdir, pilot_dirname, path_dirname, paste0("timci_path_report", '_',Sys.Date(),'.docx')),
                     file.path(subdir, pilot_dirname, path_dirname, paste0("path_data.xlsx")))
timci::send_email("pilot",
                  c("[email protected]"),
                  attachment_list)

study_dirname <- paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_main_study")
dir.create(file.path(subdir, study_dirname), showWarnings = FALSE)

# RCT / LS pilot folders
dir.create(file.path(subdir, study_dirname, rctls_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, study_dirname, rctls_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")), showWarnings = FALSE)
dir.create(file.path(subdir, study_dirname, rctls_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_followup")), showWarnings = FALSE)
# SPA / time-flow pilot folders
dir.create(file.path(subdir, study_dirname, spa_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, study_dirname, spa_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")), showWarnings = FALSE)
# Pilot qualitative folders
dir.create(file.path(subdir, study_dirname, qual_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, study_dirname, qual_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_caregiver_idis")), showWarnings = FALSE)
dir.create(file.path(subdir, study_dirname, qual_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_provider_idis")), showWarnings = FALSE)

dir.create(file.path(subdir, study_dirname, cost_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, study_dirname, report_dirname), showWarnings = FALSE)
dir.create(file.path(subdir, study_dirname, path_dirname), showWarnings = FALSE)

rctls_pid <- Sys.getenv("TIMCI_RCTLS_PID")
rctls_pp <- Sys.getenv("TIMCI_RCTLS_PP")
spa_pid <- Sys.getenv("TIMCI_SPA_PID")
qual_pid <- Sys.getenv("TIMCI_QUAL_PID")
qual_pp <- Sys.getenv("TIMCI_QUAL_PP")

# Run several Rmarkdown files to generate standardised automated reports for the main study
timci::run_rmarkdown(rctls_pid,
                     rctls_pp,
                     spa_pid,
                     qual_pid,
                     qual_pp,
                     research_facilities,
                     file.path(subdir, study_dirname, report_dirname),
                     file.path(subdir, study_dirname, rctls_dirname, "participants.zip"),
                     file.path(subdir, study_dirname, rctls_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")),
                     file.path(subdir, study_dirname, rctls_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_followup")),
                     file.path(subdir, study_dirname, qual_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_caregiver_idis")),
                     file.path(subdir, study_dirname, qual_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_provider_idis")),
                     file.path(subdir, study_dirname, spa_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")),
                     file.path(subdir, study_dirname, path_dirname))

# Send emails
attachment_list <- c(file.path(subdir, study_dirname, report_dirname, paste0("timci_rct_monitoring_report", '_',Sys.Date(),'.docx')),
                     file.path(subdir, study_dirname, report_dirname, paste0("timci_data_export_report", '_',Sys.Date(),'.docx')),
                     file.path(subdir, study_dirname, path_dirname, paste0("timci_path_report", '_',Sys.Date(),'.docx')),
                     file.path(subdir, study_dirname, path_dirname, paste0("path_data.xlsx")))
timci::send_email("main study",
                  c("[email protected]"),
                  attachment_list)

if (Sys.getenv('TIMCI_COUNTRY') == "Tanzania" || Sys.getenv('TIMCI_COUNTRY') == "India"){

    baseline_dirname <- paste0("03_", Sys.getenv('TIMCI_COUNTRY'), "_baseline")

    dir.create(file.path(subdir, baseline_dirname), showWarnings = FALSE)

    # RCT / LS pilot folders
    dir.create(file.path(subdir, baseline_dirname, rctls_dirname), showWarnings = FALSE)
    dir.create(file.path(subdir, baseline_dirname, rctls_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")), showWarnings = FALSE)
    dir.create(file.path(subdir, baseline_dirname, rctls_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_followup")), showWarnings = FALSE)
    # SPA / time-flow pilot folders
    dir.create(file.path(subdir, baseline_dirname, spa_dirname), showWarnings = FALSE)
    dir.create(file.path(subdir, baseline_dirname, spa_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")), showWarnings = FALSE)
    # Pilot qualitative folders
    dir.create(file.path(subdir, baseline_dirname, qual_dirname), showWarnings = FALSE)
    dir.create(file.path(subdir, baseline_dirname, qual_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_caregiver_idis")), showWarnings = FALSE)
    dir.create(file.path(subdir, baseline_dirname, qual_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_provider_idis")), showWarnings = FALSE)

    dir.create(file.path(subdir, baseline_dirname, cost_dirname), showWarnings = FALSE)
    dir.create(file.path(subdir, baseline_dirname, report_dirname), showWarnings = FALSE)
    dir.create(file.path(subdir, baseline_dirname, path_dirname), showWarnings = FALSE)

    rctls_pid <- Sys.getenv("TIMCI_RCTLS_PID")
    rctls_pp <- Sys.getenv("TIMCI_RCTLS_PP")
    spa_pid <- Sys.getenv("TIMCI_SPA_PID")
    qual_pid <- Sys.getenv("TIMCI_QUAL_PID")
    qual_pp <- Sys.getenv("TIMCI_QUAL_PP")

    # Run several Rmarkdown files to generate standardised automated reports for the main study
    timci::run_rmarkdown(rctls_pid,
                        rctls_pp,
                        spa_pid,
                        qual_pid,
                        qual_pp,
                        research_facilities,
                        file.path(subdir, baseline_dirname, report_dirname),
                        file.path(subdir, baseline_dirname, rctls_dirname, "participants.zip"),
                        file.path(subdir, baseline_dirname, rctls_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")),
                        file.path(subdir, baseline_dirname, rctls_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_followup")),
                        file.path(subdir, baseline_dirname, qual_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_caregiver_idis")),
                        file.path(subdir, baseline_dirname, qual_dirname, paste0("02_", Sys.getenv('TIMCI_COUNTRY'), "_provider_idis")),
                        file.path(subdir, baseline_dirname, spa_dirname, paste0("01_", Sys.getenv('TIMCI_COUNTRY'), "_database")),
                        file.path(subdir, baseline_dirname, path_dirname))

}
Clone this wiki locally