简体   繁体   中英

RMarkdown Officedown: Loop over child document containing flextable with UTF-8 encoding

Is it possible to include a child document in a loop that contains a flextable with UTF-8 encoding?

This is my YAML:

---
output: officedown::rdocx_document
---

Without using a loop, it works perfectly and the Delta ("\U0394") is displayed correctly in the output file:

```{r, echo = FALSE, message = FALSE}
library(flextable)

# Create columns
col1 <- c("a", "\U0394")
col2 <- c(1.0, 2.0)

# Create dataframe
data <- data.frame(Parameter = col1,
                   Value = col2)

# Create flextable
ft <- flextable(data)
ft
```

To loop over this flextable, I saved the code above as a child file and included it in an asis-chunk with knit_child() ...

```{r, echo = FALSE, message = FALSE, results = "asis"}
library(rmarkdown)
library(knitr)
library(flextable)

# Read child doc two times
for (i in 1:2){
  out <- knit_child("path_to_child\\child.Rmd", quiet = TRUE)
  cat(out)
  cat("\n\n")
}
```

...but then get this error, because the UTF-8 encoding is somehow not recognized anymore:

Error in read_xml.character(file) : error parsing attribute name [68]

You need to use flextable_to_rmd .

See:

You have to slightly adjust the code in "path_to_child/child.Rmd":



```{r, echo = FALSE, message = FALSE, results='asis'}
library(flextable)

# Create columns
col1 <- c("a", "\U0394")
col2 <- c(1.0, 2.0)

# Create dataframe
data <- data.frame(Parameter = col1,
                   Value = col2)

# Create flextable
ft <- flextable(data)

flextable_to_rmd(ft)
```

["

I had the same experience using "\≥70" inside a loop.<\/i>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM