Created
June 7, 2022 16:57
-
-
Save jthomasmock/8e3e616e5bdbe078bd4a85abf9bb7fc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(gt) | |
library(gtExtras) | |
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 | |
dat <- tibble( | |
group = c("a","b","c"), | |
success = c(0, 25, 50), | |
attempts = c(50, 50, 50) | |
) %>% | |
mutate( | |
percentage = success / attempts, | |
proportion = paste(success, attempts, sep = "/") | |
) %>% | |
gt() | |
out_tab <- dat %>% | |
text_transform( | |
cells_body(proportion), | |
fn = function(x){ | |
vals <- gt_index(dat, percentage) | |
col_vals <- scales::col_numeric(c("white", "red"), domain = c(0,1))(vals) | |
glue::glue('<span style=\"display: inline-block; direction: ltr; border-radius: 4px; padding-right: 2px; background-color: {col_vals}; width: 100%\"> {x} </span>') | |
} | |
) %>% | |
cols_align("center", proportion) %>% | |
gtsave_extra("out-tab.png", selector = "table") | |
magick::image_read("out-tab.png") | |
#'  | |
# Gist URL https://gist.github.com/8e3e616e5bdbe078bd4a85abf9bb7fc3 | |
# Code image at:  |
Author
jthomasmock
commented
Jun 7, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment