In the last module, you took a look at the meangirls
package by Regina George.
Now let’s help Regina make her package even better.
The usethis
package will make this process easy and foolproof. Make sure you have it installed now.
The first thing you need to do when you want to contribute to someone else’s package is to fork the package.
Recall that a fork is very different from a clone. A clone simply copy-pastes the repository, while a fork retains its link to the original repository.
Now, we could go through the tedious process of forking the repository, then all the steps after that to get it open in your local computer’s RStudio.
Instead, let’s do all these steps in one with usethis
. Open up a new RStudio session, load the usethis
package, and then run:
A lot of output will print out.
Here’s what this does:
meangirls
repo, owned by Cal-Poly-Advanced-R
on GitHub, into your GitHub account.Cal-Poly-Advanced-R
meangirls
R project.It is also possible to do this steps without create_from_github
, if you prefer or if it is not playing nice with your machine.
.Rproj
.Take a screenshot of the console output from create_from_github
OR
Take a screenshot of your RStudio window with the package project open.
We are now ready to get our hands dirty with this package.
First, build the package.
You can do this by typing Cmd/Ctrl-Shift-B
, or by clicking in the “Build” pane:
Open up README.Rmd and knit it.
Scroll to the very bottom. Do you see a problem with the output?
It’s time to fix the give_candygrams()
function so that it includes extra messages.
But first, we need to make a branch of our repo, so that when we change the code, it is carefully tracked separately.
In your console, type:
This will switch you to a new branch.
Now, we can finally edit the source code.
Fix the code in give_candygrams.R
so that the extra messages print out properly. (Hint: This is a small change in only one line of the code!)
Check that the code is fixed by re-building the package, re-knitting the README.Rmd
file, and looking at the output at the bottom.
When you are satisfied with your change, commit your changes to Git.
Then run in the console:
This will magically pop up a Pull Request window, like the one from Week 1!
Complete the Pull Request. Take a screenshot of the “Files Changed” tab of the Pull Request screen.
Unlike Week 1, I will NOT be merging your Pull Requests.
Last, let’s check out the unit tests in Regina’s package.
In any package, it’s very important to write automatic tests to check that your functions work the way you hope. As the package gets more complex, you can then keep running your tests to make sure nothing broke along the way.
Run the tests that Regina has written for her package by typing Ctrl/Cmd-Shift-T
; or from the drop-down menu in the Build pane:
Oh no! One of the unit tests failed.
In the folder tests/testthat
, find the appropriate test file. Figure out how the actual output was different from the expected output.
Start a new pull request using pr_init()
, as you did above.
Track down the error, and make changes to the function. Keep trying until all tests succeed.
When you are done, finish off the pull request, and take a screenshot.
When handing out candygrams, Santa has a whole classroom of students to announce.
Honestly, this whole movie is required viewing.
It would be tedious to have to call the give_candygrams()
function once for each student. Instead, it would be convenient to have a vector of student names and a vector of candygram counts, and run a single function like so:
students <- c("Taylor Zimmerman", "Glen Coco", "Cady Heron", "Gretchen Weiners")
counts <- c(2, 4, 1, 1)
give_many_candygrams(students, counts)
Write the function give_many_candygrams()
into the meangirls
package.
Do your best to copy the documentation style of the give_candygrams()
function. Type Ctrl-Shift-D
, or use the Build pane drop-down menu, to automatically write a documentation file for your function.
Re-build the package. Then type ?give_many_candygrams
in your console.
Take a screenshot of the “Help” pane that pops up. It should look like this, except for your new function:
Type in your console:
Edit the file that pops up, to create a unit test for your new give_many_candygrams
function.
Re-run the tests for the package.
Once all the tests pass, take a screenshot of the Build pane.
It should look like this, but with a total of 7 tests: