knitr::opts_chunk$set(echo = TRUE) ### recommended libraries library(devtools) library(tidyverse) library(fs)
This is just to be run once, behind the scenes.
#create_package(path = 'C:/Projects/Github/foofactors')
This created a number of files:
.Rbuildignore
-- lists files needed but not to be included in final built package..Rproj.user
-- internal RStudio directory..gitignore
-- ignores behind-the-scenes files from R and RStudio when pushing to Git.DESCRIPTION
-- Metadata fileNAMESPACE
-- declares functions to be exported and functions needed to be imported to function.R/
-- contains .R files with function definitions.foofactors.Rproj
-- RStudio project file.Similar to package creation, this initializes Git relationship and creates an initial commit. Can include commit message.
#use_git()
## 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:
use_r('fbind')
load_all()
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:
Non-standard license specification
Undocumented code objects: 'fbind'
And 1 note:
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:
use_test('fbind')
Then run the test (and all other test files in the package) using test()
or check()
.
library(testthat) load_all() # load tests test()
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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.