Assignment B1: Making a Function in R
Due: Tuesday November 5th, 2026 at 11:59pm
Total Points: 100
This assignment covers making a function in R, documenting it, and testing it.
Setup
Create a new repository in our organization https://github.com/UBC-STAT545-F2026. Set the owner as UBC-STAT545-F2026 and name your repository
assignmentB1-FIRSTNAME-LASTNAME.- New to the course and don’t have access to our organization? Please let the instructor know ASAP!
Clone the repository to your computer. Make a new RMarkdown (.Rmd) file containing all of your code. Be sure to use some dialogue between code chunks. Set the output to be a github_document
Exercise 1: Make a Function (25 points)
Create an R Function! It doesn’t need to be overly complicated, but it also shouldn’t be completely trivial (i.e., my function <- function(x){ print(x)})
Function Ideas
Did you repeat any code for a data analysis in STAT 545A? If so, consider making a function for this action.
Consider bundling a specific dplyr workflow (like
group_by() %>% summarise())Write a wrapper around an existing function.
For example, perhaps accepting a narrower range of inputs (like not allowing logical vectors), or providing a different output.
It’s usually better to narrow a function’s focus than to broaden, so that a function doesn’t end up doing too much.
Make a special plot that you’d want to repeat when exploring your data
Guidelines
Your function should not rely on anything from your working environment. Try clearing it to ensure your function works as intended.
Your function should not rely on “magic numbers” – pre-selected numbers (or options) that appear inside the function that can’t be accessed by a user of the function.
- For example, maybe
quantile(x, type = 1, ...)appears in your function. The choice of 1 is arbitrary – unless you’re making a function likequantile_type_1().
- For example, maybe
Your function should be reasonably flexible on its inputs. Here is an example of a function that is not reasonably flexible: a function called
count_missing_values_by_group()that only works when the data frame has columns that are all of type fct.The output is consistent – for example, always gives a list. An example of inconsistent output:
sapply(1:3, seq_len)gives a list, andsapply(1:3, sqrt)gives an (atomic) vector.Your function includes appropriate arguments. (Do you handle NA’s appropriately? Are you using the ellipsis properly? etc.)
Exercise 2: Document your Function (20 points)
In the same code chunk where you made your function, document the function using roxygen2 tags. Be sure to include:
-
- (Justification for naming is not often needed, but we want to hear your reasoning.)
Exercise 3: Include examples (15 points)
Demonstrate the usage of your function with a few examples. Use one or more new code chunks, describing what you’re doing.
Note: If you want to deliberately show an error, you can use error = TRUE in your code chunk option.
Exercise 4: Test the Function (25 points)
Write formal tests for your function. You should use at least three non-redundant uses of an expect_*() function from the testthat package, and they should be contained in a test_that() function (or more than one). They should all pass.
You can use the same expect_*() function multiple times so long as they test different cases.
Example of non-redundant inputs in the dice rolling example from class:
Rolling a positive integer number of dice
Rolling no dice
Rolling a negative number of dice
Rolling half a dice
Example of redundant inputs:
- Rolling 1 die
- Rolling 3 dice
- Rolling 100 dice
Tidy Submission (15 points)
Follow these steps to submit your work. Be sure to familiarize yourself with the rubric for a tidy submission below, before doing these steps.
See Rubric on Canvas for more details on grading.