testthat和roxygen用于不是包的分析项目。

huangapple go评论59阅读模式
英文:

testthat and roxygen for analytical projects that are not packages

问题

我尝试找出如何在处理非包的R项目时使用一些开发工具功能,比如testthat、roxygen等。在《R Packages (2e)》一书中,有很好的示例说明如何使用所有这些功能。但是当我尝试在非包项目中使用时,会收到错误消息,例如以下内容:

test()
Error in package_file():
! Could not find package root.
ℹ Is . inside a package?
Run rlang::last_trace() to see where the error occurred.

我在一个未设置为包的项目中工作。usethis包的文档明确提到了这个选项,但我找不到任何实际操作的示例。我找到的所有文档都是关于包开发的。

英文:

I try to find out how I can use some devtools features like testthat, roxygen etc., when I work with an R project that is not a package.
In the "R Packages (2e)" book, there are good examples how I can use all this features. But when I try to use this in a project that is not a package, I will receive error messages, e.g. the following:

> test()
Error in package_file():
! Could not find package root.
ℹ Is . inside a package?
Run rlang::last_trace() to see where the error occurred.

I work within a project that I did not setup as a package. The usethis package documentation mentions this option explicitly, but I could not find any examples how this works in practice. All documentations I could find refer to package development.

答案1

得分: 1

可能会更有意义,如果您只是设置一个包 - 这样做会使生活更轻松,因为roxygen2testthat大多是设计用于与包一起工作的。如果您正在使用RStudio,您还可以轻松地触发生成检查按钮来创建roxygen文档或检查您的包。

正如Karolis在评论中提到的,一个R包不过是拥有一个DESCRIPTION文件和一个R文件夹,其中存储着您的R文件。
(还有一个tests文件夹,如果您想进行测试)

但如果您不想设置一个包,当然也可以在普通项目中使用testthat。

只需编写一个testthat测试,并将其存储在一个.R文件中。

例如,将这个测试(当然用更有用的内容替换它)放在一个名为my_test.R的R文件中,假设它在一个名为tests的文件夹中。

testthat::expect_identical(1,1)

之后,您可以调用test_file()

testthat::test_file(path = "tests/my_test.R")

然后您会得到类似这样的输出:

> [ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ]

如果您有多个测试文件,您可以使用test_dir()来检查整个文件夹。

所以,要检查tests文件夹中的所有文件,您可以编写:

testthat::test_dir(path = "tests")

请注意,在您的测试文件中,您必须首先使用source加载您的代码,否则testthat不会加载您定义的函数。使用包时,您不需要这样做。

source("/R/my_code.R")
test_that("Add", 
{
   expect_equal(my_add_function(1,1), 2)
})

因此,您必须source my_code.R,以加载您的my_add_function函数,以便使用testthat进行测试。
(正如在使用包时不需要加载代码内容一样)

英文:

Probably it would make sense if you just set up a package - it makes live easier since both roxygen2 and testthat are mostly designed to work with packages. If you are using RStudio you also can just comfortably trigger the Built and Check- buttons to create roxygen documentation or check your package.

As Karolis mentioned in the comments, an R package is not much more than having a DESCRIPTION file and an R folder, where your R files are stored.
(and a folder tests, when you want to have tests)

But if you do not want to set up a package you can of course also use testthat within a normal project.

Just write a testthat test and store it in an .R file.

E.g. take this test (of course replace it with something more useful) and put it in a R file my_test.R assume it is in a folder called tests.

testthat::expect_identical(1,1)

Afterwards you can call test_file()

testthat::test_file(path = "tests/my_test.R")

And you get an output like this:

> [ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ]

If you have multiple files with tests, you can just call test_dir() to check a whole folder

So to check all files in the tests folder you would write:

testthat::test_dir(path = "tests")

Be aware, in your tests files you will have to source your code first, otherwise testthat does not have the functions you defined loaded. Using a package you do not have to do this.

source("/R/my_code.R")
test_that("Add", 
{
   expect_equal(my_add_function(1,1), 2)
})

So you have to source my_code.R to have your my_add_function function loaded, to test it with testthat.
(as mentioned with a package you don't need to source your code contents)

huangapple
  • 本文由 发表于 2023年3月31日 22:57:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定