A new R package in development. Please cite if you use it. Available from https://github.com/NicoleRadziwill/easyMTS
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 🙂
In Part 1, I set up RStudio with usethis, and created my first Minimum Viable R Package (MVRP?) which was then pushed to Github to create a new repository.
I added a README:
> use_readme_rmd() ✔ Writing 'README.Rmd' ✔ Adding '^README\\.Rmd$' to '.Rbuildignore' ● Modify 'README.Rmd' ✔ Writing '.git/hooks/pre-commit'
Things were moving along just fine, until I got this unkind message (what do you mean NOT an R package???!! What have I been doing the past hour?)
> use_testthat() Error: `use_testthat()` is designed to work with packages. Project 'easyMTS' is not an R package. > use_mit_license("Nicole Radziwill") ✔ Setting active project to 'D:/R/easyMTS' Error: `use_mit_license()` is designed to work with packages. Project 'easyMTS' is not an R package.
Making easyMTS a Real Package
I sent out a tweet hoping to find some guidance, because Stack Overflow and Google and the RStudio community were coming up blank. As soon as I did, I discovered this button in RStudio:
The first time I ran it, it complained that I needed Rtools, but that Rtools didn’t exist for version 3.6.1. I decided to try finding and installing Rtools anyway because what could I possibly lose. I went to my favorite CRAN repository and found a link for Rtools just under the link for the base install:
I’m on Windows 10, so this downloaded an .exe which I quickly right-clicked on to run… the installer did its thing, and I clicked “Finish”, assuming that all was well. Then I went back into RStudio and tried to do Build -> Clean and Rebuild… and here’s what happened:
IT WORKED!! (I think!!!)
It created a package (top right) and then loaded it into my RStudio session (bottom left)! It loaded the package name into the package console (bottom right)!
I feel like this is a huge accomplishment for now, so I’m going to move to Part 3 of my blog post. We’ll figure out how to close the gaps that I’ve invariably introduced by veering off-tutorial.
(What does this new package do? Find out here.)
I have had package-o-phobia for years, and have skillfully resisted learning how to build a new R package. However, I do have a huge collection of scripts on my hard drive with functions in them, and I keep a bunch of useful functions up on Github so anyone who wants can source and use them. I source them myself! So, really, there’s no reason to package them up and (god forbid) submit them to CRAN. I’m doing fine without packages!
Reality check: NO. As I’ve been told by so many people, if you have functions you use a lot, you should write a package. You don’t even have to think about a package as something you write so that other people can use. It is perfectly fine to write a package for an audience of one — YOU.
But I kept making excuses for myself until very recently, when I couldn’t find a package to do something I needed to do, and all the other packages were either not getting the same answers as in book examples OR they were too difficult to use. It was time.
So armed with moral support and some exciting code, I began the journey of a thousand miles with the first step, guided by Tomas Westlake and Emil Hvitfeldt and of course Hadley. I already had some of the packages I needed, but did not have the most magical one of all, usethis:
install.packages("usethis") library(usethis) library(roxygen2) library(devtools)
Finding a Package Name
First, I checked to see if the package name I wanted was available. It was not available on CRAN, which was sad:
> available("MTS") Urban Dictionary can contain potentially offensive results, should they be included? [Y]es / [N]o: 1: Y -- MTS ------------------------------------------------------------------------- Name valid: ✔ Available on CRAN: ✖ Available on Bioconductor: ✔ Available on GitHub: ✖ Abbreviations: http://www.abbreviations.com/MTS Wikipedia: https://en.wikipedia.org/wiki/MTS Wiktionary: https://en.wiktionary.org/wiki/MTS
My second package name was available though, and I think it’s even better. I’ve written code to easily create and evaluate diagnostic algorithms using the Mahalanobis-Taguchi System (MTS), so my target package name is easyMTS:
> available("easyMTS") -- easyMTS ------------------------------------------------------------ Name valid: ✔ Available on CRAN: ✔ Available on Bioconductor: ✔ Available on GitHub: ✔ Abbreviations: http://www.abbreviations.com/easy Wikipedia: https://en.wikipedia.org/wiki/easy Wiktionary: https://en.wiktionary.org/wiki/easy Sentiment:+++
Create Minimum Viable Package
Next, I set up the directory structure locally. Another RStudio session started up on its own; I’m hoping this is OK.
> create_package("D:/R/easyMTS") ✔ Creating 'D:/R/easyMTS/' ✔ Setting active project to 'D:/R/easyMTS' ✔ Creating 'R/' ✔ Writing 'DESCRIPTION' Package: easyMTS Title: What the Package Does (One Line, Title Case) Version: 0.0.0.9000 Authors@R (parsed): * First Last <firstname.lastname@example.org> [aut, cre] (<https://orcid.org/YOUR-ORCID-ID>) Description: What the package does (one paragraph). License: What license it uses Encoding: UTF-8 LazyData: true ✔ Writing 'NAMESPACE' ✔ Writing 'easyMTS.Rproj' ✔ Adding '.Rproj.user' to '.gitignore' ✔ Adding '^easyMTS\\.Rproj$', '^\\.Rproj\\.user$' to '.Rbuildignore' ✔ Opening 'D:/R/easyMTS/' in new RStudio session ✔ Setting active project to '<no active project>'
Syncing with Github
use_git_config(user.name = "nicoleradziwill", user.email = "email@example.com") browse_github_token()
This took me to a page on Github where I entered my password, and then had to go down to the bottom of the page to click on the green button that said “Generate Token.” They said I would never be able to see it again, so I gmailed it to myself for easy searchability. Next, I put this token where it is supposed to be locally:
A blank file popped up in RStudio, and I added this line, then saved the file to its default location (not my real token):
Then I had to restart R and confirm it worked:
This revealed my token! I must have done the Github setup right. Finally I could proceed with the rest of the git setup:
> use_github() ✔ Setting active project to 'D:/R/easyMTS' Error: Cannot detect that project is already a Git repository. Do you need to run `use_git()`? > use_git() ✔ Initialising Git repo ✔ Adding '.Rhistory', '.RData' to '.gitignore' There are 5 uncommitted files: * '.gitignore' * '.Rbuildignore' * 'DESCRIPTION' * 'easyMTS.Rproj' * 'NAMESPACE' Is it ok to commit them? 1: No 2: Yeah 3: Not now Selection: use_github() Enter an item from the menu, or 0 to exit Selection: 2 ✔ Adding files ✔ Commit with message 'Initial commit' ● A restart of RStudio is required to activate the Git pane Restart now? 1: No way 2: For sure 3: Nope Selection: 2
When I tried to commit to Github, it was asking me if the description was OK, but it was NOT. Every time I said no, it kicked me out. Turns out it wanted me to go directly into the DESCRIPTION file and edit it, so I did. I used Notepad because this was crashing RStudio. But this caused a new problem:
Error: Uncommited changes. Please commit to git before continuing.
This is the part of the exercise where it’s great to be living with a software engineer who uses git and Github all the time. He pointed me to a tiny little tab that said “Terminal” in the bottom left corner of RStudio, just to the right of “Console”. He told me to type this, which unstuck me:
THEN, when I went back to the Console, it all worked:
> use_git() > use_github() ✔ Checking that current branch is 'master' Which git protocol to use? (enter 0 to exit) 1: ssh <-- presumes that you have set up ssh keys 2: https <-- choose this if you don't have ssh keys (or don't know if you do) Selection: 2 ● Tip: To suppress this menu in future, put `options(usethis.protocol = "https")` in your script or in a user- or project-level startup file, '.Rprofile'. Call `usethis::edit_r_profile()` to open it for editing. ● Check title and description Name: easyMTS Description: Are title and description ok? 1: Yes 2: Negative 3: No Selection: 1 ✔ Creating GitHub repository ✔ Setting remote 'origin' to 'https://github.com/NicoleRadziwill/easyMTS.git' ✔ Pushing 'master' branch to GitHub and setting remote tracking branch ✔ Opening URL 'https://github.com/NicoleRadziwill/easyMTS'
This post is getting long, so I’ll split it into parts. See you in Part 2.
This is an updated version of my 2015 (Base R) blog post using tidyverse. Original Rmd is available on request.