我正在尝试使用软件包flextable
在我的Rmarkdown中获得一些格式良好的表(进入Word文件)。这些表通常工作正常,但是如果我将其放在if语句中,则如果if语句中还有其他内容在打印,则看不到该表。有什么想法吗?
我的示例(一起运行):
---
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)
```
第一次测试工作正常-否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语句中添加其他输出,无论是否有换行符,我的输出都不会得到任何表
## 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 ")
}
```
答案 0 :(得分:3)
您可以使用块选项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 ")
}
```
答案 1 :(得分:2)
我认为您的问题与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
来在表格前实际插入一个额外的空白行。\n
,否则对我来说是行不通的。\\newline
条目,但是我最多只能得到一个空白行。答案 2 :(得分:2)
不确定是否要考虑使用其他软件包,但这似乎可行:
---
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作为输出:
一些注意事项:
答案 3 :(得分:1)
针对仍在查看此内容的任何人更新2020年1月
从flextable版本0.5.5开始,有一个新功能docx_value
可以解决此问题,如软件包新闻所述:
docx_value
,可从非顶级显示弹性表
在R Markdown文档中调用。