【问题标题】:dplyr clashes with testthat package when matches is used使用匹配时,dplyr 与 testthat 包发生冲突
【发布时间】:2016-11-16 06:52:09
【问题描述】:

因为testthat::matchesdplyr::matches 冲突,我收到一个错误,我想知道如何使用testthat::test_file 来检查包含对matches() 的调用的函数,而不必在函数中指定dplyr::matches身体。

例如:

> testthat::test_file('tmp_fn_testthat_test.R')

Attaching package: ‘testthat’

The following object is masked from ‘package:dplyr’:

    matches

The following object is masked from ‘package:purrr’:

    is_null

 Show Traceback

 Rerun with Debug
 Error in -matches("tmp") : invalid argument to unary operator In addition: Warning message:
package ‘testthat’ was built under R version 3.2.5 

DONE =========================================================================================================================================

可以通过将以下代码保存在工作目录中名为tmp_fn_testthat_test.R 的文件中并运行命令testthat::test_file('tmp_fn_testthat_test_20161115.R') 来重现此错误。请注意,在未加载 testthat 时获取或运行 expect_equal 命令会使测试通过。

tmp_fn <- function() {
    tmp_df <- data.frame(tmp_a = 1, tmp_b = 2)

    tmp_df %>%
        select(-matches('tmp')) %>%
        ncol


}

testthat::expect_equal(tmp_fn(), 0)

【问题讨论】:

    标签: r dplyr testthat


    【解决方案1】:

    This is a known issue 与 dplyr 0.5。推荐的解决方案是使用显式命名空间前缀:dplyr::matches

    【讨论】:

    • 我明白这一点,但我的问题是我明确地试图不必加载testthat,方法是在所有测试前加上testthat::,以便在我获取文件时测试工作.无论出于何种原因,当您运行 testthat::test_file 时,它都会坚持加载包。
    【解决方案2】:

    一种解决方法似乎是注释掉testthat::test_file 定义中的library(testthat),并使函数调用显式(不确定这是否会产生不良副作用):

    my_test_that_file <- function (path, reporter = "summary", env = testthat::test_env(), start_end_reporter = TRUE, 
              load_helpers = TRUE) 
    {
        # library(testthat)
        reporter <- testthat:::find_reporter(reporter)
        if (load_helpers) {
            testthat:::source_test_helpers(dirname(path), env = env)
        }
        lister <- testthat:::ListReporter$new()
        if (!is.null(reporter)) {
            reporter <- testthat:::MultiReporter$new(reporters = list(reporter, 
                                                           lister))
        }
        else {
            reporter <- lister
        }
        testthat::with_reporter(reporter = reporter, start_end_reporter = start_end_reporter, 
                      {
                          lister$start_file(basename(path))
                          testthat::source_file(path, new.env(parent = env), chdir = TRUE)
                          testthat:::end_context()
                      })
        invisible(lister$get_results())
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 2013-11-26
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      相关资源
      最近更新 更多