File: captions_example.Rmd

package info (click to toggle)
r-cran-flextable 0.9.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,320 kB
  • sloc: javascript: 28; sh: 15; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 2,029 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
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)