R 包开发,如何找到 devtools test() 完整堆栈的警告回溯?

问题描述 投票:0回答:0

我正在第一次使用 roxygen2 创建一个包,到目前为止,该项目正在进行中。但是,我注意到 我似乎无法在我的控制台中获得与

devtools::test()
包相同的警告输出层。

快速重现我的问题:

  1. 使用 RStudio 预设创建一个新的包文件夹
  2. 更新包以使用 roxygen2 文档
  3. 把Hello.R改成
#' Example dplyr Warning Function
#'
#' @import dplyr
#' @export

warn_repex <- function() {
  testx <- data.frame("A" = c(1,1,2),"B" = c("X","X","Y"))
  testy <- data.frame("A" = c(1,2), "C" = c("A","B"))
  dplyr::left_join(testy,testx)
}
  1. 运行一次
    usethis::use_test("ExampleRunthrough")
  2. 将test-ExampleRunthrough.R的内容改成
test_that("multiplication works", {
  expect_type(warn_repex(), "list")
})

现在,当我们以用户身份逐行运行示例包的内容时,我们将在输出中收到警告:

> testx <- data.frame("A" = c(1,1,2),"B" = c("X","X","Y"))
> testy <- data.frame("A" = c(1,2), "C" = c("A","B"))
> dplyr::left_join(testy,testx)
Joining with `by = join_by(A)`
Warning in dplyr::left_join(testy, testx) :
  Each row in `x` is expected to match at most 1 row in `y`.
ℹ Row 1 of `x` matches multiple rows.
ℹ If multiple matches are expected, set `multiple = "all"` to silence this warning.
  A C B
1 1 A X
2 1 A X
3 2 B Y

但是,如果我们安装包,或使用 devtools::load_all() 并调用包函数,我们只会得到有关列连接的变量的消息。

> devtools::load_all()
ℹ Loading testingBiocCode
> warn_repex()
Joining with `by = join_by(A)`
  A C B
1 1 A X
2 1 A X
3 2 B Y

现在的问题是: 我有使用

devtools::test()
函数的部分回溯的日志:

> devtools::test()
ℹ Testing testingBiocCode
✔ | F W S  OK | Context
⠏ |         0 | ExampleRunthrough                                                                                                    Joining with `by = join_by(A)`
✔ |   1     1 | ExampleRunthrough
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Warning (test-ExampleRunthrough.R:2): multiplication works
Each row in `x` is expected to match at most 1 row in `y`.
i Row 1 of `x` matches multiple rows.
i If multiple matches are expected, set `multiple = "all"` to silence this warning.
Backtrace:
  1. testthat::expect_type(warn_repex(), "list")
       at test-ExampleRunthrough.R:2:2
 20. dplyr (local) `<fn>`(`<vctrs___>`)
 21. dplyr:::rethrow_warning_join_matches_multiple(cnd, error_call)
 22. dplyr:::warn_join(...)
 23. dplyr:::warn_dplyr(...)

然而,即使使用这三行代码,也“不可能”正确缩小最终导致警告的行,给出这 23 个调用堆栈。

因此,我显然继续冒险搜索命令,可能是组合,以递归打印警告,例如。关于弃用,从用户/开发人员的角度来看,从您的包功能中使用的所有包来看,“期间”就是这种情况

devtools::test()
.

在我的例子中,我有一个跨多个文档的代码,跨多个函数,所有这些文档都链接到一个大包装器,如果我只知道如何查看堆栈的所有步骤,我在

devtools::test()
期间看到的裁剪警告回溯似乎是完美的;在堆栈调用 1 之后发布的可重现示例中,显示跳到 20.,使(对我而言)最重要的调用 2.-19。 (我们在这里知道,错误发生的地方)对我来说是看不见的。

我已经尝试了各种回溯功能并增加了警告选项等但没有成功,没有找到任何好的方法可以比用我自己的话发布它更好地解决这个问题。 我希望它是独一无二的并且仍然很有趣,如果我错过了什么(我绝对必须错过)请告诉我!

感谢您提前抽空!

r devtools r-package rlang
© www.soinside.com 2019 - 2024. All rights reserved.