---
title: "flextable caption example"
---

```{r include=FALSE}
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"))
```

```{r}
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)) 
```

```{r}
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`:

```{r ft.align="left"}
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:

```{r ft.align="center", tab.cap='mean of carbon dioxide uptake in grass plants', tab.id='tab2', label='tab2'}
ft
```

## Cross-references

* This is a reference to flextable \@ref(tab:tab1).
* This is a reference to flextable \@ref(tab:tab2).
* This is a link to [flextable](#tab:tab1)
