【问题标题】:How to get expect_equal() function (package testthat) work with rmarkdown files?如何让 expect_equal() 函数(包 testthat)与 rmarkdown 文件一起使用?
【发布时间】:2017-02-13 20:05:10
【问题描述】:

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

文件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”的意思是“错误”,“Wykonywanie wstrzymane”的意思是“执行停止”。

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

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

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

使用testthat 中的另一个函数expect_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      

【问题讨论】:

    标签: r r-markdown testthat


    【解决方案1】:

    您可以将 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))
    ```
    

    【讨论】:

    • 我明白了,谢谢。块ex2 中的代码直接取自Hadley Wickham 的R packages 书,“测试”一章。为什么它应该在没有环绕功能test_that() 的情况下工作?
    • Hadley 确实使用了expect_equal(),而没有将test_that() 包裹起来。当我复制他关于测试一章的原始 Rmd 并尝试编织它时,我遇到了同样的问题,即失败的预期会中断编织过程。不知道为什么它似乎对他有用:/
    猜你喜欢
    • 2015-07-03
    • 2014-10-17
    • 2018-10-22
    • 1970-01-01
    • 2016-10-10
    • 2022-08-06
    • 1970-01-01
    • 2021-11-20
    • 2011-12-20
    相关资源
    最近更新 更多