Skip to content

Instantly share code, notes, and snippets.

@jthomasmock
Created June 7, 2022 16:57
Show Gist options
  • Save jthomasmock/8e3e616e5bdbe078bd4a85abf9bb7fc3 to your computer and use it in GitHub Desktop.
Save jthomasmock/8e3e616e5bdbe078bd4a85abf9bb7fc3 to your computer and use it in GitHub Desktop.
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")
#' ![](goofy-coney_reprex_files/figure-gfm/unnamed-chunk-2-1.png)
# Gist URL https://gist.github.com/8e3e616e5bdbe078bd4a85abf9bb7fc3
# Code image at: ![](https://i.imgur.com/zMnqycn.png)
@jthomasmock
Copy link
Author

jthomasmock commented Jun 7, 2022

Screen Shot 2022-06-07 at 11 58 55 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment