【问题标题】:flextable in Rmarkdown docx not printing within if statement if other textRmarkdown docx中的flextable不在if语句中打印如果其他文本
【发布时间】:2019-05-31 03:03:41
【问题描述】:

我正在尝试使用包 flextable 在我的 Rmarkdown 中获取一些格式良好的表格(转到 word 文件)。这些表通常工作得很好,但如果我把它放在 if 语句中,如果从 if 语句中打印了其他任何内容,我看不到表。有什么想法吗?


2020 年 1 月更新,所有仍在关注此内容的人

从 0.5.5 版的 flextable 开始,有一个新函数 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)

```

第一个测试工作正常 - 表格两侧没有 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   ")


}

```

输出:

【问题讨论】:

    标签: r r-markdown flextable


    【解决方案1】:

    不确定您是否会考虑使用其他软件包,但这似乎可行:

    ---
    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 视为输出:

    几点说明:

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

    【讨论】:

    • 您好,感谢您的建议,但我希望将它们格式化为 word 中的表格(我的自动报告将由其他人进入另一个报告,因此如果他们想更改某些内容,最好可以就地完成)
    • 当然有道理:)
    【解决方案2】:

    您可以使用块选项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   ")
    }
    
    ```
    

    【讨论】:

      【解决方案3】:

      我认为您的问题与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 条目,但我能得到的最多只有一个空行。

      【讨论】:

      • 嗯,那个问题说如果你有块选项 results = 'asis' 那么它应该可以工作。因为我很高兴拥有这套(并在我的示例中),所以它不应该是一个问题。所以问题仍然存在。我还没有机会测试你的代码,我想知道问题是否仍然存在,因为 asis_output 仍然在 if 语句中被调用,并且问题说“asis_output() 仅适用于顶级 R 表达式”它不会的。明天我会测试看看是否可行
      • 是的,这很奇怪。块选项和asis_output 函数之间似乎存在细微差别。如果您设置results='asis' 并执行print("a") 之类的操作,您将获得没有格式化的原始R 输出,但不是原始文本。 knit_print for flextable 使用 asis_output,因此其他块可能甚至不需要 results 选项。
      • 谢谢,这确实对我有用,尽管问题说它只适用于顶层
      【解决方案4】:

      2020 年 1 月更新,所有仍在关注此内容的人

      从 0.5.5 版的 flextable 开始,有一个新函数 docx_value 来解决这个问题,如包新闻中所述:

      弹性表 0.5.5

      新功能

      • 新功能docx_value 允许从非顶层显示弹性表 在 R Markdown 文档中调用。

      【讨论】:

        猜你喜欢
        • 2020-02-07
        • 2018-03-21
        • 2014-06-30
        • 2013-11-05
        • 2022-08-18
        • 2021-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多