Homework 4: Package divisor

HW
Week05
Published

February 19, 2026

Note: This assignment must be submitted in github classroom.

divisor

Lifecycle: experimental CRAN status

The goal of divisor is to practice writing R packages. The structure of this repository was created with the command usethis::create_tidy_package().

Before you can knit this Readme.Rmd document, you will have to install the package divisor. The best way to do that, is to first open RStudio from the divisor.Rproj in the folder and run the command devtools::install(). This will also set you up for working on the assignment questions.

Installation

You can install the development version of divisor from GitHub with:

# install.packages("pak")
pak::pak("stat-assignments/divisor")

Example

This is a basic example which shows you how to solve a common problem:

library(divisor)
## basic example code

Assignment Questions

  1. Running R CMD check (‘Cmd/Ctrl Shift E’) results in a failure. You also get a notification about a non-standard file. You can ignore that for the moment.
  • Document the error and identify its cause.
  • Suggest a fix.
  • Why does deleting the file hello.R not help?
  1. Previously, we used the modulo operator to keep an angle between 0 and 360 (and between 0 and \(2\pi\), respectively).
library(dplyr)
input_df <- readr::read_csv("input1.txt", col_names=FALSE)

input_df <- input_df |> mutate(
  direction = substr(X1, 1, 1),
  clicks_raw = readr::parse_number(X1),
  rotations = clicks_raw %/% 100,
  clicks = clicks_raw %% 100, # modulo first, direction second
  clicks = ifelse(direction=="L", -clicks, clicks)
)

input_df <- input_df |> mutate(
  total = (50 + cumsum(clicks)) %% 100
)
sum(input_df$total==0)
#> [1] 984

When we test for degrees rather than clicks, it becomes obvious that the modulo operator %% uses a comparison to 0 with ==:

input_df <- input_df |> mutate(
  degree = clicks*3.6,
  total_degree = (180 + cumsum(degree)) %% 360
)

input_df <- input_df |> mutate(
  radians = clicks/50*pi,
  total_radians = (pi + cumsum(radians)) %% (2*pi)
)
sum(input_df$total_degree == 0)
#> [1] 481
sum(near(input_df$total_degree, 0))
#> [1] 966
sum(near(input_df$total_degree, 0, tol=1))
#> [1] 966
sum(near(input_df$total_degree, 0, tol=1.8))
#> [1] 966

sum(input_df$total_radians == 0)
#> [1] 187
sum(near(input_df$total_radians, 0))
#> [1] 199
  • Overwrite the modulo operator %% such that you can specify a tolerance with tol. Set the tolerance initially to the same value that near uses.

  • Show the effect of the newly defined operator in an example.

  • Make the operator a part of the divisor package: add a help file with an example. Ensure that R CMD check passes (with the same note as before)

  1. The file input1.txt contains data that might be suitable for showcasing some examples. Read up on how to add a dataset to an R package.
  • Add a dataset called dials to your package. Make sure to add a help file for the dataset. With at least one example for how to use it.

  • Write up the origin story of your data (in code).

  • Ensure that R CMD check passes without any errors, warnings or notes.