如果其他文本,Rmarkdown docx 中的 flextable 不在 if 语句中打印
flextable in Rmarkdown docx not printing within if statement if other text
我正在尝试使用包 flextable
在我的 Rmarkdown(转到 word 文件)中获得一些格式良好的 tables。 tables 总体上工作得很好,但是如果我将它放在 if 语句中,如果 if 语句中有任何其他内容正在打印,我就看不到 table。有什么想法吗?
2020 年 1 月更新给仍在看这篇文章的人
从 flex 的 0.5.5 版开始table 有一个新函数 docx_value
来解决这个问题,我已经更新了答案以反映这一点,这样其他人就不会使用复杂的变通办法现在有一个简单的解决办法。
我的例子(运行 一起):
---
title: "Testing"
output:
word_document:
reference_docx: styles.docx
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r defaults}
library(pander)
library(knitr)
library(flextable)
```
第一个测试工作正常 - table
两侧没有 if 语句和新行
## test 1 table no if statemnets
```{r test1, echo = FALSE, results = 'asis'}
test <- data.frame (c = 1:5, x = 6:10)
testft <- flextable(test)
testft
```
第二个测试有一个没有其他文本的 if 语句并且工作正常
## test 2 if statement no other text
```{r test2, echo = FALSE, results = 'asis'}
RunTable <- TRUE
if(RunTable){
testft
}
```
但是如果我尝试在我的 if 语句中添加其他输出,无论是否有换行符,我都不会在我的输出 table 中得到任何
## test 3 if statement with other text
```{r test3, echo = FALSE, results = 'asis'}
#Hack so dat works up to year 2047 as cpp functions in padr can't handle data beyond 2038
#Get Daily Values
RunTable <- TRUE
if(RunTable){
print("before ")
testft
print("after ")
}
```
## test 4 if statement with other text and newlines
```{r test4, echo = FALSE, results = 'asis'}
RunTable <- TRUE
if(RunTable){
print("if with linebreak before ")
cat(" \n")
knit_print(testft)
cat(" \n")
print("if with linebreak after ")
}
```
输出:
我认为您的问题与 this issue 有关。
像这样更改有问题的块似乎有效:
## test 3 if statement with other text
```{r test3, echo = FALSE}
RunTable <- TRUE
if(RunTable){
text <- c(
"before ",
knit_print(testft),
"after "
)
asis_output(paste(text, collapse = "\n"))
}
```
## test 4 if statement with other text and newlines
```{r test4, echo = FALSE}
RunTable <- TRUE
if(RunTable){
text <- c(
"if with linebreak before ",
" \newline",
knit_print(testft),
" \newline\n",
"if with linebreak after "
)
asis_output(paste(text, collapse = "\n"))
}
```
关于最后一个:
- 我不得不使用
\newline
在 table 之前插入一个额外的空行。
- 我不知道为什么后面的空行需要一个额外的
\n
,否则它对我不起作用。
- 为了测试,我尝试在前后添加几个
\newline
条目,但我能得到的最多是一个空行。
不确定您是否会考虑使用不同的软件包,但这似乎可行:
---
title: "Testing"
output:
word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.height=1.5, fig.width=3, fig.align='right', fig.align = "center")
```
## R Markdown
```{r defaults}
library(pander)
library(knitr)
library(flextable)
library(tableHTML)
```
## test 1 table no if statemnets
```{r test1, echo = FALSE}
test <- data.frame (c = 1:5, x = 6:10)
tab <- tableHTML(test, widths = c(60, 60), rownames = FALSE) %>% add_theme('scientific')
tab %>% tableHTML_to_image()
```
## test 2 if statement no other text
```{r test2, echo = FALSE}
RunTable <- TRUE
if(RunTable){
tab %>% tableHTML_to_image()
}
```
```{r test3, echo = FALSE}
#Hack so dat works up to year 2047 as cpp functions in padr can't handle data beyond 2038
#Get Daily Values
RunTable <- TRUE
if(RunTable){
print("before ")
tab %>% tableHTML_to_image()
print("after ")
}
```
## test 4 if statement with other text and newlines
```{r test4, echo = FALSE}
RunTable <- TRUE
if(RunTable){
print("if with linebreak before ")
cat(" \n")
tab %>% tableHTML_to_image()
cat(" \n")
print("if with linebreak after ")
}
例如,您可以看到测试 4 作为输出:
一些注意事项:
- 您可以按照您想要的方式格式化 table。
- 代码生成图像。
您可以使用块选项 results = 'asis'
并使用 format
编写 openxml 内容,如下所示
## test 4 if statement with other text and newlines
```{r test4, echo = FALSE, results = 'asis'}
RunTable <- TRUE
if(RunTable){
print("if with linebreak before ")
cat(" \n")
cat(
paste(
"\n```{=openxml}",
format(testft, type = "docx"),
"```\n", sep = "\n")
)
cat(" \n")
print("if with linebreak after ")
}
```
2020 年 1 月更新给仍在看这篇文章的人
从 flextable 的 0.5.5 版开始,有一个新函数 docx_value
来解决这个问题,如包新闻中所述:
灵活的 0.5.5
新功能
- 新功能
docx_value
允许从非顶层显示 flextables
在 R Markdown 文档中调用。
我正在尝试使用包 flextable
在我的 Rmarkdown(转到 word 文件)中获得一些格式良好的 tables。 tables 总体上工作得很好,但是如果我将它放在 if 语句中,如果 if 语句中有任何其他内容正在打印,我就看不到 table。有什么想法吗?
2020 年 1 月更新给仍在看这篇文章的人
从 flex 的 0.5.5 版开始table 有一个新函数 docx_value
来解决这个问题,我已经更新了答案以反映这一点,这样其他人就不会使用复杂的变通办法现在有一个简单的解决办法。
我的例子(运行 一起):
---
title: "Testing"
output:
word_document:
reference_docx: styles.docx
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r defaults}
library(pander)
library(knitr)
library(flextable)
```
第一个测试工作正常 - table
两侧没有 if 语句和新行## test 1 table no if statemnets
```{r test1, echo = FALSE, results = 'asis'}
test <- data.frame (c = 1:5, x = 6:10)
testft <- flextable(test)
testft
```
第二个测试有一个没有其他文本的 if 语句并且工作正常
## test 2 if statement no other text
```{r test2, echo = FALSE, results = 'asis'}
RunTable <- TRUE
if(RunTable){
testft
}
```
但是如果我尝试在我的 if 语句中添加其他输出,无论是否有换行符,我都不会在我的输出 table 中得到任何
## test 3 if statement with other text
```{r test3, echo = FALSE, results = 'asis'}
#Hack so dat works up to year 2047 as cpp functions in padr can't handle data beyond 2038
#Get Daily Values
RunTable <- TRUE
if(RunTable){
print("before ")
testft
print("after ")
}
```
## test 4 if statement with other text and newlines
```{r test4, echo = FALSE, results = 'asis'}
RunTable <- TRUE
if(RunTable){
print("if with linebreak before ")
cat(" \n")
knit_print(testft)
cat(" \n")
print("if with linebreak after ")
}
```
输出:
我认为您的问题与 this issue 有关。 像这样更改有问题的块似乎有效:
## test 3 if statement with other text
```{r test3, echo = FALSE}
RunTable <- TRUE
if(RunTable){
text <- c(
"before ",
knit_print(testft),
"after "
)
asis_output(paste(text, collapse = "\n"))
}
```
## test 4 if statement with other text and newlines
```{r test4, echo = FALSE}
RunTable <- TRUE
if(RunTable){
text <- c(
"if with linebreak before ",
" \newline",
knit_print(testft),
" \newline\n",
"if with linebreak after "
)
asis_output(paste(text, collapse = "\n"))
}
```
关于最后一个:
- 我不得不使用
\newline
在 table 之前插入一个额外的空行。 - 我不知道为什么后面的空行需要一个额外的
\n
,否则它对我不起作用。 - 为了测试,我尝试在前后添加几个
\newline
条目,但我能得到的最多是一个空行。
不确定您是否会考虑使用不同的软件包,但这似乎可行:
---
title: "Testing"
output:
word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.height=1.5, fig.width=3, fig.align='right', fig.align = "center")
```
## R Markdown
```{r defaults}
library(pander)
library(knitr)
library(flextable)
library(tableHTML)
```
## test 1 table no if statemnets
```{r test1, echo = FALSE}
test <- data.frame (c = 1:5, x = 6:10)
tab <- tableHTML(test, widths = c(60, 60), rownames = FALSE) %>% add_theme('scientific')
tab %>% tableHTML_to_image()
```
## test 2 if statement no other text
```{r test2, echo = FALSE}
RunTable <- TRUE
if(RunTable){
tab %>% tableHTML_to_image()
}
```
```{r test3, echo = FALSE}
#Hack so dat works up to year 2047 as cpp functions in padr can't handle data beyond 2038
#Get Daily Values
RunTable <- TRUE
if(RunTable){
print("before ")
tab %>% tableHTML_to_image()
print("after ")
}
```
## test 4 if statement with other text and newlines
```{r test4, echo = FALSE}
RunTable <- TRUE
if(RunTable){
print("if with linebreak before ")
cat(" \n")
tab %>% tableHTML_to_image()
cat(" \n")
print("if with linebreak after ")
}
例如,您可以看到测试 4 作为输出:
一些注意事项:
- 您可以按照您想要的方式格式化 table。
- 代码生成图像。
您可以使用块选项 results = 'asis'
并使用 format
编写 openxml 内容,如下所示
## test 4 if statement with other text and newlines
```{r test4, echo = FALSE, results = 'asis'}
RunTable <- TRUE
if(RunTable){
print("if with linebreak before ")
cat(" \n")
cat(
paste(
"\n```{=openxml}",
format(testft, type = "docx"),
"```\n", sep = "\n")
)
cat(" \n")
print("if with linebreak after ")
}
```
2020 年 1 月更新给仍在看这篇文章的人
从 flextable 的 0.5.5 版开始,有一个新函数 docx_value
来解决这个问题,如包新闻中所述:
灵活的 0.5.5
新功能
- 新功能
docx_value
允许从非顶层显示 flextables 在 R Markdown 文档中调用。