Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove encoding warning in export_tables.R #8

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions R/create_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -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\",
Expand All @@ -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()
Expand All @@ -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)
}
24 changes: 22 additions & 2 deletions R/export_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ add_contents_sheet <- function(wb,
wb,
"Contents",
cols = 1:2,
width = c(20, "auto"),
width = c(27, "auto"),
ignoreMergedCells = TRUE
)

Expand Down Expand Up @@ -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,
Expand All @@ -206,6 +206,16 @@ format_columns <- function(wb,
)

}
# 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 = "\u00A3#,##0;-;\u00A30", halign = "right"),
gridExpand = TRUE
)

}
}

#' Format Rows
Expand Down Expand Up @@ -236,6 +246,16 @@ format_rows <- function(wb,
stack = TRUE
)
}
# Format % rows
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
)

}
}

#' Add data tables to existing worksheet
Expand Down
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions man/xlsss-package.Rd

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