From 7d151c07445c2813a50755b715ffab298f3109fd Mon Sep 17 00:00:00 2001 From: Izaak Jephson Date: Thu, 5 Sep 2024 15:44:22 +0100 Subject: [PATCH] Add functions to export wb object without saving and to create template script --- R/build_metadata.R | 28 +++++++++++-------- R/create_template.R | 67 +++++++++++++++++++++++++++++++++++++++++++++ R/export_tables.R | 56 ++++++++++++++++++++++++++++--------- R/list_tables.R | 1 - 4 files changed, 127 insertions(+), 25 deletions(-) create mode 100644 R/create_template.R delete mode 100644 R/list_tables.R diff --git a/R/build_metadata.R b/R/build_metadata.R index c71f10a..e8b21cd 100644 --- a/R/build_metadata.R +++ b/R/build_metadata.R @@ -48,21 +48,27 @@ add_sheet_to_metadata <- function(metadata, #' `name` column must match the table names specified in the metadata object. #' `table` column contains the tables to be outputted to excel #' `title` column is only used where more than one table is included on a sheet -#' and is the sub title to be printed above the table. +#' and is the subtitle to be printed above the table. #' @export create_table_layout <- function(metadata, table_data) { metadata %>% dplyr::mutate( - tables = purrr::map(.data$table_names, - ~ generate_table_metadata(.x, table_data))) %>% + tables = purrr::map( + .data$table_names, + ~ generate_table_metadata(.x, + table_data) + ) + ) %>% dplyr::mutate( - n_tables = purrr::map(.data$tables, nrow) %>% as.numeric(), - notes_start = purrr::map( - .data$tables, - ~ .x %>% - dplyr::select(.data$n_rows) %>% - purrr::map(~ sum(.x)) %>% - as.numeric() + n_tables = purrr::map(.data$tables, nrow) %>% + as.numeric(), + notes_start = + purrr::map( + .data$tables, + ~ .x %>% + dplyr::select(.data$n_rows) %>% + purrr::map(~ sum(.x)) %>% + as.numeric() ) ) %>% dplyr::select(.data$sheet_name, @@ -122,7 +128,7 @@ generate_table_metadata <- function(table_names, #' @description Helper function for quickly turning named list of tables into a #' table_data object for use in the `r xlsss::create_table_layout` function. #' @param table_list List of tables to be converted to table_data -#' +#' @export table_list_to_tibble <- function(table_list){ dplyr::tibble( name = names(table_list), diff --git a/R/create_template.R b/R/create_template.R new file mode 100644 index 0000000..e311701 --- /dev/null +++ b/R/create_template.R @@ -0,0 +1,67 @@ +#' Create Template Script to Output Tables +#' +#' @param filename File to output template script to +#' @export +create_template_output <- function(filename = "template"){ + cat( + "### Functions to amend and add ### +# Create metadata object defining each sheet and specifying which tables to include +build_metadata <- function(){ + create_metadata() %>% + # eg add first sheet with one table + add_sheet_to_metadata( + sheet_name = \"Applications by month\", + sheet_title = \"Application outcomes by month\", + table_names = \"table_1\", + table_notes = c(1,2,3)) %>% + # eg add second sheet with one table + add_sheet_to_metadata( + sheet_name = \"Applications by age and LA\", + sheet_title = \"Application outcomes by age and local authority\", + table_names = c(\"table_2\",\"table_3\"), + table_notes = c(1,2,3)) + # ...etc... +} + +# Create list of notes +build_notes <- function(){ + tribble( + ~note_number, ~note_text, + \"[note 1]\", \"note text 1\", + \"[note 2]\", \"note text 2\", + \"[note 3]\", \"note text 3\" + ) +} + +# Create tibble containing tables to output +# name column should correspond to table names in metadata, +# table column contains the table objects themselves +# title column contains title to print above table if more than one table on sheet +# otherwise this column is not used +build_tables <- function(){ + tribble( + ~name, ~table, ~title + \"table_1\", table_1, \"na\", + \"table_2\", table_2, \"Application outcomes by age\", + \"table_3\", table_3, \"Local Authority\" + ) +} + +### Add to end of pipeline ### + +metadata <- build_metadata() + +notes_list <- build_notes() + +table_data <- build_tables() + +# Output tables to file +save_output_tables( + metadata = metadata, + table_data = table_data, + notes_list = notes_list, + contents_title = \"Statistical tables as at DATE\", + workbook_filename = FILENAME) +" +, file = paste0(filename,".R"), append = FALSE) +} diff --git a/R/export_tables.R b/R/export_tables.R index 1da485e..bea6b9d 100644 --- a/R/export_tables.R +++ b/R/export_tables.R @@ -352,12 +352,6 @@ add_data_sheet <- function(wb, startCol = 1 ) - # writeData(wb, sheet_name, - # x = paste0("Data bars are used in ",if_else(n_tables == 1, "this table", "these tables"),". To remove these, select the table, go to the Home tab, click on Conditional Formatting and select Clear Rules from This Table."), - # startRow = 4, - # startCol = 1 - # ) - openxlsx::writeData(wb, sheet_name, x = paste0( "Notes are located below the", @@ -374,7 +368,7 @@ add_data_sheet <- function(wb, openxlsx::writeData(wb, sheet_name, - x = "Some rows between tables are left blank in this sheet to improve readability.", + x = "[c] indicates that a figure has been suppressed for disclosure control purposes.", startRow = 5, startCol = 1 ) @@ -544,15 +538,24 @@ tweak_formatting <- function(wb) { #' Create excel tables #' -#' @param table_layout Table layout object created by metadata functions +#' @param metadata metadata object created by metadata functions +#' @param table_data Tibble of tables. Must include columns: name, table and title. +#' `name` column must match the table names specified in the metadata object. +#' `table` column contains the tables to be outputted to excel +#' `title` column is only used where more than one table is included on a sheet +#' and is the subtitle to be printed above the table. #' @param notes_list List of notes in publication #' @param contents_title Title of contents page -#' @param workbook_filename Filename to export workbook to #' @export -make_output_tables <- function(table_layout, +make_output_tables <- function(metadata, + table_data, notes_list, - contents_title, - workbook_filename) { + contents_title) { + + if(!is_tibble(table_data)){ + table_data <- table_list_to_tibble(table_data)} + + table_layout <- create_table_layout(metadata, table_data) wb <- openxlsx::createWorkbook() @@ -568,7 +571,34 @@ make_output_tables <- function(table_layout, wb <- xlsss::tweak_formatting(wb) + wb +} + +#' Create and save excel tables +#' +#' @param metadata metadata object created by metadata functions +#' @param table_data Tibble of tables. Must include columns: name, table and title. +#' `name` column must match the table names specified in the metadata object. +#' `table` column contains the tables to be outputted to excel +#' `title` column is only used where more than one table is included on a sheet +#' and is the subtitle to be printed above the table. +#' @param notes_list List of notes in publication +#' @param contents_title Title of contents page +#' @param workbook_filename Filename to export workbook to +#' @export +save_output_tables <- function(metadata, + table_data, + notes_list, + contents_title, + workbook_filename) { + + + wb <- make_output_tables(metadata, + table_data, + notes_list, + contents_title) + openxlsx::saveWorkbook(wb, workbook_filename, overwrite = TRUE) - workbook_filename + paste0("Tables have been saved to ",workbook_filename) } diff --git a/R/list_tables.R b/R/list_tables.R deleted file mode 100644 index 8b13789..0000000 --- a/R/list_tables.R +++ /dev/null @@ -1 +0,0 @@ -