if (FALSE) { # knitr::is_html_output()
}
if (knitr::is_html_output()) options(tinytable_print_output = "html")
library(tinytable)
tt(mtcars[1:5, 1:6])
# Alignment
tt(mtcars[1:5, 1:6]) |>
style_tt(j = 1:5, align = "lcccr")
# Colors and styles
tt(mtcars[1:5, 1:6]) |>
style_tt(i = 2:3, background = "black", color = "orange", bold = TRUE)
# column selection with `j``
tt(mtcars[1:5, 1:6]) |>
style_tt(j = 5:6, background = "pink")
tt(mtcars[1:5, 1:6]) |>
style_tt(j = "drat|wt", background = "pink")
tt(mtcars[1:5, 1:6]) |>
style_tt(j = c("drat", "wt"), background = "pink")
tt(mtcars[1:5, 1:6], theme = "empty") |>
style_tt(
i = 2, j = 2,
colspan = 3,
rowspan = 2,
align = "c",
alignv = "m",
color = "white",
background = "black",
bold = TRUE)
tt(mtcars[1:5, 1:6], theme = "empty") |>
style_tt(
i = 0:3,
j = 1:3,
line = "tblr",
line_width = 0.4,
line_color = "teal")
tt(mtcars[1:5, 1:6], theme = "striped") |>
style_tt(
i = c(2, 5),
j = 3,
strikeout = TRUE,
fontsize = 0.7)
# Non-standard evaluation (NSE)
dat <- data.frame(
w = c(143002.2092, 201399.181, 100188.3883),
x = c(1.43402, 201.399, 0.134588),
y = as.Date(c(897, 232, 198), origin = "1970-01-01"),
z = c(TRUE, TRUE, FALSE)
)
tt(dat) |>
style_tt(i = w > 150000, j = c("w", "x"),
color = "white", background = "black")
tt(mtcars[1:5, 1:6]) |>
theme_html(class = "table table-dark table-hover")
inner <- "
column{1-4}={halign=c},
hlines = {fg=white},
vlines = {fg=white},
cell{1,6}{odd} = {bg=teal7},
cell{1,6}{even} = {bg=green7},
cell{2,4}{1,4} = {bg=red7},
cell{3,5}{1,4} = {bg=purple7},
cell{2}{2} = {r=4,c=2}{bg=azure7},
"
tt(mtcars[1:5, 1:4], theme = "empty") |>
theme_latex(inner = inner)
# Style group rows and non-group rows
dat <- data.frame(x = 1:6, y = letters[1:6])
dat |>
tt() |>
group_tt(i = list("Group A" = 3)) |>
style_tt(i = "groupi", background = "lightblue") |>
style_tt(i = "~groupi", background = "lightgray")
# unquote expressions
dat <- mtcars[1:10,]
dat <- dat[order(dat$am),]
tt(dat) |>
subset(mpg > 20) |>
group_tt(am)
# style elements: captions and colnames
notes <- list(
"*" = "Hello world",
"a" = "Bacon ipsum dolor amet kevin t-bone porchetta.")
tt(head(iris),
width = .5,
caption = "This is a Caption Example.",
notes = notes) |>
style_tt(2, 2, background = "pink", rowspan = 2, colspan = 2,
alignv = "m", align = "c", line = "tblr") |>
style_tt("colnames", italic = TRUE) |>
style_tt("caption", smallcap = TRUE)
Run the code above in your browser using DataLab