使用Rmarkdown从包含图像和表格的列表中创建Word文档

时间:2019-03-13 23:52:00

标签: r r-markdown

我有一个.rmd文件,在其中将以下名称为bl_convergence_acorr001.png的图像下载并保存到我的工作目录后,我创建了一个包含该图像和表的列表。然后,我试图通过Rmarkdown创建Word文档,但是我创建了打印有此列表的Word文档,而不是图像和表格。 enter image description here

---
title: "Untitled"
author: "makis"
date: "March 14, 2019"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r t3, echo=FALSE,warning=FALSE,message=FALSE}
library(officer)
library(magrittr)
library(flextable)


current_dir <- getwd()

my_doc <- read_docx() 

current_dir <- getwd()

my_doc <- read_docx() 

#####
#with baseline
#Baseline Autocorrelation Plots
bl_acorr_files=list.files(path = current_dir, pattern = c("^bl_convergence_acorr",".png"))

for( i in 1:(length(bl_acorr_files))){
  my_doc <- my_doc %>%
    slip_in_text(paste0("Figure 1 Baseline Autocorrelation Plots"), style = "strong", pos = "after") %>%
    body_add_img(src = paste0("bl_convergence_acorr00",i,".png") ,height = 5.75, width = 10 ,style = "centered")
}

summary_stats <- iris
ft <- flextable(data = summary_stats) %>%
  theme_booktabs() %>%
  autofit()
my_doc <- my_doc %>%
  slip_in_text(paste0("Table 2: Summary stats"), style = "strong", pos = "after") %>%
  body_add_flextable(ft)
my_doc
```

0 个答案:

没有答案