cell color by value and text transformations in gt
Introduction
The gt package is a popular data visualization library in R, known for its flexibility and customizability. One of its powerful features is the ability to transform cells based on specific conditions or values. In this article, we’ll explore how to use these capabilities to create tables with colored cells and apply text transformations.
Background
The gt package provides a high-level interface for creating interactive visualizations. Its core function, gt(), creates a table that can be customized using various methods, including coloring and text transformation.
One of the new features introduced in version 0.6.4 is the data_color() method, which allows us to color cells based on specific conditions or values. Another useful feature is the text_transform() function, which enables us to apply transformations to cell contents.
Example Data
To illustrate these concepts, let’s consider a sample dataset:
library(dplyr)
library(gt)
dat <- data.frame(
flag = c("safe", "out", "tie"),
url1 = c("https://www.google.com", "https://www.yahoo.com", "https://www.bing.com"),
url2 = c("https://www.one.com", "https://www.two.com", "https://www.three.com"),
value1 = c(1, NA, 3),
value2 = c(1.1, NA, 8)
)
gt(dat) %>%
cols_hide(columns = c("flag", "url1", "url2")) %>%
data_color(
direction = "row",
palette = "viridis"
) %>%
text_transform(fn = function(x) glue("<a href=\"{dat$url1}\" target=\"_blank\">{x}</a> {dat$flag}"))
This code creates a table with colored cells and applies a text transformation to the cell contents.
Coloring Cells by Value
The data_color() method allows us to color cells based on specific conditions or values. In this example, we’re using the direction = "row" argument to color cells based on the value in each row.
To achieve this, we need to define a function that takes a value and returns a color. We can use the viridis palette, which is a popular choice for data visualization.
The code snippet below demonstrates how to do this:
cols_color <- function(x) {
if (is.na(x)) return("transparent")
else return(palette("viridis", x))
}
gt(dat) %>%
cols_hide(columns = c("flag", "url1", "url2")) %>%
data_color(
direction = "row",
palette = "viridis",
fn = cols_color
)
In this example, we define a function cols_color() that takes a value and returns a color based on the value. If the value is NA, it returns transparent; otherwise, it uses the viridis palette to determine the color.
Text Transformations
The text_transform() function enables us to apply transformations to cell contents. We can use this function to create links to external websites or apply other text transformations.
Let’s demonstrate how to create a link to an external website:
gt(dat) %>%
cols_hide(columns = c("flag", "url1", "url2")) %>%
text_transform(
fn = function(x) glue("<a href=\"{dat$url1}\" target=\"_blank\">{x}</a> {dat$flag}"),
location = cells_body("value1")
)
In this example, we define a function that takes a value and returns the original value with a link to the external website.
Alternative Approach using purrr::reduce()
The purrr::reduce() function can be used as an alternative approach to apply transformations to cells. Let’s demonstrate how to do this:
cols <- grep("value*", names(dat), value = TRUE)
gt(dat) %>%
cols_hide(columns = c("flag", "url1", "url2")) %>%
data_color(
direction = "row",
palette = "viridis"
) %>%
purrr::reduce(cols, \(x, col) {
url_col <- gsub("value", "url", col)
text_transform(x,
fn = function(x) glue("<a href=\"{dat[[url_col]]}\" target=\"_blank\">{x}</a> {dat$flag}"),
location = cells_body(col)
}, .init = .)
In this example, we use the purrr::reduce() function to apply transformations to cells. We define a function that takes a value and returns the original value with a link to the external website.
Conclusion
In this article, we explored how to use the gt package to create tables with colored cells and apply text transformations. We demonstrated two approaches: using the data_color() method and applying text transformations directly to cells. Additionally, we showed an alternative approach using the purrr::reduce() function.
By mastering these techniques, you can create custom visualizations that showcase your data in a unique and engaging way.
Last modified on 2024-05-01