Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Aug 5, 2024
2 parents 27e9913 + db55019 commit 7daea56
Show file tree
Hide file tree
Showing 33 changed files with 141 additions and 135 deletions.
10 changes: 8 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ message: 'If you wish to cite the "gt" package use:'
type: software
license: MIT
title: 'gt: Easily Create Presentation-Ready Display Tables'
version: 0.10.1
version: 0.11.0
abstract: Build display tables from tabular data with an easy-to-use set of functions.
With its progressive approach, we can construct display tables with a cohesive set
of table parts. Table values can be formatted using any of the included formatting
Expand Down Expand Up @@ -34,7 +34,13 @@ authors:
given-names: JooYoung
email: [email protected]
orcid: https://orcid.org/0000-0002-4064-6012
repository: https://CRAN.R-project.org/package=gt
- family-names: Ken
given-names: Brevoort
email: [email protected]
orcid: https://orcid.org/0000-0002-4001-8358
- family-names: Olivier
given-names: Roy
repository: https://doi.org/10.32614/CRAN.package.gt
repository-code: https://github.com/rstudio/gt
url: https://gt.rstudio.com
contact:
Expand Down
12 changes: 6 additions & 6 deletions R/cols_align_decimal.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ cols_align_decimal <- function(
# helper -----------------------------------
align_to_char <- function(x, align_at = ".") {

na_x_vals <- grepl("^NA$", x)
na_x_vals <- x == "NA"
no_a_char <- !grepl(align_at, x, fixed = TRUE) & !grepl("[0-9]", x)
has_t_dec <- grepl("[0-9]\\.$", x)

Expand Down Expand Up @@ -225,7 +225,7 @@ align_to_char <- function(x, align_at = ".") {

x_piece_rhs[i] <-
gsub(
paste0(paste(rep("\U02007", n_char_extracted), collapse = ""), "$"),
paste0(strrep("\U02007", n_char_extracted), "$"),
"",
x_piece_rhs[i]
)
Expand All @@ -238,15 +238,15 @@ align_to_char <- function(x, align_at = ".") {

if (grepl(align_at, paste(x[!x_no_align], collapse = "|"), fixed = TRUE)) {

x_align[!nchar(x_rhs) > 0 & !grepl(align_at, x[!x_no_align], fixed = TRUE)] <-
sub(align_at, " ", x_align[!nchar(x_rhs) > 0], fixed = TRUE)
x_align[nchar(x_rhs) == 0 & !grepl(align_at, x[!x_no_align], fixed = TRUE)] <-
sub(align_at, " ", x_align[nchar(x_rhs) == 0], fixed = TRUE)

x_align[x_align_parens] <- paste0(x_align[x_align_parens], "\U000A0")

} else {

x_align[!nchar(x_rhs) > 0 & !grepl(align_at, x[!x_no_align], fixed = TRUE)] <-
sub(align_at, "", x_align[!nchar(x_rhs) > 0], fixed = TRUE)
x_align[nchar(x_rhs) == 0 & !grepl(align_at, x[!x_no_align], fixed = TRUE)] <-
sub(align_at, "", x_align[nchar(x_rhs) == 0], fixed = TRUE)

x_align[!x_align_parens] <- paste0(x_align[!x_align_parens], "\U000A0")
}
Expand Down
2 changes: 1 addition & 1 deletion R/cols_merge.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ cols_merge <- function(

hide_columns_from_supplied <- base::intersect(hide_columns, columns)

if (length(base::setdiff(hide_columns, columns) > 0)) {
if (length(base::setdiff(hide_columns, columns)) > 0) {
cli::cli_warn(c(
"Only a subset of columns supplied in `columns` will be hidden.",
"*" = "Use an additional `cols_hide()` expression to hide any
Expand Down
2 changes: 1 addition & 1 deletion R/compile_scss.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ compile_scss <- function(data, id = NULL) {
gt_options_tbl <-
within(
gt_options_tbl, {
value[color_rows] = value[color_rows] <-
value[color_rows] <- value[color_rows] <-
lapply(value[color_rows], html_color)
}
)
Expand Down
8 changes: 4 additions & 4 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ grid_align_gtable <- function(gtable, data) {

left <- grid::unit(0.5, "null")

} else if (grepl("\\%$", left)) {
} else if (endsWith(left, "%")) {

left <- as.numeric(sub("\\%$", "", left)) / 100
left <- grid::unit(left * 0.5, "null")
Expand All @@ -941,7 +941,7 @@ grid_align_gtable <- function(gtable, data) {

right <- grid::unit(0.5, "null")

} else if (grepl("\\%$", right)) {
} else if (endsWith(right, "%")) {

right <- as.numeric(sub("\\%$", "", right)) / 100
right <- grid::unit(right * 0.5, "null")
Expand Down Expand Up @@ -1005,7 +1005,7 @@ grid_layout_widths <- function(layout, data) {

total_width <- dt_options_get_value(data, "table_width")

if (grepl("px$", total_width)) {
if (endsWith(total_width, "px")) {

total_width <- parse_px_to_pt(total_width)
extra_width <- total_width - sum(widths)
Expand All @@ -1023,7 +1023,7 @@ grid_layout_widths <- function(layout, data) {
return(widths)
}

if (grepl("\\%$", total_width)) {
if (endsWith(total_width, "%")) {

# Set the total width in npc units
total_width <- as.numeric(sub("\\%$", "", total_width)) / 100
Expand Down
31 changes: 15 additions & 16 deletions R/fmt.R
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ format_num_to_str <- function(
is_inf <- grepl("Inf", x_str, fixed = TRUE)
x_str_numeric <- x_str[!is_inf]
has_decimal <- grepl(".", x_str_numeric, fixed = TRUE)
is_negative <- grepl("^-", x_str_numeric)
is_negative <- startsWith(x_str_numeric, "-")

integer_parts <- sub("\\..*", "", x_str_numeric)

Expand Down Expand Up @@ -757,23 +757,22 @@ to_latex_math_mode <- function(x, context) {

return(x)

} else {

# Ensure that `$` signs only surround the correct number parts
# - certain LaTeX marks operate only in text mode and we need to
# conditionally surround only the number portion in these cases
# - right now, the only marks that need to be situated outside of
# the math context are the per mille and per myriad (10,000)
# marks (provided by the `fmt_per()` function)
if (all(grepl("\\\\textper(ten)?thousand$", x))) {
out <- paste0("$", x)
out <- gsub("(\\s*?\\\\textper(ten)?thousand)", "$\\1", out)
} else {
out <- paste_between(x, x_2 = c("$", "$"))
}
}

return(out)
# Ensure that `$` signs only surround the correct number parts
# - certain LaTeX marks operate only in text mode and we need to
# conditionally surround only the number portion in these cases
# - right now, the only marks that need to be situated outside of
# the math context are the per mille and per myriad (10,000)
# marks (provided by the `fmt_per()` function)
if (all(grepl("\\\\textper(ten)?thousand$", x))) {
out <- paste0("$", x)
out <- gsub("(\\s*?\\\\textper(ten)?thousand)", "$\\1", out)
} else {
out <- paste_between(x, x_2 = c("$", "$"))
}

return(out)
}

#' Obtain the contextually correct minus mark
Expand Down
13 changes: 6 additions & 7 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,7 @@ fmt_fraction <- function(

gcd <- function(x,y) {
r <- x %% y
return(ifelse(r, gcd(y, r), y))
ifelse(r, gcd(y, r), y)
}

make_frac <- function(x, denom, simplify = TRUE) {
Expand Down Expand Up @@ -7114,7 +7114,7 @@ fmt_tf <- function(
# If using SVG graphics for either of `true_val` or `false_val` then
# we'd prefer to have center alignment of the icons
if (
grepl("^<svg ", true_val) || grepl("^<svg ", false_val)
startsWith(true_val, "<svg ") || startsWith(false_val, "<svg ")
) {
alignment <- "center"
}
Expand Down Expand Up @@ -8263,7 +8263,7 @@ fmt_url <- function(
target <- target %||% "_blank"
target_values <- NULL

if (grepl("^_", target)) {
if (startsWith(target, "_")) {
target_values <- c("_blank", "_self", "_parent", "_top")
}

Expand Down Expand Up @@ -8951,7 +8951,7 @@ fmt_email <- function(
target <- target %||% "_blank"
target_values <- NULL

if (grepl("^_", target)) {
if (startsWith(target, "_")) {
target_values <- c("_blank", "_self", "_parent", "_top")
}

Expand Down Expand Up @@ -9018,9 +9018,8 @@ fmt_email <- function(
label_separated <- unlist(strsplit(label_str, " "))

} else if (
grepl("^<", label_str) &&
grepl(">$", label_str) &&
!grepl("^<svg", label_str)
grepl("^<.*>$", label_str) &&
!startsWith(label_str, "<svg")
) {

label_separated <- unlist(strsplit(label_str, ">\\s*<"))
Expand Down
2 changes: 1 addition & 1 deletion R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ cells_title <- function(groups = c("title", "subtitle")) {
!is.character(groups) ||
length(groups) < 1 ||
!all(groups %in% c("title", "subtitle")) ||
any(duplicated(groups))
anyDuplicated(groups) > 0L
) {
cli::cli_abort(
"`groups` must be either {.val title}, {.val subtitle}, or both."
Expand Down
6 changes: 3 additions & 3 deletions R/opts.R
Original file line number Diff line number Diff line change
Expand Up @@ -1174,9 +1174,9 @@ opt_all_caps <- function(

values_vec <-
dplyr::case_when(
grepl("\\.font\\.size$", options_vec) ~ "80%",
grepl("\\.font\\.weight$", options_vec) ~ "bolder",
grepl("\\.text_transform$", options_vec) ~ "uppercase"
endsWith(options_vec, ".font.size") ~ "80%",
endsWith(options_vec, ".font.weight") ~ "bolder",
endsWith(options_vec, ".text_transform") ~ "uppercase"
)

option_value_list <- create_option_value_list(options_vec, values_vec)
Expand Down
5 changes: 3 additions & 2 deletions R/render_as_i_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ render_as_ihtml <- function(data, id) {
column_labels_border_bottom_color <- opt_val(data = data, option = "column_labels_border_bottom_color")

# Don't allow NA
column_labels_background_color = opt_val(data = data, option = "column_labels_background_color")
column_labels_background_color <- opt_val(data = data, option = "column_labels_background_color")
if (is.na(column_labels_background_color)) {
# apply all column labels formatting to both heading + groupCol styling (nothing specific for spanners styling in gt?)
column_labels_background_color <- "transparent"
}

column_labels_font_weight <- opt_val(data = data, option = "column_labels_font_weight")
# Apply stub font weight to
stub_font_weight <- opt_val(data = data, option = "stub_font_weight")
# Apply font weight to groupname_col title
row_group_font_weight <- opt_val(data = data, "row_group_font_weight")
row_group_background_color <- opt_val(data = data, "row_group_background_color")
Expand All @@ -215,7 +217,6 @@ render_as_ihtml <- function(data, id) {
all_borders <- horizontal_borders != "none" && veritcal_borders != "none"

# for row names + summary label
stub_font_weight <- opt_val(data = data, "stub_font_weight")
stub_border_color <- opt_val(data, "stub_border_color")
stub_border_style <- opt_val(data, "stub_border_style")
# Apply stub font weight to
Expand Down
2 changes: 1 addition & 1 deletion R/tab_create_modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ tab_spanner_delim <- function(
# Modify `spanner_id` to not collide with any other values
if (spanner_id %in% spanner_id_vals) {

if (grepl("^spanner-", spanner_id)) {
if (startsWith(spanner_id, "spanner-")) {

# Add number to spanner ID values on first duplication
spanner_id <- gsub("^spanner-", "spanner:1-", spanner_id)
Expand Down
8 changes: 5 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1832,9 +1832,11 @@ non_na_index <- function(
default_value = NA
) {

stopifnot(is.integer(index) || is.numeric(index))
stopifnot(all(index >= 1 | is.na(index)))
stopifnot(all(length(values) >= index | is.na(index)))
stopifnot(
is.numeric(index),
index >= 1 | is.na(index),
length(values) >= index | is.na(index)
)

# Get a vector of suffixes, which may include NA values
res <- values[index]
Expand Down
6 changes: 3 additions & 3 deletions R/utils_examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ get_example_text <- function(topic) {

example_lines <- gsub(".*preformatted.(.*)", "```{r}\n\\1", example_lines)
example_lines <- gsub("}\\if{html}{\\out{</div>}}", "```", example_lines, fixed = TRUE)
example_lines <- example_lines[!grepl("^}|<img", example_lines)]
example_lines <- example_lines[!grepl("\\if\\{html\\}", example_lines)]
example_lines <- example_lines[!grepl("^#>", example_lines)]
example_lines <- grep("^}|<img", example_lines, value = TRUE, invert = TRUE)
example_lines <- grep("\\if\\{html\\}", example_lines, value = TRUE, invert = TRUE)
example_lines <- grep("^#>", example_lines, value = TRUE, invert = TRUE)

example_lines <- gsub("\\%", "%", example_lines, fixed = TRUE)
example_lines <- gsub("\\{", "{", example_lines, fixed = TRUE)
Expand Down
8 changes: 4 additions & 4 deletions R/utils_general_str_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ paste_on_side <- function(
}

# Stop function if `x` and `x_side` are not both of class character
if (any(!inherits(x, "character"), !inherits(x_side, "character"))) {
if (!all(inherits(x, "character"), inherits(x_side, "character"))) {
cli::cli_abort(c(
"*" = "The `x` and `x_side` objects must be of class `character`."
), .internal = TRUE)
Expand Down Expand Up @@ -326,7 +326,7 @@ is_adjacent_separate <- function(group_1, group_2) {
return(FALSE)
}

return(TRUE)
TRUE
}

str_catalog <- function(
Expand Down Expand Up @@ -377,10 +377,10 @@ str_title_case <- function(x) {

s <- strsplit(y, " ", fixed = TRUE)[[1]]

paste(
paste0(
toupper(substring(s, 1, 1)),
substring(s, 2),
sep = "", collapse = " "
collapse = " "
)
}

Expand Down
28 changes: 11 additions & 17 deletions R/utils_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -2097,23 +2097,17 @@ generate_ref_line_from_keyword <- function(vals, keyword) {

rlang::arg_match0(keyword, reference_line_keywords())

if (keyword == "mean") {
ref_line <- mean(vals, na.rm = TRUE)
} else if (keyword == "median") {
ref_line <- stats::median(vals, na.rm = TRUE)
} else if (keyword == "min") {
ref_line <- min(vals, na.rm = TRUE)
} else if (keyword == "max") {
ref_line <- max(vals, na.rm = TRUE)
} else if (keyword == "first") {
ref_line <- vals[!is.na(vals)][1]
} else if (keyword == "last") {
ref_line <- vals[!is.na(vals)][length(vals[!is.na(vals)])]
} else if (keyword == "q1") {
ref_line <- as.numeric(stats::quantile(vals, 0.25, na.rm = TRUE))
} else {
ref_line <- as.numeric(stats::quantile(vals, 0.75, na.rm = TRUE))
}
ref_line <- switch(keyword,
mean = mean(vals, na.rm = TRUE),
median = stats::median(vals, na.rm = TRUE),
min = min(vals, na.rm = TRUE),
max = max(vals, na.rm = TRUE),
first = vals[!is.na(vals)][1],
last = vals[!is.na(vals)][length(vals[!is.na(vals)])],
q1 = as.numeric(stats::quantile(vals, 0.25, na.rm = TRUE)),
# default:
as.numeric(stats::quantile(vals, 0.75, na.rm = TRUE))
)

ref_line
}
Expand Down
4 changes: 2 additions & 2 deletions R/utils_render_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ parse_css <- function(data) {
# Find first and last line of definitions
start <- grep("\\{$", css)
end <- which(css == "}")
if (!length(start) == length(end)) {
if (length(start) != length(end)) {
cli::cli_abort("Formatting in {.fn compile_css} is unexpected.")
}

Expand All @@ -1408,7 +1408,7 @@ parse_css <- function(data) {
classes <- Map(`:`, start + 1, end - 1)
names(classes) <- names
classes <- lapply(classes, function(x) unlist(split[x], FALSE))
classes <- classes[grepl("^gt_", names(classes))]
classes <- classes[startsWith(names(classes), "gt_")]

# There are two entries for gt_table that we merge here
is_table <- which(names(classes) == "gt_table")
Expand Down
4 changes: 2 additions & 2 deletions R/utils_render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ footnote_mark_to_html <- function(
sup_class <- "gt_footnote_marks gt_asterisk"
}

is_sup <- grepl("\\^", spec)
is_sup <- grepl("^", spec, fixed = TRUE)

if (grepl(".", spec, fixed = TRUE)) mark <- paste0(mark, ".")
if (grepl("(", spec, fixed = TRUE)) mark <- paste0("(", mark)
Expand Down Expand Up @@ -2074,7 +2074,7 @@ build_row_styles <- function(
# colnum values. Check and throw early.
if (
!isTRUE(all(styles_resolved_row$colnum %in% c(0, seq_len(n_cols)))) ||
any(duplicated(styles_resolved_row$colnum))
anyDuplicated(styles_resolved_row$colnum) > 0L
) {
cli::cli_abort(
"`build_row_styles()` was called with invalid `colnum` values."
Expand Down
Loading

0 comments on commit 7daea56

Please sign in to comment.