0

I am trying to left align some columns in a few of my tables. Most of the time this works no problem, but other times, the columns selected with:

ft <-align(ft, j=1:2, align = "left", part = "body")

ft <-align(ft, j=4:6, align = "left", part = "body")

look correct in the Rstudio viewer. I used a loop to make 39 tables and want to add them to a word document using officer

However, when printed to a word doc using:

doc <- read_docx() %>%
body_add_flextable(tab0)

body_add_break(doc, pos="after")

for (i in 1:38) {
  body_add_flextable(doc,get(paste0("tab", i)), align = "left")
  body_add_break(doc, pos="after")
}

print(doc,target=documentname)

it center aligns the columns (only changes those that were left aligned). When I click on the text, it says that they are left aligned, but then I center and re-left align and that fixes the issue. I have only noticed the issue which character variables

I tried triming whitespace with trimws() before I make the flextable with no luck.

I tried other alignments and the issue only seems to occur with left alignment.

I don't believe my code to be wrong, since it works for my other tables. Therefore, I think trying to make reproducible code would be a waste of time.

Therefore, I'm assuming the text itself is causing the issue? Does anyone know what could be wrong with the character variables themselves? Or any other ideas?

morgan121
  • 2,213
  • 1
  • 15
  • 33

1 Answers1

0

Fixed it...

Somewhere within my flextable manipulation this leading white space was added.

So trimming white space before didn't do anything. Had to trim right before naming my table (within a loop). Here is the loop I used to correct it.

for (j in 1: length(ft[["body"]][["content"]][["content"]][["data"]])){
       ft[["body"]][["content"]][["content"]][["data"]][[j]][["txt"]] <- trimws(ft[["body"]][["content"]][["content"]][["data"]][[j]][["txt"]])
}
  • hi, please use only exposed API, using the underlying data structure make your code fragile as these structures could change over time. – David Gohel Mar 22 '19 at 08:01