ACC
ACC

Reputation: 11

Looping through data with Rmd input file with flextable results in pandoc version error - but works fine without the loop?

I'm trying to generate lots of word documents with a loop and RMarkdown. I'm using flextable to make the tables with the acceptable formatting for the report. Everything works fine until I try to loop through a dataset. Then I get an error about the pandoc version:

Error in knit_print.flextable(x, ...) : pandoc version >= 2.0 required for flextable rendering in docx "

However, It looks like I download version 2.1 on January 19th...

pandoc_version() [1] ‘1.19.2.1’

Not to mention, it runs fine without the loop when I just run it in the rmd file. (I would think it wouldn't run either way if the version wasn't correct).

I also tried sys.setenv()

Sys.setenv("RSTUDIO_PANDOC" = "PATH TO PANDOC BIN")

But still, it works for the Rmd file alone, but I get the same error about the version when I try to loop it. Again, I feel like this wouldn't work in the rmd file if this wasn't correct.

I'm including a reproducible example below. If I can provide any other information please let me know. Thanks!

My loop/R Script

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’
> 

The Rmd File to Reference

---
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 "
```

**Also - In case it's helpful, here is the traceback: **

  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/") 

Upvotes: 1

Views: 1511

Answers (1)

David Gohel
David Gohel

Reputation: 10675

You need to create a file .Rprofile at the root of your R project (see ?Startup). In that file add your R instruction to set pandoc path (Sys.setenv("RSTUDIO_PANDOC" = "PATH TO PANDOC BIN")).

rmarkdown::render is using a new R session and your variable is not set when rendering.

Upvotes: 1

Related Questions