Helper code for creating library.

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

Copied from ContDataQC so some parts are not needed. 2017-12-04.

Package

Package related code.

Vignette

  1. Need to run code below to add data to "vignette" directory so it will work.
# 1. change wd to vignettes
setwd(file.path("C:","Users","Erik.Leppo","OneDrive - Tetra Tech, Inc"
                ,"MyDocs_OneDrive","GitHub","ContDataQC","vignettes"))
#
# 2. Add data for vignette examples
# Parameters
Selection.Operation <- c("GetGageData","QCRaw", "Aggregate", "SummaryStats")
Selection.Type      <- c("Air","Water","AW","Gage","AWG","AG","WG")
Selection.SUB <- c("Data1_RAW","Data2_QC","Data3_Aggregated","Data4_Stats")
myDir.BASE <- getwd()
#
# Create data directories
myDir.create <- paste0("./",Selection.SUB[1])
  ifelse(dir.exists(myDir.create)==FALSE,dir.create(myDir.create),"Directory already exists")
myDir.create <- paste0("./",Selection.SUB[2])
  ifelse(dir.exists(myDir.create)==FALSE,dir.create(myDir.create),"Directory already exists")
myDir.create <- paste0("./",Selection.SUB[3])
  ifelse(dir.exists(myDir.create)==FALSE,dir.create(myDir.create),"Directory already exists")
myDir.create <- paste0("./",Selection.SUB[4])
  ifelse(dir.exists(myDir.create)==FALSE,dir.create(myDir.create),"Directory already exists")
#
# Save example data (assumes directory ./Data1_RAW/ exists)
myData <- data_raw_test2_AW_20130426_20130725
  write.csv(myData,paste0("./",Selection.SUB[1],"/test2_AW_20130426_20130725.csv"))
myData <- data_raw_test2_AW_20130725_20131015
  write.csv(myData,paste0("./",Selection.SUB[1],"/test2_AW_20130725_20131015.csv"))
myData <- data_raw_test2_AW_20140901_20140930
  write.csv(myData,paste0("./",Selection.SUB[1],"/test2_AW_20140901_20140930.csv"))
myData <- data_raw_test4_AW_20160418_20160726
  write.csv(myData,paste0("./",Selection.SUB[1],"/test4_AW_20160418_20160726.csv"))
myFile <- "config.TZ.Central.R"
  file.copy(file.path(path.package("ContDataQC"),"extdata",myFile)
            ,file.path(getwd(),Selection.SUB[1],myFile))
  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(baytrends)
library(devtools)
devtools::build_vignettes()

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

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

Create Package PDF reference manual

http://www.dominikschauer.com/how-to-create-an-r-package-including-documentation-vignettes-and-a-manual/

myLibrary <- "baytrends"
setwd(paste0("./", myLibrary))
system("R CMD Rd2pdf . --title=Package baytrends --output=./manual.pdf --force --no-clean --internals")

Create Package

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

Delete Namespace, Collate section of DESCRIPTION, and \man\*

Need to remove end of line after Collate. Need first entry on the same line as "Collate:".

# Library Name
myLibrary <- "baytrends"
# Load Library
#library(devtools)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# NEWS
#setwd(file.path(".", myLibrary))

# 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")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# # Library Name
# myLibrary <- "baytrends"
# # Load Library
# library(devtools)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create Package
# create(myLibrary)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Document, Install, and Reload Library
## Generate Documentation
#setwd(paste0("./", myLibrary))
devtools::document()
## Install New Package (locally)
setwd("..") # return to root directory first
devtools::install(myLibrary, quick=FALSE, reload=TRUE, build_vignettes = TRUE)

# blank line above ensures no "updates" to packages.
# ## Reload library
# library(myLibrary, character.only = TRUE)
# change wd back to package
setwd(file.path(".", myLibrary))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

# Restart R within RStudio:  Ctrl + Shift + F10
library("baytrends")
help(package="baytrends")

Use pkgdown to build website. Will need to rebuild the package to include the new material.

Can use GitHub Actions to build instead of doing manually.

library(pkgdown)
pkgdown::build_site()

Downstream dependancies

# Downstream dependencies
devtools::revdep("baytrends")

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.

Check failures (single vector so not very useful) but puts it all in one place. Have to run after using Check.

library(devtools)
dir.check <- "C:\\Users\\Erik.Leppo\\OneDrive - Tetra Tech, Inc\\MyDocs_OneDrive\\GitHub\\baytrends.Rcheck"
#
check_failures(dir.check)

check on Linux

https://r-hub.github.io/rhub/index.html https://r-hub.github.io/rhub/articles/rhub.html

library(rhub)
# validate email
validate_email()
# run check (interactive for platform)
check()

# recommended prior to CRAN submission
check_for_cran() # win and linux

check_on_linux()

Run all Examples.

# point to directory
devtools::run_examples(pkg = ".")

Alternate is testthat::test_examples() but is redundant with R CMD check.

code coverage

#library(covr)
covr::report()

Check functions and such

Index

Go straight to index.

help(package="baytrends")

Open Vignette

browseVignettes("baytrends")

Then click on "HTML" to open.

Run examples

example(analysisOrganizeData)

Test

Run all tests.

devtools::test()

Including Data

All data should have already been created.

Testings

https://testthat.r-lib.org/

https://r-pkgs.org/tests.html

Set up folders with devtools::test(). This is also the command to run tests.

Create test file usethis::use_test("name")

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 <- "tetratech/baytrends"
pkg_lic <- "GPL-3"

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

Test

Need to use testthat but not to that stage yet.

RStudio, Build - Test Package.

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)

Test Stats only

# Parameters
Selection.Operation <- c("GetGageData","QCRaw", "Aggregate", "SummaryStats")
Selection.Type      <- c("Air","Water","AW","Gage","AWG","AG","WG")
Selection.SUB <- c("Data1_RAW","Data2_QC","Data3_Aggregated","Data4_Stats")
myDir.BASE <- getwd()
#
# Summary Stats, File
## Have to use "file" version.  
## Base version builds file name and expects "DATA" prefix

# Gage File + Other File
myData.Operation <- "SummaryStats" #Selection.Operation[4]
myFile <- c("Data4Stats_01187300_Gage_20130101_20141231.csv"
            ,"Data4Stats_test2_Aw_20130101_20141231.csv")
myDir.import <- file.path(".","data-raw")
myDir.export <- file.path(".","Data4_Stats")

#Leave off myReport.format and get default (docx).
ContDataQC(myData.Operation
           , fun.myDir.import=myDir.import
           , fun.myDir.export=myDir.export
           , fun.myFile=myFile)

SiteID, Date, Time, Date.Time, Parameters, Flag.Parameters

Only one flag per parameter. Do not need individual test flags.

"F" (fail) flags are changed to NA so they are dropped from the statistics that calculated.

Mocked up 2 files.



leppott/baytrends033 documentation built on Feb. 17, 2024, 9:27 a.m.