Skip to content

Commit

Permalink
creation of the prep_mdb_table_extract helper function for the prep_m…
Browse files Browse the repository at this point in the history
…db_merge function
  • Loading branch information
DemevengDerrick committed Jun 6, 2024
1 parent bb70e8b commit 06ad5f4
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 9 deletions.
19 changes: 10 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ Imports:
future,
purrr,
archive,
doParallel
doParallel,
RODBC
Suggests:
testthat,
progressr,
janitor,
withr,
stringdist,
epiCleanr,
crayon,
glue
testthat,
progressr,
janitor,
withr,
stringdist,
epiCleanr,
crayon,
glue
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(get_updated_ona_data)
export(prep_geonames)
export(prep_match_datatypes)
export(prep_match_names)
export(prep_mdb_table_extract)
export(read)
export(save)
import(doFuture)
Expand Down
93 changes: 93 additions & 0 deletions R/prep_mdb_merge.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
################################################################################
# #
# FUNCTION FOR MERGING DIFFERENT MDB FILES #
# #
################################################################################

#' Function to extract tables from multiple Microsoft Access databases.
#'
#' @param mdb_folder Path to the folder containing the .mdb files
#' @param target_table The table to be extracted from each mdb file. This should
#' unique name accross all the .mdb files.
#'
#' @return Returns a list all all the extracted tables indexed by their names.
#' @export
#'
#' @examples
#'
#' # How to run the function
#' prep_mdb_table_extract(mdb_folder, target_table)
#'
#'

prep_mdb_table_extract <- function(mdb_folder, target_table){
# Get the start time of the function
start_time <- Sys.time()
tryCatch({
# a) check if the foldername exist
if(!dir.exists(mdb_folder)){
cli::cli_alert_danger(paste("Folder path", mdb_folder, "does not exist."))
}

# b) get the list of all the files with mdb extension from the folder
mdb_files <-
dir(path = mdb_folder,
pattern = "\\.mdb",
full.names = T)

if(length(mdb_files > 0)){
# c) loop through each .mdb file and look for the target table
afp_list <- list()

cli::cli_progress_bar("Extracting data", total = 100)
for(mdb_file in mdb_files) {

#print(path)
ch1 <-
RODBC::odbcDriverConnect(paste0(
"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=",
mdb_file
))
# Get list of all tables in the mdb
tables <- RODBC::sqlTables(ch1)
# Get the only tables with type TABLE
tables <- tables$TABLE_NAME[tables$TABLE_TYPE == "TABLE"]
# Check if the target table exist in list of tables in an .mdb
if(target_table %in% tables){
query <- RODBC::sqlQuery(ch1, query = paste("select * from ", target_table))
afp_list[[paste0(basename(mdb_file), "_", target_table)]] <- query
}else{
cli::cli_text(paste("No table named : ", target_table, " in ", basename(mdb_file)))
}
# close db connection
RODBC::odbcCloseAll()
cli::cli_progress_update(total = length(mdb_files))
}

# print the time taken to complete the process
elapsed_time <- Sys.time() - start_time
cli::cli_alert_success(
paste("Process completed sucessfully in ",
elapsed_time, "Sec"
))
# return the list of tables
return(afp_list)
} else{
# print the time taken to complete the process
cli::cli_alert_warning(
paste("No mdb file to extract table from"))
}

}, error = function(e){
# print out the error message
cli::cli_alert_danger(e$message)
}, warning = function(w){
# print out the warning message
cli::cli_alert_warning(w$message)
})
}


#prep_mdb_table_extract(mdb_folder, target_table)


27 changes: 27 additions & 0 deletions man/prep_mdb_table_extract.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-prep_mdb_merge.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("Expect No Error test", {
# no error
fake_folder <- "output/mdb_files"
target_tab <- "polio"
expect_no_error(object = prep_mdb_table_extract(fake_folder, target_tab))

})








0 comments on commit 06ad5f4

Please sign in to comment.