rmarkDown主持人:循环循环,包含UTF-8编码的弹力

发布于 2025-02-08 10:21:35 字数 965 浏览 2 评论 0 原文

是否可以在包含UTF-8编码的可弹力的循环中包括一个子文件?

这是我的yaml:

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

如果不使用循环,它可以完美工作,而delta(“ \ u0394”)在输出文件中正确显示:

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

要循环通过此flextable,我将上面的代码保存为子文件,并将其包含在一个中ASIS-chunk带有 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")
}
```

...但随后得到此错误,因为UTF-8编码已不再识别:

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

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雪碎 2025-02-15 10:21:35

我在循环中使用“ \ u226570”的经验。这是由于系统语言环境的各个方面。遵循以下内容:

  1. 使用 sessionInfo()并检查语言环境信息。
  2. 将语言环境更改为 English_united States.utf8 或其他UTF8样式(例如 en_gb.utf-8 ),如果尚未设置它们。您可以使用 sys.setlocale(“ lc_all”,“ English_united States.utf8”)来执行此操作。
  3. 再次检查会话信息,如果环境设置仍未改变,则可能不支持UTF-8(例如,更新版本的 Windows 具有更好的UTF-8支持)。

我认为您的问题将在设置有效的UTF-8语言环境后得到解决。

I had the same experience using "\u226570" inside a loop. It is due to the aspects of your system locale. Follow this:

  1. Use sessionInfo() and check the locale information.
  2. Change locale to English_United States.utf8 or other utf8 styles (like en_GB.UTF-8) if they are not already set. You can do this using Sys.setlocale("LC_ALL","English_United States.utf8").
  3. Check session information again and if locale settings are still unchanged, probably your OS does not support UTF-8 (e.g, updated versions of Windows have better UTF-8 support).

I think your problem would be resolved after setting a valid UTF-8 locale.

待天淡蓝洁白时 2025-02-15 10:21:35

您需要使用 flextable_to_rmd

请参阅:

您必须在“ path_to_to_to_to_to_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)
```

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文