Tenho o seguinte documento rmarkdown em R que gera um objeto flextable.
Meu problema é que nas duas colunas 4 e 5 os nomes dos cabeçalhos não aparecem por completo e as últimas letras ficam ocultas.
---
title: "flex_width_issue"
output: pdf_document
date: "2025-02-08"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(officer)
library(flextable)
ft3 = structure(list("Project Number" = c(4107L, 1770L, 1979L, 9252L,
2581L, 8360L, 6290L, 1002L, 7300L, 2925L), "Client Company" = c("Dynamic Build Concept Agency",
"Nova", "Alpha Corp", "Global Innovations", "Core Metrics", "Vision Group for Property Holdings",
"United Firm for Urban Growth Projects", "Eastern Gate Real Estate Investment Group (EGRIG)",
"Eastern Gate Real Estate Investment Group (EGRIG)", "Eastern Gate Real Estate Investment Group (EGRIG)"
), `organizational growth planning` = c(5, 5, 4.83, 4.67, 4.17,
4, 3.83, 3.67, 3.5, 2.83), competency = c(5, 5, 4.83, 4.67, 4.27,
4.08, 4.25, 4, 3.5, 3.25), compression = c(5, 5, 5, 4.67, 4.38,
4.67, 4.67, 4, 3.67, 3), `International development project` = c(5,
4.57, 4.43, 4.43, 3.83, 4.17, 3.57, 3.14, 2.71, 2.71), `Team spirit` = c(5,
5, 5, 4.5, 4.21, 4.5, 4.5, 3.5, 3.5, 3), Plan = c(5, 5, 4, 4,
3.6, 2, 3, 4, 3, 3), PIR = c(5, 5, 4.17, 4.67, 4.07, 4.17, 4.33,
3.67, 3.67, 3.33), Success = c(5, 5, 4, 4, 4.08, 5, 3, 4, 2.67,
3), plant = c(100, 98.92, 90.65, 89.03, 81.6, 81.48, 77.88, 74.95,
65.55, 60.3)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
map_color2 = function(value) {
case_when(
value >= 1 & value < 1.5 ~ "#ed2e1c", # [1–1.5[
value >= 1.5 & value <= 2.5 ~ "#e09c95", # [1.5–2.5]
value > 2.5 & value < 3.5 ~ "#85c1e9", # ]2.5–3.5[
value >= 3.5 & value <= 4.5 ~ "#7FF98B", # [3.5–4.5]
value > 4.5 & value <= 5 ~ "#04B431", # ]4.5–5]
TRUE ~ "white" # Default color for values outside the range
)
}
colors2 = ft3 %>%
select(3:(ncol(.) - 1)) %>%
mutate(across(everything(), ~ map_color2(.))) # Apply map_color column-wise
ft4 = ft3%>%
flextable::flextable()
# Apply map_color to columns from 3 to the second-to-last column
ft4%>%
border_outer(part = "all") %>%
border_inner(part = "all") %>%
bg(part = "header", bg = "grey") %>%
bold(part = "header") %>%
align(align = "center", part = "header") %>%
theme_zebra()%>%
bg(bg = "grey", part = "header")%>%
align(align="center", part="all")%>%
fit_to_width(max_width = 7)%>%
fontsize(size=7, part="all")%>%
flextable::bg(i = rep(1:nrow(ft3)),
j = rep(3:(ncol(ft3) - 1)),
bg = unlist(colors2))%>%
align(j = 2, align = "center", part = "all")
can anyone help me with this ?
How this can fit in width and show all the header columns names clearly in full ?
Você pode usar uma função para autodimensionar sua tabela, embora seja melhor escolher uma paisagem para tabelas largas. Não é perfeito, mas funciona por enquanto. Talvez use abreviações em seus cabeçalhos.
Você pode até melhorar o uso do espaço envolvendo apenas cabeçalhos que excedam o comprimento máximo de nchars em suas colunas.