Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rmarkdown docx中的flextable未在if语句中打印if其他文本_R_R Markdown_Flextable - Fatal编程技术网

Rmarkdown docx中的flextable未在if语句中打印if其他文本

Rmarkdown docx中的flextable未在if语句中打印if其他文本,r,r-markdown,flextable,R,R Markdown,Flextable,我正在尝试使用包flextable在我的Rmarkdown(转到word文件)中获得一些格式良好的表。这些表通常工作正常,但是如果我把它放在if语句中,如果if语句中还打印了其他内容,我就看不到该表。知道发生了什么事吗 更新2020年1月的信息,供仍在关注此问题的人使用 从flextable的0.5.5版开始,有一个新的函数docx_value来解决这个问题,我已经更新了答案以反映这一点,这样其他人就不会使用复杂的解决方法,现在有了一个简单的解决方案 我的示例(一起运行): 第一次测试工作正

我正在尝试使用包
flextable
在我的Rmarkdown(转到word文件)中获得一些格式良好的表。这些表通常工作正常,但是如果我把它放在if语句中,如果if语句中还打印了其他内容,我就看不到该表。知道发生了什么事吗



更新2020年1月的信息,供仍在关注此问题的人使用

从flextable的0.5.5版开始,有一个新的函数
docx_value
来解决这个问题,我已经更新了答案以反映这一点,这样其他人就不会使用复杂的解决方法,现在有了一个简单的解决方案


我的示例(一起运行):

第一次测试工作正常-无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

```
##测试1如果状态为空,则表否
```{r test1,echo=FALSE,results='asis'}

测试我想你的问题与你的健康有关。 像这样改变有问题的块似乎是可行的:

## 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"))
}
```
---
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   ")


}

##测试3 if语句和其他文本
```{r test3,echo=FALSE}

RunTaby

不确定是否考虑不同的包,但这似乎起作用:

## 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"))
}
```
---
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   ")


}

---
标题:“测试”
输出:
word\u文件
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE,fig.height=1.5,fig.width=3,fig.align='right',fig.align=“center”)
```
##降价
```{r默认值}
图书馆(潘德尔)
图书馆(knitr)
图书馆(flextable)
图书馆(表格HTML)
```
##测试1如果状态为,则表no
```{r test1,echo=FALSE}
测试%tableHTML_到_图像()
```
##测试2如果语句没有其他文本
```{r test2,echo=FALSE}
运行表%tableHTML\u到\u图像()
}
```
```{r test3,echo=FALSE}
#Hack使dat工作到2047年,因为padr中的cpp函数无法处理2038年以后的数据
#获取每日价值
运行表%tableHTML\u到\u图像()
打印(“之后”)
}
```
##使用其他文本和换行符测试4 if语句
```{r test4,echo=FALSE}
运行表%tableHTML\u到\u图像()
猫(“\n”)
打印(“如果后面有换行符”)
}
例如,您可以将测试4视为输出:

几点注意:

  • 您可以按照您想要的方式设置表格的格式
  • 代码生成一个图像

您可以使用chunk选项
results='asis'
并使用
格式编写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   ")
}

```
##测试4 if语句与其他文本和换行符
```{r test4,echo=FALSE,results='asis'}

RunTable更新2020年1月的信息,供仍在关注此问题的人使用

从flextable的0.5.5版开始,有一个新功能
docx_value
来解决此问题,如软件包新闻中所述:

flextable 0.5.5 新功能
  • 新功能
    docx_值
    用于从非顶级显示flextables 在R标记文档中调用

Hmm,这个问题说,如果您有chunk选项results='asis',那么它应该可以工作。因为我很高兴拥有这一套(在我的例子中也是如此),所以这不应该是一个问题。所以这个问题仍然存在。我还没有机会测试您的代码,我想知道问题是否仍然存在,因为if语句中仍然调用了
asis\u output
,问题是“
asis\u output()
只在顶级R表达式中工作”,但它不会。我明天会测试一下,看看这是否是一个可行的解决办法。哇,这很奇怪。chunk选项和asis_输出函数之间似乎存在细微差别。如果您设置
results='asis'
并执行类似于
print(“a”)
的操作,您将获得未格式化的原始R输出,但不是原始文本
knit_print
for
flextable
使用
asis_输出
,因此其他块可能甚至不需要
结果
选项。谢谢,这对我来说确实有效,尽管问题是它只在顶级工作。谢谢你的建议,但我希望它们在word中被格式化为表格(我的自动报告将由其他人写入另一份报告,因此,如果他们想更改某些内容,如果他们可以就地更改就好了)确实有意义:)
## 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   ")
}

```