Helper code for creating library.

Erik.Leppo@tetratech.com 2017-09-26

Package

Package related code.

Vignette

  1. Need to run code below to add data to "vignette" directory so it will work.

  1. Knit the vignette in the vignette folder to ensure all of the code works and is properly displayed. This can take 2 to 5 min.

  2. Use the code below to "build" the vignette and then use it in the pkg.

# generate Vignette
library(BioMonTools)
library(devtools)
devtools::build_vignettes()

# create vignette folder and default file
#devtools::use_vignette("BCGcalc_Vignette")

devtools::build() more useful and build_vignettes()

Create Package

Use the code below to create the package. Assumes you are in the development package directory

# Library Name
# Remove all files in "Results" folder
# Triggered here so can run different files
path_results <- file.path("inst", "shiny-examples", "BioMonTools", "results")
fn_results <- list.files(path_results
                         , full.names = TRUE
                         , include.dirs = TRUE
                         , recursive = TRUE)
unlink(fn_results, recursive = TRUE) # to include dir use unlink instead of file.remove
# Copy file to ensure directory not empty
path_shiny <- file.path("inst", "shiny-examples", "BioMonTools")
fn_copy    <- "remove.txt"
path_from  <- file.path(path_shiny, "data", fn_copy)
path_to    <- file.path(path_shiny, "results", fn_copy)
file.copy(path_from, path_to)
# NEWS
# Render then Copy NEWS so picked up in help
rmarkdown::render("NEWS.rmd", "all")
file.copy("NEWS.md", "NEWS", overwrite = TRUE)
file.remove("NEWS.html")
#file.remove("NEWS.md")
#
myPkg <- "BioMonTools"
# Load Library
library(devtools)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create Package
# create(myPkg)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Document, Install, and Reload Library
## Generate Documentation
#setwd(paste0("./",myPkg))  # will fail as already in this directory
devtools::document()
## Install New Package (locally)
#setwd("..") # return to root directory first
# change to build_vignettes = TRUE, use FALSE for speed
devtools::install(".", quick = FALSE, reload = TRUE, build_vignettes = FALSE) 

# blank line above allows code to continue when install asks about package updates 
## Reload library
library(myPkg, character.only = TRUE)
# change wd back to package
#setwd(paste0("./",myPkg))
# Help page
help(package=(myPkg))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

After creating the package reload it after restarting R within RStudio (Ctrl + Shift + F10).

# Restart R within RStudio:  Ctrl + Shift + F10
myPkg <- "BioMonTools"
library(myPkg, character.only = TRUE)
help(package=(myPkg))
library(testthat)
#
citation(myPkg)

Package Down

Create pkgdown website.

pkgdown::build_site()

code coverage

#library(covr)
covr::report()

other good stuff

g <- goodpractice::gp()

s_check <- devtools::spell_check()

Run All Examples

Run all examples at once rather than going to each function individually.

Add "/_test" to gitignore

(Needs tweaking to get it working properly)

#pkg <- "BioMonTools" # from root directory of pkg
#setwd("./_test")
devtools::run_examples()

Data Documentation

Use promptData to get data.r listing for columns. Short cuts creating documentation.

promptData(data_diatom_mmi_qc)

Check

# Check for errors (or press Cmd + Shift + E in RStudio)
#http://r-pkgs.had.co.nz/check.html
devtools::check()

If use RStudio, Build - Check Package, the output is easier to read.

Test

Need to use testthat but not to that stage yet.

RStudio, Build - Test Package.

CI, Badges, Code Coverage, etc

badger package creates the text for pkg readme file.

Also https://shields.io/
https://github.com/badges/shields Can make some badges dynamic, static ones below

issues

GitHub issues

release

GitHub release

downloads

Github all releases

pkg_GH <- "leppott/BioMonTools"
pkg_lic <- "MIT"

# GitHub Action, CI
usethis::use_github_actions()
usethis::use_github_actions_badge()
# badger::badge_github_actions(pkg_GH)

# code coverage
usethis::use_github_action("test-coverage")

# GitHub Action, pkgdown
usethis::use_github_action("pkgdown")

# Badges
# lifecycle
badger::badge_lifecycle("stable", "green") # stable/green, maturing/dormant/blue
# maintained
# license
badger::badge_license(pkg_lic)
# code quality
badger::badge_codefactor(pkg_GH)
# code testing coverage
badger::badge_codecov(pkg_GH)
# cmd check
badger::badge_github_actions(pkg_GH)

Other Stuff

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Upload to Github via GitHub Desktop utility
# 0. download from web via "clone or download" via "Open in Desktop" (GitHub Desktop) if not already in GitHub Desktop
# 1. Make changes in download/clone folder. (done above)
# 3. Open GH Desktop commit changes then sync.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# install from GitHub (via devtools)
devtools::install_github(paste0("leppott/",myLibrary))
#



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# remove installed packages (if needed for troubleshooting)
search() # find
#detach(3) # remove by number
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# to build package
#https://thepoliticalmethodologist.com/2014/08/14/building-and-maintaining-r-packages-with-devtools-and-roxygen2/
# To build the package as a compressed file in your working directory, run build(current.code, path=getwd()).

# to save internal data for examples
# example
#http://r-pkgs.had.co.nz/data.html#data-sysdata
# have to be at root directory (above package)
#devtools::use_data(NV.predictors,NV.bugs,pkg="MMIcalcNV",internal=TRUE,overwrite=TRUE)
## verify with data()

# To save RMD files
# http://stackoverflow.com/questions/30377213/how-to-include-rmarkdown-file-in-r-package
# /pkg/inst/rmd/
# system.file("rmd/file.Rmd", package="packagename")
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/
# Create Package
# create(myLibrary)


leppott/BioMonTools documentation built on March 1, 2025, 7:18 a.m.