1

Lovely day to you all!

I want to insert a mschart into an Rmd which I would like to knit to docx. I have no idea how to insert the plot. For "normal" docx this works:

ms_linechart(data = mtcars, x = "vs", y = "mpg", group = "gear") -> chart
doc <- read_docx()
doc <- body_add_chart(doc, chart = chart, style = "centered")
print(doc, target = "example.docx")

Here's my code so far which is not working:

---
date: "`r Sys.Date()`"
author: "Your Name"
title: "Untitled"
output: 
  officedown::rdocx_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.cap = TRUE)
library(officedown)
library(flextable)
library(officer)
library(mschart)
```

```{r}
body_add_chart(ms_linechart(data = mtcars, x = "vs", y = "mpg", group = "gear"))

```

Thanks for your help!

2 Answers 2

0

I e-mailed the developer and it is not possible.

0

A workaround, which is not ideal, but that might be helpful for some:

In a chunk:


#Create plot object
my_plot <- 
data %>% 
  mschart::ms_scatterchart('x', 'y')

#Create a doc object using officer. Add chart.  
doc <- 
officer::read_docx() %>% 
body_add_chart(chart = my_plot)

#Save document to a folder
print(doc, target = here::here(path_results, "my_plot.docx"))

#Pour that document into the rmarkdown 
block_pour_docx(here::here(path_results, "my_plot.docx"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.