flextableofficerofficedown

Table title on a different page when using a flextable object in a officedown document


I'm trying to put together a document where a "Tables" section will be found at the end of it. There, all the tables that I will have cited throughout the document will be shown.

My problem is that whenever I have a long table spanning several pages, the table title is left alone on one page and the table follows on the next page. This occurs in both portrait and landscape mode.

My question is: What Can I do with that so that table titles are found just above their associated tables, on the same page?

Here is a snapshot of the problem:

portrait tables In portrait mode, the table is ok when showing only the first few lines (using the r head() function, left), while the whole table is incorrect (two pages on the right).

landscape tables The same applies in landscape mode.

And here is the reproducible example.


```
---
output: officedown::rdocx_document
---
```

```{r setup, echo=F, message=F, warning=F}
knitr::opts_chunk$set(echo = F, 
                      collapse = T, 
                      fig.align = "center", 
                      fig.width = 6, 
                      fig.height = 8, 
                      fig.cap = T,
                      fig.pos = "!h",
                      message = F, 
                      warning = F)

library(magrittr) # for using the %>% 
library(dplyr)
library(officedown)
library(officer)
library(flextable)

# portrait section
portrait <- prop_section(type = "continuous")

# landscape section
landscape <- prop_section(page_size = page_size(orient = "landscape"), type = "continuous")

# Function for setting widths
FitFlextableToPage <- function(ft, pgwidth = 6){

  ft_out <- ft %>% autofit()

  ft_out <- width(ft_out, width = dim(ft_out)$widths*pgwidth /(flextable_dim(ft_out)$widths))
  return(ft_out)
}

set.seed(12345) # for reproducibility when setting table1
```

```{r, echo = F}
# Creation of table1
years <- 1990:2022
table1 <- tibble(year = years,
                 dat1 = sample(rnorm(n = 1000, 0, 2), size = length(years), replace = T)) %>% 
          mutate(year = as.character(year),
                 dat2 = dat1 * 10,
                 dat3 = dat1 * 1000)
#table1
```

# portrait table
```{r}
foot <- "Preliminary data." # footnote
col_names <- c("Year", "Column 1", "Column 2", "Column 3") # table headers

# Setting the footnote
cor <- which(with(table1, year %in% 2021:2022))
# length(cor) # 2

# Headers
names(table1) <- col_names
```

```{r table1, tab.cap = "Header of table1 in portrait style."}
table1 %>% head() %>% flextable() %>% autofit()
```
\newpage
```{r table2, tab.cap = "All of table1 in portrait style + footnote."}
table1 %>% 
  flextable() %>% 
  footnote(i = cor, j = 1, value = as_paragraph(foot),
           ref_symbols = "a", part = "body")

block_section(portrait) # end of portrait section
```

# landscape table
Let's put these same tables on a landscape page.

```{r table3, tab.cap = "Header of table1 in landscape style."}
table1 %>% head() %>% flextable() %>% autofit()
```
\newpage

```{r table4, tab.cap = "All of table1 in landscape style + footnote."}
table1 %>% 
  flextable() %>% 
  footnote(i = cor, j = 1, value = as_paragraph(foot),
           ref_symbols = "a", part = "body") %>% 
  FitFlextableToPage(pgwidth = 9.5)

block_section(landscape) # end of landscape section
```

# New section in portrait style

bla bla bla.


Solution

  • As David Gohel kindly replied, the chunk option ft.keepnext was the key here and needed to be set to FALSE in the example. This was done by adding an extra argument in the opts_chunk$set() at the beginning of the script, such as:

    knitr::opts_chunk$set(echo = F, 
                          collapse = T, 
                          fig.align = "center", 
                          fig.width = 6, 
                          fig.height = 8, 
                          fig.cap = T,
                          fig.pos = "!h",
                          message = F, 
                          warning = F,
                          ft.keepnext = F) # the argument added