captions examples

Packages

library(flextable)
library(officer)
library(magrittr)
library(data.table)
library(magrittr)

Data

We will transform the data with the function as_grouped_data() to be able to organize them in groups by set of rows, a delimiter is added to highlight the change in the grouping level.

data_CO2 <- dcast(as.data.table(CO2), 
  Treatment + conc ~ Type, value.var = "uptake", fun.aggregate = mean)
data_CO2
##      Treatment conc   Quebec Mississippi
##  1: nonchilled   95 15.26667    11.30000
##  2: nonchilled  175 30.03333    20.20000
##  3: nonchilled  250 37.40000    27.53333
##  4: nonchilled  350 40.36667    29.90000
##  5: nonchilled  500 39.60000    30.60000
##  6: nonchilled  675 41.50000    30.53333
##  7: nonchilled 1000 43.16667    31.60000
##  8:    chilled   95 12.86667     9.60000
##  9:    chilled  175 24.13333    14.76667
## 10:    chilled  250 34.46667    16.10000
## 11:    chilled  350 35.80000    16.60000
## 12:    chilled  500 36.66667    16.63333
## 13:    chilled  675 37.50000    18.26667
## 14:    chilled 1000 40.83333    18.73333
data_CO2 <- as_grouped_data(x = data_CO2, groups = c("Treatment"))

flextable used for illustration

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) %>% 
  colformat_double(i = ~ !is.na(conc), digits = 2) %>% 
  colformat_double(i = ~ !is.na(conc), j = 1, digits = 0) %>% 
  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:

set_caption(ft, 
  caption = "mean of carbon dioxide uptake in grass plants")

It is also possible to use a knitr chunk option specific to flextable named tab.cap:

```{r tab.cap='mean ...'}```
ft