From 871ca59187f2dbd31e42950502e6a65d3805d51f Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Tue, 23 Jul 2024 23:35:36 -0700 Subject: [PATCH 1/8] Code quality fixes --- R/cols_align_decimal.R | 12 ++++---- R/cols_merge.R | 2 +- R/compile_scss.R | 2 +- R/export.R | 8 +++--- R/fmt.R | 31 ++++++++++----------- R/format_data.R | 13 ++++----- R/helpers.R | 2 +- R/opts.R | 6 ++-- R/render_as_i_html.R | 6 ++-- R/tab_create_modify.R | 2 +- R/utils.R | 8 ++++-- R/utils_examples.R | 6 ++-- R/utils_general_str_formatting.R | 8 +++--- R/utils_plots.R | 28 ++++++++----------- R/utils_render_grid.R | 8 +++--- R/utils_render_html.R | 4 +-- R/utils_render_latex.R | 16 +++++------ R/utils_render_rtf.R | 6 ++-- R/utils_render_xml.R | 6 ++-- R/utils_units.R | 13 ++------- R/z_utils_render_footnotes.R | 6 ++-- tests/performance-monitoring/dplyr-full.qmd | 8 +++--- tests/performance-monitoring/dplyr.qmd | 4 +-- tests/testthat/test-as_word.R | 16 +++++------ tests/testthat/test-color_handling.R | 2 +- tests/testthat/test-data_color.R | 4 +-- tests/testthat/test-fmt_auto.R | 2 +- tests/testthat/test-summary_rows.R | 8 +++--- tests/testthat/test-tab_footnote.R | 4 +-- tests/testthat/test-tab_style_body.R | 2 +- vignettes/gt-datasets.Rmd | 2 +- 31 files changed, 116 insertions(+), 129 deletions(-) diff --git a/R/cols_align_decimal.R b/R/cols_align_decimal.R index ab7bd4ecb1..42a92c1efb 100644 --- a/R/cols_align_decimal.R +++ b/R/cols_align_decimal.R @@ -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) @@ -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] ) @@ -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") } diff --git a/R/cols_merge.R b/R/cols_merge.R index cee96b940f..695e075afd 100644 --- a/R/cols_merge.R +++ b/R/cols_merge.R @@ -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 diff --git a/R/compile_scss.R b/R/compile_scss.R index c94963792c..28db93b947 100644 --- a/R/compile_scss.R +++ b/R/compile_scss.R @@ -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) } ) diff --git a/R/export.R b/R/export.R index 0f540c92ab..ef5a7f2158 100644 --- a/R/export.R +++ b/R/export.R @@ -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") @@ -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") @@ -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) @@ -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 diff --git a/R/fmt.R b/R/fmt.R index 9d75c00de9..3664764e81 100644 --- a/R/fmt.R +++ b/R/fmt.R @@ -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) @@ -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 diff --git a/R/format_data.R b/R/format_data.R index b186e2793e..2cbc6fcb4f 100644 --- a/R/format_data.R +++ b/R/format_data.R @@ -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) { @@ -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("^$", label_str) && - !grepl("^$", label_str) && + !startsWith(label_str, "\\s*<")) diff --git a/R/helpers.R b/R/helpers.R index 433322350e..0f349a610e 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -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." diff --git a/R/opts.R b/R/opts.R index 4faf16f15f..449d7d8a57 100644 --- a/R/opts.R +++ b/R/opts.R @@ -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) diff --git a/R/render_as_i_html.R b/R/render_as_i_html.R index 19c07d3a98..ca711f43dc 100644 --- a/R/render_as_i_html.R +++ b/R/render_as_i_html.R @@ -181,7 +181,7 @@ render_as_ihtml <- function(data, id) { column_labels_border_bottom_width <- opt_val(data = data, option = "column_labels_border_bottom_width") 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") # Apply stub font weight to stub_font_weight <- opt_val(data = data, option = "stub_font_weight") @@ -194,8 +194,8 @@ render_as_ihtml <- function(data, id) { column_labels_font_weight <- opt_val(data = data, option = "column_labels_font_weight") # Apply font weight to groupname_col title - row_group_font_weight = opt_val(data = data, "row_group_font_weight") - table_body_font_weight = opt_val(data = data, "table_font_weight") + row_group_font_weight <- opt_val(data = data, "row_group_font_weight") + table_body_font_weight <- opt_val(data = data, "table_font_weight") # for row names + summary label stub_font_weight <- opt_val(data = data, "stub_font_weight") # #1693 table font size diff --git a/R/tab_create_modify.R b/R/tab_create_modify.R index 55a27e4dd1..de5dfccbd8 100644 --- a/R/tab_create_modify.R +++ b/R/tab_create_modify.R @@ -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) diff --git a/R/utils.R b/R/utils.R index f36fa57ab9..2b0c6155fc 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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] diff --git a/R/utils_examples.R b/R/utils_examples.R index e0673f3cf2..b93df84a16 100644 --- a/R/utils_examples.R +++ b/R/utils_examples.R @@ -91,9 +91,9 @@ get_example_text <- function(topic) { example_lines <- gsub(".*preformatted.(.*)", "```{r}\n\\1", example_lines) example_lines <- gsub("}\\if{html}{\\out{}}", "```", example_lines, fixed = TRUE) - example_lines <- example_lines[!grepl("^}|", example_lines)] + example_lines <- grep("^}|", example_lines, value = TRUE, invert = TRUE) example_lines <- gsub("\\%", "%", example_lines, fixed = TRUE) example_lines <- gsub("\\{", "{", example_lines, fixed = TRUE) diff --git a/R/utils_general_str_formatting.R b/R/utils_general_str_formatting.R index 07d104c0b0..ffee017176 100644 --- a/R/utils_general_str_formatting.R +++ b/R/utils_general_str_formatting.R @@ -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) @@ -326,7 +326,7 @@ is_adjacent_separate <- function(group_1, group_2) { return(FALSE) } - return(TRUE) + TRUE } str_catalog <- function( @@ -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 = " " ) } diff --git a/R/utils_plots.R b/R/utils_plots.R index bf6aa28f21..f79fd1dd99 100644 --- a/R/utils_plots.R +++ b/R/utils_plots.R @@ -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 } diff --git a/R/utils_render_grid.R b/R/utils_render_grid.R index fd4f5aa3e5..f3cf86bd13 100644 --- a/R/utils_render_grid.R +++ b/R/utils_render_grid.R @@ -948,14 +948,14 @@ render_grid_svg <- function(label, style, margin) { width <- height <- NULL # Try if any height is declared in style attribute - if (any(grepl("^height:", svg_style))) { + if (any(startsWith(svg_style, "height:"))) { height <- gsub("^height:", "", svg_style[grep("^height:", svg_style)]) %>% parse_fontsize(style$text_gp$fontsize) %>% grid::unit(.grid_unit) } # Try if any width is declared in style attribute - if (any(grepl("^width:", svg_style))) { + if (any(startsWith(svg_style, "width:"))) { width <- gsub("^width:", "", svg_style[grep("^width:", svg_style)]) %>% parse_fontsize(style$text_gp$fontsize) %>% grid::unit(.grid_unit) @@ -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.") } @@ -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 <- grep("^gt_", names(classes), value = TRUE) # There are two entries for gt_table that we merge here is_table <- which(names(classes) == "gt_table") diff --git a/R/utils_render_html.R b/R/utils_render_html.R index fbab1cfcc5..133c4c7500 100644 --- a/R/utils_render_html.R +++ b/R/utils_render_html.R @@ -51,7 +51,7 @@ footnote_mark_to_html <- function( sup_class <- "gt_footnote_marks gt_asterisk" } - is_sup <- grepl("\\^", spec) + is_sup <- startsWith(spec, "^") if (grepl(".", spec, fixed = TRUE)) mark <- paste0(mark, ".") if (grepl("(", spec, fixed = TRUE)) mark <- paste0("(", mark) @@ -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." diff --git a/R/utils_render_latex.R b/R/utils_render_latex.R index 1cf36e013e..42ea41f078 100644 --- a/R/utils_render_latex.R +++ b/R/utils_render_latex.R @@ -57,7 +57,7 @@ footnote_mark_to_latex <- function( if (grepl("\\(|\\[", spec)) mark <- sprintf_unless_na("(%s", mark) if (grepl("\\)|\\]", spec)) mark <- sprintf_unless_na("%s)", mark) - if (grepl("\\^", spec)) { + if (startsWith(spec, "^")) { mark <- sprintf_unless_na("\\textsuperscript{%s}", mark) } @@ -131,12 +131,12 @@ latex_group_row <- function( #' @noRd create_wrap_start_l <- function(data) { - tbl_pos = ifelse(check_quarto(), - "", - paste0("[", - dt_options_get_value(data = data, - option = "latex_tbl_pos"), - "]")) + tbl_pos <- ifelse(check_quarto(), + "", + paste0("[", + dt_options_get_value(data = data, + option = "latex_tbl_pos"), + "]")) ifelse(dt_options_get_value(data = data, option = "latex_use_longtable"), "\\begingroup\n", @@ -587,7 +587,7 @@ create_body_component_l <- function(data, colwidth_df) { } # Insert indentation where necessary - if (has_stub_column && any(!is.na(stub_df$indent))) { + if (has_stub_column && !all(is.na(stub_df$indent))) { stub_indent_length <- dt_options_get_value( diff --git a/R/utils_render_rtf.R b/R/utils_render_rtf.R index e4b769936d..871bc37144 100644 --- a/R/utils_render_rtf.R +++ b/R/utils_render_rtf.R @@ -807,7 +807,7 @@ footnote_mark_to_rtf <- function( rtf_raw( paste0( "{", - if (grepl("\\^", spec)) "\\super " else NULL, + if (startsWith(spec, "^")) "\\super " else NULL, if (grepl("i", spec, fixed = TRUE)) "\\i " else NULL, if (grepl("b", spec, fixed = TRUE)) "\\b " else NULL ) @@ -1026,7 +1026,7 @@ create_heading_component_rtf <- function(data) { unlist() if (is.null(col_widths)) { - n_cols <- nrow(dplyr::filter(boxh, type %in% c("default", "stub"))) + n_cols <- sum(boxh$type %in% c("default", "stub")) col_widths <- rep("100%", n_cols) } @@ -1814,7 +1814,7 @@ create_footer_component_rtf <- function(data) { if (is.null(col_widths)) { - n_cols <- nrow(dplyr::filter(boxh, type %in% c("default", "stub"))) + n_cols <- sum(boxh$type %in% c("default", "stub")) col_widths <- rep("100%", n_cols) } diff --git a/R/utils_render_xml.R b/R/utils_render_xml.R index 90c03da9c9..05edf29016 100644 --- a/R/utils_render_xml.R +++ b/R/utils_render_xml.R @@ -755,7 +755,7 @@ footnote_mark_to_xml <- function( xml_r( xml_rPr( xml_baseline_adj( - v_align = if (grepl("\\^", spec)) "superscript" else "baseline" + v_align = if (startsWith(spec, "^")) "superscript" else "baseline" ), if (grepl("i", spec, fixed = TRUE)) xml_i(active = TRUE) else NULL, if (grepl("b", spec, fixed = TRUE)) xml_b(active = TRUE) else NULL @@ -2609,7 +2609,7 @@ process_cell_content_ooxml_t <- function( # If it's already set to preserve, respect preservation - if (!text_tag_attr == "preserve") { + if (text_tag_attr != "preserve") { xml_text(txt) <- white_space_in_text(x = text_tag_content, whitespace = whitespace) xml_attr(txt, attr = "xml:space") <- white_space_to_t_xml_space(whitespace) } @@ -2792,7 +2792,7 @@ process_white_space_br_in_xml <- function(x, ..., whitespace = NULL) { break_tag <- paragraph_children[[break_tag_loc]] ## if the br is between two runs, replace with space - if (any(run_tags_locs > break_tag_loc) & any(run_tags_locs < break_tag_loc)) { + if (any(run_tags_locs > break_tag_loc) && any(run_tags_locs < break_tag_loc)) { replacement_br <- xml_r(xml_rPr(), xml_t(" ", xml_space = "preserve")) diff --git a/R/utils_units.R b/R/utils_units.R index 9609c0cd6a..bc14ca5e4f 100644 --- a/R/utils_units.R +++ b/R/utils_units.R @@ -129,7 +129,7 @@ define_units <- function(units_notation, is_chemical_formula = FALSE) { } # Conversion to subscripted, italicized 'x' - if (grepl("_x$", x)) { + if (endsWith(x, "_x")) { x <- gsub("^(.*)([a-zA-Z])_x$", "\\1\\2_*x*", x) } @@ -229,7 +229,7 @@ define_units <- function(units_notation, is_chemical_formula = FALSE) { FUN.VALUE = character(1L), USE.NAMES = FALSE, FUN = function(x) { - if (grepl("^/", x) && nchar(x) > 1) { + if (grepl("^/.", x)) { x <- gsub("^/", "", x) x <- paste0(x, "^-1") } @@ -248,14 +248,7 @@ define_units <- function(units_notation, is_chemical_formula = FALSE) { chemical_formula <- FALSE exponent <- NULL - if ( - is_chemical_formula || - ( - grepl("^%", tokens_vec_i) && - grepl("%$", tokens_vec_i) && - nchar(tokens_vec_i) > 2 - ) - ) { + if (is_chemical_formula || grepl("^%.+%$", tokens_vec_i)) { # Case where the unit is marked as a chemical formula chemical_formula <- TRUE diff --git a/R/z_utils_render_footnotes.R b/R/z_utils_render_footnotes.R index edb67a8056..87c132b548 100644 --- a/R/z_utils_render_footnotes.R +++ b/R/z_utils_render_footnotes.R @@ -416,7 +416,7 @@ set_footnote_marks_columns <- function(data, context = "html") { dplyr::ungroup() %>% dplyr::distinct(grpname, fs_id_coalesced) - for (i in seq(nrow(footnotes_columns_group_marks))) { + for (i in seq_len(nrow(footnotes_columns_group_marks))) { spanners <- dt_spanners_get(data = data) @@ -603,7 +603,7 @@ apply_footnotes_to_output <- function(data, context = "html") { # Footnote placement on the right of the cell text - if (context == "html" && grepl("

\n$", text)) { + if (context == "html" && endsWith(text, "

\n")) { text <- paste0( @@ -622,7 +622,7 @@ apply_footnotes_to_output <- function(data, context = "html") { # non-breaking space (added here as Unicode's 'NO-BREAK SPACE', # "U+00A0") separates the marks from the text content - if (context == "html" && grepl("^

", text)) { + if (context == "html" && startsWith(text, "

")) { text <- paste0( diff --git a/tests/performance-monitoring/dplyr-full.qmd b/tests/performance-monitoring/dplyr-full.qmd index 6c49f51b10..80fc5dc67a 100644 --- a/tests/performance-monitoring/dplyr-full.qmd +++ b/tests/performance-monitoring/dplyr-full.qmd @@ -127,7 +127,7 @@ format_pvalue <- function(p) glue::glue("{scales::pvalue(p)} {gtools::stars.pval get_cell_dim <- function(x) { dim <- NULL if (is.list(x) || is.vector(x)) dim <- length(x) - if (length(dim(x) > 1)) dim <- glue::glue('{dim(x)[1]} x {dim(x)[2]}') + if (length(dim(x)) > 1) dim <- glue::glue('{dim(x)[1]} x {dim(x)[2]}') return(dim) } @@ -396,7 +396,7 @@ IRIS <- as.data.table(iris)[, Species := as.character(Species)] ```{r} -mtcars |> filter(cyl >= 6 & disp < 180) +mtcars |> filter(cyl >= 6, disp < 180) MT[cyl >= 6 & disp < 180] ``` @@ -930,7 +930,7 @@ copy(MT)[, GRP := .GRP, keyby = cyl][] ```{r} mtcars |> arrange(gear) |> group_by(gear) |> mutate(I_GRP = row_number()) -copy(MT)[, I_GRP := 1:.N, keyby = gear][] +copy(MT)[, I_GRP := seq_len(.N), keyby = gear][] ``` @@ -1020,7 +1020,7 @@ MT[, .N, by = .(cyl > 6)] **Group > filter > summarize** ```{r} -mtcars |> filter(cyl >= 6 & disp >= 200) |> summarize(N = n()) +mtcars |> filter(cyl >= 6, disp >= 200) |> summarize(N = n()) MT[cyl >= 6 & disp >= 200, .(.N)] ``` diff --git a/tests/performance-monitoring/dplyr.qmd b/tests/performance-monitoring/dplyr.qmd index 8ddb480e8a..82ba65c86a 100644 --- a/tests/performance-monitoring/dplyr.qmd +++ b/tests/performance-monitoring/dplyr.qmd @@ -129,7 +129,7 @@ format_pvalue <- function(p) glue::glue("{scales::pvalue(p)} {gtools::stars.pval get_cell_dim <- function(x) { dim <- NULL if (is.list(x) || is.vector(x)) dim <- length(x) - if (length(dim(x) > 1)) dim <- glue::glue('{dim(x)[1]} x {dim(x)[2]}') + if (length(dim(x)) > 1) dim <- glue::glue('{dim(x)[1]} x {dim(x)[2]}') return(dim) } @@ -408,7 +408,7 @@ IRIS <- as.data.table(iris)[, Species := as.character(Species)] ```{r} -mtcars |> filter(cyl >= 6 & disp < 180) +mtcars |> filter(cyl >= 6, disp < 180) MT[cyl >= 6 & disp < 180] ``` diff --git a/tests/testthat/test-as_word.R b/tests/testthat/test-as_word.R index f4ca7d2c02..b12b01479b 100644 --- a/tests/testthat/test-as_word.R +++ b/tests/testthat/test-as_word.R @@ -2323,8 +2323,8 @@ test_that("markdown with img refs work",{ skip_on_ci() check_suggests() - ref_png <- file.path(system.file(package = "gt"),"/graphics/test_image.png") - ref_svg <- file.path(system.file(package = "gt"),"/graphics/test_image.svg") + ref_png <- system.file("graphics", "test_image.png", package = "gt") + ref_svg <- system.file("graphics", "test_image.svg", package = "gt") temp_png <- file.path(tempdir(),"test_image.png") temp_svg <- file.path(tempdir(),"test_image.svg") @@ -2382,9 +2382,9 @@ test_that("table with image refs work - local only",{ skip_on_ci() check_suggests() - ref_png <- file.path(system.file(package = "gt"),"/graphics/test_image.png") - ref_svg <- file.path(system.file(package = "gt"),"/graphics/test_image.svg") - ref_wide_svg <- file.path(system.file(package = "gt"),"/graphics/gt_parts_of_a_table.svg") + ref_png <- system.file("graphics", "test_image.png", package = "gt") + ref_svg <- system.file("graphics", "test_image.svg", package = "gt") + ref_wide_svg <- system.file("graphics", "gt_parts_of_a_table.svg", package = "gt") temp_png <- file.path(tempdir(),"test_image.png") temp_svg <- file.path(tempdir(),"test_image.svg") @@ -2513,9 +2513,9 @@ test_that("table with image refs work - local only - setting image widths and he skip_on_ci() check_suggests() - ref_png <- file.path(system.file(package = "gt"),"/graphics/test_image.png") - ref_svg <- file.path(system.file(package = "gt"),"/graphics/test_image.svg") - ref_wide_svg <- file.path(system.file(package = "gt"),"/graphics/gt_parts_of_a_table.svg") + ref_png <- system.file("graphics", "test_image.png", package = "gt") + ref_svg <- system.file("graphics", "test_image.svg", package = "gt") + ref_wide_svg <- system.file("graphics", "gt_parts_of_a_table.svg", package = "gt") temp_png <- file.path(tempdir(),"test_image.png") temp_svg <- file.path(tempdir(),"test_image.svg") diff --git a/tests/testthat/test-color_handling.R b/tests/testthat/test-color_handling.R index 961eea3c96..f6cff83e41 100644 --- a/tests/testthat/test-color_handling.R +++ b/tests/testthat/test-color_handling.R @@ -1,7 +1,7 @@ # Create a table that can be used for testing test_tbl <- sza %>% - dplyr::filter(latitude == 50 & !is.na(sza)) %>% + dplyr::filter(latitude == 50, !is.na(sza)) %>% dplyr::group_by(month) %>% dplyr::summarize(min_sza = min(sza)) diff --git a/tests/testthat/test-data_color.R b/tests/testthat/test-data_color.R index 2abaaae008..16ba8cda92 100644 --- a/tests/testthat/test-data_color.R +++ b/tests/testthat/test-data_color.R @@ -1,7 +1,7 @@ # Create a table that can be used for testing test_tbl <- sza %>% - dplyr::filter(latitude == 50 & !is.na(sza)) %>% + dplyr::filter(latitude == 50, !is.na(sza)) %>% dplyr::group_by(month) %>% dplyr::summarize(min_sza = min(sza)) @@ -904,7 +904,7 @@ test_that("The direction of coloring can be column-wise or row-wise", { # Generate a gt table based on the `sza` dataset sza_gt_tbl <- sza %>% - dplyr::filter(latitude == 20 & tst <= "1200") %>% + dplyr::filter(latitude == 20, tst <= "1200") %>% dplyr::select(-latitude) %>% dplyr::filter(!is.na(sza)) %>% tidyr::spread(key = "tst", value = sza) %>% diff --git a/tests/testthat/test-fmt_auto.R b/tests/testthat/test-fmt_auto.R index 4b52c96f97..de5b00fab9 100644 --- a/tests/testthat/test-fmt_auto.R +++ b/tests/testthat/test-fmt_auto.R @@ -18,7 +18,7 @@ test_that("fmt_auto() works correctly", { sp500_tbl <- sp500 %>% - dplyr::filter(date >= "2015-01-05" & date <= "2015-01-16") %>% + dplyr::filter(date >= "2015-01-05", date <= "2015-01-16") %>% dplyr::arrange(date) %>% dplyr::mutate(week = paste0("W", strftime(date, format = "%V"))) diff --git a/tests/testthat/test-summary_rows.R b/tests/testthat/test-summary_rows.R index 8dca0a0fda..b1db716a37 100644 --- a/tests/testthat/test-summary_rows.R +++ b/tests/testthat/test-summary_rows.R @@ -3,8 +3,8 @@ tbl <- sp500 %>% dplyr::filter( - date >= "2015-01-05" & - date <= "2015-01-16" + date >= "2015-01-05", + date <= "2015-01-16" ) %>% dplyr::arrange(date) %>% dplyr::mutate( @@ -1068,7 +1068,7 @@ test_that("Summary rows can be created when there is no stub", { # four columns of values tbl_2 <- sp500 %>% - dplyr::filter(date >= "2015-01-05" & date <= "2015-01-09") %>% + dplyr::filter(date >= "2015-01-05", date <= "2015-01-09") %>% dplyr::arrange(date) %>% dplyr::select(-adj_close, -volume) %>% gt() @@ -1886,7 +1886,7 @@ test_that("Creating summary rows works for hidden columns", { # 'low' columns tbl <- sp500 %>% - dplyr::filter(date >= "2015-01-05" & date <= "2015-01-16") %>% + dplyr::filter(date >= "2015-01-05", date <= "2015-01-16") %>% dplyr::arrange(date) %>% dplyr::mutate( week = paste0( diff --git a/tests/testthat/test-tab_footnote.R b/tests/testthat/test-tab_footnote.R index 657fee30c8..7a6c9419f1 100644 --- a/tests/testthat/test-tab_footnote.R +++ b/tests/testthat/test-tab_footnote.R @@ -136,8 +136,8 @@ data_3 <- data_4 <- sp500 %>% dplyr::filter( - date >= "2015-01-05" & - date <= "2015-01-10" + date >= "2015-01-05", + date <= "2015-01-10" ) %>% dplyr::select(-c(adj_close, volume, high, low)) %>% gt() %>% diff --git a/tests/testthat/test-tab_style_body.R b/tests/testthat/test-tab_style_body.R index 1ea6c3c473..c44220d6de 100644 --- a/tests/testthat/test-tab_style_body.R +++ b/tests/testthat/test-tab_style_body.R @@ -3,7 +3,7 @@ test_that("tab_style_body() works correctly", { # Generate a table that will be used in checks of body styling gt_tbl <- sp500 %>% - dplyr::filter(date >= "2015-01-05" & date <= "2015-01-16") %>% + dplyr::filter(date >= "2015-01-05", date <= "2015-01-16") %>% dplyr::arrange(date) %>% dplyr::mutate(week = paste0("W", strftime(date, format = "%V"))) %>% dplyr::select(-adj_close, -volume) %>% diff --git a/vignettes/gt-datasets.Rmd b/vignettes/gt-datasets.Rmd index 5dfcb9cb62..6625f01344 100644 --- a/vignettes/gt-datasets.Rmd +++ b/vignettes/gt-datasets.Rmd @@ -323,7 +323,7 @@ down_arrow <- "" # Create a gt table based on a preprocessed `sp500` sp500 |> - filter(date >= start_date & date <= end_date) |> + filter(date >= start_date, date <= end_date) |> select(-adj_close) |> gt() |> tab_header( From ee92923c549d2b9265eb022880bb41dccfd4a289 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 24 Jul 2024 09:52:40 -0700 Subject: [PATCH 2/8] revert Co-authored-by: olivroy <52606734+olivroy@users.noreply.github.com> --- R/utils_render_grid.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils_render_grid.R b/R/utils_render_grid.R index f3cf86bd13..123916f1d3 100644 --- a/R/utils_render_grid.R +++ b/R/utils_render_grid.R @@ -955,7 +955,7 @@ render_grid_svg <- function(label, style, margin) { } # Try if any width is declared in style attribute - if (any(startsWith(svg_style, "width:"))) { + if (any(grepl("^width:", svg_style))) { width <- gsub("^width:", "", svg_style[grep("^width:", svg_style)]) %>% parse_fontsize(style$text_gp$fontsize) %>% grid::unit(.grid_unit) From 5a7b7e0605bd214d846a74ce10b1d5354ac22227 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 24 Jul 2024 09:52:47 -0700 Subject: [PATCH 3/8] revert Co-authored-by: olivroy <52606734+olivroy@users.noreply.github.com> --- R/utils_render_grid.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils_render_grid.R b/R/utils_render_grid.R index 123916f1d3..68e6ce8861 100644 --- a/R/utils_render_grid.R +++ b/R/utils_render_grid.R @@ -948,7 +948,7 @@ render_grid_svg <- function(label, style, margin) { width <- height <- NULL # Try if any height is declared in style attribute - if (any(startsWith(svg_style, "height:"))) { + if (any(grepl("^height:", svg_style))) { height <- gsub("^height:", "", svg_style[grep("^height:", svg_style)]) %>% parse_fontsize(style$text_gp$fontsize) %>% grid::unit(.grid_unit) From 0aeb6dd03205cf7071b3258a3e189203c33c19bf Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 24 Jul 2024 10:31:27 -0700 Subject: [PATCH 4/8] correct fix for lint->startsWith() Co-authored-by: olivroy <52606734+olivroy@users.noreply.github.com> --- R/utils_render_grid.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils_render_grid.R b/R/utils_render_grid.R index 68e6ce8861..0fde9c0e52 100644 --- a/R/utils_render_grid.R +++ b/R/utils_render_grid.R @@ -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 <- grep("^gt_", names(classes), value = TRUE) + classes <- classes[startsWith(names(classes), "gt_")] # There are two entries for gt_table that we merge here is_table <- which(names(classes) == "gt_table") From 8c533e075bd038eb9a0ab4f2f441f8a2a9b2bd30 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 24 Jul 2024 10:32:26 -0700 Subject: [PATCH 5/8] correct fix for ^ regex Co-authored-by: olivroy <52606734+olivroy@users.noreply.github.com> --- R/utils_render_html.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils_render_html.R b/R/utils_render_html.R index 133c4c7500..9f72f799bc 100644 --- a/R/utils_render_html.R +++ b/R/utils_render_html.R @@ -51,7 +51,7 @@ footnote_mark_to_html <- function( sup_class <- "gt_footnote_marks gt_asterisk" } - is_sup <- startsWith(spec, "^") + is_sup <- grepl("^", spec, fixed = TRUE) if (grepl(".", spec, fixed = TRUE)) mark <- paste0(mark, ".") if (grepl("(", spec, fixed = TRUE)) mark <- paste0("(", mark) From 6c50a30198657479f57bc2b42ea1f948f512cafd Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 24 Jul 2024 10:32:40 -0700 Subject: [PATCH 6/8] correct fix for ^ regex Co-authored-by: olivroy <52606734+olivroy@users.noreply.github.com> --- R/utils_render_latex.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils_render_latex.R b/R/utils_render_latex.R index 42ea41f078..b39e87388b 100644 --- a/R/utils_render_latex.R +++ b/R/utils_render_latex.R @@ -57,7 +57,7 @@ footnote_mark_to_latex <- function( if (grepl("\\(|\\[", spec)) mark <- sprintf_unless_na("(%s", mark) if (grepl("\\)|\\]", spec)) mark <- sprintf_unless_na("%s)", mark) - if (startsWith(spec, "^")) { + if (grepl("^", spec, fixed = TRUE)) { mark <- sprintf_unless_na("\\textsuperscript{%s}", mark) } From 1cc7f41133031b5464b6848f6bcd3d69ea7e6871 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 24 Jul 2024 10:32:51 -0700 Subject: [PATCH 7/8] correct fix for ^ regex Co-authored-by: olivroy <52606734+olivroy@users.noreply.github.com> --- R/utils_render_rtf.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils_render_rtf.R b/R/utils_render_rtf.R index 871bc37144..ab4392ce1a 100644 --- a/R/utils_render_rtf.R +++ b/R/utils_render_rtf.R @@ -807,7 +807,7 @@ footnote_mark_to_rtf <- function( rtf_raw( paste0( "{", - if (startsWith(spec, "^")) "\\super " else NULL, + if (grepl("^", spec, fixed = TRUE)) "\\super " else NULL, if (grepl("i", spec, fixed = TRUE)) "\\i " else NULL, if (grepl("b", spec, fixed = TRUE)) "\\b " else NULL ) From 582b3a71fda1b9e125be476c19a0dc83750b7cbe Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 24 Jul 2024 10:33:13 -0700 Subject: [PATCH 8/8] correct fix for ^ regex Co-authored-by: olivroy <52606734+olivroy@users.noreply.github.com> --- R/utils_render_xml.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils_render_xml.R b/R/utils_render_xml.R index 05edf29016..b9130a54ca 100644 --- a/R/utils_render_xml.R +++ b/R/utils_render_xml.R @@ -755,7 +755,7 @@ footnote_mark_to_xml <- function( xml_r( xml_rPr( xml_baseline_adj( - v_align = if (startsWith(spec, "^")) "superscript" else "baseline" + v_align = if (grepl("^", spec, fixed = TRUE)) "superscript" else "baseline" ), if (grepl("i", spec, fixed = TRUE)) xml_i(active = TRUE) else NULL, if (grepl("b", spec, fixed = TRUE)) xml_b(active = TRUE) else NULL