flextable caption example

library(knitr)
library(flextable)
library(magrittr)
library(data.table)
opts_chunk$set(echo = FALSE)

data_CO2 <- dcast(as.data.table(CO2), 
  Treatment + conc ~ Type, value.var = "uptake", fun.aggregate = mean)
data_CO2 <- as_grouped_data(x = data_CO2, groups = c("Treatment"))
ft <- as_flextable( data_CO2 ) %>% 
  bold(j = 1, i = ~ !is.na(Treatment), bold = TRUE, part = "body" ) %>% 
  bold(part = "header", bold = TRUE ) %>% 
  width(width = 1.5) %>% 
  compose(i = ~ !is.na(conc), j = "conc", 
          value = as_paragraph(
            as_chunk(conc, formatter = function(x) sprintf("%.0f", x))
          )
  ) %>% add_footer_lines("dataset CO2 has been used for this flextable") %>% 
  bg(bg = "#FFFFFF", part = "footer") %>% 
  set_header_labels(conc = "Concentration") %>% 
  width(width = c(1.5, 1, 1)) 
ft <- as_flextable( data_CO2 ) %>% 
  bold(j = 1, i = ~ !is.na(Treatment), bold = TRUE, part = "body" ) %>% 
  bold(part = "header", bold = TRUE ) %>% 
  width(width = 1.5) %>% 
  compose(i = ~ !is.na(conc), j = "conc", 
          value = as_paragraph(
            as_chunk(conc, formatter = function(x) sprintf("%.0f", x))
          )
  ) %>% add_footer_lines("dataset CO2 has been used for this flextable") %>% 
  bg(bg = "#FFFFFF", part = "footer") %>% 
  set_header_labels(conc = "Concentration") %>% 
  width(width = c(1.5, 1, 1)) 

Captions

Now, let's add a caption with set_caption:

library(officer)
set_caption(ft, 
  caption = "mean of carbon dioxide uptake in grass plants", 
  style = "Table Caption", 
  autonum = run_autonum(seq_id = "tab", bkm = "tab1"))

Or use kintr chunk options:

ft

Cross-references



Try the flextable package in your browser

Any scripts or data that you put into this service are public.

flextable documentation built on Oct. 30, 2024, 9:15 a.m.