diff --git a/R/export.R b/R/export.R index 43ebe3ecc..1743f413f 100644 --- a/R/export.R +++ b/R/export.R @@ -128,21 +128,23 @@ gtsave <- function(data, # Use the appropriate save function based # on the filename extension - switch(file_ext, - "htm" = , - "html" = gt_save_html(data, filename, path, ...), - "ltx" = , # We don't verbally support using `ltx` - "rnw" = , - "tex" = gt_save_latex(data, filename, path, ...), - "rtf" = gt_save_rtf(data, filename, path, ...), - "png" = , - "pdf" = gt_save_webshot(data, filename, path, ...), - { - stop("The file extension used (`.", file_ext, "`) doesn't have an ", - "associated saving function. ", - ext_supported_text, - call. = FALSE) - } + switch( + file_ext, + "htm" = , + "html" = gt_save_html(data = data, filename, path, ...), + "ltx" = , # We don't verbally support using `ltx` + "rnw" = , + "tex" = gt_save_latex(data = data, filename, path, ...), + "rtf" = gt_save_rtf(data = data, filename, path, ...), + "png" = , + "pdf" = gt_save_webshot(data = data, filename, path, ...), + { + stop( + "The file extension used (`.", file_ext, "`) doesn't have an ", + "associated saving function. ", ext_supported_text, + call. = FALSE + ) + } ) } @@ -336,11 +338,12 @@ as_raw_html <- function(data, # Create inline styles html_table <- - html_table %>% - inline_html_styles(css_tbl = get_css_tbl(data)) + inline_html_styles( + html = html_table, + css_tbl = get_css_tbl(data = data) + ) } else { - html_table <- as.character(as.tags.gt_tbl(data)) } @@ -394,7 +397,7 @@ as_latex <- function(data) { stop_if_not_gt(data = data) # Build all table data objects through a common pipeline - data <- data %>% build_data(context = "latex") + data <- build_data(data = data, context = "latex") # Composition of LaTeX ---------------------------------------------------- @@ -423,10 +426,7 @@ as_latex <- function(data) { # `latex_dependency()` function to load latex packages # without requiring the user to do so if (requireNamespace("rmarkdown", quietly = TRUE)) { - - latex_packages <- - lapply(latex_packages(), rmarkdown::latex_dependency) - + latex_packages <- lapply(latex_packages(), rmarkdown::latex_dependency) } else { latex_packages <- NULL } @@ -486,7 +486,7 @@ as_rtf <- function(data, stop_if_not_gt(data = data) # Build all table data objects through a common pipeline - data <- data %>% build_data(context = "rtf") + data <- build_data(data = data, context = "rtf") # Composition of RTF ------------------------------------------------------ @@ -507,24 +507,25 @@ as_rtf <- function(data, # Compose the RTF table rtf_table <- - rtf_file( - document = { - rtf_table( - rows = c( - heading_component, - columns_component, - body_component, - footnotes_component, - source_notes_component + as_rtf_string( + rtf_file( + document = { + rtf_table( + rows = c( + heading_component, + columns_component, + body_component, + footnotes_component, + source_notes_component + ) ) - ) - }, - page_numbering = page_numbering - ) %>% - as_rtf_string() + }, + page_numbering = page_numbering + ) + ) if (isTRUE(getOption('knitr.in.progress'))) { - rtf_table <- rtf_table %>% knitr::raw_output() + rtf_table <- knitr::raw_output(rtf_table) } rtf_table @@ -603,9 +604,12 @@ extract_summary <- function(data) { # Stop function if there are no # directives to create summary rows if (!dt_summary_exists(data = data)) { - stop("There is no summary list to extract.\n", - "Use the `summary_rows()` function to generate summaries.", - call. = FALSE) + + stop( + "There is no summary list to extract.\n", + "* Use the `summary_rows()` function to generate summaries.", + call. = FALSE + ) } # Build the `data` using the standard @@ -614,5 +618,5 @@ extract_summary <- function(data) { # Extract the list of summary data frames # that contains tidy, unformatted data - dt_summary_df_data_get(data = built_data) %>% as.list() + as.list(dt_summary_df_data_get(data = built_data)) } diff --git a/R/format_data.R b/R/format_data.R index 2be5a2790..03526fc22 100644 --- a/R/format_data.R +++ b/R/format_data.R @@ -180,6 +180,7 @@ fmt_number <- function(data, suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by) # Stop function if any columns have data that is incompatible + # with this formatter if ( !column_classes_are_valid( data = data, @@ -458,9 +459,17 @@ fmt_scientific <- function(data, # Stop function if any columns have data that is incompatible # with this formatter - if (!column_classes_are_valid(data, {{ columns }}, valid_classes = c("numeric", "integer"))) { - stop("The `fmt_scientific()` function can only be used on `columns` with numeric data", - call. = FALSE) + if ( + !column_classes_are_valid( + data = data, + columns = {{ columns }}, + valid_classes = c("numeric", "integer") + ) + ) { + stop( + "The `fmt_scientific()` function can only be used on `columns` with numeric data.", + call. = FALSE + ) } # Pass `data`, `columns`, `rows`, and the formatting @@ -943,9 +952,17 @@ fmt_percent <- function(data, # Stop function if any columns have data that is incompatible # with this formatter - if (!column_classes_are_valid(data, {{ columns }}, valid_classes = c("numeric", "integer"))) { - stop("The `fmt_percent()` function can only be used on `columns` with numeric data", - call. = FALSE) + if ( + !column_classes_are_valid( + data = data, + columns = {{ columns }}, + valid_classes = c("numeric", "integer") + ) + ) { + stop( + "The `fmt_percent()` function can only be used on `columns` with numeric data.", + call. = FALSE + ) } if (scale_values) { @@ -954,6 +971,7 @@ fmt_percent <- function(data, scale_by <- 1.0 } + # Pass `data`, `columns`, `rows`, and other options to `fmt_symbol()` fmt_symbol( data = data, columns = {{ columns }}, @@ -1109,9 +1127,17 @@ fmt_currency <- function(data, # Stop function if any columns have data that is incompatible # with this formatter - if (!column_classes_are_valid(data, {{ columns }}, valid_classes = c("numeric", "integer"))) { - stop("The `fmt_currency()` function can only be used on `columns` with numeric data", - call. = FALSE) + if ( + !column_classes_are_valid( + data = data, + columns = {{ columns }}, + valid_classes = c("numeric", "integer") + ) + ) { + stop( + "The `fmt_currency()` function can only be used on `columns` with numeric data.", + call. = FALSE + ) } # Stop function if `currency` does not have a valid value @@ -1125,6 +1151,7 @@ fmt_currency <- function(data, use_subunits = use_subunits ) + # Pass `data`, `columns`, `rows`, and other options to `fmt_symbol()` fmt_symbol( data = data, columns = {{ columns }}, @@ -1267,6 +1294,8 @@ fmt_bytes <- function(data, byte_units <- c("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB") } + # Pass `data`, `columns`, `rows`, and the formatting + # functions as a function list to `fmt()` fmt( data = data, columns = {{ columns }}, @@ -1416,9 +1445,17 @@ fmt_date <- function(data, # Stop function if any columns have data that is incompatible # with this formatter - if (!column_classes_are_valid(data, {{ columns }}, valid_classes = c("Date", "character"))) { - stop("The `fmt_date()` function can only be used on `columns` with `character` or `Date` values", - call. = FALSE) + if ( + !column_classes_are_valid( + data = data, + columns = {{ columns }}, + valid_classes = c("Date", "character") + ) + ) { + stop( + "The `fmt_date()` function can only be used on `columns` with `character` or `Date` values.", + call. = FALSE + ) } # Pass `data`, `columns`, `rows`, and the formatting @@ -1547,9 +1584,16 @@ fmt_time <- function(data, # Stop function if any columns have data that is incompatible # with this formatter - if (!column_classes_are_valid(data, {{ columns }}, valid_classes = "character")) { - stop("The `fmt_date()` function can only be used on `columns` with `character` values", - call. = FALSE) + if ( + !column_classes_are_valid( + data = data, + columns = {{ columns }}, + valid_classes = "character") + ) { + stop( + "The `fmt_date()` function can only be used on `columns` with `character` values.", + call. = FALSE + ) } # Pass `data`, `columns`, `rows`, and the formatting @@ -1671,9 +1715,16 @@ fmt_datetime <- function(data, # Stop function if any columns have data that is incompatible # with this formatter - if (!column_classes_are_valid(data, {{ columns }}, valid_classes = "character")) { - stop("The `fmt_datetime()` function can only be used on `columns` with `character` values", - call. = FALSE) + if ( + !column_classes_are_valid( + data = data, + columns = {{ columns }}, + valid_classes = "character" + )) { + stop( + "The `fmt_datetime()` function can only be used on `columns` with `character` values.", + call. = FALSE + ) } # Pass `data`, `columns`, `rows`, and the formatting @@ -2082,6 +2133,7 @@ fmt_missing <- function(data, #' **gt**. Along with the `columns` and `rows` arguments that provide some #' precision in targeting data cells, the `fns` argument allows you to define #' one or more functions for manipulating the raw data. +#' #' If providing a single function to `fns`, the recommended format is in the #' form: `fns = function(x) ...`. This single function will format the targeted #' data cells the same way regardless of the output format (e.g., HTML, LaTeX, diff --git a/R/modify_columns.R b/R/modify_columns.R index a0eae3510..b336830a6 100644 --- a/R/modify_columns.R +++ b/R/modify_columns.R @@ -102,6 +102,7 @@ cols_align <- function(data, } for (i in seq(column_names)) { + data <- dt_boxhead_edit( data = data, @@ -133,9 +134,9 @@ cols_align <- function(data, #' table and its container can be individually modified with the `table.width` #' and `container.width` arguments within [tab_options()]). #' -#' @inheritParams cols_align +#' @param .data A table object that is created using the [gt()] function. #' @param ... Expressions for the assignment of column widths for the table -#' columns in `data`. Two-sided formulas (e.g, ` ~ `) can be used, +#' columns in `.data`. Two-sided formulas (e.g, ` ~ `) can be used, #' where the left-hand side corresponds to selections of columns and the #' right-hand side evaluates to single-length character values in the form #' `{##}px` (i.e., pixel dimensions); the [px()] helper function is best used @@ -180,21 +181,23 @@ cols_align <- function(data, #' 4-2 #' #' @export -cols_width <- function(data, +cols_width <- function(.data, ..., .list = list2(...)) { # Perform input object validation - stop_if_not_gt(data = data) + stop_if_not_gt(data = .data) # Collect a named list of column widths widths_list <- .list - # If nothing is provided, return `data` unchanged + # If nothing is provided, return `.data` unchanged if (length(widths_list) == 0) { - stop("Nothing was provided to `...`:\n", - " * Use formula expressions to define custom column widths", - call. = FALSE) + stop( + "Nothing was provided to `...`:\n", + " * Use formula expressions to define custom column widths", + call. = FALSE + ) } all_formulas <- @@ -207,15 +210,17 @@ cols_width <- function(data, ) if (!all_formulas) { - stop("Only two-sided formulas should be provided to `...`", - call. = FALSE) + stop( + "Only two-sided formulas should be provided to `...`", + call. = FALSE + ) } columns_used <- NULL for (width_item in widths_list) { - cols <- width_item %>% rlang::f_lhs() + cols <- rlang::f_lhs(width_item) # The default use of `resolve_cols_c()` won't work here if there # is a table stub column (because we need to be able to set the @@ -224,7 +229,7 @@ cols_width <- function(data, columns <- resolve_cols_c( expr = !!cols, - data = data, + data = .data, excl_stub = FALSE ) %>% base::setdiff(columns_used) @@ -240,9 +245,10 @@ cols_width <- function(data, if (is.numeric(width)) width <- paste_right(as.character(width), "px") for (column in columns) { - data <- + + .data <- dt_boxhead_edit( - data = data, + data = .data, var = column, column_width = list(width) ) @@ -250,27 +256,27 @@ cols_width <- function(data, } unset_widths <- - dt_boxhead_get(data = data) %>% + dt_boxhead_get(data = .data) %>% .$column_width %>% lapply(is.null) %>% unlist() if (any(unset_widths)) { - columns_unset <- dt_boxhead_get_vars(data = data)[unset_widths] + columns_unset <- dt_boxhead_get_vars(data = .data)[unset_widths] for (column in columns_unset) { - data <- + .data <- dt_boxhead_edit( - data = data, + data = .data, var = column, column_width = list("") ) } } - data + .data } #' Relabel one or more columns @@ -296,8 +302,8 @@ cols_width <- function(data, #' `fmt*()` functions) even though we may lose distinguishability in column #' labels once they have been relabeled. #' -#' @inheritParams cols_align -#' @param ... One or more named arguments of column names from the input `data` +#' @param .data A table object that is created using the [gt()] function. +#' @param ... One or more named arguments of column names from the input `.data` #' table along with their labels for display as the column labels. We can #' optionally wrap the column labels with [md()] (to interpret text as #' Markdown) or [html()] (to interpret text as HTML). @@ -347,58 +353,67 @@ cols_width <- function(data, #' #' @import rlang #' @export -cols_label <- function(data, +cols_label <- function(.data, ..., .list = list2(...)) { - # Perform input object validation - stop_if_not_gt(data = data) - # Collect a named list of column labels labels_list <- .list + # Perform input object validation + stop_if_not_gt(data = .data) + # If nothing is provided, return `data` unchanged if (length(labels_list) == 0) { - return(data) + return(.data) } # Test for names being NULL if (is.null(names(labels_list))) { - stop("Named arguments are required for `cols_label()`.", call. = FALSE) + stop( + "Named arguments are required for `cols_label()`.", + call. = FALSE + ) } # Test for any missing names if (any(names(labels_list) == "")) { - stop("All arguments to `cols_label()` must be named.", call. = FALSE) + stop( + "All arguments to `cols_label()` must be named.", + call. = FALSE + ) } # Stop function if any of the column names specified are not in `cols_labels` - if (!all(names(labels_list) %in% dt_boxhead_get_vars(data = data))) { - stop("All column names provided must exist in the input `data` table.") + if (!all(names(labels_list) %in% dt_boxhead_get_vars(data = .data))) { + stop( + "All column names provided must exist in the input `.data` table.", + call. = FALSE + ) } # Filter the list of labels by the var names in `data` labels_list <- - labels_list[names(labels_list) %in% dt_boxhead_get_vars(data = data)] + labels_list[names(labels_list) %in% dt_boxhead_get_vars(data = .data)] # If no labels remain after filtering, return the data if (length(labels_list) == 0) { - return(data) + return(.data) } nm_labels_list <- names(labels_list) for (i in seq_along(labels_list)) { - data <- + .data <- dt_boxhead_edit_column_label( - data = data, + data = .data, var = nm_labels_list[i], column_label = labels_list[[i]] ) } - data + .data } #' Move one or more columns to the start @@ -480,13 +495,18 @@ cols_move_to_start <- function(data, # Stop function if no `columns` are provided if (length(columns) == 0) { - stop("Columns must be provided.", call. = FALSE) + stop( + "Columns must be provided.", + call. = FALSE + ) } # Stop function if any of the `columns` don't exist in `vars` if (!all(columns %in% vars)) { - stop("All `columns` must exist and be visible in the input `data` table.", - call. = FALSE) + stop( + "All `columns` must exist and be visible in the input `data` table.", + call. = FALSE + ) } # Get the remaining column names in the table @@ -494,9 +514,10 @@ cols_move_to_start <- function(data, new_vars <- append(other_columns, columns, after = 0) - data <- dt_boxhead_set_var_order(data = data, vars = new_vars) - - data + dt_boxhead_set_var_order( + data = data, + vars = new_vars + ) } #' Move one or more columns to the end @@ -577,13 +598,18 @@ cols_move_to_end <- function(data, # Stop function if no `columns` are provided if (length(columns) == 0) { - stop("Columns must be provided.", call. = FALSE) + stop( + "Columns must be provided.", + call. = FALSE + ) } # Stop function if any of the `columns` don't exist in `vars` if (!all(columns %in% vars)) { - stop("All `columns` must exist and be visible in the input `data` table.", - call. = FALSE) + stop( + "All `columns` must exist and be visible in the input `data` table.", + call. = FALSE + ) } # Get the remaining column names in the table @@ -591,9 +617,10 @@ cols_move_to_end <- function(data, new_vars <- append(other_columns, columns) - data <- dt_boxhead_set_var_order(data = data, vars = new_vars) - - data + dt_boxhead_set_var_order( + data = data, + vars = new_vars + ) } #' Move one or more columns @@ -673,25 +700,34 @@ cols_move <- function(data, # Stop function if `after` contains multiple columns if (length(after) > 1) { - stop("Only one column name should be supplied to `after`.", - call. = FALSE) + stop( + "Only one column name should be supplied to `after`.", + call. = FALSE + ) } # Stop function if `after` doesn't exist in `vars` if (!(after %in% vars)) { - stop("The column supplied to `after` doesn't exist in the input `data` table.", - call. = FALSE) + stop( + "The column supplied to `after` doesn't exist in the input `data` table.", + call. = FALSE + ) } # Stop function if no `columns` are provided if (length(columns) == 0) { - stop("Columns must be provided.", call. = FALSE) + stop( + "Columns must be provided.", + call. = FALSE + ) } # Stop function if any of the `columns` don't exist in `vars` if (!all(columns %in% vars)) { - stop("All `columns` must exist and be visible in the input `data` table.", - call. = FALSE) + stop( + "All `columns` must exist and be visible in the input `data` table.", + call. = FALSE + ) } # Get the remaining column names in the table @@ -704,9 +740,10 @@ cols_move <- function(data, new_vars <- append(other_columns, moving_columns, after = after_index) - data <- dt_boxhead_set_var_order(data = data, vars = new_vars) - - data + dt_boxhead_set_var_order( + data = data, + vars = new_vars + ) } #' Hide one or more columns @@ -801,19 +838,25 @@ cols_hide <- function(data, # Stop function if no `columns` are provided if (length(columns) == 0) { - stop("Columns must be provided.", call. = FALSE) + stop( + "Columns must be provided.", + call. = FALSE + ) } # Stop function if any of the `columns` don't exist in `vars` if (!all(columns %in% vars)) { - stop("All `columns` must exist in the input `data` table.", - call. = FALSE) + stop( + "All `columns` must exist in the input `data` table.", + call. = FALSE + ) } # Set the `"hidden"` type for the `columns` in `_dt_boxhead` - data <- dt_boxhead_set_hidden(data = data, vars = columns) - - data + dt_boxhead_set_hidden( + data = data, + vars = columns + ) } #' Unhide one or more columns @@ -894,19 +937,25 @@ cols_unhide <- function(data, # Stop function if no `columns` are provided if (length(columns) == 0) { - stop("Columns must be provided.", call. = FALSE) + stop( + "Columns must be provided.", + call. = FALSE + ) } # Stop function if any of the `columns` don't exist in `vars` if (!all(columns %in% vars)) { - stop("All `columns` must exist in the input `data` table.", - call. = FALSE) + stop( + "All `columns` must exist in the input `data` table.", + call. = FALSE + ) } # Set the `"visible"` type for the `columns` in `_dt_boxhead` - data <- dt_boxhead_set_not_hidden(data = data, vars = columns) - - data + dt_boxhead_set_not_hidden( + data = data, + vars = columns + ) } #' Merge two columns to a value & uncertainty column @@ -1031,7 +1080,11 @@ cols_merge_uncert <- function(data, data = data ) - data <- cols_hide(data = data, columns = col_uncert) + data <- + cols_hide( + data = data, + columns = col_uncert + ) } data @@ -1151,7 +1204,11 @@ cols_merge_range <- function(data, data = data ) - data <- cols_hide(data = data, columns = col_end) + data <- + cols_hide( + data = data, + columns = col_end + ) } data @@ -1317,7 +1374,11 @@ cols_merge_n_pct <- function(data, data = data ) - data <- data %>% cols_hide(columns = col_pct) + data <- + cols_hide( + data = data, + columns = col_pct + ) } data @@ -1427,26 +1488,30 @@ cols_merge <- function(data, hide_columns_from_supplied <- base::intersect(hide_columns, columns) if (length(base::setdiff(hide_columns, columns) > 0)) { - warning("Only the columns supplied in `columns` will be hidden.\n", - " * use `cols_hide()` to hide any out of scope columns", - call. = FALSE) + warning( + "Only the columns supplied in `columns` will be hidden.\n", + " * use `cols_hide()` to hide any out of scope columns", + call. = FALSE + ) } if (length(hide_columns_from_supplied) > 0) { - data <- cols_hide(data = data, columns = hide_columns_from_supplied) + + data <- + cols_hide( + data = data, + columns = hide_columns_from_supplied + ) } } # Create an entry and add it to the `_col_merge` attribute - data <- - dt_col_merge_add( - data = data, - col_merge = dt_col_merge_entry( - vars = columns, - type = "merge", - pattern = pattern - ) + dt_col_merge_add( + data = data, + col_merge = dt_col_merge_entry( + vars = columns, + type = "merge", + pattern = pattern ) - - data + ) } diff --git a/R/modify_rows.R b/R/modify_rows.R index 933ff30c5..4a74e2ef1 100644 --- a/R/modify_rows.R +++ b/R/modify_rows.R @@ -75,5 +75,8 @@ row_group_order <- function(data, groups <- c(unique(groups), base::setdiff(arrange_groups, unique(groups))) # Create and store a list of row groups in the intended ordering - dt_row_groups_set(data = data, row_groups = groups) + dt_row_groups_set( + data = data, + row_groups = groups + ) } diff --git a/R/opts.R b/R/opts.R index 9bf2ff2c7..02994f203 100644 --- a/R/opts.R +++ b/R/opts.R @@ -87,7 +87,7 @@ opt_footnote_marks <- function(data, # Validate input for `marks` validate_marks(marks) - tab_options(data, footnotes.marks = marks) + tab_options(data = data, footnotes.marks = marks) } #' Option to add or remove row striping @@ -149,7 +149,10 @@ opt_row_striping <- function(data, # Perform input object validation stop_if_not_gt(data = data) - tab_options(data, row.striping.include_table_body = row_striping) + tab_options( + data = data, + row.striping.include_table_body = row_striping + ) } #' Option to align the table header @@ -215,7 +218,10 @@ opt_align_table_header <- function(data, align <- match.arg(align) - tab_options(data, heading.align = align) + tab_options( + data = data, + heading.align = align + ) } #' Option to use all caps in select table locations @@ -289,8 +295,11 @@ opt_all_caps <- function(data, # Ensure that all named locations are valid if (!all(locations %in% c("column_labels", "stub", "row_group"))) { - stop("The available locations are `column_labels`, `stub`, and `row_group`.", - call. = FALSE) + + stop( + "The available locations are `column_labels`, `stub`, and `row_group`.", + call. = FALSE + ) } # Create a regex pattern to obtain arg names for all specific `locations` @@ -317,7 +326,10 @@ opt_all_caps <- function(data, option_value_list <- create_default_option_value_list(options_vec) } - tab_options_multi(data, option_value_list) + tab_options_multi( + data = data, + options = option_value_list + ) } #' Option to set table lines to different extents @@ -396,7 +408,10 @@ opt_table_lines <- function(data, option_value_list <- create_default_option_value_list(options_vec) } - tab_options_multi(data, option_value_list) + tab_options_multi( + data = data, + options = option_value_list + ) } #' Option to wrap an outline around the entire table @@ -499,7 +514,11 @@ opt_table_outline <- function(data, ) option_value_list <- create_option_value_list(options_vec, values_vec) - tab_options_multi(data, option_value_list) + + tab_options_multi( + data = data, + options = option_value_list + ) } #' Option to define a custom font for the table @@ -603,26 +622,58 @@ opt_table_font <- function(data, style = NULL, add = TRUE) { - existing_fonts <- dt_options_get_value(data = data, option = "table_font_names") - existing_additional_css <- dt_options_get_value(data = data, option = "table_additional_css") + existing_fonts <- + dt_options_get_value( + data = data, + option = "table_font_names" + ) + + existing_additional_css <- + dt_options_get_value( + data = data, + option = "table_additional_css" + ) font <- normalize_font_input(font_input = font) additional_css <- c(font$import_stmt, existing_additional_css) - data <- tab_options(data = data, table.font.names = c(font$name, if (add) existing_fonts)) - data <- tab_options(data = data, table.additional_css = additional_css) + data <- + tab_options( + data = data, + table.font.names = c(font$name, if (add) existing_fonts) + ) + + data <- + tab_options( + data = data, + table.additional_css = additional_css + ) if (!is.null(weight)) { if (is.numeric(weight)) weight <- as.character(weight) - data <- tab_options(data = data, table.font.weight = weight) - data <- tab_options(data = data, column_labels.font.weight = weight) + data <- + tab_options( + data = data, + table.font.weight = weight + ) + + data <- + tab_options( + data = data, + column_labels.font.weight = weight + ) } if (!is.null(style)) { - data <- tab_options(data = data, table.font.style = style) + + data <- + tab_options( + data = data, + table.font.style = style + ) } data @@ -695,7 +746,10 @@ opt_css <- function(data, allow_duplicates = FALSE) { existing_additional_css <- - dt_options_get_value(data = data, option = "table_additional_css") + dt_options_get_value( + data = data, + option = "table_additional_css" + ) css <- paste(css, collapse = "\n") @@ -705,7 +759,10 @@ opt_css <- function(data, additional_css <- c(existing_additional_css, css) - tab_options(data = data, table.additional_css = additional_css) + tab_options( + data = data, + table.additional_css = additional_css + ) } normalize_font_input <- function(font_input) { diff --git a/R/summary_rows.R b/R/summary_rows.R index 4f31943fc..0ae62614f 100644 --- a/R/summary_rows.R +++ b/R/summary_rows.R @@ -112,7 +112,11 @@ summary_rows <- function(data, stub_available <- dt_stub_df_exists(data = data) # Resolve the column names - columns <- resolve_cols_c(expr = {{ columns }}, data = data) + columns <- + resolve_cols_c( + expr = {{ columns }}, + data = data + ) # If there isn't a stub available, create an # 'empty' stub (populated with empty strings); @@ -131,18 +135,24 @@ summary_rows <- function(data, add_where = "bottom" ) + nrow_data <- nrow(data$`_data`) + # Add the `"rowname"` column into `_data` data$`_data` <- data$`_data` %>% - dplyr::mutate(rowname = rep("", nrow(data$`_data`))) %>% - dplyr::select(dplyr::everything(), rowname) + dplyr::mutate(rowname = rep("", .env$nrow_data)) %>% + dplyr::select(dplyr::everything(), .data$rowname) # Place the `rowname` values into `stub_df$rowname`; these are # empty strings which will provide an empty stub for locations # adjacent to the body rows stub_df[["rowname"]] <- "" - data <- dt_stub_df_set(data = data, stub_df = stub_df) + data <- + dt_stub_df_set( + data = data, + stub_df = stub_df + ) } # Derive the summary labels @@ -170,9 +180,10 @@ summary_rows <- function(data, formatter_options = formatter_options ) - data <- dt_summary_add(data = data, summary = summary_list) - - data + dt_summary_add( + data = data, + summary = summary_list + ) } #' Add grand summary rows using aggregation functions @@ -242,11 +253,12 @@ grand_summary_rows <- function(data, stop_if_not_gt(data = data) summary_rows( - data, + data = data, groups = NULL, columns = {{ columns }}, fns = fns, missing_text = missing_text, formatter = formatter, - ...) + ... + ) } diff --git a/R/tab_create_modify.R b/R/tab_create_modify.R index b546c7618..db4f8c255 100644 --- a/R/tab_create_modify.R +++ b/R/tab_create_modify.R @@ -152,8 +152,8 @@ tab_spanner <- function(data, # Move columns into place data <- - data %>% cols_move( + data = data, columns = column_names, after = column_names[1] ) @@ -418,7 +418,11 @@ tab_row_group <- function(data, # Warn user about `others_label` deprecation if (!is.null(others_label)) { - data <- tab_options(data = data, row_group.default_label = others_label) + data <- + tab_options( + data = data, + row_group.default_label = others_label + ) warning( "The `others_label` argument has been deprecated in gt 0.3.0:\n", @@ -461,6 +465,7 @@ tab_row_group <- function(data, arrange_groups_vars <- c(id, stats::na.omit(arrange_groups_vars)) arrange_groups_vars <- unique(arrange_groups_vars) arrange_groups_vars <- arrange_groups_vars[arrange_groups_vars %in% stub_df$group_id] + if (dt_stub_groupname_has_na(data = data)) { arrange_groups_vars <- c(arrange_groups_vars, NA_character_) } @@ -469,13 +474,10 @@ tab_row_group <- function(data, arrange_groups_vars <- character(0) } - data <- - dt_row_groups_set( - data = data, - row_groups = arrange_groups_vars - ) - - data + dt_row_groups_set( + data = data, + row_groups = arrange_groups_vars + ) } #' Add label text to the stubhead @@ -612,6 +614,7 @@ tab_footnote <- function(data, # Resolve the locations of the targeted data cells and append # the footnotes for (loc in locations) { + data <- set_footnote( loc = loc, @@ -1813,9 +1816,10 @@ tab_options <- function(data, ) # Write the modified options table back to `data` - data <- dt_options_set(data = data, options = opts_df) - - data + dt_options_set( + data = data, + options = opts_df + ) } preprocess_tab_option <- function(option, var_name, type) { diff --git a/man/cols_label.Rd b/man/cols_label.Rd index e5c0a8224..2ba6d82b7 100644 --- a/man/cols_label.Rd +++ b/man/cols_label.Rd @@ -4,12 +4,12 @@ \alias{cols_label} \title{Relabel one or more columns} \usage{ -cols_label(data, ..., .list = list2(...)) +cols_label(.data, ..., .list = list2(...)) } \arguments{ -\item{data}{A table object that is created using the \code{\link[=gt]{gt()}} function.} +\item{.data}{A table object that is created using the \code{\link[=gt]{gt()}} function.} -\item{...}{One or more named arguments of column names from the input \code{data} +\item{...}{One or more named arguments of column names from the input \code{.data} table along with their labels for display as the column labels. We can optionally wrap the column labels with \code{\link[=md]{md()}} (to interpret text as Markdown) or \code{\link[=html]{html()}} (to interpret text as HTML).} diff --git a/man/cols_width.Rd b/man/cols_width.Rd index b510eeb42..19ea646ba 100644 --- a/man/cols_width.Rd +++ b/man/cols_width.Rd @@ -4,13 +4,13 @@ \alias{cols_width} \title{Set the widths of columns} \usage{ -cols_width(data, ..., .list = list2(...)) +cols_width(.data, ..., .list = list2(...)) } \arguments{ -\item{data}{A table object that is created using the \code{\link[=gt]{gt()}} function.} +\item{.data}{A table object that is created using the \code{\link[=gt]{gt()}} function.} \item{...}{Expressions for the assignment of column widths for the table -columns in \code{data}. Two-sided formulas (e.g, \verb{ ~ }) can be used, +columns in \code{.data}. Two-sided formulas (e.g, \verb{ ~ }) can be used, where the left-hand side corresponds to selections of columns and the right-hand side evaluates to single-length character values in the form \verb{\{##\}px} (i.e., pixel dimensions); the \code{\link[=px]{px()}} helper function is best used diff --git a/man/fmt.Rd b/man/fmt.Rd index f4ea11e09..6996a1f89 100644 --- a/man/fmt.Rd +++ b/man/fmt.Rd @@ -35,6 +35,7 @@ than any of the specialized \verb{fmt_*()} functions that are available in \strong{gt}. Along with the \code{columns} and \code{rows} arguments that provide some precision in targeting data cells, the \code{fns} argument allows you to define one or more functions for manipulating the raw data. + If providing a single function to \code{fns}, the recommended format is in the form: \code{fns = function(x) ...}. This single function will format the targeted data cells the same way regardless of the output format (e.g., HTML, LaTeX, diff --git a/tests/gt-examples/01-html-script/html-01-iris.R b/tests/gt-examples/01-html-script/html-01-iris.R index 300b8a67d..6c99ca177 100644 --- a/tests/gt-examples/01-html-script/html-01-iris.R +++ b/tests/gt-examples/01-html-script/html-01-iris.R @@ -2,7 +2,7 @@ library(gt) # Create a display table based on `iris` iris_tbl <- - gt(data = iris) %>% + gt(iris) %>% tab_spanner_delim(delim = ".") %>% cols_move_to_start(columns = Species) %>% fmt_number( diff --git a/tests/gt-examples/01-html-script/html-02-morley.R b/tests/gt-examples/01-html-script/html-02-morley.R index eda6c76d8..80d9630a6 100644 --- a/tests/gt-examples/01-html-script/html-02-morley.R +++ b/tests/gt-examples/01-html-script/html-02-morley.R @@ -2,7 +2,7 @@ library(gt) # Create a display table based on `morley` morley_tbl <- - gt(data = morley) %>% + gt(morley) %>% fmt_number( columns = Speed, decimals = 0, diff --git a/tests/gt-examples/01-html-script/html-03-pressure.R b/tests/gt-examples/01-html-script/html-03-pressure.R index a8076f55a..95d82b34c 100644 --- a/tests/gt-examples/01-html-script/html-03-pressure.R +++ b/tests/gt-examples/01-html-script/html-03-pressure.R @@ -2,7 +2,7 @@ library(gt) # Create a display table based on `pressure` pressure_tbl <- - gt(data = pressure) %>% + gt(pressure) %>% fmt_scientific( columns = pressure, decimals = 2 diff --git a/tests/gt-examples/01-html-script/html-04-sleep.R b/tests/gt-examples/01-html-script/html-04-sleep.R index 7ef312544..3a3b8505c 100644 --- a/tests/gt-examples/01-html-script/html-04-sleep.R +++ b/tests/gt-examples/01-html-script/html-04-sleep.R @@ -2,7 +2,7 @@ library(gt) # Create a display table based on `sleep` sleep_tbl <- - gt(data = sleep) %>% + gt(sleep) %>% fmt_scientific(columns = extra) %>% tab_footnote( footnote = "This is a footnote", diff --git a/tests/gt-examples/01-html-script/html-05-airquality.R b/tests/gt-examples/01-html-script/html-05-airquality.R index 5e035d6b6..cf4ede0ce 100644 --- a/tests/gt-examples/01-html-script/html-05-airquality.R +++ b/tests/gt-examples/01-html-script/html-05-airquality.R @@ -2,7 +2,7 @@ library(gt) # Create a display table based on `airquality` airquality_tbl <- - gt(data = airquality) %>% + gt(airquality) %>% cols_move_to_start(columns = c(Month, Day)) %>% cols_label(Solar.R = html("Solar
Radiation")) %>% fmt_number( diff --git a/tests/gt-examples/01-html-script/html-09-formatting_uncertainties.R b/tests/gt-examples/01-html-script/html-09-formatting_uncertainties.R index bc0ba67bd..fd7ecfec7 100644 --- a/tests/gt-examples/01-html-script/html-09-formatting_uncertainties.R +++ b/tests/gt-examples/01-html-script/html-09-formatting_uncertainties.R @@ -13,7 +13,7 @@ tbl <- # Create a display table with uncertainties uncert_tbl <- - gt(data = tbl) %>% + gt(tbl) %>% cols_merge_uncert( col_val = value_1, col_uncert = uncertainty diff --git a/tests/gt-examples/01-html-script/html-12-adding_spanner_column_labels.R b/tests/gt-examples/01-html-script/html-12-adding_spanner_column_labels.R index 79b9fad35..a45557d72 100644 --- a/tests/gt-examples/01-html-script/html-12-adding_spanner_column_labels.R +++ b/tests/gt-examples/01-html-script/html-12-adding_spanner_column_labels.R @@ -12,7 +12,7 @@ tbl <- # Create a display table gt_tbl <- - gt(data = tbl) %>% + gt(tbl) %>% tab_spanner( label = "v_1_2", columns = c(v_1, v_2) diff --git a/tests/gt-examples/02-html-rmd/html-01-iris.Rmd b/tests/gt-examples/02-html-rmd/html-01-iris.Rmd index ce1ba4ab8..f64eb3eb3 100644 --- a/tests/gt-examples/02-html-rmd/html-01-iris.Rmd +++ b/tests/gt-examples/02-html-rmd/html-01-iris.Rmd @@ -13,7 +13,7 @@ library(gt) Create a display table based on `iris`. ```{r} -gt(data = iris) %>% +gt(iris) %>% tab_spanner_delim(delim = ".") %>% cols_move_to_start(columns = Species) %>% fmt_number( diff --git a/tests/gt-examples/02-html-rmd/html-02-morley.Rmd b/tests/gt-examples/02-html-rmd/html-02-morley.Rmd index 88b8ba73a..96198c1c4 100644 --- a/tests/gt-examples/02-html-rmd/html-02-morley.Rmd +++ b/tests/gt-examples/02-html-rmd/html-02-morley.Rmd @@ -12,7 +12,7 @@ library(gt) Create a display table based on `morley`. ```{r} -gt(data = morley) %>% +gt(morley) %>% fmt_number( columns = Speed, decimals = 0, diff --git a/tests/gt-examples/02-html-rmd/html-03-pressure.Rmd b/tests/gt-examples/02-html-rmd/html-03-pressure.Rmd index 5554bdde0..271fa78fe 100644 --- a/tests/gt-examples/02-html-rmd/html-03-pressure.Rmd +++ b/tests/gt-examples/02-html-rmd/html-03-pressure.Rmd @@ -12,7 +12,7 @@ library(gt) Create a display table based on `pressure`. ```{r} - gt(data = pressure) %>% +gt(pressure) %>% fmt_scientific( columns = pressure, decimals = 2 diff --git a/tests/gt-examples/02-html-rmd/html-04-sleep.Rmd b/tests/gt-examples/02-html-rmd/html-04-sleep.Rmd index e9505fca9..037d0102e 100644 --- a/tests/gt-examples/02-html-rmd/html-04-sleep.Rmd +++ b/tests/gt-examples/02-html-rmd/html-04-sleep.Rmd @@ -12,7 +12,7 @@ library(gt) Create a display table based on `sleep`. ```{r} -gt(data = sleep) %>% +gt(sleep) %>% fmt_scientific(columns = extra) %>% tab_footnote( footnote = "This is a footnote", diff --git a/tests/gt-examples/02-html-rmd/html-05-airquality.Rmd b/tests/gt-examples/02-html-rmd/html-05-airquality.Rmd index 1770696f3..486555a28 100644 --- a/tests/gt-examples/02-html-rmd/html-05-airquality.Rmd +++ b/tests/gt-examples/02-html-rmd/html-05-airquality.Rmd @@ -13,7 +13,7 @@ library(gt) Create a display table based on `airquality`. ```{r} -gt(data = airquality) %>% +gt(airquality) %>% cols_move_to_start(columns = c(Month, Day)) %>% cols_label(Solar.R = html("Solar
Radiation")) %>% fmt_number( diff --git a/tests/gt-examples/02-html-rmd/html-09-formatting_uncertainties.Rmd b/tests/gt-examples/02-html-rmd/html-09-formatting_uncertainties.Rmd index 9712c27dc..ef3ffab4b 100644 --- a/tests/gt-examples/02-html-rmd/html-09-formatting_uncertainties.Rmd +++ b/tests/gt-examples/02-html-rmd/html-09-formatting_uncertainties.Rmd @@ -25,7 +25,7 @@ tbl <- ``` ```{r} -gt(data = tbl) %>% +gt(tbl) %>% cols_merge_uncert( col_val = value_1, col_uncert = uncertainty diff --git a/tests/gt-examples/02-html-rmd/html-12-adding_spanner_column_labels.Rmd b/tests/gt-examples/02-html-rmd/html-12-adding_spanner_column_labels.Rmd index d805eb82b..254bd0c1c 100644 --- a/tests/gt-examples/02-html-rmd/html-12-adding_spanner_column_labels.Rmd +++ b/tests/gt-examples/02-html-rmd/html-12-adding_spanner_column_labels.Rmd @@ -24,7 +24,7 @@ tbl <- ``` ```{r} -gt(data = tbl) %>% +gt(tbl) %>% tab_spanner( label = "v_1_2", columns = c(v_1, v_2) diff --git a/tests/gt-examples/03-latex/latex-01-iris.Rmd b/tests/gt-examples/03-latex/latex-01-iris.Rmd index f21e9f6e8..626f0f2d1 100644 --- a/tests/gt-examples/03-latex/latex-01-iris.Rmd +++ b/tests/gt-examples/03-latex/latex-01-iris.Rmd @@ -13,7 +13,7 @@ library(gt) Create a display table based on `iris` Edgar Anderson's Iris Data. ```{r} -gt(data = iris) %>% +gt(iris) %>% tab_spanner_delim(delim = ".") %>% cols_move_to_start(columns = Species) %>% fmt_number( diff --git a/tests/gt-examples/03-latex/latex-02-morley.Rmd b/tests/gt-examples/03-latex/latex-02-morley.Rmd index 33de93908..209c13a68 100644 --- a/tests/gt-examples/03-latex/latex-02-morley.Rmd +++ b/tests/gt-examples/03-latex/latex-02-morley.Rmd @@ -12,7 +12,7 @@ library(gt) Create a display table based on `morley` Michelson Speed of Light Data ```{r} -gt(data = morley %>% dplyr::mutate(Speed = as.numeric(Speed))) %>% +gt(morley %>% dplyr::mutate(Speed = as.numeric(Speed))) %>% fmt_number( columns = Speed, decimals = 0, diff --git a/tests/gt-examples/03-latex/latex-03-pressure.Rmd b/tests/gt-examples/03-latex/latex-03-pressure.Rmd index 32495ae87..3a661eda7 100644 --- a/tests/gt-examples/03-latex/latex-03-pressure.Rmd +++ b/tests/gt-examples/03-latex/latex-03-pressure.Rmd @@ -12,7 +12,7 @@ library(gt) Create a display table based on `pressure`. ```{r} -gt(data = pressure) %>% +gt(pressure) %>% fmt_scientific( columns = pressure, decimals = 2 diff --git a/tests/gt-examples/03-latex/latex-04-sleep.Rmd b/tests/gt-examples/03-latex/latex-04-sleep.Rmd index 3d26abf7c..2cee6ca3d 100644 --- a/tests/gt-examples/03-latex/latex-04-sleep.Rmd +++ b/tests/gt-examples/03-latex/latex-04-sleep.Rmd @@ -12,7 +12,7 @@ library(gt) Create a display table based on `sleep`. ```{r} -gt(data = sleep) %>% +gt(sleep) %>% fmt_scientific(columns = extra) %>% tab_footnote( footnote = "This is a footnote", diff --git a/tests/gt-examples/03-latex/latex-05-airquality.Rmd b/tests/gt-examples/03-latex/latex-05-airquality.Rmd index c16fae20b..01e4956d6 100644 --- a/tests/gt-examples/03-latex/latex-05-airquality.Rmd +++ b/tests/gt-examples/03-latex/latex-05-airquality.Rmd @@ -12,7 +12,7 @@ library(gt) Create a display table based on `airquality`. ```{r} -gt(data = airquality) %>% +gt(airquality) %>% cols_move_to_start(columns = c(Month, Day)) %>% fmt_number( columns = Wind, diff --git a/tests/gt-examples/03-latex/latex-09-formatting_uncertainties.Rmd b/tests/gt-examples/03-latex/latex-09-formatting_uncertainties.Rmd index 59cc3f7a1..eaacbbcf3 100644 --- a/tests/gt-examples/03-latex/latex-09-formatting_uncertainties.Rmd +++ b/tests/gt-examples/03-latex/latex-09-formatting_uncertainties.Rmd @@ -26,7 +26,7 @@ tbl <- ```{r} -gt(data = tbl) %>% +gt(tbl) %>% cols_merge_uncert( col_val = value_1, col_uncert = uncertainty diff --git a/tests/gt-examples/03-latex/latex-12-adding_spanner_column_labels.Rmd b/tests/gt-examples/03-latex/latex-12-adding_spanner_column_labels.Rmd index cc19b6cf4..329db2fd0 100644 --- a/tests/gt-examples/03-latex/latex-12-adding_spanner_column_labels.Rmd +++ b/tests/gt-examples/03-latex/latex-12-adding_spanner_column_labels.Rmd @@ -24,7 +24,7 @@ tbl <- ``` ```{r} -gt(data = tbl) %>% +gt(tbl) %>% tab_spanner( label = "v_1_2", columns = c(v_1, v_2) diff --git a/tests/gt-examples/04-rtf/rtf_tests.R b/tests/gt-examples/04-rtf/rtf_tests.R index 02aeee57e..32a3f5dab 100644 --- a/tests/gt-examples/04-rtf/rtf_tests.R +++ b/tests/gt-examples/04-rtf/rtf_tests.R @@ -35,7 +35,7 @@ unicode_tbl %>% gtsave("tests/gt-examples/rtf_output/unicode.rtf") # Create a display table based on `iris` iris_tbl <- - gt(data = iris) %>% + gt(iris) %>% tab_spanner_delim(delim = ".") %>% cols_move_to_start(columns = Species) %>% fmt_number( @@ -71,7 +71,7 @@ passthrough_tbl %>% gtsave("tests/gt-examples/rtf_output/passthrough.rtf") # Create a display table based on `morley` morley_tbl <- - gt(data = morley) %>% + gt(morley) %>% fmt_number( columns = Speed, decimals = 0, @@ -82,7 +82,7 @@ morley_tbl %>% gtsave("tests/gt-examples/rtf_output/morley.rtf") # Create a display table based on `pressure` pressure_tbl <- - gt(data = pressure) %>% + gt(pressure) %>% fmt_scientific( columns = pressure, decimals = 2 @@ -92,7 +92,7 @@ pressure_tbl %>% gtsave("tests/gt-examples/rtf_output/pressure.rtf") # Create a display table based on `sleep` sleep_tbl <- - gt(data = sleep) %>% + gt(sleep) %>% fmt_scientific(columns = extra) %>% tab_footnote( footnote = "This is a footnote", @@ -103,7 +103,7 @@ sleep_tbl %>% gtsave("tests/gt-examples/rtf_output/sleep.rtf") # Create a display table based on `airquality` airquality_tbl <- - gt(data = airquality) %>% + gt(airquality) %>% cols_move_to_start(columns = c(Month, Day)) %>% cols_label(Solar.R = md("Solar \nRadiation")) %>% fmt_number( @@ -212,7 +212,7 @@ tbl <- # Create a display table with uncertainties uncert_tbl <- - gt(data = tbl) %>% + gt(tbl) %>% cols_merge_uncert( col_val = value_1, col_uncert = uncertainty @@ -299,7 +299,7 @@ tbl <- # Create a display table footnotes_tbl <- - gt(data = tbl, groupname_col = "date") %>% + gt(tbl, groupname_col = "date") %>% tab_header(title = md("The Table `Title`"), subtitle = "The subtitle.") %>% tab_spanner( label = "values", @@ -355,7 +355,7 @@ tbl <- # Create a display table spanner_tbl <- - gt(data = tbl) %>% + gt(tbl) %>% tab_spanner( label = "v_1_2", columns = c("v_1", "v_2") diff --git a/tests/testthat/test-cols_align.R b/tests/testthat/test-cols_align.R index 07bfb8593..908765d93 100644 --- a/tests/testthat/test-cols_align.R +++ b/tests/testthat/test-cols_align.R @@ -23,7 +23,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_html` object with `gt()`; the `mpg`, # `cyl`, and `drat` columns are aligned left tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "left", columns = c(mpg, cyl, drat)) %>% render_as_html() %>% xml2::read_html() @@ -44,7 +44,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_html` object with `gt()`; columns `1` (`mpg`), # `2` (`cyl`), and `3` (`disp`) are aligned left tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "left", columns = 1:3) %>% render_as_html() %>% xml2::read_html() @@ -65,25 +65,25 @@ test_that("the `cols_align()` function works correctly", { # Expect that supplying an `align` value that is not `left`, `center`, # or `right` will result in an error expect_error( - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "righter", columns = c(mpg, cyl, drat))) # Expect that supplying a column name that doesn't exist in the # table columns will result in an error expect_error( - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "right", columns = car)) # Expect that supplying any column index that doesn't exist in the # table will result in an error expect_error( - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "right", columns = c(1, 20))) # Create a `tbl_html` object with `gt()`; align all # columns to the left tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "left") %>% render_as_html() %>% xml2::read_html() @@ -98,7 +98,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_html` object with `gt()`; align all # columns (using `columns = everything()`) to the left tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "left", columns = everything()) %>% render_as_html() %>% xml2::read_html() @@ -113,7 +113,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_html` object with the `sp500` data # frame and `auto`-align all columns tbl_html <- - gt(data = sp500) %>% + gt(sp500) %>% cols_align(align = "auto") %>% render_as_html() %>% xml2::read_html() diff --git a/tests/testthat/test-cols_label.R b/tests/testthat/test-cols_label.R index 50b19e90d..9d04ac050 100644 --- a/tests/testthat/test-cols_label.R +++ b/tests/testthat/test-cols_label.R @@ -133,4 +133,47 @@ test_that("the function `cols_label()` works correctly", { expect_error( gt(tbl) %>% cols_label(col_a = "col_1")) + + # Expect no partial matching issues with column names and arguments + expect_error( + regexp = NA, + dplyr::tribble( + ~a , ~d, + 1, 4, + 5, 8 + ) %>% + gt() %>% + cols_label( + a = "label a", + d = "label d" + ) + ) + expect_error( + regexp = NA, + dplyr::tribble( + ~a , ~dat, + 1, 4, + 5, 8 + ) %>% + gt() %>% + cols_label( + a = "label a", + dat = "label dat" + ) + ) + + # Do expect an error in the unlikely case that a column + # name is close enough to `.data` + expect_error( + dplyr::tribble( + ~a , ~.dat, + 1, 4, + 5, 8 + ) %>% + gt() %>% + cols_label( + a = "label a", + .dat = "label dat" + ) + ) }) diff --git a/tests/testthat/test-cols_move.R b/tests/testthat/test-cols_move.R index 415b08af7..27fb39f43 100644 --- a/tests/testthat/test-cols_move.R +++ b/tests/testthat/test-cols_move.R @@ -17,7 +17,7 @@ test_that("the `cols_move()` function works correctly", { # Create a `tbl_html` object with `gt()`; the `mpg`, # `cyl`, and `drat` columns placed after `drat` tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c(mpg, cyl, disp), after = drat) # Expect a particular ordering of columns in `_boxh` @@ -41,7 +41,7 @@ test_that("the `cols_move()` function works correctly", { # Create a `tbl_html` object with `gt()`; the `mpg`, # `cyl`, and `drat` columns placed after `drat` using vectors tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c("mpg", "cyl", "disp"), after = c("drat")) # Expect a particular ordering of columns in `_boxh` @@ -65,7 +65,7 @@ test_that("the `cols_move()` function works correctly", { # Create a `tbl_html` object with `gt()`; the `mpg`, # `cyl`, and `drat` columns placed after `carb` (the end of the series) tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c(mpg, cyl, disp), after = carb) # Expect a particular ordering of columns in the internal `boxh_df` @@ -88,17 +88,17 @@ test_that("the `cols_move()` function works correctly", { # Expect an error if more than one column provided in `after` expect_error( - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c(mpg, cyl, disp), after = c(am, wt))) # Expect an error if the column provided in `after` doesn't exist expect_error( - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c(mpg, cyl, disp), after = wts)) # Expect an error if any of the `columns` doesn't exist in `data_df` expect_error( - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c(mpg, cyls, disp), after = wt)) }) @@ -110,7 +110,7 @@ test_that("the `cols_move_to_start()` function works correctly", { # Create a `tbl_html` object with `gt()`; the `gear`, # and `carb` columns placed at the start tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_start(columns = c(gear, carb)) # Expect a particular ordering of columns in the internal `boxh_df` @@ -134,7 +134,7 @@ test_that("the `cols_move_to_start()` function works correctly", { # Create a `tbl_html` object with `gt()`; the `gear`, # and `carb` columns placed at the start using vectors tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_start(columns = c("gear", "carb")) # Expect a particular ordering of columns in the internal `boxh_df` @@ -157,7 +157,7 @@ test_that("the `cols_move_to_start()` function works correctly", { # Expect an error if any of the `columns` doesn't exist in `data_df` expect_error( - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_start(columns = c(mpg, cyls, disp))) }) @@ -169,7 +169,7 @@ test_that("the `cols_move_to_end()` function works correctly", { # Create a `tbl_html` object with `gt()`; the `gear`, # and `carb` columns placed at the end tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_end(columns = c(gear, carb)) # Expect a particular ordering of columns in the internal `boxh_df` @@ -193,7 +193,7 @@ test_that("the `cols_move_to_end()` function works correctly", { # Create a `tbl_html` object with `gt()`; the `gear`, # and `carb` columns placed at the start using vectors tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_end(columns = c("gear", "carb")) # Expect a particular ordering of columns in the internal `boxh_df` @@ -216,6 +216,6 @@ test_that("the `cols_move_to_end()` function works correctly", { # Expect an error if any of the `columns` doesn't exist in `data_df` expect_error( - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_end(columns = c(mpg, cyls, disp))) }) diff --git a/tests/testthat/test-cols_width.R b/tests/testthat/test-cols_width.R index b7d16b887..ee4b6525d 100644 --- a/tests/testthat/test-cols_width.R +++ b/tests/testthat/test-cols_width.R @@ -560,6 +560,51 @@ test_that("the function `cols_width()` works correctly with a simple table", { "" ) %>% expect_true() + + # Expect no partial matching issues with column names and arguments + expect_error( + regexp = NA, + dplyr::tribble( + ~a , ~d, + 1, 4, + 5, 8 + ) %>% + gt() %>% + cols_width( + a ~ px(100), + d ~ px(125) + ) + ) + expect_error( + regexp = NA, + dplyr::tribble( + ~a , ~dat, + 1, 4, + 5, 8 + ) %>% + gt() %>% + cols_width( + a ~ px(100), + dat ~ px(125) + ) + ) + + # Don't expect an error even in the unlikely case that a column + # name is close enough to `.data` (this is due to the use of the + # formula syntax) + expect_error( + regexp = NA, + dplyr::tribble( + ~a , ~.dat, + 1, 4, + 5, 8 + ) %>% + gt() %>% + cols_width( + a ~ px(100), + .dat ~ px(125) + ) + ) }) test_that("the function `cols_width()` works correctly with a complex table", { diff --git a/tests/testthat/test-conditional_fmt.R b/tests/testthat/test-conditional_fmt.R index 01c6db158..5d37a7122 100644 --- a/tests/testthat/test-conditional_fmt.R +++ b/tests/testthat/test-conditional_fmt.R @@ -14,7 +14,7 @@ data_tbl <- # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset -tab <- gt(data = data_tbl) +tab <- gt(data_tbl) # Create an input data frame with dates, times, and date-times (all # as character) @@ -28,7 +28,7 @@ time_tbl <- # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset -tab_time <- gt(data = time_tbl) +tab_time <- gt(time_tbl) test_that("the `fmt_number()` function works with conditional `rows`", { diff --git a/tests/testthat/test-fmt_bytes.R b/tests/testthat/test-fmt_bytes.R index acba23596..9441460ba 100644 --- a/tests/testthat/test-fmt_bytes.R +++ b/tests/testthat/test-fmt_bytes.R @@ -31,7 +31,7 @@ test_that("the `fmt_bytes()` function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) @@ -374,7 +374,7 @@ test_that("the `fmt_bytes()` function format to specified significant figures", # Create a `gt_tbl` object with `gt()` and the # `numbers_tbl` dataset - tab <- gt(data = numbers_tbl) + tab <- gt(numbers_tbl) # Format the `num` column to 5 significant figures expect_equal( diff --git a/tests/testthat/test-fmt_currency.R b/tests/testthat/test-fmt_currency.R index 70602006b..250a902ae 100644 --- a/tests/testthat/test-fmt_currency.R +++ b/tests/testthat/test-fmt_currency.R @@ -17,22 +17,11 @@ test_that("the `fmt_currency()` function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) - # Expect certain named attributes - # expect_true( - # all( - # names(attributes(tab)) %in% - # c("names", "class", "row.names", - # "boxh_df", "stub_df", "footnotes_df", "styles_df", - # "rows_df", "cols_df", "col_labels", "grp_labels", - # "arrange_groups", "data_df", "opts_df", "formats", "transforms") - # ) - # ) - # Extract vectors from the table object for comparison # to the original dataset char_1 <- (tab %>% dt_data_get())[["char_1"]] @@ -283,7 +272,7 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num` column to 2 decimal places, have the # `suffixing` option set to TRUE (default labels, all @@ -400,7 +389,7 @@ test_that("the `fmt_currency()` function can scale/suffix larger numbers", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl_2` dataset - tab_2 <- gt(data = data_tbl_2) + tab_2 <- gt(data_tbl_2) # # Adjust the `decimals` value to verify that @@ -517,7 +506,7 @@ test_that("the `currency()` helper function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num_1` column using the `currency()` helper function; # extract `output_df` in the HTML context and compare to expected values diff --git a/tests/testthat/test-fmt_date_time.R b/tests/testthat/test-fmt_date_time.R index 5b6cd01ab..5920da385 100644 --- a/tests/testthat/test-fmt_date_time.R +++ b/tests/testthat/test-fmt_date_time.R @@ -10,20 +10,11 @@ test_that("the `fmt_date()` function works correctly", { # Create a `tab` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) - # Expect certain named attributes - # expect_true( - # all( - # names(attributes(tab)) %in% - # c("names", "class", "row.names", - # "boxh_df", "stub_df", "footnotes_df", "styles_df", - # "rows_df", "cols_df", "col_labels", "grp_labels", - # "arrange_groups", "data_df", "opts_df", "formats", "transforms"))) - # Extract a vector from the table object for comparison # to the original dataset date <- (tab %>% dt_data_get())[["date"]] @@ -139,9 +130,8 @@ test_that("the `fmt_date()` function works correctly", { dplyr::tibble(date = as.Date(c( "2017-10-15", "2013-02-22", "2014-09-22", "2018-01-10"))) - # Create a `tab` object with `gt()` and the - # `data_tbl` dataset - tab <- gt(data = data_tbl) + # Create a `tab` object with `gt()` and the `data_tbl` dataset + tab <- gt(data_tbl) # # Format `date` in various date formats and verify the output @@ -250,20 +240,11 @@ test_that("the `fmt_time()` function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) - # Expect certain named attributes - # expect_true( - # all( - # names(attributes(tab)) %in% - # c("names", "class", "row.names", - # "boxh_df", "stub_df", "footnotes_df", "styles_df", - # "rows_df", "cols_df", "col_labels", "grp_labels", - # "arrange_groups", "data_df", "opts_df", "formats", "transforms"))) - # Extract a vector from the table object for comparison # to the original dataset time <- (tab %>% dt_data_get())[["time"]] @@ -325,20 +306,11 @@ test_that("the `fmt_datetime()` function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) - # Expect certain named attributes - # expect_true( - # all( - # names(attributes(tab)) %in% - # c("names", "class", "row.names", - # "boxh_df", "stub_df", "footnotes_df", "styles_df", - # "rows_df", "cols_df", "col_labels", "grp_labels", - # "arrange_groups", "data_df", "opts_df", "formats", "transforms"))) - # Extract a vector from the table object for comparison # to the original dataset datetime <- (tab %>% dt_data_get())[["datetime"]] diff --git a/tests/testthat/test-fmt_engineering.R b/tests/testthat/test-fmt_engineering.R index e1c9f446c..8205e39bc 100644 --- a/tests/testthat/test-fmt_engineering.R +++ b/tests/testthat/test-fmt_engineering.R @@ -36,7 +36,7 @@ test_that("the `fmt_engineering()` function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) @@ -385,7 +385,7 @@ test_that("`fmt_engineering() can handle extremely large and small values", { ) # Create a `gt_tbl` object with `gt()` and the`data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num` column to 5 decimal places, use all other defaults expect_equal( diff --git a/tests/testthat/test-fmt_integer.R b/tests/testthat/test-fmt_integer.R index 406d6f564..86b55e629 100644 --- a/tests/testthat/test-fmt_integer.R +++ b/tests/testthat/test-fmt_integer.R @@ -14,7 +14,7 @@ test_that("the `fmt_integer()` function works correctly in the HTML context", { ) # Create a `gt_tbl` object with `gt()` and the `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) @@ -238,7 +238,7 @@ test_that("the `fmt_integer()` function can scale/suffix larger numbers", { ) # Create a `gt_tbl` object with `gt()` and the `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num` column, have the `suffixing` option # set to TRUE (default labels, all 4 ranges used) diff --git a/tests/testthat/test-fmt_missing.R b/tests/testthat/test-fmt_missing.R index 1bc073adc..30952370b 100644 --- a/tests/testthat/test-fmt_missing.R +++ b/tests/testthat/test-fmt_missing.R @@ -12,20 +12,11 @@ test_that("the `fmt_missing()` function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) - # Expect certain named attributes - # expect_true( - # all( - # names(attributes(tab)) %in% - # c("names", "class", "row.names", - # "boxh_df", "stub_df", "footnotes_df", "styles_df", - # "rows_df", "cols_df", "col_labels", "grp_labels", - # "arrange_groups", "data_df", "opts_df", "formats", "transforms"))) - # Extract vectors from the table object for comparison # to the original dataset num_1 <- (tab %>% dt_data_get())[["num_1"]] diff --git a/tests/testthat/test-fmt_number.R b/tests/testthat/test-fmt_number.R index 6f6e00d85..8df074abe 100644 --- a/tests/testthat/test-fmt_number.R +++ b/tests/testthat/test-fmt_number.R @@ -15,7 +15,7 @@ test_that("the `fmt_number()` function works correctly in the HTML context", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) @@ -324,7 +324,7 @@ test_that("the `fmt_number()` function can scale/suffix larger numbers", { ) # Create a `gt_tbl` object with `gt()` and the `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num` column to 2 decimal places, have the `suffixing` option # set to TRUE (default labels, all 4 ranges used) @@ -442,9 +442,8 @@ test_that("the `fmt_number()` function can scale/suffix larger numbers", { # numeric column and with one row data_tbl_2 <- data.frame(num = 999.9999) - # Create a `gt_tbl` object with `gt()` and the - # `data_tbl_2` dataset - tab_2 <- gt(data = data_tbl_2) + # Create a `gt_tbl` object with `gt()` and the `data_tbl_2` dataset + tab_2 <- gt(data_tbl_2) # # Adjust the `decimals` value to verify that @@ -528,7 +527,7 @@ test_that("the `fmt_number()` function format to specified significant figures", numbers_tbl <- dplyr::tibble(num = numbers) # Create a `gt_tbl` object with `gt()` and the `numbers_tbl` dataset - tab <- gt(data = numbers_tbl) + tab <- gt(numbers_tbl) # Format the `num` column to 5 significant figures expect_equal( @@ -625,9 +624,8 @@ test_that("the `drop_trailing_dec_mark` option works in select `fmt_*()` functio # Create a single-column tibble with these values in `num` numbers_tbl <- dplyr::tibble(num = numbers) - # Create a `gt_tbl` object with `gt()` and the - # `numbers_tbl` dataset - tab <- gt(data = numbers_tbl) + # Create a `gt_tbl` object with `gt()` and the `numbers_tbl` dataset + tab <- gt(numbers_tbl) # Format the `num` column using `fmt_number()` with default options expect_equal( @@ -766,7 +764,7 @@ test_that("`fmt_number()` with `suffixing = TRUE` works with small numbers", { ) # Create a `gt_tbl` object with `gt()` and the `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num` column to 2 decimal places, have the `suffixing` option # set to TRUE; we shouldn't expect to see any suffixes diff --git a/tests/testthat/test-fmt_passthrough.R b/tests/testthat/test-fmt_passthrough.R index abdeaf648..1eed4c696 100644 --- a/tests/testthat/test-fmt_passthrough.R +++ b/tests/testthat/test-fmt_passthrough.R @@ -16,20 +16,11 @@ test_that("the `fmt_passthrough()` function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) - # Expect certain named attributes - # expect_true( - # all( - # names(attributes(tab)) %in% - # c("names", "class", "row.names", - # "boxh_df", "stub_df", "footnotes_df", "styles_df", - # "rows_df", "cols_df", "col_labels", "grp_labels", - # "arrange_groups", "data_df", "opts_df", "formats", "transforms"))) - # Extract vectors from the table object for comparison # to the original dataset char_1 <- (tab %>% dt_data_get())[["char_1"]] diff --git a/tests/testthat/test-fmt_percent.R b/tests/testthat/test-fmt_percent.R index 937e6ebd1..082ec5af0 100644 --- a/tests/testthat/test-fmt_percent.R +++ b/tests/testthat/test-fmt_percent.R @@ -13,9 +13,8 @@ test_that("the `fmt_percent()` function works correctly in the HTML context", { stringsAsFactors = FALSE ) - # Create a `gt_tbl` object with `gt()` and the - # `data_tbl` dataset - tab <- gt(data = data_tbl) + # Create a `gt_tbl` object with `gt()` and the `data_tbl` dataset + tab <- gt(data_tbl) # Expect an error when attempting to format a column # that does not exist diff --git a/tests/testthat/test-fmt_scientific.R b/tests/testthat/test-fmt_scientific.R index d79f69574..cec23d914 100644 --- a/tests/testthat/test-fmt_scientific.R +++ b/tests/testthat/test-fmt_scientific.R @@ -17,7 +17,7 @@ test_that("the `fmt_scientific()` function works correctly", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) @@ -359,7 +359,7 @@ test_that("`fmt_scientific()` can handle extremely large and small values", { # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num` column to 5 decimal places, use all # other defaults; extract values in the default context diff --git a/tests/testthat/test-l_cols_align.R b/tests/testthat/test-l_cols_align.R index b401a361b..24ac15ce6 100644 --- a/tests/testthat/test-l_cols_align.R +++ b/tests/testthat/test-l_cols_align.R @@ -14,7 +14,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_latex` object with `gt()`; the `mpg`, # `cyl`, and `drat` columns are aligned left tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "left", columns = c(mpg, cyl, drat)) %>% as_latex() %>% as.character() @@ -25,7 +25,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_latex` object with `gt()`; columns `1` (`mpg`), # `2` (`cyl`), and `3` (`disp`) are aligned right tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "left", columns = 1:3) %>% as_latex() %>% as.character() @@ -36,7 +36,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_latex` object with `gt()`; align all # columns to the left tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "left") %>% as_latex() %>% as.character() @@ -47,7 +47,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_latex` object with `gt()`; align all # columns (using `columns = everything()`) to the left tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_align(align = "left", columns = everything()) %>% as_latex() %>% as.character() @@ -58,7 +58,7 @@ test_that("the `cols_align()` function works correctly", { # Create a `tbl_latex` object with the `sp500` data # frame and `auto`-align all columns tbl_latex <- - gt(data = sp500) %>% + gt(sp500) %>% cols_align(align = "auto") %>% as_latex() %>% as.character() diff --git a/tests/testthat/test-l_cols_move.R b/tests/testthat/test-l_cols_move.R index d62c8aa01..6c78a3171 100644 --- a/tests/testthat/test-l_cols_move.R +++ b/tests/testthat/test-l_cols_move.R @@ -8,7 +8,7 @@ test_that("the `cols_move()` function works correctly", { # Create a `tbl_latex` object with `gt()`; the `mpg`, # `cyl`, and `drat` columns placed after `drat` tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c(mpg, cyl, disp), after = drat) # Expect a characteristic pattern @@ -22,7 +22,7 @@ test_that("the `cols_move()` function works correctly", { # Create a `tbl_latex` object with `gt()`; the `mpg`, # `cyl`, and `drat` columns placed after `drat` using vectors tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c("mpg", "cyl", "disp"), after = c("drat")) # Expect a characteristic pattern @@ -36,7 +36,7 @@ test_that("the `cols_move()` function works correctly", { # Create a `tbl_latex` object with `gt()`; the `mpg`, # `cyl`, and `drat` columns placed after `carb` (the end of the series) tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move(columns = c(mpg, cyl, disp), after = carb) # Expect a characteristic pattern @@ -53,7 +53,7 @@ test_that("the `cols_move_to_start()` function works correctly", { # Create a `tbl_latex` object with `gt()`; the `gear`, # and `carb` columns placed at the start tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_start(columns = c(gear, carb)) # Expect a characteristic pattern @@ -67,7 +67,7 @@ test_that("the `cols_move_to_start()` function works correctly", { # Create a `tbl_latex` object with `gt()`; the `gear`, # and `carb` columns placed at the start using vectors tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_start(columns = c("gear", "carb")) # Expect a characteristic pattern @@ -84,7 +84,7 @@ test_that("the `cols_move_to_end()` function works correctly", { # Create a `tbl_latex` object with `gt()`; the `gear`, # and `carb` columns placed at the end tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_end(columns = c(gear, carb)) # Expect a characteristic pattern @@ -98,7 +98,7 @@ test_that("the `cols_move_to_end()` function works correctly", { # Create a `tbl_latex` object with `gt()`; the `gear`, # and `carb` columns placed at the end using vectors tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% cols_move_to_end(columns = c("gear", "carb")) # Expect a characteristic pattern diff --git a/tests/testthat/test-l_conditional_fmt.R b/tests/testthat/test-l_conditional_fmt.R index 89f56ecbe..3d02db0fb 100644 --- a/tests/testthat/test-l_conditional_fmt.R +++ b/tests/testthat/test-l_conditional_fmt.R @@ -13,9 +13,8 @@ data_tbl <- stringsAsFactors = FALSE ) -# Create a `tbl_latex` object with `gt()` and the -# `data_tbl` dataset -tbl_latex <- gt(data = data_tbl) +# Create a `tbl_latex` object with `gt()` and the `data_tbl` dataset +tbl_latex <- gt(data_tbl) # Create an input data frame with dates, times, and date-times (all # as character) @@ -28,9 +27,8 @@ time_tbl <- stringsAsFactors = FALSE ) -# Create a `tbl_latex_time` object with `gt()` and the -# `data_tbl` dataset -tbl_latex_time <- gt(data = time_tbl) +# Create a `tbl_latex_time` object with `gt()` and the `data_tbl` dataset +tbl_latex_time <- gt(time_tbl) test_that("the `fmt_number()` function works with conditional `rows`", { diff --git a/tests/testthat/test-l_fmt_currency.R b/tests/testthat/test-l_fmt_currency.R index 32f0994fb..30d88b4ee 100644 --- a/tests/testthat/test-l_fmt_currency.R +++ b/tests/testthat/test-l_fmt_currency.R @@ -17,7 +17,7 @@ test_that("the `fmt_currency()` function works correctly", { # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + tbl_latex <- gt(data_tbl) # Format the `num_1` column as USD, use all other defaults; # extract `output_df` and compare to expected values diff --git a/tests/testthat/test-l_fmt_date_time.R b/tests/testthat/test-l_fmt_date_time.R index 19b6c11ec..22f2bb081 100644 --- a/tests/testthat/test-l_fmt_date_time.R +++ b/tests/testthat/test-l_fmt_date_time.R @@ -8,9 +8,8 @@ test_that("the `fmt_date()` function works correctly", { dplyr::tibble(date = c( "2017-10-15", "2013-02-22", "2014-09-22", "2018-01-10")) - # Create a `tbl_latex` object with `gt()` and the - # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + # Create a `tbl_latex` object with `gt()` and the `data_tbl` dataset + tbl_latex <- gt(data_tbl) # # Format `date` in various date formats and verify the output @@ -112,9 +111,8 @@ test_that("the `fmt_date()` function works correctly", { dplyr::tibble(date = as.Date(c( "2017-10-15", "2013-02-22", "2014-09-22", "2018-01-10"))) - # Create a `tbl_latex` object with `gt()` and the - # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + # Create a `tbl_latex` object with `gt()` and the `data_tbl` dataset + tbl_latex <- gt(data_tbl) # # Format `date` in various date formats and verify the output @@ -220,9 +218,8 @@ test_that("the `fmt_time()` function works correctly", { dplyr::tibble(time = c( "12:35:23", "15:01:34", "09:45:23", "01:32:00")) - # Create a `tbl_latex` object with `gt()` and the - # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + # Create a `tbl_latex` object with `gt()` and the `data_tbl` dataset + tbl_latex <- gt(data_tbl) # # Format `time` in various date formats and verify the output @@ -270,9 +267,8 @@ test_that("the `fmt_datetime()` function works correctly", { "2017-08-05 09:45:23", "2017-10-23 01:32:00")) - # Create a `tbl_latex` object with `gt()` and the - # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + # Create a `tbl_latex` object with `gt()` and the `data_tbl` dataset + tbl_latex <- gt(data_tbl) # # Format `time` in various date formats and verify the output diff --git a/tests/testthat/test-l_fmt_engineering.R b/tests/testthat/test-l_fmt_engineering.R index 70403f887..c7635f8ab 100644 --- a/tests/testthat/test-l_fmt_engineering.R +++ b/tests/testthat/test-l_fmt_engineering.R @@ -36,7 +36,7 @@ test_that("the `fmt_engineering()` function works correctly in the LaTeX context # Create a `gt_tbl` object with `gt()` and the # `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) @@ -264,7 +264,7 @@ test_that("`fmt_engineering() can handle extremely large and small values", { ) # Create a `gt_tbl` object with `gt()` and the`data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num` column to 5 decimal places, use all other defaults expect_equal( diff --git a/tests/testthat/test-l_fmt_integer.R b/tests/testthat/test-l_fmt_integer.R index 66b66c3db..fc07c9854 100644 --- a/tests/testthat/test-l_fmt_integer.R +++ b/tests/testthat/test-l_fmt_integer.R @@ -14,7 +14,7 @@ test_that("the `fmt_integer()` function works correctly in the LaTeX context", { ) # Create a `gt_tbl` object with `gt()` and the `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Expect that the object has the correct classes expect_is(tab, c("gt_tbl", "data.frame")) @@ -199,7 +199,7 @@ test_that("the `fmt_integer()` function can scale/suffix larger numbers", { ) # Create a `gt_tbl` object with `gt()` and the `data_tbl` dataset - tab <- gt(data = data_tbl) + tab <- gt(data_tbl) # Format the `num` column, have the `suffixing` option # set to TRUE (default labels, all 4 ranges used) diff --git a/tests/testthat/test-l_fmt_missing.R b/tests/testthat/test-l_fmt_missing.R index 957c59837..7725f8960 100644 --- a/tests/testthat/test-l_fmt_missing.R +++ b/tests/testthat/test-l_fmt_missing.R @@ -9,9 +9,8 @@ test_that("the `fmt_missing()` function works correctly", { num_2 = c(34, 74, 23, 93, 35, 76, 57), stringsAsFactors = FALSE) - # Create a `tbl_latex` object with `gt()` and the - # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + # Create a `tbl_latex` object with `gt()` and the `data_tbl` dataset + tbl_latex <- gt(data_tbl) # # Use `fmt_missing()` with various options diff --git a/tests/testthat/test-l_fmt_number.R b/tests/testthat/test-l_fmt_number.R index 145d75395..3d37b4149 100644 --- a/tests/testthat/test-l_fmt_number.R +++ b/tests/testthat/test-l_fmt_number.R @@ -15,7 +15,7 @@ test_that("the `fmt_number()` function works correctly in the LaTeX context", { # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + tbl_latex <- gt(data_tbl) # Format the `num_1` column to 2 decimal places, use all other defaults expect_equal( diff --git a/tests/testthat/test-l_fmt_passthrough.R b/tests/testthat/test-l_fmt_passthrough.R index 18aae9378..a8c062a32 100644 --- a/tests/testthat/test-l_fmt_passthrough.R +++ b/tests/testthat/test-l_fmt_passthrough.R @@ -16,7 +16,7 @@ test_that("the `fmt_passthrough()` function works correctly", { # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + tbl_latex <- gt(data_tbl) # # Format `fmt_passthrough()` with various options diff --git a/tests/testthat/test-l_fmt_percent.R b/tests/testthat/test-l_fmt_percent.R index f1a37493c..a59c14329 100644 --- a/tests/testthat/test-l_fmt_percent.R +++ b/tests/testthat/test-l_fmt_percent.R @@ -15,7 +15,7 @@ test_that("the `fmt_percent()` function works correctly in the LaTeX context", { # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + tbl_latex <- gt(data_tbl) # Format the `num_1` column to 2 decimal places, use all # other defaults; extract `output_df` and compare to expected values diff --git a/tests/testthat/test-l_fmt_scientific.R b/tests/testthat/test-l_fmt_scientific.R index c94a9595a..f1d995c1b 100644 --- a/tests/testthat/test-l_fmt_scientific.R +++ b/tests/testthat/test-l_fmt_scientific.R @@ -17,7 +17,7 @@ test_that("the `fmt_scientific()` function works correctly", { # Create a `tbl_latex` object with `gt()` and the # `data_tbl` dataset - tbl_latex <- gt(data = data_tbl) + tbl_latex <- gt(data_tbl) # Format the `num_1` column to 2 decimal places, use all # other defaults; extract `output_df` in the HTML context diff --git a/tests/testthat/test-l_table_parts.R b/tests/testthat/test-l_table_parts.R index 4afca2951..358a2535b 100644 --- a/tests/testthat/test-l_table_parts.R +++ b/tests/testthat/test-l_table_parts.R @@ -7,10 +7,9 @@ mtcars_short <- mtcars[1:5, ] test_that("a gt table contains the expected heading components", { - # Create a `tbl_latex` object with `gt()`; this table - # contains a title + # Create a `tbl_latex` object with `gt()`; this table contains a title tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% tab_header(title = "test title") # Expect a characteristic pattern @@ -24,7 +23,7 @@ test_that("a gt table contains the expected heading components", { # Create a `tbl_latex` object with `gt()`; this table # contains a title and a subtitle tbl_latex <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% tab_header(title = "test title", subtitle = "test subtitle") # Expect a characteristic pattern @@ -80,7 +79,7 @@ test_that("a gt table contains the expected stubhead label", { # Create a `tbl_latex` object with `gt()`; this table # contains a stub and a stubhead caption tbl_latex <- - gt(data = mtcars_short, rownames_to_stub = TRUE) %>% + gt(mtcars_short, rownames_to_stub = TRUE) %>% tab_stubhead(label = "the mtcars") # Expect a characteristic pattern @@ -99,7 +98,7 @@ test_that("a gt table contains the expected column spanner labels", { # contains the spanner heading `perimeter` over the # `peri` and `shape` column labels tbl_latex <- - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = "perimeter", columns = c("peri", "shape") @@ -121,7 +120,7 @@ test_that("a gt table contains the expected column spanner labels", { # `peri` and `shape` column labels (this time, using # `c()` to define the columns) tbl_latex <- - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = "perimeter", columns = c(peri, shape) @@ -141,7 +140,7 @@ test_that("a gt table contains the expected column spanner labels", { # Expect an error when using column labels # that don't exist expect_error( - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = "perimeter", columns = c(peris, shapes) diff --git a/tests/testthat/test-r_table_parts.R b/tests/testthat/test-r_table_parts.R index fbc176526..86abccc59 100644 --- a/tests/testthat/test-r_table_parts.R +++ b/tests/testthat/test-r_table_parts.R @@ -6,7 +6,7 @@ test_that("a gt table contains the expected column spanner labels", { # contains the spanner heading `perimeter` over the # `peri` and `shape` column labels tbl_rtf <- - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = "perimeter", columns = c("peri", "shape") diff --git a/tests/testthat/test-table_parts.R b/tests/testthat/test-table_parts.R index c30aa7298..e6e3d71c7 100644 --- a/tests/testthat/test-table_parts.R +++ b/tests/testthat/test-table_parts.R @@ -44,7 +44,7 @@ test_that("a gt table contains the expected heading components", { # Create a `tbl_html` object with `gt()`; this table # contains a title tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% tab_header(title = "test heading") %>% render_as_html() %>% xml2::read_html() @@ -69,7 +69,7 @@ test_that("a gt table contains the expected heading components", { # Create a `gt_tbl` object with `gt()`; this table # contains a title and a subtitle tbl_html <- - gt(data = mtcars_short) %>% + gt(mtcars_short) %>% tab_header( title = "test title", subtitle = "test subtitle") %>% @@ -132,7 +132,7 @@ test_that("a gt table contains the expected stubhead label", { # Create a `tbl_html` object with `gt()`; this table # contains a stub and a stubhead label tbl_html <- - gt(data = mtcars_short, rownames_to_stub = TRUE) %>% + gt(mtcars_short, rownames_to_stub = TRUE) %>% tab_stubhead(label = "the mtcars") %>% render_as_html() %>% xml2::read_html() @@ -153,7 +153,7 @@ test_that("a gt table contains the expected spanner column labels", { # contains the spanner heading `perimeter` over the # `peri` and `shape` column labels tbl_html <- - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = "perimeter", columns = c("peri", "shape")) %>% @@ -172,7 +172,7 @@ test_that("a gt table contains the expected spanner column labels", { # `peri` and `shape` column labels (this time, using # `c()` to define the columns) tbl_html <- - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = "perimeter", columns = c(peri, shape)) %>% @@ -190,7 +190,7 @@ test_that("a gt table contains the expected spanner column labels", { # contains the spanner heading `perimeter` that is formatted # with Markdown via `md()` tbl_html <- - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = md("*perimeter*"), columns = c("peri", "shape")) %>% @@ -205,7 +205,7 @@ test_that("a gt table contains the expected spanner column labels", { # contains the spanner heading `perimeter` that is formatted # with HTML via `html()` tbl_html <- - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = html("perimeter"), columns = c("peri", "shape")) %>% @@ -219,7 +219,7 @@ test_that("a gt table contains the expected spanner column labels", { # Expect an error when using column labels # that don't exist expect_error( - gt(data = rock) %>% + gt(rock) %>% tab_spanner( label = "perimeter", columns = c(peris, shapes)) @@ -320,7 +320,7 @@ test_that("`tab_spanner()` exclusively uses IDs for arranging spanners", { test_that("`tab_spanner()` doesn't adversely affect column alignment", { tbl_html <- - gt(data = airquality) %>% + gt(airquality) %>% cols_move_to_start(columns = c(Month, Day)) %>% cols_label(Solar.R = html("Solar
Radiation")) %>% tab_spanner( diff --git a/vignettes/intro-creating-gt-tables.Rmd b/vignettes/intro-creating-gt-tables.Rmd index d2c5868f6..035d55c48 100644 --- a/vignettes/intro-creating-gt-tables.Rmd +++ b/vignettes/intro-creating-gt-tables.Rmd @@ -52,7 +52,7 @@ The main entry point into the **gt** API is the `gt()` function. If we pass `isl ```{r simple_gt_table} # Create a display table showing ten of # the largest islands in the world -gt_tbl <- gt(data = islands_tbl) +gt_tbl <- gt(islands_tbl) # Show the gt Table gt_tbl @@ -302,7 +302,7 @@ airquality_m <- # Create a display table using the `airquality` # dataset; arrange columns into groups gt_tbl <- - gt(data = airquality_m) %>% + gt(airquality_m) %>% tab_header( title = "New York Air Quality Measurements", subtitle = "Daily measurements in New York City (May 1-10, 1973)"