1

I've been trying for a while to make the table take up almost the entire width of the Word page, but I haven't been successful. I've used the functions: table_width width fit_to_width without success.

I've been trying for a while to make the table take up almost the entire width of the Word page, but I haven't been successful. I've used the functions: table_width width fit_to_width without success


# Charger la bibliothèque pour manipuler les dataframes
library(officer)
library(flextable)

# Créer les données
colonne1 <- c("Nombre totaux d'arceaux vélo", "Nombre d'arceaux vélos installés")
colonne2 <- c(10, 5)

# Créer le dataframe
df <- data.frame(Colonne1 = colonne1, Colonne2 = colonne2)

#Mise en forme 
TABLEAU <- flextable(df) %>%
  align(align = "left", part = "all") %>%
  bg(bg = "#D3D3D3", part = "header") %>%
  bold(part = "header") %>%
  align(align = "left", part = "header") %>%
  set_caption(paste("BL", commune, "en", annee_prec)) %>%
  fit_to_width(max_width = 6.5)%>%
  autofit()

TABLEAU

# Créer un nouvel objet Word
doc <- read_docx()

# Ajouter le tableau au document Word
doc <- body_add_flextable(doc, value = TABLEAU)

# Sauvegarder le document Word (you need to change it for you)
print(doc, target = "Desktop/file.docx")

What i want

pleemore
  • 21
  • 2
  • 1
    I'm afraid this is caused by word predefined page width. Per default word uses the same page width across document regardless table/paragraph width. To somehow change it, you have to put a page/section break before table, and set the page width for the new page/section where you will render the table. I'm not familiar with `officer` package, but just looking on function it seems that `body_add_break()`, `body_end_block_section()` and `page_mar()` can be useful. – Grzegorz Sapijaszko Jun 28 '23 at 18:39

1 Answers1

0

To create a table in R using the 'officer' and 'flextable' packages that fits the entire width of the document, you will need to make use of the docx_dim() function. This function allows you to retrieve the width of a Word page.

Once you have obtained the page width, you will need to perform a calculation to adjust the table dimensions accordingly. This involves comparing the table width to the page width and determining how to set columns' widths, there is no single rule for that, it's up to you to do the calculations that will satisfy your specific needs. You may want also to know what are the minimum columns' widths in addition to the page width, use dim_pretty().

The following code/answer assumes all columns have the same widths (width/ncol(df)).

library(officer)
library(flextable)
library(magrittr)

doc <- read_docx()
word_size <- docx_dim(doc)
width <- word_size$page['width'] - word_size$margins['left'] - word_size$margins['right']

colonne1 <- c("Nombre totaux d'arceaux vélo", "Nombre d'arceaux vélos installés")
colonne2 <- c(10, 5)
commune <- "blah"
annee_prec <- "1987"
df <- data.frame(Colonne1 = colonne1, Colonne2 = colonne2)

#Mise en forme 
TABLEAU <- flextable(df) %>%
  align(align = "left", part = "all") %>%
  bg(bg = "#D3D3D3", part = "header") %>%
  bold(part = "header") %>%
  align(align = "left", part = "header") %>%
  set_caption(paste("BL", commune, "en", annee_prec)) %>%
  width(width = width/ncol(df))

doc <- body_add_flextable(doc, value = TABLEAU)

print(doc, target = "file.docx")

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34