Skip to content

Instantly share code, notes, and snippets.

@padpadpadpad
Created March 25, 2019 12:00
Show Gist options
  • Save padpadpadpad/41dc07aeb7edb6141f074dd94ccf62d9 to your computer and use it in GitHub Desktop.
Save padpadpadpad/41dc07aeb7edb6141f074dd94ccf62d9 to your computer and use it in GitHub Desktop.
example contrast table using emmeans, gt and html tools.
# load packages
library(gt)
library(htmltools)
library(emmeans)
library(dplyr)
library(tidyr)
# load example data
data(fiber)
?fiber
# run linear model
mod <- lm(strength ~ machine, fiber)
# do pairwise contrasts
emmeans::emmeans(mod, pairwise ~ machine)
# make contrast table
table <- emmeans::emmeans(mod, pairwise ~ machine)$contrasts %>%
data.frame(stringsAsFactors = FALSE) %>%
mutate(., p.value = round(p.value, 3)) %>%
mutate_at(., c('estimate', 'SE', 'df', 't.ratio'), function(x){round(x, 2)}) %>%
rename(`p value` = p.value, `t-ratio` = t.ratio, `d.f.` = df) %>%
gt(.) %>%
cols_align('center') %>%
tab_source_note(
source_note = "P value adjustment: tukey method for comparing a family of 3 estimates."
) %>%
tab_style(
style = cells_styles(
text_weight = "bold"),
locations = cells_data(
rows = `p value` < 0.05)
) %>%
gt:::as.tags.gt_tbl()
# change font
table[[1]]$children[[1]] <- gsub(
"font-family: [[:print:]]*\n",
"font-famuly: 'Times New Roman';\n",
table[[1]]$children[[1]]
)
html_print(table)
@ulisolovieva
Copy link

Hi! Thank you for sharing.
I keep getting this error, any ideas?

Error in writeImpl(text) :
Text to be written must be a length-one character vector

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