From d8c775fbd588372103770bb6416064e4b6b5be2b Mon Sep 17 00:00:00 2001 From: Izaak Jephson Date: Thu, 5 Sep 2024 17:33:04 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Add=20=C2=A3=20formatting=20to=20columns=20?= =?UTF-8?q?and=20%=20formatting=20by=20row?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/export_tables.R | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/R/export_tables.R b/R/export_tables.R index 21b9fff..1fcb0fe 100644 --- a/R/export_tables.R +++ b/R/export_tables.R @@ -46,7 +46,7 @@ add_contents_sheet <- function(wb, wb, "Contents", cols = 1:2, - width = c(20, "auto"), + width = c(27, "auto"), ignoreMergedCells = TRUE ) @@ -196,7 +196,7 @@ format_columns <- function(wb, - # Format % columns + # Format % columns } else if (stringr::str_detect(colnames(table[column]), "[Pp]ercent|[Pp]ercentage|[Pp]roportion")) { openxlsx::addStyle(wb, sheet_name, rows = start_row:end_row, @@ -206,6 +206,16 @@ format_columns <- function(wb, ) } + # Format £ columns + if (str_detect(colnames(table[column]), "[Vv]alue")) { + addStyle(wb, sheet_name, + rows = start_row:end_row, + cols = column, + style = createStyle(numFmt = "£#,##0;-;£0", halign = "right"), + gridExpand = TRUE + ) + + } } #' Format Rows @@ -236,6 +246,16 @@ format_rows <- function(wb, stack = TRUE ) } + # Format % rows + else if (str_starts(table[[table_row, 1]],"Percentage")) { + addStyle(wb, sheet_name, + rows = sheet_row, + cols = 2:end_col, + style = createStyle(numFmt = "0%;-;0%", halign = "right"), + gridExpand = TRUE + ) + + } } #' Add data tables to existing worksheet From 6e4ce8b1b623c00d57d5e2d6871f1822ce7a704e Mon Sep 17 00:00:00 2001 From: Izaak Jephson Date: Thu, 5 Sep 2024 17:42:33 +0100 Subject: [PATCH 2/4] Update template to include formatting tweaks --- R/create_template.R | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/R/create_template.R b/R/create_template.R index e311701..9740073 100644 --- a/R/create_template.R +++ b/R/create_template.R @@ -25,7 +25,7 @@ build_metadata <- function(){ # Create list of notes build_notes <- function(){ - tribble( + tibble::tribble( ~note_number, ~note_text, \"[note 1]\", \"note text 1\", \"[note 2]\", \"note text 2\", @@ -34,19 +34,32 @@ build_notes <- function(){ } # 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( + tibble::tribble( ~name, ~table, ~title \"table_1\", table_1, \"na\", \"table_2\", table_2, \"Application outcomes by age\", - \"table_3\", table_3, \"Local Authority\" + \"table_3\", table_3, \"Application outcomes by local authority\" ) } +# Apply bespoke formatting to final workbook if required +tweak_formatting <- function(wb, tweak) { +# Use openxlsx functions to apply whatever formatting adjustments are needed +# Below is simply an example + setColWidths(wb, + \"Applications by month\", + cols = 1, + width = 22, + ignoreMergedCells = TRUE) +} + + ### Add to end of pipeline ### metadata <- build_metadata() @@ -55,13 +68,21 @@ notes_list <- build_notes() table_data <- build_tables() -# Output tables to file + +# Create workbook object save_output_tables( metadata = metadata, table_data = table_data, notes_list = notes_list, - contents_title = \"Statistical tables as at DATE\", - workbook_filename = FILENAME) + contents_title = \"Statistical tables as at DATE\") + +# Apply final formatting adjustments +wb <- tweak_formatting(wb) + +# Output final workbook +openxlsx::saveWorkbook(wb, FILENAME, overwrite = TRUE) + + " , file = paste0(filename,".R"), append = FALSE) } From d0701e0d6bd46c768a83f8d4565df46a24f6b995 Mon Sep 17 00:00:00 2001 From: Izaak Jephson Date: Mon, 9 Sep 2024 14:17:20 +0100 Subject: [PATCH 3/4] Fix error in export_tables namespace --- R/export_tables.R | 26 +++++++++++++------------- README.Rmd | 3 ++- README.md | 3 ++- man/xlsss-package.Rd | 6 +++--- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/R/export_tables.R b/R/export_tables.R index 1fcb0fe..dab39f7 100644 --- a/R/export_tables.R +++ b/R/export_tables.R @@ -206,13 +206,13 @@ format_columns <- function(wb, ) } - # Format £ columns - if (str_detect(colnames(table[column]), "[Vv]alue")) { - addStyle(wb, sheet_name, - rows = start_row:end_row, - cols = column, - style = createStyle(numFmt = "£#,##0;-;£0", halign = "right"), - gridExpand = TRUE + # Format GBP columns + if (stringr::str_detect(colnames(table[column]), "[Vv]alue")) { + openxlsx::addStyle(wb, sheet_name, + rows = start_row:end_row, + cols = column, + style = openxlsx::createStyle(numFmt = "£#,##0;-;£0", halign = "right"), + gridExpand = TRUE ) } @@ -247,12 +247,12 @@ format_rows <- function(wb, ) } # Format % rows - else if (str_starts(table[[table_row, 1]],"Percentage")) { - addStyle(wb, sheet_name, - rows = sheet_row, - cols = 2:end_col, - style = createStyle(numFmt = "0%;-;0%", halign = "right"), - gridExpand = TRUE + else if (stringr::str_starts(table[[table_row, 1]],"Percentage")) { + openxlsx::addStyle(wb, sheet_name, + rows = sheet_row, + cols = 2:end_col, + style = openxlsx::createStyle(numFmt = "0%;-;0%", halign = "right"), + gridExpand = TRUE ) } diff --git a/README.Rmd b/README.Rmd index 4fb4925..6b7ea7a 100644 --- a/README.Rmd +++ b/README.Rmd @@ -27,7 +27,8 @@ You can install the development version of xlsss from [GitHub](https://github.co ``` r # install.packages("devtools") -devtools::install_github("ScotGovAnalysis/xlsss") +devtools::install_github("ScotGovAnalysis/xlsss", + upgrade = "never") ``` ## Getting started diff --git a/README.md b/README.md index e14f027..b36e881 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,8 @@ You can install the development version of xlsss from ``` r # install.packages("devtools") -devtools::install_github("ScotGovAnalysis/xlsss") +devtools::install_github("ScotGovAnalysis/xlsss", + upgrade = "never") ``` ## Getting started diff --git a/man/xlsss-package.Rd b/man/xlsss-package.Rd index 78f8c08..3720103 100644 --- a/man/xlsss-package.Rd +++ b/man/xlsss-package.Rd @@ -4,12 +4,12 @@ \name{xlsss-package} \alias{xlsss} \alias{xlsss-package} -\title{xlsss: What the Package Does (One Line, Title Case)} +\title{xlsss: Automate Accessible Table Production for Social Security Scotland} \description{ -What the package does (one paragraph). +Automates the process of outputting statistical tables to excel in a format that complies with accessibility requirements. } \author{ -\strong{Maintainer}: First Last \email{first.last@example.com} (\href{https://orcid.org/YOUR-ORCID-ID}{ORCID}) +\strong{Maintainer}: Izaak Jephson \email{first.last@example.com} } \keyword{internal} From 164bc2c4ec3e2c7154b07d8fd30b87e4baad12e9 Mon Sep 17 00:00:00 2001 From: Izaak Jephson Date: Mon, 9 Sep 2024 14:54:41 +0100 Subject: [PATCH 4/4] Remove encoding warning in export_tables.R --- R/export_tables.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/export_tables.R b/R/export_tables.R index dab39f7..0828f7e 100644 --- a/R/export_tables.R +++ b/R/export_tables.R @@ -211,7 +211,7 @@ format_columns <- function(wb, openxlsx::addStyle(wb, sheet_name, rows = start_row:end_row, cols = column, - style = openxlsx::createStyle(numFmt = "£#,##0;-;£0", halign = "right"), + style = openxlsx::createStyle(numFmt = "\u00A3#,##0;-;\u00A30", halign = "right"), gridExpand = TRUE )