Write an R Package from Scratch with Github

Writing an R package is simple. Writing an R package via Github is simple and smart. Github adds all the traditional benefits of version control, in addition to showing off your work and providing and facilitating publication of your package. This tutorial was inspired by a blog post from the beautiful Hillary Parker last year. I used her tut myself, but trying to integrate it with Github leads to some headaches and I felt there were a couple of other small additions to be made.

 This has been sitting in my Evernote for some time, so I figured it was about time to upload to my own highly neglected blog, however as a caveate I’ll say that I still need to append more sample code and such, so watch for updates.

 
Step 0: Load the necessary packages  
if (!require(“pacman”)) install.packages(“pacman”) # Don’t use pacman yet? Get ready to fall in love
pacman::p_load("devtools", "roxygen2")

 

Step 1: Create your package directory
 
* Create a new repo on Github with the name of your package 
* Create a new project in RStudio from the Github repo
* Open a .R file to begin writing code
* Open the automatically generated README.md file and edit appropriately
 
Step 2: Add functions
 
* Enter your functions and save the file (i.e. dog_function.R) 
* You can move this to the R folder once it has been automatically created in Step 3, or feel free to create the folder before saving the .R file (remember not to overwrite it in the next step)
 

Step 3: Add minimal documentation

* Utilize roxygen2 by typing create(“packagename”)

* Copy the files in this newly created folder — except the .Rproj and .gitignore files — to the top level folder you cloned from Github
* Delete the folder created by roxygen2 
* Edit the files to reflect the details of your package, such as its license and author
 

Step 4: Add optional, but recommended example and docs

 

4a. data
 
* dir.create(“data”) # Example .RData goes here (optional, but strongly recommended)
* include a file called datalist to list the data in this folder, for example:
 
4b. vignettes
 
* dir.create(“vignettes”) # From the top level folder that you created on Github
* Add a .pdf, .Rnw vignette files here
 
 
4c. man
 
* dir.create(“man”) # From the top level folder that you created on Github
* Add .Rd manual files here
 
Step 5: Process your documentation
 
setwd("./dogs")
document()
 
Step 6: Install your package!
 
setwd("..")
install("dogs")