Skip to content

Commit

Permalink
improves edc_db_to_excel()
Browse files Browse the repository at this point in the history
renamed from edc_save_db_to_excel
  • Loading branch information
DanChaltiel committed Jul 25, 2024
1 parent 52e3285 commit 855ea15
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export(assert_no_rows)
export(build_lookup)
export(check_subjid)
export(crf_status_plot)
export(edc_browse_excel)
export(edc_data_stop)
export(edc_data_warn)
export(edc_data_warnings)
export(edc_db_to_excel)
export(edc_example)
export(edc_example_ae)
export(edc_example_mixed)
Expand All @@ -31,7 +33,6 @@ export(edc_pal_crf)
export(edc_peek_options)
export(edc_population_plot)
export(edc_reset_options)
export(edc_save_db_to_excel)
export(edc_swimmerplot)
export(edc_warn_extraction_date)
export(edc_warn_patient_diffs)
Expand Down
43 changes: 31 additions & 12 deletions R/save_to_excel.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#'
#' Because RStudio is not very good at showing data, it can be more convenient to browse the
#' database using MS Excel. This function turns the whole TM export (or any named list of datasets)
#' into an Excel workbook, with one tab for each dataset.
#' into an Excel workbook, with one tab for each dataset.\cr
#' Use `edc_db_to_excel()` to create the file and `edc_browse_excel()` to open it.
#'
#' @param filename the path to the Excel output file. Default to a temporary file. Use the special value `TRUE` to save in "data/database_{date_extraction}.xlsx".
#' @param datasets a named list of dataframes. Default to the TM export.
#' @param filename the path to the Excel output file. Default to a dated file in the "data" folder.
#' @param overwrite whether to overwrite any existing file. Default to `TRUE`.
#' @param open whether to open the Excel file afterward. Default to `TRUE`.
#' @param overwrite whether to overwrite any existing file. Default to `FALSE`.
#' @param open whether to open the Excel file afterward. Default to `FALSE`.
#' @param ... unused
#'
#' @return nothing
Expand All @@ -18,7 +19,8 @@
#' \dontrun{
#' tm = edc_example()
#' load_list(tm)
#' edc_save_db_to_excel() #default arguments are usually OK
#' edc_db_to_excel() #default arguments are usually OK
#' edc_db_to_excel(filename=TRUE)
#' }
#' @importFrom cli cli_abort cli_inform
#' @importFrom dplyr arrange
Expand All @@ -28,15 +30,24 @@
#' @importFrom rlang check_dots_empty check_installed is_named sym
#' @importFrom stringr str_ends
#' @importFrom utils browseURL
edc_save_db_to_excel = function(...,
datasets=get_datasets(),
filename=NULL,
overwrite=TRUE,
open=TRUE){
edc_db_to_excel = function(filename=tempfile(fileext=".xlsx"),
...,
datasets=get_datasets(),
overwrite=FALSE,
open=FALSE){
check_installed("openxlsx", "for `edc_save_to_excel()` to work.")
check_dots_empty()
assert(is_named(datasets))
if(is.null(filename)) filename = glue("data/database_{str_replace_all(date_extraction,'/','-')}.xlsx")
if(file_exists(filename) && !overwrite){
cli_inform("Excel file {.path {filename}} already exists.")
edcimport_env$excel_db_path = filename
if(open) browseURL(filename)
return(invisible(filename))
}

if(isTRUE(filename) && exists("date_extraction")){
filename = glue("data/database_{str_replace_all(date_extraction,'/','-')}.xlsx")
}
assert(str_ends(filename, ".xlsx"))
filename = clean_filename(filename)
dir_create(dirname(filename))
Expand All @@ -53,13 +64,15 @@ edc_save_db_to_excel = function(...,
openxlsx::setColWidths(wb, sheet=.y, cols=2:ncol(.x), widths="auto")
openxlsx::setColWidths(wb, sheet=.y, cols=1, widths=2.14)

})
}, .progress=TRUE)
rslt = openxlsx::saveWorkbook(wb, filename, overwrite=overwrite, returnValue=TRUE)
if(!rslt){
cli_abort("Could not create the Excel workbook.")
return(FALSE)
}

edcimport_env$excel_db_path = filename

size = file_size(filename)
cli_inform("Excel file {.path {filename}} has been created ({size}).")
if(open){
Expand All @@ -69,6 +82,12 @@ edc_save_db_to_excel = function(...,
invisible(filename)
}

#' @rdname edc_db_to_excel
#' @export
edc_browse_excel = function(){
browseURL(edcimport_env$excel_db_path)
}


#' @importFrom fs path path_sanitize
clean_filename = function(filename){
Expand Down
29 changes: 17 additions & 12 deletions man/edc_save_db_to_excel.Rd → man/edc_db_to_excel.Rd

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

0 comments on commit 855ea15

Please sign in to comment.