Adding Tables w/o Column Headers in Officer

706 views Asked by At

Trying to add a table from a data frame in a .docx document using officer, but without including a row for the column header. I've tried removing the column names from the data frame before adding it to the document, but that has not worked and leads to gobbly-gook getting added in the top row instead of the top row just not existing. Any ideas on how to accomplish this?

NarrativeDoc <- read_docx()
tmp = data.frame(RowHead1=c('Fact 1','Fact 2'),
                 RowHead2=c("I like chicken","I am not a chicken"),
                 stringsAsFactors =FALSE)
names(tmp)=NULL
NarrativeDoc <- body_add_table(NarrativeDoc,tmp)
print(NarrativeDoc,
      target="C:\\Users\\jclark_v\\Documents\\R\\TestNoHeader.docx")
1

There are 1 answers

2
David Gohel On BEST ANSWER

I have added argument header to body_add_table (you will need to update from github)

NarrativeDoc <- read_docx()
tmp = data.frame(RowHead1=c('Fact 1','Fact 2'),
                 RowHead2=c("I like chicken","I am not a chicken"),
                 stringsAsFactors =FALSE)
NarrativeDoc <- body_add_table(NarrativeDoc,tmp, header = FALSE)
NarrativeDoc <- body_add_par(NarrativeDoc, "")
NarrativeDoc <- body_add_table(NarrativeDoc,tmp, header = TRUE)
print(NarrativeDoc,
      target="TestNoHeader.docx")