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

Reworked styling options #39

Merged
merged 1 commit into from
Dec 26, 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: tablespan
Type: Package
Title: Create Satisficing 'Excel', 'HTML', 'LaTeX', and 'RTF' Tables using a Simple Formula
Version: 0.1.8
Version: 0.2.0
Authors@R: c(person(given = "Jannik H.", family = "Orzek",
role = c("aut", "cre", "cph"),
email = "[email protected]",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ export(as_excel)
export(as_gt)
export(cell_style)
export(create_data_styles)
export(style_color)
export(tablespan)
export(tbl_styles)
import(gt)
import(openxlsx)
importFrom(dplyr,all_of)
importFrom(grDevices,col2rgb)
importFrom(gt,tab_header)
importFrom(methods,is)
importFrom(openxlsx,createStyle)
Expand Down
151 changes: 93 additions & 58 deletions R/as_excel.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ as_excel <- function(tbl,
locations = locations,
styles = styles)

create_outlines(tbl = tbl,
workbook = workbook,
sheet = sheet,
locations = locations,
styles = styles)

write_title(tbl = tbl,
workbook = workbook,
sheet = sheet,
Expand All @@ -124,6 +118,13 @@ as_excel <- function(tbl,
locations = locations,
styles = styles)

# We create the outlines last as we may have to overwrite some border colors.
create_outlines(tbl = tbl,
workbook = workbook,
sheet = sheet,
locations = locations,
styles = styles)

return(workbook)
}

Expand All @@ -145,22 +146,67 @@ fill_background <- function(tbl,
sheet,
locations,
styles){
# To fill the background, we have to find the dimensions
# of the tabel first.
min_row <- locations$row |> unlist() |> min()
max_row <- locations$row |> unlist() |> max()
min_col <- locations$col |> unlist() |> min()
max_col <- locations$col |> unlist() |> max()

max_row <- max_row - is.null(tbl$footnote)

# Title
if(!is.null(tbl$title))
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$bg_title,
rows = locations$row$start_row_title:locations$row$end_row_title,
cols = locations$col$start_col_title:locations$col$end_col_title,
gridExpand = TRUE,
stack = TRUE)
# Subtitle
if(!is.null(tbl$subtitle))
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$bg_subtitle,
rows = locations$row$start_row_subtitle:locations$row$end_row_subtitle,
cols = locations$col$start_col_subtitle:locations$col$end_col_subtitle,
gridExpand = TRUE,
stack = TRUE)
# Header LHS
if(!is.null(tbl$header$lhs))
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$bg_header_lhs,
rows = locations$row$start_row_header:locations$row$end_row_header,
cols = locations$col$start_col_header_lhs:locations$col$end_col_header_lhs,
gridExpand = TRUE,
stack = TRUE)
# Header RHS
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$background_style,
rows = min_row:max_row,
cols = min_col:max_col,
style = styles$bg_header_lhs,
rows = locations$row$start_row_header:locations$row$end_row_header,
cols = locations$col$start_col_header_rhs:locations$col$end_col_header_rhs,
gridExpand = TRUE,
stack = TRUE)
# Rownames
if(!is.null(tbl$header$lhs))
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$bg_rownames,
rows = locations$row$start_row_data:locations$row$end_row_data,
cols = locations$col$start_col_header_lhs:locations$col$end_col_header_lhs,
gridExpand = TRUE,
stack = TRUE)
# Data
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$bg_data,
rows = locations$row$start_row_data:locations$row$end_row_data,
cols = locations$col$start_col_header_rhs:locations$col$end_col_header_rhs,
gridExpand = TRUE,
stack = TRUE)
# Footnote
if(!is.null(tbl$footnote))
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$bg_data,
rows = locations$row$start_row_footnote:locations$row$end_row_footnote,
cols = locations$col$start_col_footnote:locations$col$end_col_footnote,
gridExpand = TRUE,
stack = TRUE)
}

#' create_outlines
Expand Down Expand Up @@ -190,47 +236,39 @@ create_outlines <- function(tbl,
# top line
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$hline_style,
style = styles$hline,
rows = locations$row$start_row_header,
cols = left_most:locations$col$end_col_header_rhs,
stack = TRUE)

# line between header and data
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$hline_style,
rows = locations$row$end_row_header + 1,
cols = left_most:locations$col$end_col_header_rhs,
stack = TRUE)

# bottom line
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$hline_style,
style = styles$hline,
rows = locations$row$end_row_data + 1,
cols = left_most:locations$col$end_col_header_rhs,
stack = TRUE)

# left line
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$vline_style,
style = styles$vline,
rows = locations$row$start_row_header:locations$row$end_row_data,
cols = left_most,
stack = TRUE)

# right line
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$vline_style,
style = styles$vline,
rows = locations$row$start_row_header:locations$row$end_row_data,
cols = locations$col$end_col_header_rhs + 1,
stack = TRUE)

# row name separator
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$vline_style,
style = styles$vline,
rows = locations$row$start_row_header:locations$row$end_row_data,
cols = locations$col$start_col_header_rhs,
stack = TRUE)
Expand Down Expand Up @@ -265,7 +303,7 @@ write_title <- function(tbl,
rowNames = FALSE)
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$title_style,
style = styles$cell_title,
rows = locations$row$start_row_title,
cols = locations$col$start_col_title,
stack = TRUE)
Expand All @@ -285,7 +323,7 @@ write_title <- function(tbl,
rowNames = FALSE)
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$subtitle_style,
style = styles$cell_subtitle,
rows = locations$row$start_row_subtitle,
cols = locations$col$start_col_subtitle,
stack = TRUE)
Expand Down Expand Up @@ -327,8 +365,8 @@ write_header <- function(workbook,
max_level = max_level,
start_row = locations$row$start_row_header,
start_col = locations$col$start_col_header_lhs,
header_style = styles$header_style,
vline_style = styles$vline_style)
header_style = styles$cell_header_lhs,
vline = styles$vline)

}else{
max_level <- header$rhs$level
Expand All @@ -340,8 +378,8 @@ write_header <- function(workbook,
max_level = max_level,
start_row = locations$row$start_row_header,
start_col = locations$col$start_col_header_rhs,
header_style = styles$header_style,
vline_style = styles$vline_style)
header_style = styles$cell_header_rhs,
vline = styles$vline)

}

Expand All @@ -357,7 +395,7 @@ write_header <- function(workbook,
#' @param start_row integer specifying row to write to
#' @param start_col integer specifying column to write to
#' @param header_style openxlsx style for the header
#' @param vline_style openxlsx style for the vertical lines in the header
#' @param vline openxlsx style for the vertical lines in the header
#' @import openxlsx
#' @noRd
write_header_entry <- function(workbook,
Expand All @@ -367,7 +405,7 @@ write_header_entry <- function(workbook,
start_row,
start_col,
header_style,
vline_style){
vline){

# write current entry name into table
if(header_entry$name != "_BASE_LEVEL_"){
Expand All @@ -389,23 +427,6 @@ write_header_entry <- function(workbook,
cols = start_col:(start_col + header_entry$width - 1),
gridExpand = TRUE,
stack = TRUE)

# add vertical line to the left
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = vline_style,
rows = (start_row + (max_level - header_entry$level) - 1):(start_row + max_level - 2),
cols = start_col,
gridExpand = TRUE,
stack = TRUE)
# add vertical line to the right
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = vline_style,
rows = (start_row + (max_level - header_entry$level) - 1):(start_row + max_level - 2),
cols = (start_col + header_entry$width),
gridExpand = TRUE,
stack = TRUE)
}

# entries may have sub-entries, that also have to be written down
Expand All @@ -418,7 +439,7 @@ write_header_entry <- function(workbook,
start_row = start_row,
start_col = start_col_entry,
header_style = header_style,
vline_style = vline_style)
vline = vline)
start_col_entry <- start_col_entry + entry$width
}
}
Expand Down Expand Up @@ -453,6 +474,13 @@ write_data <- function(workbook,
startRow = locations$row$end_row_header + 1,
rowNames = FALSE,
colNames = FALSE)
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$cell_rownames,
rows = locations$row$start_row_data : (locations$row$end_row_data),
cols = locations$col$start_col_header_lhs : locations$col$end_col_header_lhs,
stack = TRUE,
gridExpand = TRUE)

for(sty in styles$data_styles){
for(j in seq_len(ncol(table_data$row_data))){
Expand Down Expand Up @@ -483,6 +511,13 @@ write_data <- function(workbook,
startRow = locations$row$end_row_header + 1,
rowNames = FALSE,
colNames = FALSE)
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$cell_data,
rows = locations$row$start_row_data : locations$row$end_row_data,
cols = locations$col$start_col_header_rhs : locations$col$end_col_header_rhs,
stack = TRUE,
gridExpand = TRUE)

for(sty in styles$data_styles){
for(j in seq_len(ncol(table_data$col_data))){
Expand Down Expand Up @@ -549,7 +584,7 @@ write_footnote <- function(tbl,
rowNames = FALSE)
openxlsx::addStyle(wb = workbook,
sheet = sheet,
style = styles$footnote_style,
style = styles$cell_footnote,
rows = locations$row$start_row_footnote,
cols = locations$col$start_col_footnote,
stack = TRUE)
Expand Down
29 changes: 21 additions & 8 deletions R/create_test_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,34 @@ create_test_files_cars <- function(){
file = paste0(target_dir, "cars.xlsx"),
overwrite = TRUE)

# Different colors
wb <- as_excel(tbl = tbl, styles = style_color(primary_color = "#987349"))

openxlsx::saveWorkbook(wb,
file = paste0(target_dir, "cars_987349.xlsx"),
overwrite = TRUE)

wb <- as_excel(tbl = tbl, styles = style_color(primary_color = "#893485"))

openxlsx::saveWorkbook(wb,
file = paste0(target_dir, "cars_893485.xlsx"),
overwrite = TRUE)

# Complex merging of rownames
summarized_table_merge <- summarized_table
summarized_table_merge[ ,"vs"] <- 1
summarized_table_merge[1,"vs"] <- 0
summarized_table_merge[ , "N"] <- 1

tbl <- tablespan(data = summarized_table_merge,
formula = Cylinder:cyl + Engine:vs + N ~
(`Horse Power` = Mean:mean_hp + SD:sd_hp) +
(`Weight` = Mean:mean_wt + SD:sd_wt),
title = "Motor Trend Car Road Tests",
subtitle = "A table created with tablespan",
footnote = "Data from the infamous mtcars data set.")
tbl_merge <- tablespan(data = summarized_table_merge,
formula = Cylinder:cyl + Engine:vs + N ~
(`Horse Power` = Mean:mean_hp + SD:sd_hp) +
(`Weight` = Mean:mean_wt + SD:sd_wt),
title = "Motor Trend Car Road Tests",
subtitle = "A table created with tablespan",
footnote = "Data from the infamous mtcars data set.")

wb <- as_excel(tbl = tbl)
wb <- as_excel(tbl = tbl_merge)
openxlsx::saveWorkbook(wb,
file = paste0(target_dir, "cars_complex_merge.xlsx"),
overwrite = TRUE)
Expand Down
Loading
Loading