0

The issue I'm having is with formatting longer strings that do not have a space or separator (index sequence column in image below). In the below image you can see that:

a) in rows with a background color the sequence is covered by the next cell

b) in rows without a background color the sequence invades the next cell

I'd like to add text wrapping to make sure all of the text fits in the cells but cannot figure out how to get it to wrap properly. When I run the code in R studio everything looks ok but when I knit it to pdf much of the data does not fit and the table looks poor.

Image from R studio:

enter image description here

Image from PDF:

enter image description here

Example Code:

---
title: "Untitled"
output: pdf_document
date: "2023-08-16"
---
{r}
library(flextable)
library(magrittr)
set_flextable_defaults(font.family = 'DejaVuSansMono')
data <- read.csv("/Users/kregan1/temp/test.csv", 
check.names=FALSE)
ft <- flextable(data)
ft <- autofit(ft) %>% theme_zebra()%>%bg(bg = "#44B29C", part = "header")%>% align(align="center", part="all")%>%fit_to_width(max_width = 7.5)%>% fontsize(size=7, part="all")
ft

test.csv:

"cell","project","master sample","library","insertsize","targeted frg size","sample name","external id","strain","species","library type","index","index sequence","size selected","size selected size"
"A01_1","XVOLT","MS100301111","PL100300001-1","15,436","20,000","LIB_1000","PACB001",NA,"Homo sapiens","HiFi Multiplex","bc2027","TGACTGTAGCGAGTAT","N",0
"A01_1","XVOLT","MS100301112","PL100300002-1","15,618","20,000","LIB_1001","PACB001",NA,"Homo sapiens","HiFi Multiplex","bc2028","ACTGCAGCACGAGTAT","N",0
kevin41
  • 183
  • 2
  • 14
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data, i.e. a snippet of the dataset you pass to `flextable`. Most of the code you added is IMHO irrelevant for the question as it just deals with preparing the data passed to flextable in the end. – stefan Aug 15 '23 at 20:10
  • 1
    Edited the post to include a reproducible example and better explain the issue @stefan – kevin41 Aug 16 '23 at 12:23

1 Answers1

0

I was able to solve my issue by changing the below line. I believe the fontsize at the end was overriding the previous fit_to_width. Switching the order of the 2 fixed the issue of data not fitting in cells without having to wrap the text.

Original:

ft <- autofit(ft) %>% theme_zebra()%>%bg(bg = "#44B29C", part = "header")%>% align(align="center", part="all")%>%fit_to_width(max_width = 7.5)%>% fontsize(size=7, part="all")

Fixed:

ft <- autofit(ft) %>% theme_zebra()%>%bg(bg = "#44B29C", part = "header")%>% align(align="center", part="all")%>% fontsize(size=7, part="all")%>% fit_to_width(max_width = 7.5)
kevin41
  • 183
  • 2
  • 14