Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created March 28, 2024 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewheiss/ebe5156df7b64da9a14a27a1590b763b to your computer and use it in GitHub Desktop.
Save andrewheiss/ebe5156df7b64da9a14a27a1590b763b to your computer and use it in GitHub Desktop.
library(tidyverse)
library(tinytable)
inline_listify <- function(x) {
numbers <- seq_along(x)
prefixed <- paste0("(", numbers, ") ", x)
collapsed <- paste(prefixed, collapse = "; ")
return(collapsed)
}
transparency <- c("Doesn't engage in transparency", "Engages in transparency")
accountability <- c("Doesn't engage in accountability", "Engages in accountability")
govt <- c("Friendly relationship with government", "Criticized by government", "Under government crackdown")
orgs <- c("Amnesty International", "Greenpeace", "Oxfam", "Red Cross")
issues <- c("Emergency response", "Environment", "Human rights", "Refugee relief")
funding <- c("Funded primarily by many small private donations", "Funded primarily by a handful of wealthy private donors", "Funded primarily by government grants")
tbl_features_levels <- tribble(
~type, ~feature, ~levels,
"H~1a~", "Financial transparency", inline_listify(transparency),
"H~1b~", "Accountability", inline_listify(accountability),
"H~2~", "Relationship with host government", inline_listify(govt),
"Comparison", "Organization", inline_listify(orgs),
"Comparison", "Issue area", inline_listify(issues),
"Comparison", "Funding source", inline_listify(funding)
)
# A table!
tbl_settings <- "
colspec={X[0.15,l] X[0.2,l] X[0.5,l]},
rowsep=5pt,
cells={font=\\footnotesize},
"
tbl_features_levels |>
set_names(c("Purpose", "Feature/Attribute", "Levels")) |>
tt(width = 0.9) |>
format_tt(j = 1, markdown = TRUE) |>
style_tt(tabularray_inner = tbl_settings) |>
style_tt(j = 1, bootstrap_css = "width: 15%;") |>
style_tt(j = 2, bootstrap_css = "width: 20%;") |>
style_tt(j = 3, bootstrap_css = "width: 50%;")
# Another table!
withr::with_seed(12345, {
tbl_example_task <- tribble(
~Attribute, ~`Option 1`, ~`Option 2`, ~`Option 3`, ~None,
"Organization", sample(orgs, 1), sample(orgs, 1), sample(orgs, 1), "—",
"Issue area", sample(issues, 1), sample(issues, 1), sample(issues, 1), "—",
"Transparency", sample(transparency, 1), sample(transparency, 1), sample(transparency, 1), "—",
"Accountability", sample(accountability, 1), sample(accountability, 1), sample(accountability, 1), "—",
"Funding sources", sample(funding, 1), sample(funding, 1), sample(funding, 1), "—",
"Relationship with host government", sample(govt, 1), sample(govt, 1), sample(govt, 1), "—"
)
})
tbl_settings <- "
colspec={X[0.2,l] X[0.2,c] X[0.2,c] X[0.2,c] X[0.2,c]},
rowsep=5pt,
cells={font=\\footnotesize},
"
tbl_example_task |>
tt(width = 1) |>
style_tt(tabularray_inner = tbl_settings) |>
style_tt(j = 1, align = "l") |>
style_tt(j = 2:5, align = "c") |>
style_tt(j = 1:5, bootstrap_css = "width: 20%;")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment