@cderv Thanks for your response. I have gone through the documentations you mention in detail before and could not find any officeR or mscharts function to return a MSChart while knitting an Rmd. I have come up with the following solution but am not sure how scalable this is.
The process I followed was to
- Set my YAML output as
officedown::rdocx_document:
- Add r code chunks where I needed the the MSCharts to be output
- The code chunks would create the chart object (using
mscharts::my_barchart ()
),
- Write the chart object to a temp word document (using
officer::body_add_chart()
)
- Inserting the chrt object back into the document (using
officer::block_pour_docx()
)
Thoughts on this process? It seems a bit hacky; there might be a better/native way to do it using OfficeR/mscharts?
---
title: "MS Word with editable charts"
output:
officedown::rdocx_document:
reference_docx: WordTemplate.docx
toc: yes
---
# Editable Chart1
```{r editable chart, echo=FALSE, include=TRUE}
library(mschart)
library(officer)
filename = paste0("Charts/Chart_1",format(Sys.time(), "%y%m%d%H%M%S"),".docx")
my_barchart <- ms_barchart(data = browser_data,
x = "browser", y = "value", group = "serie")
my_barchart <- chart_settings( my_barchart, grouping = "stacked",
gap_width = 50, overlap = 100 )
doc <- read_docx() %>%
body_add_chart(chart = my_barchart, style = "centered") %>%
print(target = filename)
block_pour_docx(filename)
``
# Editable Chart2
```{r editable chart2, echo=FALSE, include=TRUE}
library(mschart)
library(officer)
filename = paste0("Charts/Chart_2",format(Sys.time(), "%y%m%d%H%M%S"),".docx")
my_scatter <- ms_scatterchart(data = iris, x = "Sepal.Length",
y = "Sepal.Width", group = "Species")
my_scatter <- chart_data_fill(my_scatter,
values = c(virginica = "#6FA2FF", versicolor = "#FF6161", setosa = "#81FF5B") )
doc <- read_docx() %>%
body_add_chart(chart = my_scatter, style = "centered") %>%
print(target = filename)
block_pour_docx(filename)
``
Which gives 