My First R Package (Part 3)
After refactoring my programming so that it was only about 10 lines of code, using 12 functions I wrote an loaded in via the source command, I went through all the steps in Part 1 of this blog post and Part 2 of this blog post to set up the R package infrastructure using testthis in RStudio. Then things started humming along with the rest of the setup:
> use_mit_license("Nicole Radziwill") ✔ Setting active project to 'D:/R/easyMTS' ✔ Setting License field in DESCRIPTION to 'MIT + file LICENSE' ✔ Writing 'LICENSE.md' ✔ Adding '^LICENSE\\.md$' to '.Rbuildignore' ✔ Writing 'LICENSE' > use_testthat() ✔ Adding 'testthat' to Suggests field in DESCRIPTION ✔ Creating 'tests/testthat/' ✔ Writing 'tests/testthat.R' ● Call `use_test()` to initialize a basic test file and open it for editing. > use_vignette("easyMTS") ✔ Adding 'knitr' to Suggests field in DESCRIPTION ✔ Setting VignetteBuilder field in DESCRIPTION to 'knitr' ✔ Adding 'inst/doc' to '.gitignore' ✔ Creating 'vignettes/' ✔ Adding '*.html', '*.R' to 'vignettes/.gitignore' ✔ Adding 'rmarkdown' to Suggests field in DESCRIPTION ✔ Writing 'vignettes/easyMTS.Rmd' ● Modify 'vignettes/easyMTS.Rmd' > use_citation() ✔ Creating 'inst/' ✔ Writing 'inst/CITATION' ● Modify 'inst/CITATION'
Add Your Dependencies
> use_package("ggplot2") ✔ Adding 'ggplot2' to Imports field in DESCRIPTION ● Refer to functions with `ggplot2::fun()` > use_package("dplyr") ✔ Adding 'dplyr' to Imports field in DESCRIPTION ● Refer to functions with `dplyr::fun()` > use_package("magrittr") ✔ Adding 'magrittr' to Imports field in DESCRIPTION ● Refer to functions with `magrittr::fun()` > use_package("tidyr") ✔ Adding 'tidyr' to Imports field in DESCRIPTION ● Refer to functions with `tidyr::fun()` > use_package("MASS") ✔ Adding 'MASS' to Imports field in DESCRIPTION ● Refer to functions with `MASS::fun()` > use_package("qualityTools") ✔ Adding 'qualityTools' to Imports field in DESCRIPTION ● Refer to functions with `qualityTools::fun()` > use_package("highcharter") Registered S3 method overwritten by 'xts': method from as.zoo.xts zoo Registered S3 method overwritten by 'quantmod': method from as.zoo.data.frame zoo ✔ Adding 'highcharter' to Imports field in DESCRIPTION ● Refer to functions with `highcharter::fun()` > use_package("cowplot") ✔ Adding 'cowplot' to Imports field in DESCRIPTION ● Refer to functions with `cowplot::fun()`
Adding Data to the Package
I want to include two files, one data frame containing 50 observations of a healthy group with 5 predictors each, and another data frame containing 15 observations from an abnormal or unhealthy group (also with 5 predictors). I made sure the two CSV files I wanted to add to the package were in my working directory first by using dir().
> use_data_raw() ✔ Creating 'data-raw/' ✔ Adding '^data-raw$' to '.Rbuildignore' ✔ Writing 'data-raw/DATASET.R' ● Modify 'data-raw/DATASET.R' ● Finish the data preparation script in 'data-raw/DATASET.R' ● Use `usethis::use_data()` to add prepared data to package > mtsdata1 <- read.csv("MTS-Abnormal.csv") %>% mutate(abnormal=1) > usethis::use_data(mtsdata1) ✔ Creating 'data/' ✔ Saving 'mtsdata1' to 'data/mtsdata1.rda' > mtsdata2 <- read.csv("MTS-Normal.csv") %>% mutate(normal=1) > usethis::use_data(mtsdata2) ✔ Saving 'mtsdata2' to 'data/mtsdata2.rda'
Magically, this added my two files (in .rds format) into my /data directory. (Now, though, I don’t know why the /data-raw directory is there… maybe we’ll figure that out later.) I decided it was time to commit these to my repository again:
Following the instruction above, I re-knit the README.Rmd and then it was possible to commit everything to Github again. At which point I ended up in a fistfight with git, again saved only by my software engineer partner who uses Github all the time:
I think it should be working. The next test will be if anyone can install this from github using devtools. Let me know if it works for you… it works for me locally, but you know how that goes. The next post will show you how to use it 🙂
Pingback: My First R Package (Part 3) – Technology Revolution