HW5: Custom Functions
Download and Instructions
Here’s your fifth set of koans. This set teaches you to write your own functions. Everything you have typed so far has been a call to a function somebody else wrote: mean(), filter(), ggplot(), lm(). By the end of this homework you will have written a dozen of your own, and in HW6 and HW7 you will run them hundreds of times.
There are four koans. The first is a review of dplyr, ggplot2, and lm() on a data set you haven’t seen: the course evaluations behind Hamermesh and Parker’s “Beauty in the Classroom”, 463 courses at UT Austin with a rating of each instructor’s looks alongside the students’ rating of the course. You use all seven dplyr verbs, draw the four plots from HW3, and read the intercept and slope of the line that gives the paper its title. The second and third koans teach functions in pairs: you do something once with an anonymous function, written right where it is needed, and then give it a name so you can use it again. The second koan’s functions take one input; the third’s take several, with default arguments, randomness, and a function that calls another function you wrote. The fourth koan’s functions take whole tibbles and hand back tibbles, numbers, and plots. Every function in it comes back in HW6 and HW7.
Getting the data
The data set is public, so this time there is nothing to paste from Canvas. The first koan reads it straight from the web, which means you need an internet connection while you work (the other three koans re-run the first). If read_csv() fails with a message about a URL or a connection, check that you are online and try again.
Setting up
First, download koans5.zip by clicking the link in the first paragraph. Unzip it, and then rename the unzipped folder so that it has your name in it, for example koans5_lastname. Do this before you open anything. Your Downloads folder already holds the earlier koans folders and their zip files, and they all look alike. Renaming right away means you always know which folder you are editing and, when you finish, which folder to zip and upload. The original koans5.zip stays in Downloads with the unedited koans in it, so if you skip this step it is easy to upload the wrong one.
Then open the project by double-clicking koans5.Rproj inside your renamed folder (not the individual .R files). If nothing happens when you double-click koans5.Rproj, open RStudio and navigate to File > Open Project, and then find koans5.Rproj inside koans5_lastname in your Downloads folder.
You set up the keyboard shortcut for running tests in HW1. If you’re on a new computer, redo that step:
Macs: Tools > Modify keyboard shortcuts > Run a test file > Cmd Shift T > Apply
Windows: Tools > Modify keyboard shortcuts > Run a test file > Ctrl Shift T > Apply
Now open koan_01_beauty_pays.R in the R/ folder and run it top to bottom before you run any tests. Koans 2, 3, and 4 each begin with the line source("R/koan_01_beauty_pays.R"), which re-runs the first koan, so whenever you restart R, that one line brings the data back.
Then work through the remaining koans in order: koan_02_one_input.R, koan_03_several_inputs.R, and koan_04_tibbles.R. They work the same way as HW2 through HW4: each file teaches with live examples that you should run as you read, and under each question header you’ll find a copy of the most recent example, which you edit into your answer. Run your code in the console (Ctrl/Cmd + Enter) and look at the result, save your edits (Ctrl/Cmd + S), and press Ctrl+Shift+T (Cmd+Shift+T on Mac) to run the tests.
Two things to watch for in the function koans. First, when a question asks you to write a named function and then use it, both go under the question header: the definition first, then the line that uses it. The tests look for the function by the name the question gives it (share_below, roll_dice, fake_slope, and so on), check its arguments and defaults, call it on a few inputs of their own, and then check the last line. A function with a different name, or arguments in a different order, fails with a message saying so. Second, the questions with coins and dice are random, so your answer will be a different number every time you run it. The tests call your function many times and check that its results stay in the right range and average out where they should, so there is no number to match; what matters is that the function is written correctly.
The same two cautions carry over from earlier homeworks: a question you haven’t edited yet is reported as skipped, and a syntax error in one file stops the tests for all four, with a message that points at the line.
Turning it in
Before you zip anything, run the tests one last time and read the output. Every koan must be at 100% passing, with no errors. Do not upload a submission that stops with an error, and do not upload one with failing or skipped tests. My grader runs the same tests you do, so if the tests stop on your computer they stop on mine too, and questions the tests cannot reach get no credit even if the code underneath them is right. If the tests will not run, that is the thing to fix, not something to upload and hope about. Also check that your edits are saved: the tests read the files on disk, so if the test output does not match what you see in the editor, you have unsaved changes.
When every test passes, go to your Downloads folder and find the folder you renamed, koans5_lastname. Compress that folder into a zip file (not the original koans5.zip you downloaded, and not one of the earlier homework folders). Finally, upload that zip file to Canvas for me to grade. The koans are due before class on Monday 11/2. If you get stuck, come to office hours or email to make an appointment.
Check Your Understanding
The twenty multiple choice questions below cover the four koans in this set: defining a function and what it hands back, inputs matched by position or by name and their defaults, handing a function to across() or geom_function() by name or as an anonymous function, and functions whose first input is a tibble. Every question restates the code it needs, so you can answer each one from what’s on the screen.