简体   繁体   中英

Creating short examples for CRAN package

I'm currently building a package that I hope to upload to 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. 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?

I'm using devtools and roxygen2 to write my documentation, so it would be great to get an answer using these tools if possible.

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 ).

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 . 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 :

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

then put all the testing code in myPackage/tests/testthat , eg 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 will not be copied to the final package.

See the manual , section 2.1.1, example subsection. You can enclose your more heavy examples into \\dontrun and they will not be checked.

If the only problem is CRAN loading, you should use \\donttest{} command to prevent running the examples on CRAN. Note that the examples will still run when the user type example(xx)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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