knitr::opts_chunk$set(echo = TRUE, results = 'asis')
library(huxtable)
library(magrittr)

catn <- function (x) cat(x, '\n')

This can be run at the console, or rendered to markdown with rmarkdown::render.

catn('# Basic huxtable')

ht <- huxtable(a = letters[1:3], b = letters[4:6])

print_screen(ht)
print_md(ht)
catn('# Borders')

ht %<>% set_all_borders(1)

print_screen(ht)
print_md(ht)
catn('# Alignment')

ht  %>% insert_column(1:3) %>% set_align(c('right', 'center', 'left'), byrow = TRUE) -> ht2
print_screen(ht2)
print_md(ht2)
catn('# Caption')

ht %<>% set_caption('A table')
print_screen(ht)
print_md(ht)

for (al in c('topleft', 'topright', 'topcenter', 'top', 'bottomleft', 'bottomright', 'bottomcenter', 'bottom')) {
  tmp <- ht %>% set_caption_pos(al) %>% set_caption(paste('Align set to', al))
  tmp[1, ] <- c('Some long content', 'Some more long content')
  print_screen(tmp)
}
catn('# Header in print_md')
print_md(ht)
print_md(ht, header = FALSE)
catn('# Varying min_width and max_width')

print_screen(ht)
print_screen(ht, min_width = 60)
print_screen(ht, max_width = 15)
print_md(ht)
print_md(ht, min_width = 60)
print_md(ht, max_width = 15)
catn('# Long content, with spaces')

ht[1, 2] <- 'Some very long content that rambles on a lot...'
print_screen(ht)
print_md(ht)
ht %>% set_caption('Low max_width') %T>% print_screen(max_width = 30) %>% print_md(max_width = 30)
ht %>% set_caption('wrap = TRUE') %>% set_wrap(TRUE) %T>% print_screen(max_width = 60) %>% print_md
catn('# Long content, no spaces')

ht[1, 2] <- 'Someverylongcontentthatramblesonalotwithnospaces.'
print_screen(ht)
print_md(ht)
ht %>% set_caption('Low max_width') %T>% print_screen(max_width = 30) %>% print_md(max_width = 30)
ht %>% set_caption('wrap = TRUE') %>% set_wrap(TRUE) %T>% print_screen(max_width = 60) %>% print_md
catn('# Colspan and rowspan')

colspan(ht)[1, 1] <- 2
print_screen(ht)
catn('print_md should give a warning')
print_md(ht)
colspan(ht)[1, 1] <- 1
rowspan(ht)[1, 1] <- 2
print_screen(ht)
print_md(ht)
catn('# Colspan and rowspan with long content')
ht[1, 1] <- 'Some more long content, ever so ever so long!'
colspan(ht)[1, 1] <- 2
print_screen(ht)
wrap(ht)[1, 1] <- TRUE
print_screen(ht)
wrap(ht)[1, 1] <- FALSE
colspan(ht)[1, 1] <- 1
rowspan(ht)[1, 1] <- 2
print_screen(ht)
wrap(ht)[1, 1] <- TRUE
print_screen(ht)
wrap(ht)[1, 1] <- FALSE
catn('# Do borders collapse? The next pairs should be the same')
ht %<>% set_all_borders(0)
left_border(ht)[1, 2] <- 1
print_screen(ht)
left_border(ht)[1, 2] <- 0
right_border(ht)[1, 1] <- 1
print_screen(ht)
right_border(ht)[1, 1] <- 0

top_border(ht)[2, 1] <- 1
print_screen(ht)
top_border(ht)[2, 1] <- 0
bottom_border(ht)[1, 1] <- 1
print_screen(ht)
catn('# Colours, bold italic')
ht2 <- hux(a = c('normal', 'bold', 'italic', 'bold italic'), b = c('normal', 'red', 'green bg', 'red, green bg'))
bold(ht2)[c(2,4), 1] <- TRUE
italic(ht2)[3:4, 1] <- TRUE
text_color(ht2)[c(2, 4), 2] <- 'red'
background_color(ht2)[3:4, 2] <- 'green'
ht2
catn('# Border colours')
ht2 <- set_all_borders(ht2, 1)
left_border_color(ht2)[1, 1]   <- 'red'
right_border_color(ht2)[1, 1]  <- 'green'
top_border_color(ht2)[1, 1]    <- 'purple'
bottom_border_color(ht2)[1, 1] <- 'blue'
ht2[1, 1] <- 'multicoloured borders?'
ht2


hughjonesd/huxtable documentation built on Aug. 2, 2024, 1:25 p.m.