knitr::opts_chunk$set(echo = TRUE)

### recommended libraries
library(devtools)
library(tidyverse)
library(fs)

Create package directory

This is just to be run once, behind the scenes.

#create_package(path = 'C:/Projects/Github/foofactors')

This created a number of files:

Initializing Git

Similar to package creation, this initializes Git relationship and creates an initial commit. Can include commit message.

#use_git()

Writing first functions

## See how factors play together
(a <- factor(c('character', 'hits', 'your', 'eyeballs')))
(b <- factor(c('but', 'integer', 'where it', 'counts')))
c(a, b)

## concatenate those properly!
factor(c(as.character(a), as.character(b)))

## create the function to do this
#fbind <- function(a, b) {
#  factor(c(as.character(a), as.character(b)))
#}

fbind was written to fbind.R in the R/ folder. We can test it using:

The function does not show up in the environment, however, as this simulates loading a library (e.g., library(foofactors)).

load_all()

Use check() to verify everything is in working order.

This outputs 2 warnings:

  1. Non-standard license specification
  2. Undocumented code objects: 'fbind'

And 1 note:

  1. Non-standard file/directory found at top level: 'foofactors.Rmd'

We can add a standard license via use_mit_license('Todd Ellis') to solve 1.

The latter requires creating a .Rd file with markup language offering details on the function, but we can use the Roxygen2 library to do this int he .R file itself.

See top of fbind.R for an example. Once written, run with document().

Once that's done, run check() again, and then install().

Reset R or empty the workspace and then we can use the library as it is in R:

library(foofactors)

a <- factor(c("character", "hits", "your", "eyeballs"))
b <- factor(c("but", "integer", "where it", "counts"))

fbind(a, b)

It works! We can now create unit tests (not really sure what this mean...) by using use_testthat().

This creates empty test files for us to fill.

Create a test file using:

Then run the test (and all other test files in the package) using test() or check().

library(testthat)
load_all() # load tests
test()

Using other packages

Use use_package() to make calls to other packages. All used packages need to be pulled, including base tools like the stats or utils libraries.

Our next function uses part of the forcats library.

#use_package('forcats')
#use_r('fcount')

This throws an import reference to the description file, and creates a new .R file for the function fcount().

load_all()
fcount(iris$Species)

This stage was uploaded to our Github repo, and we can now edit a README.Rmd file using use_readme_rmd().

Once finishing edits on the .Rmd file, run rmarkdown::render('README.Rmd') to render the .md file for GitHub. This needs to be run anytime the .Rmd file is edited.

Note: Since this tutorial started, updates to the tidyverse using the vctrs package led to errors here in package dependencies and I had to manually delete the past installation.



toddellis/foofactors documentation built on March 2, 2020, 12:20 a.m.