我正在尝试使用循环和 RMarkdown生成大量word 文档。我正在使用 flextable 制作具有报告可接受格式的表格。一切正常,直到我尝试遍历数据集。然后我收到有关 pandoc 版本的错误:
knit_print.flextable(x, ...) 中的错误:在 docx 中进行 flextable 渲染需要 pandoc 版本 >= 2.0“
但是,看起来我在 1 月 19 日下载了 2.1 版...
pandoc_version() [1] '1.19.2.1'</p>
更不用说,当我在 rmd 文件中运行它时,它在没有循环的情况下运行良好。(如果版本不正确,我认为它不会以任何方式运行)。
我也试过 sys.setenv()
Sys.setenv("RSTUDIO_PANDOC" = "PATH TO PANDOC BIN")
但是,它仍然适用于单独的 Rmd 文件,但是当我尝试循环它时,我得到了关于版本的相同错误。同样,如果这不正确,我觉得这在 rmd 文件中不起作用。
我在下面包含了一个可重现的示例。如果我可以提供任何其他信息,请告诉我。谢谢!
我的循环/R 脚本
DAT<-dplyr::sample_n(iris, 10)
Sys.setenv("RSTUDIO_PANDOC" = "PATH TO PANDOC BIN")
for (i in 1:nrow(DAT)){
rmarkdown::render(input = "Loop Testing R Markdown.Rmd",
output_format = "word_document",
output_file = paste("Documents", i, ".docx", sep=''),
output_dir = "Documents/")
}
> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rmarkdown_1.8 knitr_1.17 bindrcpp_0.2 dplyr_0.7.4
qsurvey_0.0.3 installr_0.19.0
[7] stringr_1.2.0 officer_0.2.1 flextable_0.4.2 pander_0.6.1
kableExtra_0.7.0
loaded via a namespace (and not attached):
[1] zip_1.0.0 Rcpp_0.12.14 bindr_0.1
pillar_1.0.1 compiler_3.4.3
[6] plyr_1.8.4 highr_0.6 R.methodsS3_1.7.1
R.utils_2.6.0 base64enc_0.1-3
[11] tools_3.4.3 digest_0.6.13 uuid_0.1-2 lubridate_1.7.1 jsonlite_1.5
[16] evaluate_0.10.1 tibble_1.4.1 viridisLite_0.2.0 pkgconfig_2.0.1 rlang_0.1.6
[21] shiny_1.0.5 curl_3.1 yaml_2.1.16 httr_1.3.1 xml2_1.1.1
[26] htmlwidgets_0.9 gdtools_0.1.6 hms_0.4.0 DT_0.2 rprojroot_1.3-1
[31] glue_1.2.0 data.table_1.10.4-3 R6_2.2.2 readr_1.1.1 magrittr_1.5
[36] backports_1.1.2 scales_0.5.0 htmltools_0.3.6 assertthat_0.2.0 rvest_0.3.2
[41] xtable_1.8-2 mime_0.5 colorspace_1.3-2 httpuv_1.3.5 stringi_1.1.6
[46] visNetwork_2.0.2 munsell_0.4.3 R.oo_1.21.0
> pandoc_version()
[1] ‘1.19.2.1’
>
要引用的 Rmd 文件
---
output:
word_document:
reference_docx: mystyles.docx
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r results='asis', include=FALSE}
## Load Packages
library(knitr)
library(rmarkdown)
library(dplyr)
```
```{r results="asis"}
#DAT<-iris # I comment this out when I run the loop, but leave it in when testing the format.
library(pander)
panderOptions('knitr.auto.asis', FALSE)
First_N<-DAT[[i,1]]
Last_N<-DAT[[i,2]]
Name<-c(First_N, " ", Last_N)
Name<-paste(Name, collapse = "")
cat("# ", Name, "\n")
```
## Title of Report Section
```{r, demo1, include=FALSE}
library(pander)
panderOptions('knitr.auto.asis', FALSE)
DemoTbl_1<-matrix(nrow = 2, ncol = 4)
DemoTbl_1[[1,1]]<- "Name:"
DemoTbl_1[[1,2]]<- Name
DemoTbl_1[[1,3]]<- "State:"
DemoTbl_1[[1,4]]<-DAT[[i,3]]
DemoTbl_1[[2,1]]<- "Other Feild Title:"
DemoTbl_1[[2,2]]<-DAT[[i,4]]
DemoTbl_1[[2,4]]<-DAT[[i,5]]
DemoTbl_1[[2,3]]<- "Pther Feild Title"
library("flextable")
library("officer")
myft<-regulartable(as.data.frame(DemoTbl_1))
myft <- bg(myft, bg = "#000000", j=1)
myft <- color(myft, color = "#ffffff", j=1)
myft <- border(myft, border = fp_border(color = "#000000"), part = "all")
myft <- align( myft, align = "center", part = "all" )
myft <- width(myft, width = 1.5, j=1)
myft <- width(myft, width = 3, j=2)
myft <- width(myft, width = 1.5, j=3)
myft <- width(myft, width = 1.25, j=4)
myft<- delete_part(myft, part = "header")
myft <- bg(myft, bg = "#000000", j=3)
myft <- color(myft, color = "#ffffff", j=3)
```
```{r, results='asis', echo=FALSE}
#DemoTbl_1 # This prints fine
myft ## This seems to be giving me the error "uitting from lines 77-78
(Loop_Testing_R_Markdown.Rmd) Error in knit_print.flextable(x, ...) : pandoc
version >= 2.0 required for flextable rendering in docx "
```
**另外 - 如果有帮助,这里是回溯:**
Error in knit_print.flextable(x, ...) :
pandoc version >= 2.0 required for flextable rendering in docx
20. stop("pandoc version >= 2.0 required for flextable rendering in docx")
19. knit_print.flextable(x, ...)
18. knit_print(x, ...)
17. withVisible(knit_print(x, ...))
16. fun(x, options = options)
15. value_fun(ev$value, ev$visible)
14. withVisible(value_fun(ev$value, ev$visible))
13 .withCallingHandlers(withVisible(value_fun(ev$value, ev$visible)),
warning = wHandler, error = eHandler, message = mHandler)
12. handle(pv <- withCallingHandlers(withVisible(value_fun(ev$value,
ev$visible)), warning = wHandler, error = eHandler, message = mHandler))
11. evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos,
debug = debug, last = i == length(out), use_try = stop_on_error !=
2L, keep_warning = keep_warning, keep_message = keep_message,
output_handler = output_handler, include_timing = include_timing)
10. evaluate(code, envir = env, new_device = FALSE, keep_warning = !isFALSE(options$warning),
keep_message = !isFALSE(options$message), stop_on_error = if (options$error &&
options$include) 0L else 2L, output_handler = knit_handlers(options$render,
options))
9. in_dir(input_dir(), evaluate(code, envir = env, new_device = FALSE,
keep_warning = !isFALSE(options$warning), keep_message = !isFALSE(options$message),
stop_on_error = if (options$error && options$include) 0L else 2L,
output_handler = knit_handlers(options$render, options)))
8. block_exec(params)
7. call_block(x)
6. process_group.block(group)
5. process_group(group)
4. withCallingHandlers(if (tangle) process_tangle(group) else process_group(group),
error = function(e) {
setwd(wd)
cat(res, sep = "\n", file = output %n% "") ...
3. process_file(text, output)
2. knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet,
encoding = encoding)
1. rmarkdown::render(input = "Loop Testing R Markdown.Rmd", output_format = "word_document",
output_file = paste("Documents", i, ".docx", sep = ""), output_dir = "Documents/")