library(officer)
if (require("ggplot2")) {
set.seed(2)
doc <- read_docx()
z <- body_append_start_context(doc)
for (i in seq_len(3)) {
df <- data.frame(x = runif(10), y = runif(10))
gg <- ggplot(df, aes(x = x, y = y)) + geom_point()
write_elements_to_context(
context = z,
block_gg(
value = gg
)
)
}
doc <- body_append_stop_context(z)
print(doc, target = tempfile(fileext = ".docx"))
}
Run the code above in your browser using DataLab