如何获得 expect_equal() 函数(包 testthat)与 rmarkdown 文件一起使用?

How to get expect_equal() function (package testthat) work with rmarkdown files?

我想在 rmarkdown 文件中使用包 testthat 中的函数 expect_equal(),但是渲染的执行(从 Knit 按钮开始 RStudio) 停止并且没有输出产生。

文件中给出了一个最小示例 error.Rmd:

---
title: "error"
author: "N"
date: "4 października 2016"
output: html_document
---

```{r, echo = FALSE}
library(testthat)
```

```{r ex1, error=TRUE}
a <- (1:5)
a[p]
```

```{r ex2, error=TRUE}
expect_equal(10, 10)
expect_equal(10, 10 + 1e-7)
expect_equal(10, 11)
```

在第二个块 ex2 中,行 expect_equal(10, 11) 应该会产生一个错误,但是文件的渲染不应该因为块选项 error=TRUE 而停止。但是,渲染停止并且没有产生输出。

渲染输出如下:

processing file: error.Rmd
  |.........                                                    |  14%
  ordinary text without R code

  |...................                                          |  29%
label: unnamed-chunk-1 (with options) 
List of 1
 $ echo: logi FALSE

  |............................                                 |  43%
  ordinary text without R code

  |.....................................                        |  57%
label: ex1 (with options) 
List of 1
 $ error: logi TRUE

  |..............................................               |  71%
  ordinary text without R code

  |........................................................     |  86%
label: ex2 (with options) 
List of 1
 $ error: logi TRUE


BŁĄD: 10 not equal to 11.
1/1 mismatches
[1] 10 - 11 == -1
Wykonywanie wstrzymane 

波兰语 "BŁĄD" 表示 "ERROR","Wykonywanie wstrzymane" 表示 "Execution halted"。

ex1 按预期工作:表达式 a[p] 导致错误,错误消息放在输出中并继续渲染。

在块 ex2 中,前两个表达式不会产生错误,第三个表达式被注释掉后,输出会正确呈现。

使用包 testthat 中的其他函数代替第三个表达式,例如all.equal(10,11) 运行良好,不停止渲染。

使用 testthatexpect_identical() 中的另一个函数也会停止渲染过程。

使用关键字 rrmarkdowntestthatexpect_equal"stop on error" 的不同组合搜索未得到有效的问题解决方案.

你能重现这种意外行为吗?

有什么建议,如何在rmarkdown中使用函数expect_equal并获得最终输出(如果此行为不是函数expect_equal本身错误的结果)?

我的sessionInfo():

R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=pl_PL.UTF-8       LC_NUMERIC=C               LC_TIME=pl_PL.UTF-8        LC_COLLATE=pl_PL.UTF-8    
 [5] LC_MONETARY=pl_PL.UTF-8    LC_MESSAGES=pl_PL.UTF-8    LC_PAPER=pl_PL.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=pl_PL.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] testthat_1.0.2       rmarkdown_1.0.9016   RevoUtilsMath_10.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7      crayon_1.3.2     digest_0.6.10    assertthat_0.1   R6_2.1.3         formatR_1.4      magrittr_1.5    
 [8] evaluate_0.9     stringi_1.1.2    RevoUtils_10.0.1 tools_3.3.1      stringr_1.1.0    rsconnect_0.4.3  yaml_2.1.13     
[15] htmltools_0.3.5  knitr_1.14       tibble_1.2      

您可以将 test_that() 包裹在您的 expect_equal() 表达式中:

---
title: "error"
author: "N"
date: "4 października 2016"
output: html_document
---

```{r, echo = FALSE}
library(testthat)
```

```{r ex1, error=TRUE}
a <- (1:5)
a[p]
```

```{r ex2, error=TRUE}
test_that(desc = 1, expect_equal(10, 10))
test_that(desc = 2, expect_equal(10, 10 + 1e-7))
test_that(desc = 3, expect_equal(10, 11))
```