首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用matches时,dplyr与testthat包发生冲突

使用matches时,dplyr与testthat包发生冲突
EN

Stack Overflow用户
提问于 2016-11-15 10:30:06
回答 2查看 152关注 0票数 2

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

例如:

代码语言:javascript
复制
> 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命令可使测试通过。

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

发布于 2016-11-15 21:40:51

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

票数 2
EN

Stack Overflow用户

发布于 2016-11-16 06:52:10

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

代码语言:javascript
复制
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())
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40601061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档