1

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 Answers1

2

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")
David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Thanks for the solution and the development of this package. Another solution that I came up with used flextable, loaded up the data frame minus the first row, and made the headers of the flextable the first row of the data using the set_header_labels function. – Jason Clark Sep 06 '17 at 14:14
  • 1
    I will add an option to drop headers from flextable later. – David Gohel Sep 06 '17 at 15:42