简体   繁体   English

为CRAN包创建简短示例

[英]Creating short examples for CRAN package

I'm currently building a package that I hope to upload to CRAN. 我正在构建一个我希望上传到CRAN的软件包。 The examples I've included in my help documentation currently take some time to check (~12min in total), and I'm aware that this can be an issue when trying to load on CRAN. 我在帮助文档中包含的示例目前需要一些时间来检查(总共约12分钟),并且我知道在尝试加载CRAN时这可能是一个问题。 While I could make my examples run faster, I'm worried this will make them less meaningful for users reading the help files. 虽然我可以让我的示例运行得更快,但我担心这会使用户对阅读帮助文件的意义降低。 Is there a way to include some fast (but less meaningful) examples that will be checked by CRAN but won't be visible to users in the help documentation? 有没有办法包含一些快速(但不太有意义)的示例,这些示例将由CRAN检查但在帮助文档中对用户不可见?

I'm using devtools and roxygen2 to write my documentation, so it would be great to get an answer using these tools if possible. 我正在使用devtools和roxygen2来编写我的文档,所以如果可能的话,使用这些工具获得答案会很棒。

Thanks for your help. 谢谢你的帮助。

Sounds like you would like to do some unit testing, which is a very good idea when working with more complex packages. 听起来你想做一些单元测试,这在使用更复杂的软件包时是一个非常好的主意。 Check out the testthat package ( article , source ). 查看testthat包( 文章来源 )。

Short and purely technical examples that are needed to ensure correct operation of your package, but not very relevant to the end user, would then go into a separate folder myPackage/tests . 确保正确操作包但与最终用户无关的简短纯技术示例将进入单独的文件夹myPackage/tests These will automatically be run every time you build the package, but will not be included in the documentation files. 这些将在每次构建程序包时自动运行,但不会包含在文档文件中。

The recommended solution is to make a file called myPackage/tests/run-all.R : 建议的解决方案是创建一个名为myPackage/tests/run-all.R

library(testthat)
library(myPackage)
test_package("myPackage")

then put all the testing code in myPackage/tests/testthat , eg myPackage/tests/testthat/foo.R : 然后将所有测试代码放在myPackage/tests/testthat ,例如myPackage/tests/testthat/foo.R

context("Basic operation")

test_that("Default execution", {
    a <- 4
    b <- 34
    expect_equal(myFunction(a, b), a + b)
})

The reason for putting the tests in myPackage/tests/testthat rather than directly in myPackage/tests is that myPackage/tests/testthat will be included in the final package (albeit in a different folder) which allows the user to also run the tests. 将测试放在myPackage/tests/testthat而不是直接放在myPackage/testsmyPackage/tests/testthat将包含在最终包中(尽管在不同的文件夹中),允许用户也运行测试。 myPackage/tests will not be copied to the final package. myPackage/tests不会被复制到最终包中。

See the manual , section 2.1.1, example subsection. 请参阅手册 ,第2.1.1节, example小节。 You can enclose your more heavy examples into \\dontrun and they will not be checked. 您可以将更重的示例包含在\\dontrun并且不会检查它们。

If the only problem is CRAN loading, you should use \\donttest{} command to prevent running the examples on CRAN. 如果唯一的问题是CRAN加载,则应使用\\donttest{}命令阻止在CRAN上运行示例。 Note that the examples will still run when the user type example(xx) 请注意,当用户键入example(xx)时,示例仍将运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM