Last active
July 10, 2022 18:32
-
-
Save CorradoLanera/bd56d0f2caae06b61342019007d56dba to your computer and use it in GitHub Desktop.
Script to start the development of R projects/packages in a reproducible way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' --- | |
#' title: "Setup (with `{renv}`)" | |
#' author: "Corrado Lanera" | |
#' date: "`r date()`" | |
#' output: | |
#' html_document: | |
#' toc: true | |
#' toc_float: true | |
#' keep_md: true | |
#' --- | |
#' | |
#' ```{r setup, include=FALSE} | |
#' knitr::opts_chunk$set( | |
#' echo = TRUE, | |
#' eval = FALSE | |
#' ) | |
#' ``` | |
#' | |
#' > NOTE: For projects which would not use `{renv}` support, | |
#' see `01-pkgsetup.R` (here, in this gist). | |
#' | |
#' | |
#' Prerequisites | |
#' ==================================================================== | |
#' | |
# install.packages("usethis") | |
# install.packages("available") | |
available::available("<packagename>") | |
usethis::create_package("path/to/packagename") | |
usethis::use_directory("dev", ignore = TRUE) | |
# install.packages("fs") | |
fs::file_create("dev/01-setup.R") | |
rstudioapi::navigateToFile("dev/01-setup.R") | |
#' | |
#' Copy there the whole content of this file, and keep track there | |
#' of everything you do in the setup process. You can omit | |
#' unnecessary comments and packages' installations. | |
#' | |
usethis::use_roxygen_md() | |
usethis::use_news_md() | |
#' | |
#' From now on, document everything into the `NEWS.md` | |
#' To fill the NEWS, refer to | |
#' [this](https://style.tidyverse.org/news.html) | |
#' | |
#' | |
#' Documentiation | |
#' ==================================================================== | |
#' | |
#' DESCRIPTION | |
#' -------------------------------------------------------------------- | |
#' | |
#' Update/fill the DESCRIPTION file. Remember to include in the | |
#' `Authors@R` field at least: | |
#' | |
#' - aut: author(s) | |
#' - cre: mantainer of the package (only one) | |
#' - cph: copyright holder(s) | |
#' | |
#' eg: | |
#' | |
person( | |
given = "Corrado", | |
family = "Lanera", | |
role = c("aut", "cre", "cph"), | |
email = "corrado.lanera@gmail.com", | |
comment = c(ORCID = "0000-0002-0520-7428") | |
) | |
usethis::use_gpl3_license("Corrado Lanera") | |
usethis::use_package_doc() | |
#' | |
#' README | |
#' -------------------------------------------------------------------- | |
#' | |
#' Initialize the README file (ie the package's landing page) | |
#' | |
# install.packages("rmarkdown") | |
usethis::use_readme_rmd() | |
usethis::use_logo("path/to/package_logo.png") # not in the pkg folder! | |
usethis::use_cran_badge() | |
usethis::use_lifecycle_badge("Maturing") | |
#' | |
#' Our package is not on CRAN, change the README accordingly: | |
#' | |
#' You can install the development version from | |
#' [GitHub](https://github.com/) with the following procedure: | |
#' | |
#' ```{r, eval = FALSE} | |
#' # install.packages("devtools") | |
#' devtools::install_github("CorradoLanera/<package_name>") | |
#' ``` | |
#' | |
#' Remove everything else that is not necessary and | |
#' | |
usethis::use_code_of_conduct() | |
usethis::use_spell_check() | |
spelling::spell_check_package() | |
spelling::update_wordlist() | |
#' | |
#' > knit the README | |
#' | |
#' | |
#' Supporting Folders | |
#' ==================================================================== | |
#' | |
#' Data raw | |
#' -------------------------------------------------------------------- | |
#' | |
#' If raw data are stored inside the main package folder call | |
usethis::use_data_raw() | |
#' | |
#' and comment the line with `usethis::use_data(...)` in it until it | |
#' will be used. | |
#' | |
#' | |
#' Other folder (eg Analyses) | |
#' -------------------------------------------------------------------- | |
#' | |
#' | |
#' usethis::use_directory("analysis", ignore = TRUE) | |
#' usethis::use_directory("output", ignore = TRUE) | |
#' | |
#' | |
#' Test suit | |
#' ==================================================================== | |
#' | |
usethis::use_testthat() | |
#' | |
#' Create a simple test to check everything works. | |
#' You can delete it AFTER you have written another test at least. | |
#' | |
#' Check it works: | |
#' | |
usethis::use_test("foo") # `test_that("foo works", expect_null(foo()))` | |
devtools::test() # see it fails!! | |
usethis::use_r("foo") # define `foo <- function() NULL` | |
devtools::test() # see it passes!! | |
#' | |
#' delete `foo` only after have included another function! | |
#' | |
#' | |
#' Quality assurance | |
#' ==================================================================== | |
#' | |
usethis::use_tidy_description() | |
# install.packages("lintr) | |
lintr::lint_package() | |
#' | |
#' and fix the spelling script that lint | |
#' have opened... ;-) | |
#' | |
if (requireNamespace("spelling", quietly = TRUE)) { | |
spelling::spell_check_test( | |
vignettes = TRUE, | |
error = FALSE, | |
skip_on_cran = TRUE | |
) | |
} | |
#' | |
#' Check again: | |
#' | |
lintr::lint_package() | |
devtools::check_man() | |
devtools::test() | |
devtools::check() | |
#' | |
#' Activate Git/GitHub | |
#' ==================================================================== | |
#' | |
usethis::use_git() | |
usethis::git_vaccinate() | |
#' | |
#' Commit everything before to continue! | |
#' | |
# remember to open and activate PuTTY | |
usethis::use_github( | |
# "<organization>", # eg, "UBESP-DCTV" | |
# private = TRUE # is a private repo? | |
) | |
#' | |
#' > NOTE: If required set the upstream using your preferred | |
#' GUI^[GitKraken: https://www.gitkraken.com/invite/fas3vkyk is free, | |
#' multiplatform (Win, Mac, Linux),and super cool :-).], or by command | |
#' line running (on the Terminal): | |
#' | |
#' git push --set-upstream origin master | |
#' | |
usethis::use_tidy_github() | |
#' | |
#' Activate {renv} for reproducibility | |
#' ==================================================================== | |
#' | |
#' > Note: This should be done AFTER git initialization | |
#' | |
#' We will use "explicit" snapshot whith the intent | |
#' of capture what is included in the | |
#' DESCRIPTION file only. | |
#' | |
renv::init(settings = list(snapshot.type = "explicit")) | |
renv::status() # just to check | |
#' | |
#' Package website documentation | |
#' ==================================================================== | |
#' | |
#' > Disclaimer (2020-06-22): This and the following actions are mine | |
#' modification of the ones you can find in | |
#' https://github.com/r-lib/actions/blob/master/examples/pkgdown.yaml | |
#' The changes are made to made the action able to be used with `{renv}` | |
#' as suggested in | |
#' https://rstudio.github.io/renv/articles/ci.html#github-actions | |
#' | |
#' As soon/if there will be implemented _official_ Actions, I will | |
#' substitute these with those ones. | |
#' | |
usethis::use_github_action( | |
url = "https://raw.githubusercontent.com/CorradoLanera/actions/master/pkgdown.yaml" | |
) | |
usethis::use_github_actions_badge("pkgdown") | |
#' | |
#' Bonus: | |
#' | |
renv::install("GuangchuangYu/badger") | |
badger::badge_custom("WEBsite", "click-me", "orange", "http://corradolanera.github.io/<packagename>/") | |
#' | |
#' And add it between title and logo in the README and knit it. | |
#' | |
#' | |
#' Continuous Integration | |
#' ==================================================================== | |
#' | |
#' Lint (static code-quality checks) | |
#' -------------------------------------------------------------------- | |
#' | |
usethis::use_github_action( | |
url = "https://raw.githubusercontent.com/CorradoLanera/actions/master/lint-renv.yaml" | |
) | |
usethis::use_github_actions_badge("lint") | |
#' | |
#' WARNING: if you do not use {renv} for your project, call | |
#' | |
#' usethis::use_github_action("lint") | |
#' usethis::use_github_actions_badge("lint") | |
#' | |
#' R-CDM-check and coverage | |
#' -------------------------------------------------------------------- | |
usethis::use_github_action( | |
url = "https://raw.githubusercontent.com/CorradoLanera/actions/master/R-CMD-check-renv.yaml" | |
) | |
usethis::use_github_actions_badge("R-CMD-check") | |
usethis::use_github_action( | |
url = "https://raw.githubusercontent.com/CorradoLanera/actions/master/covr-renv.yaml" | |
) | |
usethis::use_github_actions_badge("test-coverage") | |
#' | |
#' WARNING: if you do not use {renv} for your project, call | |
#' | |
#' | |
#' usethis::use_github_action("check-full", | |
#' save_as = "R-CMD-check.yaml" | |
#' ) | |
#' usethis::use_github_actions_badge("R-CMD-check") | |
#' | |
#' usethis::use_github_action("test-coverage", | |
#' save_as = "covr.yaml" | |
#' ) | |
#' usethis::use_github_actions_badge("covr") | |
#' | |
#' | |
#' Bonus: | |
#' ==================================================================== | |
#' | |
#' Automating background continuous tests on RStudio (free command line) | |
#' -------------------------------------------------------------------- | |
#' | |
renv::install("CorradoLanera/autotestthat") # {renv} installation | |
#' | |
#' Final checks and update version | |
#' ==================================================================== | |
#' | |
usethis::use_tidy_description() | |
devtools::check_man() | |
spelling::spell_check_package() | |
spelling::update_wordlist() | |
lintr::lint_package() | |
renv::status() | |
devtools::check() | |
#' | |
#' > Update and knit the `README.Rmd` | |
#' | |
usethis::use_version("dev") | |
#' | |
#' Start to develop | |
#' ==================================================================== | |
#' | |
fs::file_create("dev/02-development.R") | |
rstudioapi::navigateToFile("dev/02-development.R") | |
#' | |
#' commit and push... | |
#' Happy packaging! | |
#' | |
#' | |
#' Optional | |
#' ==================================================================== | |
#' | |
#' Add pipe (`%>%`) support | |
#' -------------------------------------------------------------------- | |
#' | |
usethis::use_pipe() | |
#' | |
#' Add `tibble` support | |
#' -------------------------------------------------------------------- | |
#' | |
usethis::use_tibble() | |
# required if used w/ Windows machines (even for CI/CD) | |
renv::install("tidyverse/tibble", INSTALL_opts = c("--no-multiarch")) | |
#' | |
#' Add additional CI | |
#' -------------------------------------------------------------------- | |
#' | |
#' > To be added correctly setup for using {renv} | |
#' | |
#' ### AppVejor (windows) | |
#' | |
#' | |
#' ### Travis (Mac + Linux) | |
#' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' --- | |
#' title: "Setup (without `{renv}`)" | |
#' author: "Corrado Lanera" | |
#' date: "`r date()`" | |
#' output: | |
#' html_document: | |
#' toc: true | |
#' toc_float: true | |
#' keep_md: true | |
#' --- | |
#' | |
#' ```{r setup, include=FALSE} | |
#' knitr::opts_chunk$set( | |
#' echo = TRUE, | |
#' eval = FALSE | |
#' ) | |
#' ``` | |
#' | |
#' | |
#' Prerequisites | |
#' ==================================================================== | |
#' | |
# install.packages("usethis") | |
# install.packages("available") | |
available::available("pkgsetup") | |
usethis::create_package("~/Documents/cl/pkgsetup") | |
usethis::use_directory("dev", ignore = TRUE) | |
# install.packages("fs") | |
fs::file_create("dev/01-setup.R") | |
rstudioapi::navigateToFile("dev/01-setup.R") | |
#' | |
#' Copy there the whole content of this file, and keep track there | |
#' of everything you do in the setup process. You can omit | |
#' unnecessary comments and packages' installations. | |
#' | |
usethis::use_roxygen_md() | |
usethis::use_news_md() | |
#' | |
#' From now on, document everything into the `NEWS.md` | |
#' To fill the NEWS, refer to | |
#' [this](https://style.tidyverse.org/news.html) | |
#' | |
#' | |
#' Documentiation | |
#' ==================================================================== | |
#' | |
#' DESCRIPTION | |
#' -------------------------------------------------------------------- | |
#' | |
#' Update/fill the DESCRIPTION file. Remember to include in the | |
#' `Authors@R` field at least: | |
#' | |
#' - aut: author(s) | |
#' - cre: mantainer of the package (only one) | |
#' - cph: copyright holder(s) | |
#' | |
#' eg: | |
#' | |
person( | |
given = "Corrado", | |
family = "Lanera", | |
role = c("aut", "cre", "cph"), | |
email = "corrado.lanera@gmail.com", | |
comment = c(ORCID = "0000-0002-0520-7428") | |
) | |
usethis::use_gpl3_license("Corrado Lanera") | |
usethis::use_package_doc() | |
#' | |
#' README | |
#' -------------------------------------------------------------------- | |
#' | |
#' Initialize the README file (ie the package's landing page) | |
#' | |
# install.packages("rmarkdown") | |
usethis::use_readme_rmd() | |
usethis::use_logo("~/Pictures/pkgsetup_hex.png") | |
usethis::use_cran_badge() | |
usethis::use_lifecycle_badge("Maturing") | |
#' | |
#' Our package is not on CRAN, change the README accordingly: | |
#' | |
#' You can install the development version from | |
#' [GitHub](https://github.com/) with the following procedure: | |
#' | |
#' ```{r, eval = FALSE} | |
#' # install.packages("devtools") | |
#' devtools::install_github("CorradoLanera/<package_name>") | |
#' ``` | |
#' | |
#' Remove everything else that is not necessary and | |
#' | |
usethis::use_code_of_conduct() | |
usethis::use_spell_check() | |
spelling::spell_check_package() | |
spelling::update_wordlist() | |
#' | |
#' > knit the README | |
#' | |
#' | |
#' Supporting Folders | |
#' ==================================================================== | |
#' | |
#' Data raw | |
#' -------------------------------------------------------------------- | |
#' | |
#' If raw data are stored inside the main package folder call | |
usethis::use_data_raw() | |
#' | |
#' and comment the line with `usethis::use_data(...)` in it until it | |
#' will be used. | |
#' | |
#' | |
#' Other folder (eg Analyses) | |
#' -------------------------------------------------------------------- | |
#' | |
#' | |
#' usethis::use_directory("analysis", ignore = TRUE) | |
#' usethis::use_directory("output", ignore = TRUE) | |
#' | |
#' | |
#' Test suit | |
#' ==================================================================== | |
#' | |
usethis::use_testthat() | |
#' | |
#' Create a simple test to check everything works. | |
#' You can delete it AFTER you have written another test at least. | |
#' | |
#' Check it works: | |
#' | |
usethis::use_test("foo") # `test_that("foo works", expect_null(foo()))` | |
devtools::test() # see it fails!! | |
usethis::use_r("foo") # define `foo <- function() NULL` | |
devtools::test() # see it passes!! | |
#' | |
#' delete `foo` only after have included another function! | |
#' | |
#' | |
#' Quality assurance | |
#' ==================================================================== | |
#' | |
usethis::use_tidy_description() | |
# install.packages("lintr) | |
lintr::lint_package() | |
#' | |
#' and fix the spelling script that lint | |
#' have opened... ;-) | |
#' | |
if (requireNamespace("spelling", quietly = TRUE)) { | |
spelling::spell_check_test( | |
vignettes = TRUE, | |
error = FALSE, | |
skip_on_cran = TRUE | |
) | |
} | |
#' | |
#' Check again: | |
#' | |
lintr::lint_package() | |
devtools::check_man() | |
devtools::test() | |
devtools::check() | |
#' | |
#' Activate Git/GitHub | |
#' ==================================================================== | |
#' | |
usethis::use_git() | |
usethis::git_vaccinate() | |
#' | |
#' Commit everything before to continue! | |
#' | |
# remember to open and activate PuTTY | |
usethis::use_github( | |
# "<organization>", # eg, "UBESP-DCTV" | |
# private = TRUE # is a private repo? | |
) | |
#' | |
#' > NOTE: If required set the upstream using your preferred | |
#' GUI^[GitKraken: https://www.gitkraken.com/invite/fas3vkyk is free, | |
#' multiplatform (Win, Mac, Linux),and super cool :-).], or by command | |
#' line running (on the Terminal): | |
#' | |
#' git push --set-upstream origin master | |
#' | |
usethis::use_tidy_github() | |
#' | |
#' Package website documentation | |
#' ==================================================================== | |
#' | |
usethis::use_github_action("pkgdown") | |
usethis::use_github_actions_badge("pkgdown") | |
#' | |
#' Bonus: | |
#' | |
remotes::install_github("GuangchuangYu/badger") | |
badger::badge_custom("WEBsite", "click-me", "orange", "http://corradolanera.github.io/pkgsetup/") | |
#' | |
#' And add it between title and logo in the README and knit it. | |
#' | |
#' | |
#' Continuous Integration | |
#' ==================================================================== | |
#' | |
#' Lint (static code-quality checks) | |
#' -------------------------------------------------------------------- | |
#' | |
usethis::use_github_action("lint") | |
usethis::use_github_actions_badge("lint") | |
#' R-CDM-check and coverage | |
#' -------------------------------------------------------------------- | |
usethis::use_github_actions_badge("check-full", | |
save_as = "R-CMD-check.yaml" | |
) | |
usethis::use_github_actions_badge("R-CMD-check") | |
usethis::use_github_action("test-coverage", | |
save_as = "covr.yaml" | |
) | |
usethis::use_github_actions_badge("covr") | |
#' | |
#' Bonus: | |
#' ==================================================================== | |
#' | |
#' Automating background continuous tests on RStudio (free command line) | |
#' -------------------------------------------------------------------- | |
#' | |
remotes::install_github("CorradoLanera/autotestthat") | |
#' | |
#' Final checks and update version | |
#' ==================================================================== | |
#' | |
usethis::use_tidy_description() | |
devtools::check_man() | |
spelling::spell_check_package() | |
spelling::update_wordlist() | |
lintr::lint_package() | |
renv::status() | |
devtools::check() | |
#' | |
#' > Update and knit the `README.Rmd` | |
#' | |
usethis::use_version("dev") | |
#' | |
#' Start to develop | |
#' ==================================================================== | |
#' | |
fs::file_create("dev/02-development.R") | |
rstudioapi::navigateToFile("dev/02-development.R") | |
#' | |
#' commit and push... | |
#' Happy packaging! | |
#' | |
#' | |
#' Optional | |
#' ==================================================================== | |
#' | |
#' Add pipe (`%>%`) support | |
#' -------------------------------------------------------------------- | |
#' | |
usethis::use_pipe() | |
#' | |
#' Add `tibble` support | |
#' -------------------------------------------------------------------- | |
#' | |
usethis::use_tibble() | |
#' | |
#' Add additional CI | |
#' -------------------------------------------------------------------- | |
#' | |
#' ### AppVejor (windows) | |
#' | |
usethis::use_appveyor() | |
#' | |
#' Add the following lines to `appveyor.yml` right indented below | |
#' "environment": | |
#' | |
#' global: | |
#' WARNINGS_ARE_ERRORS: 1 | |
#' | |
#' matrix: | |
#' - R_VERSION: devel | |
#' GCC_PATH: mingw_32 | |
#' | |
#' - R_VERSION: devel | |
#' R_ARCH: x64 | |
#' GCC_PATH: mingw_64 | |
#' | |
#' - R_VERSION: release | |
#' R_ARCH: x64 | |
#' | |
#' - R_VERSION: stable | |
#' | |
#' - R_VERSION: patched | |
#' | |
#' - R_VERSION: oldrel | |
#' CRAN: http://cran.rstudio.com | |
#' | |
#' - R_VERSION: 3.4 | |
#' CRAN: http://cran.rstudio.com | |
#' | |
#' - R_VERSION: 3.3 | |
#' CRAN: http://cran.rstudio.com | |
#' | |
#' - R_VERSION: 3.2 | |
#' CRAN: http://cran.rstudio.com | |
#' | |
#' | |
#' ### Travis (Mac + Linux) | |
#' | |
usethis::use_tidy_ci() | |
#' | |
#' # R for travis: see documentation at https://docs.travis-ci.com/user/languages/r | |
#' | |
#' language: R | |
#' cache: packages | |
#' | |
#' matrix: | |
#' include: | |
#' - r: devel | |
#' os: linux | |
#' before_install: | |
#' - sudo apt-get install libgsl0-dev libxml2-dev openjdk-7-* | |
#' | |
#' - r: devel | |
#' os: osx | |
#' | |
#' - r: release | |
#' os: osx | |
#' | |
#' - r: release | |
#' os: linux | |
#' before_install: | |
#' - sudo apt-get install libgsl0-dev libxml2-dev openjdk-7-* | |
#' after_success: | |
#' - Rscript -e 'covr::codecov()' | |
#' | |
#' - r: oldrel | |
#' os: osx | |
#' | |
#' - r: oldrel | |
#' before_install: | |
#' - sudo apt-get install libgsl0-dev libxml2-dev openjdk-7-* | |
#' | |
#' # system requirements for rJava and topicmodels packages | |
#' # http://stackoverflow.com/questions/16438073/unable-to-install-rjava-in-r-3-0-in-ubuntu-13-04?rq=1 | |
#' # http://stackoverflow.com/questions/25759007/error-installing-topicmodels-package-non-zero-exit-status-ubuntu | |
#' | |
#' | |
#' r_packages: devtools | |
#' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' --- | |
#' title: "Development" | |
#' author: "Corrado Lanera" | |
#' date: "`r date()`" | |
#' output: | |
#' html_document: | |
#' toc: true | |
#' toc_float: true | |
#' keep_md: true | |
#' --- | |
#' | |
#' ```{r setup, include=FALSE} | |
#' knitr::opts_chunk$set( | |
#' echo = TRUE, | |
#' eval = FALSE | |
#' ) | |
#' ``` | |
#' | |
#' | |
#' Development history | |
#' ==================================================================== | |
#' List here all the command executed during development of the package | |
#' | |
# renv::install("CorradoLanera/autotestthat") | |
autotestthat::auto_test_package_job() # before every start! | |
# usethis::use_test("<function_name>") | |
# usethis::use_r("<function_name>") | |
# ... | |
# ... | |
# ... | |
#' | |
#' Check cycles | |
#' ==================================================================== | |
#' | |
#' Before pushes | |
#' -------------------------------------------------------------------- | |
#' | |
usethis::use_tidy_description() | |
spelling::spell_check_package() | |
spelling::update_wordlist() | |
#' | |
#' Before pull requests | |
#' -------------------------------------------------------------------- | |
#' | |
lintr::lint_package() | |
goodpractice::gp() | |
# The following calls run into your (interactive) session | |
# Use the corresponding RStudio button under the "Build" | |
# tab to execute them mantaining free your console | |
# (and running them in a non-interactive session) | |
devtools::test() | |
devtools::check() | |
#' | |
#' > Update the `NEWS.md` file | |
#' | |
usethis::use_version() | |
#' | |
#' CRAN submission's cycle | |
#' ==================================================================== | |
#' | |
# usethis::use_release_issue() # at the first start only | |
devtools::build_readme() | |
devtools::check(remote = TRUE, manual = TRUE) | |
devtools::check_win_devel() | |
cran_prep <- rhub::check_for_cran() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment