An updated version of my question. Below is code to produce two Word documents. The first document contains a series of table titles, each with an accompanying bookmark. The second document contains an actual table.
What I'd like to be able to do is to determine what the table title in the second document should be based on what is specified in the first document. I believe the mechanics of this might involve finding the relevant bookmark in the first document, moving up a line to where the actual title is, and then copying the title, so that it can be used in the second document.
library(officer)
library(magrittr)
library(flextable)
read_docx() %>%
body_add_par(value = "Fred Table", style = "table title") %>%
body_add_par("") %>%
body_bookmark(id = "FredBMK") %>%
body_add_par("") %>%
body_add_par(value = "Sally Table", style = "table title") %>%
body_add_par("") %>%
body_bookmark(id = "SallyBMK") %>%
body_add_par("") %>%
body_add_par(value = "George Table", style = "table title") %>%
body_add_par("") %>%
body_bookmark(id = "GeorgeBMK") %>%
body_add_par("") %>%
body_add_par(value = "Sample Data from the mtcars Dataset", style = "table title") %>%
body_add_par("") %>%
body_bookmark(id = "mtcarsBMK") %>%
body_add_par("") %>%
body_add_par(value = "Susan Table", style = "table title") %>%
body_add_par("") %>%
body_bookmark(id = "SusanBMK") %>%
body_add_par("") %>%
print(target = "Test Report Skeleton.docx")
read_docx() %>%
body_add_par(value = "Table Title (Corresponding to mtcarsBMK) from Other Document Goes Here", style = "table title") %>%
body_add_par("") %>%
body_add_flextable(flextable(mtcars[1:12, 1:3])) %>%
print(target = "Test Target Table.docx")
Original Question:
I'm using the R officer package to generate Word documents. Imagine a scenario where text initially is synchronized in two word documents. One is a larger report and the other is a table that is generated and then automatically inserted into the report. The title of the table starts out the same in both documents. Now suppose a medical writer manually alters the title of the table in the report. I'd like to be able to detect that and then automatically update the title in the table so it matches what is in the report.
The officer package documentation shows how to replace text within a single document with a user specified text string. It's not clear to me though if it could be used to do what I'm trying to accomplish. Neither is it clear to me that it can't be done within officer.
Below is some code that makes two word documents. One represents a report where changes have been made to a table title. The other represents the original table for which the title needs to be updated to match the report. The difference is minor. There is all caps for a word in one title and not in the other.
My hope is that it will be clear to someone how to detect the change in the first document and then to update the title in the second document.
library(officer)
library(magrittr)
read_docx() %>%
body_add_par(value = "AWESOME Table", style = "table title") %>%
body_add_par("") %>%
body_bookmark(id = "AwesomeBMK") %>%
body_add_par("(Awesome table appears here immediately after AwesomeBMK bookmark)") %>%
print(target = "Awesome Report.docx")
read_docx() %>%
body_add_par(value = "Awesome Table", style = "table title") %>%
body_add_par("") %>%
body_bookmark(id = "AwesomeBMK") %>%
body_add_par("(Awesome table appears here immediately after AwesomeBMK bookmark)") %>%
print(target = "Awesome Table.docx")
body_bookmark(id = "SusanBMK")
as some kind of marker, which seems to be a good idea but people can accidentally delete it? – Johannes Stötzer