### Merge and Stack Text Example Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_merge_stack.html This example demonstrates how to use gt_merge_stack to combine team nicknames and divisions, applying specific formatting for each. It requires the gt and dplyr packages and reads data from a URL. ```r library(gt) teams <- "https://github.com/nflverse/nflfastR-data/raw/master/teams_colors_logos.rds" team_df <- readRDS(url(teams)) stacked_tab <- team_df %>% dplyr::select(team_nick, team_abbr, team_conf, team_division, team_wordmark) %>% head(8) %>% gt(groupname_col = "team_conf") %>% gt_merge_stack(col1 = team_nick, col2 = team_division) %>% gt_img_rows(team_wordmark) ``` -------------------------------- ### Install gtExtras from GitHub Source: https://github.com/jthomasmock/gtextras/blob/master/README.md Install the development version of gtExtras directly from GitHub. This is useful for accessing the latest features or bug fixes before they are released on CRAN. Ensure 'remotes' is installed if you haven't used it before. ```r # if needed install.packages("remotes") remotes::install_github("jthomasmock/gtExtras") ``` -------------------------------- ### Format Numeric Column with fmt_pad_num Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/fmt_pad_num.html This example demonstrates how to use fmt_pad_num to format a numeric column, specifying the number of decimal places to display. It requires the gt package. ```r library(gt) padded_tab <- data.frame(numbers = c(1.2345, 12.345, 123.45, 1234.5, 12345)) %>% gt() %>% fmt_pad_num(columns = numbers, nsmall = 4) ``` -------------------------------- ### Generating and Displaying Tables in Two Columns Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_two_column_layout.html This example demonstrates a more advanced approach using a custom function to generate and format gt tables before arranging them in a two-column layout. It utilizes gt_double_table to create the tables. ```R my_gt_fn <- function(x) { gt(x) %>% gtExtras::gt_color_rows(columns = row_n, domain = 1:32) } my_tables <- gt_double_table(my_cars, my_gt_fn, nrows = nrow(my_cars) / 2) gt_two_column_layout(my_tables) ``` -------------------------------- ### Install gtExtras from CRAN Source: https://github.com/jthomasmock/gtextras/blob/master/README.md Install the gtExtras package from the Comprehensive R Archive Network (CRAN). This is the standard method for installing stable releases. ```r install.packages("gtExtras") ``` -------------------------------- ### Create Two Tables for Two-Column Layout Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_two_column_layout.html This example demonstrates how to create two separate gt tables from a dataset, preparing them for use with the gt_two_column_layout function. It involves splitting the data and applying styling to each table individually. ```R library(gt) my_cars <- mtcars %>% dplyr::mutate(row_n = dplyr::row_number(), .before = mpg) %>% dplyr::select(row_n, mpg:drat) tab1 <- my_cars %>% dplyr::slice(1:16) %>% gt() %>% gtExtras::gt_color_rows(columns = row_n, domain = 1:32) tab2 <- my_cars %>% dplyr::slice(17:32) %>% gt() ``` -------------------------------- ### Create Example Win/Loss Dataframe Source: https://github.com/jthomasmock/gtextras/blob/master/docs/index.html Generates a tibble with team names, win/loss/tie counts, and a list-column of game outcomes. This is used to demonstrate the `gt_plt_winloss` function. ```r create_input_df <- function(repeats = 3){ input_df <- dplyr::tibble( team = c("A1", "B2", "C3", "C4"), Wins = c(3, 2, 1, 1), Losses = c(2, 3, 2, 4), Ties = c(0, 0, 2, 0), outcomes = list( c(1, .5, 0) %>% rep(each = repeats), c(0, 1, 0.5) %>% rep(each = repeats), c(0, 0.5, 1) %>% rep(each = repeats), c(0.5, 1, 0) %>% rep(each = repeats) ) ) input_df } create_input_df(5) %>% dplyr::glimpse() ``` -------------------------------- ### Apply ESPN Theme to a gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_theme_espn.html This example demonstrates how to apply the ESPN theme to a gt table created from the head of the mtcars dataset. Ensure the gt package is loaded. ```R library(gt) themed_tab <- head(mtcars) %>% gt() %>% gt_theme_espn() ``` -------------------------------- ### Apply Excel Theme to gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_theme_excel.html Demonstrates how to apply the Excel theme to a gt table using the gt_theme_excel function. This example also includes adding a table header. ```R library(gt) themed_tab <- head(mtcars) %>% gt() %>% gt_theme_excel() %>% tab_header(title = "Styled like your old pal, Excel") ``` -------------------------------- ### Create gt table with distribution plots Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_plt_dist.html This example demonstrates how to create a gt table with distribution plots (density plots by default) in a column. The data must be pre-processed into a list of numeric values for the target column. ```R library(gt) gt_sparkline_tab <- mtcars %>% dplyr::group_by(cyl) %>% # must end up with list of data for each row in the input dataframe dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>% gt() %>% gt_plt_dist(mpg_data) ``` -------------------------------- ### Apply FiveThirtyEight theme to a gt table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_theme_538.html This example demonstrates how to apply the FiveThirtyEight theme to a gt table using the gt_theme_538 function. It requires the gt package and the gtExtras package to be loaded. ```r library(gt) themed_tab <- head(mtcars) %>% gt() %>% gt_theme_538() ``` -------------------------------- ### Generate DataFrame with Groups and Custom Mean/SD Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/generate_df.html This example demonstrates how to use generate_df to create a dataframe with 100 rows distributed across 5 groups. It customizes the mean for each group and then summarizes the generated data to show that the means and standard deviations are approximately as specified. The default sd calculation (mean/10) is also shown. ```R library(dplyr) #> #> Attaching package: ‘dplyr’ #> The following objects are masked from ‘package:stats’: #> #> filter, lag #> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union generate_df( 100L, n_grps = 5, mean = seq(10, 50, length.out = 5) ) %>% group_by(grp) %>% summarise( mean = mean(values), # mean is approx mean sd = sd(values), # sd is approx sd n = n(), # each grp is of length n # showing that the sd default of mean/10 works `mean/sd` = round(mean / sd, 1) ) #> # A tibble: 5 × 5 #> grp mean sd n `mean/sd` #> #> 1 grp-1 10.1 1.07 100 9.4 #> 2 grp-2 19.7 1.97 100 10 #> 3 grp-3 30.1 2.80 100 10.8 #> 4 grp-4 39.9 3.95 100 10.1 #> 5 grp-5 50.0 5.31 100 9.4 ``` -------------------------------- ### Create gt Table with Win/Loss Plot Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_plt_winloss.html This example demonstrates how to create a gt table and add a win/loss point plot to a specified column. Ensure the column is a list-column containing win/loss/tie data before applying the function. ```R library(gt) set.seed(37) data_in <- dplyr::tibble( grp = rep(c("A", "B", "C"), each = 10), wins = sample(c(0,1,.5), size = 30, prob = c(0.45, 0.45, 0.1), replace = TRUE) ) %>% dplyr::group_by(grp) %>% dplyr::summarize(wins=list(wins), .groups = "drop") data_in win_table <- data_in %>% gt() %>% gt_plt_winloss(wins) ``` -------------------------------- ### Create a gt Badge with Custom Palette Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_badge.html This example demonstrates how to use the gt_badge function to add badges to a 'cyl' column in a gt table. It shows how to specify a custom color palette using a named vector to map specific cylinder counts to colors. ```r library(gt) head(mtcars) %>% dplyr::mutate(cyl = paste(cyl, " Cyl")) %>% gt() %>% gt_badge(cyl, palette = c("4 Cyl"="red","6 Cyl"="blue","8 Cyl"="green")) ``` -------------------------------- ### Create a Point Plot in a gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_plt_point.html This example demonstrates how to create a point plot for a column in a gt table. It uses gt_duplicate_column to create a new column for plotting and then applies gt_plt_point with specified accuracy and width. ```r point_tab <- dplyr::tibble(x = c(seq(1.2e6, 2e6, length.out = 5))) %>% gt::gt() %>% gt_duplicate_column(x,dupe_name = "point_plot") %>% gt_plt_point(point_plot, accuracy = .1, width = 25) %>% gt::fmt_number(x, suffixing = TRUE, decimals = 1) ``` -------------------------------- ### Apply Dark Theme to a gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_theme_dark.html This example demonstrates how to apply the dark theme to a gt table. It first creates a basic gt table from the head of the mtcars dataset and then applies the gt_theme_dark function to it, along with a table header. ```R library(gt) dark_tab <- head(mtcars) %>% gt() %>% gt_theme_dark() %>% tab_header(title = "Dark mode table") ``` -------------------------------- ### Create Table with Dot and Bar Chart Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_plt_dot.html This example demonstrates how to use gt_plt_dot to add a colored dot and bar chart to a gt table. It requires the gt and dplyr packages. The palette is specified using a package name from {paletteer}. ```R library(gt) dot_bar_tab <- mtcars %>% head() %>% dplyr::mutate(cars = sapply(strsplit(rownames(.)," "), `[` , 1)) %>% dplyr::select(cars, mpg, disp) %>% gt() %>% gt_plt_dot(disp, cars, palette = "ggthemes::fivethirtyeight") %>% cols_width(cars ~ px(125)) ``` -------------------------------- ### Add Sparklines to a gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_plt_sparkline.html This example demonstrates how to add sparklines to a gt table. The data for the sparkline must be pre-formatted as a list of numeric values in the target column. Requires the gt and dplyr packages. ```r library(gt) gt_sparkline_tab <- mtcars %>% dplyr::group_by(cyl) %>% # must end up with list of data for each row in the input dataframe dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>% gt() %>% gt_plt_sparkline(mpg_data) ``` -------------------------------- ### Create a Percentile Dot Plot Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_plt_percentile.html This example demonstrates how to create a percentile dot plot using the gt_plt_percentile function. It first creates a tibble, converts it to a gt table, duplicates a column, and then applies the gt_plt_percentile function to the duplicated column. ```r library(gt) dot_plt <- dplyr::tibble(x = c(seq(10, 90, length.out = 5))) %>% gt() %>% gt_duplicate_column(x,dupe_name = "dot_plot") %>% gt_plt_percentile(dot_plot) ``` -------------------------------- ### Generate two gt tables using gt_double_table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_double_table.html This example demonstrates using gt_double_table to create a list of two gt tables. It takes the mtcars dataset and the previously defined my_gt_function, specifying the number of rows for the split. The output is a list of two gt_tbl objects. ```R two_tables <- gt_double_table(mtcars, my_gt_function, nrows = 16) # list of two gt_tbl objects # ready to pass to gtExtras::gt_two_column_layout() str(two_tables, max.level = 1) ``` -------------------------------- ### Basic Rank Change Indicators Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_fa_rank_change.html Converts a column of integers into rank change indicators (arrows). Use 'font_color = "match"' to align arrow and text colors. This example uses the default palette and arrow type. ```r rank_table <- dplyr::tibble(x = c(1:3, -1, -2, -5, 0)) %>% gt::gt() %>% gt_fa_rank_change(x, font_color = "match") ``` -------------------------------- ### Add Font Awesome Icons to a gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_add_fa_icons.html This example demonstrates how to use gt_add_fa_icons to replace values in the 'cyl' column with 'car' Font Awesome icons within a gt table. It requires the gt and gtExtras libraries. ```r library(gt) library(gtExtras) fa_cars <- mtcars[1:5, 1:4] %>% head() %>% gt() %>% gt_add_fa_icons(cyl, name = "car") ``` -------------------------------- ### Add Web Images to gt Table Rows Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_img_rows.html This example demonstrates how to use gt_img_rows to embed web images into a gt table. It fetches team data, selects relevant columns, creates a gt table, and then applies gt_img_rows to the 'logo' column, specifying 'web' as the image source and setting a height. ```R library(gt) teams <- "https://github.com/nflverse/nflfastR-data/raw/master/teams_colors_logos.rds" team_df <- readRDS(url(teams)) logo_table <- team_df %>% dplyr::select(team_wordmark, team_abbr, logo = team_logo_espn, team_name:team_conf) %>% head() %>% gt() %>% gt_img_rows(columns = team_wordmark, height = 25) %>% gt_img_rows(columns = logo, img_source = "web", height = 30) %>% tab_options(data_row.padding = px(1)) ``` -------------------------------- ### Prepare Sample Data and Create gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_index.html This snippet demonstrates preparing sample data using dplyr and creating a basic gt table with row groups. It highlights how gt orders row groups based on the first observation of unique items. ```R library(gt) # This is a key step, as gt will create the row groups # based on first observation of the unique row items # this sampling will return a row-group order for cyl of 6,4,8 set.seed(1234) sliced_data <- mtcars %>% dplyr::group_by(cyl) %>% dplyr::slice_head(n = 3) %>% dplyr::ungroup() %>% # randomize the order dplyr::slice_sample(n = 9) # not in "order" yet sliced_data$cyl #> [1] 6 6 6 4 8 4 8 8 4 # But unique order of 6,4,8 unique(sliced_data$cyl) #> [1] 6 4 8 # creating a standalone basic table test_tab <- sliced_data %>% gt(groupname_col = "cyl") ``` -------------------------------- ### Add a dashed divider to a column Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_add_divider.html This example demonstrates how to add a dashed divider to the 'cyl' column of a gt table. It requires the gt and gtExtras packages. ```r library(gt) basic_divider <- head(mtcars) %>% gt() %>% gt_add_divider(columns = "cyl", style = "dashed") ``` -------------------------------- ### Add Sparklines to a gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_sparkline.html This example demonstrates how to use gt_sparkline to add sparklines to a column in a gt table. Ensure the data for the column is a list of numeric values. ```r library(gt) gt_sparkline_tab <- mtcars %>% dplyr::group_by(cyl) %>% dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>% gt() %>% gt_sparkline(mpg_data) ``` -------------------------------- ### Create gt Table with HTML Bar Plots Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_plt_bar_pct.html Demonstrates creating a gt table and adding two types of HTML bar plots: one where values are scaled to the max and another where they are not. It also shows how to align columns and set column widths. ```R library(gt) gt_bar_plot_tab <- mtcars %>% head() %>% dplyr::select(cyl, mpg) %>% dplyr::mutate(mpg_pct_max = round(mpg/max(mpg) * 100, digits = 2), mpg_scaled = mpg/max(mpg) * 100) %>% dplyr::mutate(mpg_unscaled = mpg) %>% gt() %>% gt_plt_bar_pct(column = mpg_scaled, scaled = TRUE) %>% gt_plt_bar_pct(column = mpg_unscaled, scaled = FALSE, fill = "blue", background = "lightblue") %>% cols_align("center", contains("scale")) %>% cols_width(4 ~ px(125), 5 ~ px(125)) ``` -------------------------------- ### Create a gt table with color boxes Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_color_box.html Demonstrates how to apply gt_color_box to a gt table. The first call uses a named palette from the ggsci package, while the second uses a custom vector of colors. Both specify the domain for value mapping. ```R library(gt) test_data <- dplyr::tibble(x = letters[1:10], y = seq(100, 10, by = -10), z = seq(10, 100, by = 10)) color_box_tab <- test_data %>% gt() %>% gt_color_box(columns = y, domain = 0:100, palette = "ggsci::blue_material") %>% gt_color_box(columns = z, domain = 0:100, palette = c("purple", "lightgrey", "green")) ``` -------------------------------- ### Summarize data for sparklines Source: https://github.com/jthomasmock/gtextras/blob/master/docs/articles/plots-in-gt.html Prepares data for sparklines by grouping and summarizing, creating a list-column of values for each group. This is a necessary step before applying gt_sparkline(). ```r car_summary <- mtcars %>% dplyr::group_by(cyl) %>% dplyr::summarize( mean = mean(mpg), sd = sd(mpg), # must end up with list of data for each row in the input dataframe mpg_data = list(mpg), .groups = "drop" ) ``` -------------------------------- ### Add sparklines to a gt table column Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_kable_sparkline.html This example demonstrates how to use gt_kable_sparkline to add sparklines to a gt table. The data in the specified column must be a list of numeric values. The height of the sparkline can be adjusted. ```r library(gt) kable_sparkline_tab <- mtcars %>% dplyr::group_by(cyl) %>% dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>% gt() %>% gt_kable_sparkline(mpg_data, height = 45) ``` -------------------------------- ### Add FontAwesome Icons to a gt Column Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_fa_column.html This example demonstrates how to use gt_fa_column to replace character strings in a 'man' column with FontAwesome icons. The 'man' column is created based on the 'am' column in the mtcars dataset. ```r library(gt) fa_cars <- mtcars %>% head() %>% dplyr::select(cyl, mpg, am, gear) %>% dplyr::mutate(man = ifelse(am == 1, "cog", "cogs")) %>% gt() %>% gt_fa_column(man) ``` -------------------------------- ### Create gt Table with List Column Data Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_plt_bar_stack.html This snippet demonstrates how to prepare data for gt_plt_bar_stack by grouping and summarizing it into a list column. This is a prerequisite for using the function. ```r library(gt) library(dplyr) ex_df <- dplyr::tibble( x = c("Example 1","Example 1", "Example 1","Example 2","Example 2","Example 2", "Example 3","Example 3","Example 3","Example 4","Example 4", "Example 4"), measure = c("Measure 1","Measure 2", "Measure 3","Measure 1","Measure 2","Measure 3", "Measure 1","Measure 2","Measure 3","Measure 1","Measure 2", "Measure 3"), data = c(30, 20, 50, 30, 30, 40, 30, 40, 30, 30, 50, 20) ) tab_df <- ex_df %>% group_by(x) %>% summarise(list_data = list(data)) ``` -------------------------------- ### Add Font Awesome Rating Icons to a gt Column Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_fa_rating.html This example demonstrates how to use gt_fa_rating to display a 'rating' column in a gt table using Font Awesome 'r-project' icons. It requires the gt and dplyr packages. ```r library(gt) set.seed(37) rating_table <- mtcars %>% dplyr::select(mpg:wt) %>% dplyr::slice(1:5) %>% dplyr::mutate(rating = sample(1:5, size = 5, TRUE)) %>% gt() %>% gt_fa_rating(rating, icon = "r-project") ``` -------------------------------- ### Plot NFL Game Results with gt_plt_winloss Source: https://github.com/jthomasmock/gtextras/blob/master/docs/index.html Visualizes NFL game results for a specific season and conference using `gt_plt_winloss`. This example integrates data from the `nflreadr` package and applies several gt functions for enhanced table presentation. ```r library(dplyr) library(tidyr) library(nflreadr) games_df <- nflreadr::load_schedules() %>% filter(season == 2020, game_type == "REG") %>% select(game_id, team_home = home_team, team_away = away_team, result, week) %>% pivot_longer(contains('team'), names_to = 'home_away', values_to = 'team', names_prefix = 'team_') %>% mutate( result = ifelse(home_away == 'home', result, -result), win = ifelse(result == 0 , 0.5, ifelse(result > 0, 1, 0)) ) %>% select(week, team, win) %>% mutate( team = case_when( team == 'STL' ~ 'LA', team == 'OAK' ~ 'LV', team == 'SD' ~ 'LAC', T ~ team ) ) team_df <- nflreadr::load_teams() %>% select(team_wordmark, team_abbr, team_conf, team_division) joined_df <- games_df %>% group_by(team) %>% summarise( Wins = length(win[win==1]), Losses = length(win[win==0]), outcomes = list(win), .groups = "drop") %>% left_join(team_df, by = c("team" = "team_abbr")) %>% select(team_wordmark, team_conf, team_division, Wins:outcomes) final_df <- joined_df %>% filter(team_conf == "AFC") %>% group_by(team_division) %>% arrange(desc(Wins)) %>% ungroup() %>% arrange(team_division) final_df %>% gt(groupname_col = "team_division") %>% gt_plt_winloss(outcomes, max_wins = 16) %>% gt_img_rows(columns = team_wordmark) %>% gt_theme_538() %>% tab_header(title = "2020 Results by Division for the AFC") ``` -------------------------------- ### Saving Tables in Two Columns to a File Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_two_column_layout.html This snippet illustrates how to save a two-column gt table layout directly to a file, such as a PNG image. Specify the output, filename, and dimensions as needed. ```R gt_two_column_layout(my_tables, output = "save", filename = "basic-two-col.png", vwidth = 550, vheight = 620) ``` -------------------------------- ### Displaying Tables in Two Columns Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_two_column_layout.html This snippet shows how to arrange two gt tables into a two-column layout for viewing. Ensure tables are in a list before passing to the function. ```R listed_tables <- list(tab1, tab2) gt_two_column_layout(listed_tables) ``` -------------------------------- ### HTML Helpers Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/index.html Utilize various HTML helper functions to simplify the creation of common HTML elements within gt tables, such as tooltips and hyperlinks. ```APIDOC ## HTML Helpers Various HTML helpers to avoid repeated boilerplate ### `gt_label_details()` Add a simple table with column names and matching labels ### `with_tooltip()` A helper to add basic tooltip inside a gt table ### `gt_hyperlink()` Add a basic hyperlink in a gt table ``` -------------------------------- ### gt_color_box Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_color_box.html Creates color boxes in a gt table. Numeric formatting arguments can be passed via '...'. ```APIDOC ## gt_color_box ### Description Creates `PFF`-style colorboxes in a `gt` table. Numeric formatting arguments can be passed via `...` and are forwarded to `scales::label_number()`. ### Arguments * **gt_object** (gt_tbl) - An existing gt table object. * **columns** (character or vector of character) - The columns to apply color boxes to. * **palette** (character or function) - The colors or color function to map values to. Can be a character vector (e.g., `c("white", "red")` or hex colors) or a named palette from the `{paletteer}` package (e.g., `"package::palette_name"`). The string `'pff'` will use a blue -> green -> yellow -> orange -> red palette. * **...** - Additional arguments passed to `scales::label_number()`, primarily for formatting numbers inside the color box. * **domain** (numeric vector) - The possible values that can be mapped, e.g., `c(0, 100)`. * **width** (numeric) - The width of the coloring area in pixels, defaults to 70. * **font_weight** (character) - The font weight for the text inside the box, defaults to "bold". ### Value An object of class `gt_tbl`. ### Examples ```R library(gt) test_data <- dplyr::tibble( x = letters[1:10], y = seq(100, 10, by = -10), z = seq(10, 100, by = 10) ) color_box_tab <- test_data %>% gt() %>% gt_color_box(columns = y, domain = 0:100, palette = "ggsci::blue_material") %>% gt_color_box(columns = z, domain = 0:100, palette = c("purple", "lightgrey", "green")) ``` ``` -------------------------------- ### Repeat Font Awesome Icons in a gt Table Column Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_fa_repeats.html Use this snippet to replace integer values in a specified column of a gt table with repeated Font Awesome icons. Ensure the 'fontawesome' package is installed and the 'gt' package is loaded. The 'name' argument specifies the icon to use. ```R library(gt) mtcars[1:5, 1:4] %>% gt() %>% gt_fa_repeats(cyl, name = "car") ``` -------------------------------- ### Pad numbers for decimal alignment Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/pad_fn.html Use pad_fn within gt::fmt to align numbers at the decimal point. Ensure a monospace font is applied to the cells for correct visual alignment. ```R library(gt) padded_tab <- data.frame(x = c(1.2345, 12.345, 123.45, 1234.5, 12345)) %>% gt() %>% fmt(fns = function(x){pad_fn(x, nsmall = 4)}) %>% tab_style( # MUST USE A MONO-SPACED FONT # https://fonts.google.com/?category=Monospace style = cell_text(font = google_font("Fira Mono")), locations = cells_body(columns = x) ) ``` -------------------------------- ### Utilities Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/index.html Access helper functions and utilities that extend the functionality of gt tables, including text alignment and number padding. ```APIDOC ## Utilities Helper functions and utilities with features not _yet_ built into [gt](https://gt.rstudio.com/). ### `fmt_symbol_first()` Aligning first-row text only ### `fmt_pad_num()` ``` -------------------------------- ### fmt_pad_num Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/fmt_pad_num.html Formats numeric columns to align at the decimal point by removing trailing zeroes and adding padding spaces. It's a wrapper around gt::fmt() and gtExtras::pad_fn(), and requires monospaced fonts for proper display. ```APIDOC ## fmt_pad_num ### Description Formats numeric columns to align at the decimal point by removing trailing zeroes and adding padding spaces. This function requires the use of monospaced fonts. ### Function Signature `fmt_pad_num(gt_object, columns, nsmall = 2, gfont = "Fira Mono")` ### Arguments * **gt_object** (gt_tbl) - An existing gt table object. * **columns** (character or vector) - The columns to format. Accepts column names, indices, or select helper functions like `starts_with()`, `ends_with()`, etc. * **nsmall** (numeric) - The maximum number of decimal places to display. Defaults to 2. * **gfont** (character) - The name of a Google Font to use. Must be a monospaced font. Defaults to "Fira Mono". ### Value An object of class `gt_tbl` with the specified columns formatted. ### Examples ```R library(gt) padded_tab <- data.frame(numbers = c(1.2345, 12.345, 123.45, 1234.5, 12345)) %>% gt() %>% fmt_pad_num(columns = numbers, nsmall = 4) ``` ``` -------------------------------- ### Load Libraries for gtExtras and ggplot2 Source: https://github.com/jthomasmock/gtextras/blob/master/docs/articles/plots-in-gt.html Load the required R libraries for using gt, gtExtras, dplyr, and ggplot2. Ensure dplyr is loaded without conflicts. ```r library(gt) library(gtExtras) library(dplyr, warn.conflicts = FALSE) library(ggplot2) ``` -------------------------------- ### fmt_pct_extra Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/fmt_pct_extra.html Converts numeric columns to percentages, with an option to display values less than 1% as '<1%' in grey. It leverages the scales::label_percent function for formatting. ```APIDOC ## fmt_pct_extra ### Description Converts numeric columns to percentages, with an option to display values less than 1% as '<1%' in grey. It leverages the scales::label_percent function for formatting. ### Usage ```r fmt_pct_extra(gt_object, columns, ..., scale = 1) ``` ### Arguments * `gt_object` (gt table): An existing gt table. * `columns` (character or vector): The columns to affect. * `...` (Additional arguments): Additional arguments passed to `scales::label_percent()`. * `scale` (numeric): A number to multiply values by, defaults to 1. ### Value A gt table. ### Examples ```r library(gt) pct_tab <- dplyr::tibble(x = c(.001,.05,.008,.1,.2,.5,.9)) %>% gt::gt() %>% fmt_pct_extra(x, scale = 100, accuracy=.1) ``` ``` -------------------------------- ### Create Percent Bars in gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/articles/plots-in-gt.html This snippet shows how to use gt_plt_bar_pct() to add percentage bars to a gt table. It demonstrates both scaling raw values and using pre-scaled percentage values, with options for custom fill and background colors. ```r mtcars %>% head() %>% dplyr::select(cyl, mpg) %>% dplyr::mutate(mpg_pct_max = round(mpg/max(mpg) * 100, digits = 2), mpg_scaled = mpg/max(mpg) * 100) %>% dplyr::mutate(mpg_unscaled = mpg) %>% gt() %>% gt_plt_bar_pct(column = mpg_scaled, scaled = TRUE) %>% gt_plt_bar_pct(column = mpg_unscaled, scaled = FALSE, fill = "blue", background = "lightblue") %>% cols_align("center", contains("scale")) %>% cols_width(4 ~ px(125), 5 ~ px(125)) ``` -------------------------------- ### Generate Win Loss Plot Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/articles/plots-in-gt.html Renders a gt table with inline Win Loss plots ('pills') and team wordmarks. It uses gtExtras for plotting and image embedding, and applies a theme. ```r final_df %>% gt(groupname_col = "team_division") %>% cols_label(team_wordmark = "") %>% cols_align("left", team_division) %>% gtExtras::gt_plt_winloss(outcomes, max_wins = 16, type = "pill") %>% gtExtras::gt_img_rows(columns = team_wordmark, height = 20) %>% gtExtras::gt_theme_538() %>% tab_header( title = gtExtras::add_text_img( "2020 Results by Division", url = "https://github.com/nflverse/nflfastR-data/raw/master/AFC.png", height = 30 ) ) %>% tab_options(data_row.padding = px(2)) ``` -------------------------------- ### Decimal Align Numeric Columns with pad_fn Source: https://github.com/jthomasmock/gtextras/blob/master/docs/index.html Use pad_fn() with gt::fmt() to decimal align numeric values in a column. This is useful when you want to align numbers by their decimal point but do not need to display extra trailing zeros. ```R data.frame(x = c(1.2345, 12.345, 123.45, 1234.5, 12345)) %>% gt() %>% fmt(fns = function(x){pad_fn(x, nsmall = 4)}) %>% tab_style( # MUST USE A MONO-SPACED FONT style = cell_text(font = google_font("Fira Mono")), locations = cells_body(columns = x) ) ``` -------------------------------- ### Apply Dot Matrix Theme to gt Table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_theme_dot_matrix.html Applies the dot matrix theme to an existing gt table. The `color` argument can be used to customize the row striping color. Additional arguments can be passed to `gt::tab_options()`. ```R library(gt) themed_tab <- head(mtcars) %>% gt() %>% gt_theme_dot_matrix() %>% tab_header(title = "Styled like dot matrix printer paper") ``` -------------------------------- ### gt_double_table Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_double_table.html This function takes data and a gt-generating function to create a list of two tables, suitable for use with gt_two_column_layout. ```APIDOC ## gt_double_table ### Description The `gt_double_table` function takes some data and a user-supplied function to generate two tables in a list. The user-defined function should accept a single argument (the data) and return a gt table object. ### Usage ```r gt_double_table(data, gt_fn, nrows = NULL, noisy = TRUE) ``` ### Arguments * **data** (tibble or dataframe) - The data to be passed into the supplied `gt_fn`. * **gt_fn** (function) - A user-defined function that accepts one argument (the data) and returns a gt table. It should follow the pattern `gt_function <- function(x) gt(x) %>% more_gt_code...`. * **nrows** (numeric, optional) - The number of rows to split the data at. Defaults to `NULL`, which attempts an approximate 50/50 split. * **noisy** (logical) - Indicates whether to return a warning if the `nrows` argument is not supplied. Defaults to `TRUE`. ### Value A `list` containing two `gt` table objects. ### Examples ```r library(gt) # Define a custom gt function my_gt_function <- function(x) { gt(x) %>% gtExtras::gt_color_rows(columns = mpg, domain = range(mtcars$mpg)) %>% tab_options(data_row.padding = px(3)) } # Create two tables using gt_double_table two_tables <- gt_double_table(mtcars, my_gt_function, nrows = 16) # The result is a list of two gt_tbl objects str(two_tables, max.level = 1) ``` ``` -------------------------------- ### Basic Row Highlighting Source: https://github.com/jthomasmock/gtextras/blob/master/docs/reference/gt_highlight_rows.html Applies a default light blue background and normal font weight to the second row of a gt table. This is useful for visually distinguishing specific entries. ```r library(gt) basic_use <- head(mtcars[,1:5]) %>% tibble::rownames_to_column("car") %>% gt() %>% gt_highlight_rows(rows = 2, font_weight = "normal") ```