(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 <[email protected]> [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 protected]") 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.