嵌套在某些条件下执行的循环

时间:2019-03-16 17:28:16

标签: r

我想生成以下屏幕截图。简而言之,应将“ Hello World”放置在“图1”标题下,将“晚安世界”放置在“图2”标题下。取而代之的是,我得到了与两个短语相关的两个标题。 enter image description here

library(officer)
library(magrittr)
library(flextable)

my_doc2 <- read_docx() 

n=2


bl=c('1','2','3')
b2=c('1','2')


for(j in 1:n){
for( i in 1:(length(bl))){
  my_doc2 <- my_doc2 %>%
    body_add_par(paste0("Figure",j), style = "graphic title") %>%
  body_add_par(paste0("Hello world",i), style = "Normal")
}
  for( i in 1:(length(b2))){
    my_doc2 <- my_doc2 %>%
      body_add_par(paste0("Figure",j), style = "graphic title") %>%
      body_add_par(paste0("Goodnight world",i), style = "Normal")
  }
}


#writing to word file
print(my_doc2, target = "second_example.docx")

0 个答案:

没有答案