# Publishing on bioconductor https://contributions.bioconductor.org/tests.html tests for bioconductor packages https://github.com/MetaboComp/batchCorr/tree/master # Publishing on CRAN We follow the instruction below and published the MUVR2 package https://cran.r-project.org/web/packages/MUVR2/index.html The gitlab repository https://gitlab.com/YingxiaoYan/MUVR2 It is a tedious and time-consuming process. ## 0. How to do checking `devtools::check()` This equals to the check button of the package `wd<-getwd()` Specifying the working directory This is to save all the result of check in the check_ouput `check_output<-reprex::reprex( input=sprintf("devtools::check(pkg='%s',vignettes=FALSE)\n",wd), comment="????") ## comment line is the start of each line ` The following code is to save the check result to a txt file in the working directory ``` sink("output_text.txt") check_output sink() ``` ## 1. build vignette `usethis::use_vignette("The name of the rmd") `This is to generate the skeleten of the rmd file `devtools::build_rmd("vignettes/the name of the rmd.rmd")` This is to render the vignette and generate the html file ## 2. testing ### Structure and flows `usethis::use_testthat(3) ` This is to build the test/testhat folder for adding tests `usethis::use_r("R_function.R")` This is to open the R function that you want to test `usethis::use_test() ` Witht R function opened, this is to initialize a bascis test file and open it for editing `testthat::test_file("tests/testthat/test-R_function.R") `After you edit the test-R_function. This is to actually test the file ### how to write the test file ``` test_that("multiplication works", { expect_equal(2 * 2, 4) }) ``` ![image](https://hackmd.io/_uploads/r1UzubsBA.png) `expect_lt()` is a testing function in the testthat package for R that checks if the first value is less than the second value. `expect_identical()` is a testing function in the testthat package for R that checks if the first value is equal to the second value. ### load data to use If you want to use some data that needs to be loaded you need to specify it in the `test/testthat/helper.R`. ![image](https://hackmd.io/_uploads/BJECyNZR0.png) The data will be loaded in the R CMD check, only at that time will be loaded automaticlly. ### Set up some universal objects for all tests If you don't want to repeatedly run some code during tests. You could make them universal by `test/testthat/setup.R`. ![image](https://hackmd.io/_uploads/BkSZ-4bRC.png) ### parallization It will not work well in the @examples and unit tests. Do not use it. ## 3. Writing examples - Do no use` \donttest{}` or `\dontrun{}` if the examples are executable in < 5 sec - Use `\donttest{}` for functions that takes a lot of time. Do not use more than 2 cores in your examples and then donttest will work. - `\dontrun{}` should only be used if the example really cannot be executed (e.g. because of missing additional software, missing API keys,more than 2 computation cores) by the user. That's why wrapping examples in `\dontrun{}` adds the comment ("# Not run:") as a warning for the user. An example is shown below ![image](https://hackmd.io/_uploads/SyedgIb0R.png) ## 4. Writing decriptions Here is an example of how The desciption file of MUVR2 is written. Please take caution on: - Always explain all the abbreviations/acronyms - Add a bug report link - Version - How the authors are written; - How doi is written ``` Package: MUVR2 Title: Multivariate Methods with Unbiased Variable Selection Version: 0.1.0 Authors@R: c(person(given = "Carl", family = "Brunius", role = "aut", email = "carl.brunius@chalmers.se"), person(given = "Yingxiao", family = "Yan", role = c("aut", "cre"), email = "yingxiao@chalmers.se")) Author: Carl Brunius [aut], Yingxiao Yan [aut, cre] Maintainer: Yingxiao Yan <yingxiao@chalmers.se> Description: Predictive multivariate modelling for metabolomics. Types: Classification and regression. Methods: Partial Least Squares, Random Forest ans Elastic Net Data structures: Paired and unpaired Validation: repeated double cross-validation (Westerhuis et al. (2008)<doi:10.1007/s11306-007-0099-6>, Filzmoser et al. (2009)<doi:10.1002/cem.1225>) Variable selection: Performed internally, through tuning in the inner cross-validation loop. URL: https://github.com/MetaboComp/MUVR2 BugReports: https://github.com/MetaboComp/MUVR2/issues Depends: R (>= 3.2.2) Imports: stats, graphics, randomForest, ranger, pROC, doParallel, foreach, caret, glmnet, splines, dplyr, psych, magrittr, mgcv, grDevices, parallel, survival License: GPL-3 LazyData: true RoxygenNote: 7.3.2 Encoding: UTF-8 Suggests: testthat (>= 3.0.0) Config/testthat/edition: 3 ``` ## 5. write imported packages This could be messy. Some times writing import only in the Description file is not enough. You may need to specificy which functions are used from which package in your R scripts. Below is an example: ![image](https://hackmd.io/_uploads/SkzVW8ZCC.png) Package names is first written and then the names of the functions used are listed. ## 6. CRAN examination Do the following things in your terminal of the R project for your package. Change the directory to where your package folder is at: `cd Desktop` Check the R package. You need to pass the check with out error `R CMD check MUVR2 --as-cran` Build the R package. The R package will then be downloaded as a tar.gz file `R CMD build` Submit the tar.gz file to CRAN https://cran.r-project.org/submit.html Some other tips before CRAN submission(They have asked us to change that) - Change all cat() to message() to warning() - Always add a return value for all functions - Change all `T `and `F` to `TRUE` and `FALSE`