Skip to content

Commit

Permalink
split the gt themes out into gt_base and _ojo _okpi versions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjbe committed Nov 7, 2024
1 parent 6a4172a commit 61658f1
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 88 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(geom_path)
export(geom_point)
export(geom_step)
export(geom_text)
export(gt_base)
export(gt_ojo)
export(gt_okpi)
export(ojo_analyst_name_text)
Expand Down
168 changes: 81 additions & 87 deletions R/ojo_gt.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
#' gt_okpi Function
#' gt_base Function
#'
#' @param data A data frame to be converted into a gt table.
#' @param title Optional. A title for the table. Default is NA.
#' @param subtitle Optional. A subtitle for the table. Default is NA.
#' @param font_size The font size for the table. Default is 14.
#' @param font The font family to use. Default is Roboto Condensed.
#' @param format_cols Should gt::fmt_auto() be applied to all cols?
#' @param analyst_name The name of the analyst to credit in the footnote
#' @param source The source / domain of the data
#'
#' @return A gt table based on the input data frame with specified modifications.
#' @examples
#' \dontrun{
#' okpi_gt(data = mtcars, title = "Motor Trend Car Road Tests", subtitle = "From mtcars")
#' gt_base(data = mtcars, title = "Motor Trend Car Road Tests", subtitle = "From mtcars")
#' }
#' @export
gt_okpi <- function(data,
title = NA, subtitle = NA,
font_size = 14,
font = "Roboto Condensed",
format_cols = TRUE,
analyst_name = NA, source = NA
) {

#'
gt_base <- function(data,
title = NA, subtitle = NA,
font_size = 14,
format_cols = TRUE,
analyst_name = NA, source = NA
){
x <- data |>
gt::gt() |>
gt::tab_options(
Expand All @@ -38,34 +36,6 @@ gt_okpi <- function(data,
data_row.padding = gt::px(7),
column_labels.font.size = gt::px(font_size)
) |>
gt::opt_table_font(
font = gt::google_font(font)
) |>
gt::tab_style(
style = list(
gt::cell_text(weight = "bold",
color = ojothemes::okpi_red,
size = gt::px(font_size * 2)),
gt::css(padding.left = "10px",
border.left = paste0("10px solid ", okpi_red, ";"))
),
locations = gt::cells_title(groups = "title")
) |>
gt::tab_style(
style = list(
gt::cell_text(style = "italic",
color = ojothemes::okpi_blue,
size = gt::px(font_size * 1.25)),
gt::css(padding.left = "10px",
border.left = paste0("10px solid ", okpi_red, ";"))
),
locations = gt::cells_title(groups = "subtitle")
) |>
gt::tab_style(
style = gt::cell_text(color = "#A9A9A9",
transform = "uppercase"),
locations = gt::cells_column_labels(tidyselect::everything())
) |>
gt::tab_spanner(
label = stringr::str_to_title(colnames(data))
) |>
Expand All @@ -74,8 +44,15 @@ gt_okpi <- function(data,
table.font.size = "medium",
heading.title.font.size = "medium",
heading.subtitle.font.size = "small"
) |>
gt::tab_style(
style = gt::cell_text(color = "#333333",
transform = "uppercase",
weight = "bold"),
locations = gt::cells_column_labels(tidyselect::everything())
)


# Add title / subtitle?
if (!is.na(title) | !is.na(subtitle)) {
x <- x |>
Expand All @@ -98,7 +75,7 @@ gt_okpi <- function(data,
return(x)
}

#' gt_ojo Function
#' gt_okpi Function
#'
#' @param data A data frame to be converted into a gt table.
#' @param title Optional. A title for the table. Default is NA.
Expand All @@ -115,6 +92,64 @@ gt_okpi <- function(data,
#' okpi_gt(data = mtcars, title = "Motor Trend Car Road Tests", subtitle = "From mtcars")
#' }
#' @export
gt_okpi <- function(data,
title = NA, subtitle = NA,
font_size = 14,
font = "Roboto Condensed",
format_cols = TRUE,
analyst_name = NA, source = NA){

x <- data |>
ojothemes::gt_base(title = title,
subtitle = subtitle,
font_size = font_size,
format_cols = format_cols,
analyst_name = analyst_name,
source = source) |>
gt::opt_table_font(
font = gt::google_font(font)
) |>
gt::tab_style(
style = list(
gt::cell_text(weight = "bold",
color = ojothemes::okpi_red,
size = gt::px(font_size * 2)),
gt::css(padding.left = "10px",
border.left = paste0("10px solid ", okpi_red, ";"))
),
locations = gt::cells_title(groups = "title")
) |>
gt::tab_style(
style = list(
gt::cell_text(style = "italic",
color = "#333333",
size = gt::px(font_size * 1.25)),
gt::css(padding.left = "10px",
border.left = paste0("10px solid ", okpi_red, ";"))
),
locations = gt::cells_title(groups = "subtitle")
)

return(x)
}

#' gt_ojo Function
#'
#' @param data A data frame to be converted into a gt table.
#' @param title Optional. A title for the table. Default is NA.
#' @param subtitle Optional. A subtitle for the table. Default is NA.
#' @param font_size The font size for the table. Default is 14.
#' @param font The font family to use. Default is Roboto mono
#' @param format_cols Should gt::fmt_auto() be applied to all cols?
#' @param analyst_name The name of the analyst to credit in the footnote
#' @param source The source / domain of the data
#'
#' @return A gt table based on the input data frame with specified modifications.
#' @examples
#' \dontrun{
#' okpi_gt(data = mtcars, title = "Motor Trend Car Road Tests", subtitle = "From mtcars")
#' }
#' @export
gt_ojo <- function(data,
title = NA, subtitle = NA,
font_size = 14,
Expand All @@ -124,20 +159,12 @@ gt_ojo <- function(data,
) {

x <- data |>
gt::gt() |>
gt::tab_options(
heading.align = "left",
column_labels.border.top.style = "none",
table.border.top.style = "none",
column_labels.border.bottom.style = "none",
column_labels.border.bottom.width = 1,
column_labels.border.bottom.color = "#A9A9A9",
table_body.border.top.style = "none",
table_body.border.bottom.color = "white",
heading.border.bottom.style = "none",
data_row.padding = gt::px(7),
column_labels.font.size = gt::px(font_size)
) |>
ojothemes::gt_base(title = title,
subtitle = subtitle,
font_size = font_size,
format_cols = format_cols,
analyst_name = analyst_name,
source = source) |>
gt::opt_table_font(
font = gt::google_font(font)
) |>
Expand All @@ -160,41 +187,8 @@ gt_ojo <- function(data,
border.left = paste0("10px solid ", okpi_yellow, ";"))
),
locations = gt::cells_title(groups = "subtitle")
) |>
gt::tab_style(
style = gt::cell_text(color = "#A9A9A9",
transform = "uppercase"),
locations = gt::cells_column_labels(tidyselect::everything())
) |>
gt::tab_spanner(
label = stringr::str_to_title(colnames(data))
) |>
gt::tab_options(
column_labels.font.size = "medium",
table.font.size = "medium",
heading.title.font.size = "medium",
heading.subtitle.font.size = "small"
)

# Add title / subtitle?
if (!is.na(title) | !is.na(subtitle)) {
x <- x |>
gt::tab_header(title = title,
subtitle = subtitle)
}

# Add col formatting?
if (format_cols) {
x <- x |>
gt::fmt_auto(lg_num_pref = "suf")
}

if (!is.na(analyst_name) | !is.na(source)) {
x <- x |>
ojo_gt_captions(analyst_name = analyst_name,
source = source)
}

return(x)
}

1 change: 1 addition & 0 deletions R/ojo_labs.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ojo_source_text <- function(source = NA) {
source == "oscn" ~ "Source: OK Policy Institute analysis of Oklahoma State Courts Network data.",
source == "ocdc" ~ "Source: OK Policy Institute analysis of data from the Oklahoma County Detention Center's 'Jailtracker' system.",
source == "ppb" ~ "Source: OK Policy Institute analysis of Oklahoma Pardon and Parole Board records.",
# TODO: add OJA
is.na(source) ~ "", # If source = NA, just leave that out of the caption
TRUE ~ source # If source is anything else, just print it verbatim
)
Expand Down
42 changes: 42 additions & 0 deletions man/gt_base.Rd

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

2 changes: 1 addition & 1 deletion man/gt_ojo.Rd

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

0 comments on commit 61658f1

Please sign in to comment.