Skip to content

Commit

Permalink
Merge pull request #3 from izaak-jephson/main
Browse files Browse the repository at this point in the history
Add functions to export wb object without saving and to create template script
  • Loading branch information
izaak-jephson authored Sep 5, 2024
2 parents 3430044 + 7d151c0 commit d9b0605
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 25 deletions.
28 changes: 17 additions & 11 deletions R/build_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
67 changes: 67 additions & 0 deletions R/create_template.R
Original file line number Diff line number Diff line change
@@ -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)
}
56 changes: 43 additions & 13 deletions R/export_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
)
Expand Down Expand Up @@ -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()

Expand All @@ -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)
}
1 change: 0 additions & 1 deletion R/list_tables.R

This file was deleted.

0 comments on commit d9b0605

Please sign in to comment.