The goal of sparkle is to insert displays into 'Microsoft Word' documents at specific locations. Using keywords and comments, users can uniquely identify both where and what to add to the word document. In short, it adds sparkle to the report.
You can install the development version of sparkle like so:
devtools::install_github("GSK-Biostatistics/sparkle")Simple use case of this package is to use it to add contents to specific locations in your document.
To do this, create a word document. Where content is wanted, create a new paragraph and write "insert". Then, highlight the word "insert" and add a comment. In the comment, write "object" or "file", indicating whether the contents to add is an R object or file contents. Next, write either the name of the object or the file path to the file to be added.
Once the document has been completed and filled with the insert statements, we
can process and add the results. The doc can be loaded using load_docx().
doc <- load_docx("my_dull_document.docx")To add "sparkle" the document, there are two methods. For the finest control,
use the add_sparkle() function. You specify the comment id and value to add
to the document at that point. {sparkle} "polishes" the value into valid content
to then add it to the document in the place of that comment. "Polishing" is handled
by the R package {polish}, which means the conversion of the R object into valid
ooxml.
The comments that exist and its meta information, such as comment_id, in the doc
are viewed via comment_df().
comment_df(doc)
add_sparkle(docx = doc, comment_id = 0, value = mtcars2)To do a batch update, where document text is consistent and comment text can be
used to indicate what content to add, use add_sparkles_pattern().
add_sparkles_pattern(
docx = doc,
docx_text_pattern = fixed("{{insert}}"),
comment_text_func = function(...){
tibble(values = 1:5, random = runif(5))
}
)Save the updated document.
save_docx(doc, "my_sparkling_report.docx")
