I am in the process of making a dashboard. Basically, I am pulling out selected information from a database that I then want to display in a word template. I set up the template with a table (2 rows, 3 columns). In each cell I put a bookmark. Then, using the R package ReporteRs I put specific information from R into a cell within the template based on the bookmark.
ISSUE: I cant seem to insert tables into a cell of the template.
Tried: I thought it might be due to size of the table (as i experienced that size was the issue when inserting figures), but this is not the case.
Below is an example with the error. In order to run this you have to create a Word document with a table with a bookmark in one of the cells called 'test'.
doc = docx( title = "Dashboard",template="H:\\test.docx")
myt <-FlexTable(cars[c(1:10),])
doc <-addFlexTable(doc,myt,bookmark="test")
writeDoc(doc, "H:\\testresult.docx")
If you then create a bookmark outside of the table and assign the table to the new bookmark, it seems to work.
Does anyone know how to insert a table (data frame as a table) into any cell of my template?
If argument bookmark is used, content (plots, paragraphs or images) will replace the whole paragraph containing the bookmark.
Behavior is different for tables: tables are added after the paragraph that contains the bookmark. A workaround is to add a paragraph after the bookmarked paragraph in the cell of the template. Then use deleteBookmark
to delete the paragraph containing the bookmark.
doc <- docx( title = "Dashboard",template="H:\\test.docx")
myt <- FlexTable(cars[c(1:10),])
# make sure there is one new paragraph after the paragraph that contains 'test'
# add the FlexTable just after the paragraph containing bookmark 'test'
doc <- addFlexTable(doc,myt,bookmark="test")
# then delete the paragraph containing bookmark 'test'
deleteBookmark(doc, bookmark= "test")
writeDoc(doc, "H:\\testresult.docx")
You may use Sections with columns instead (and eventually column breaks):
doc = docx( )
doc = addSection(doc, landscape = TRUE, ncol = 2 )
doc = addPlot( doc = doc, fun = function() {
barplot( 1:8, col = 1:8 )
}, width = 3, height = 3, pointsize = 5)
doc = addColumnBreak(doc )
doc = addFlexTable(doc, FlexTable(head(iris) ) )