-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.Rmd
111 lines (76 loc) · 3.82 KB
/
main.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
title: "Package development exercise"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(error = TRUE)
```
## Aims
* To practice setting up a package
* To practice writing modular code
* To practice writing code to meet requirements
## Resources
You can find the documentation for devtools and usethis by following these links:
* [Devtools](https://github.com/r-lib/devtools)
* [Usethis](https://github.com/r-lib/usethis)
## Initial setup
Make sure you've opened the examplePackage project in RStudio and that you have the following packages and their dependencies installed:
* usethis
* devtools
* rmarkdown
* dplyr
## Package setup
The following setup stages will create a package structure you can work with.
First, you need to turn your project folder into a package (say "No" to overwriting existing files):
```
usethis::create_package("../examplePackage")
```
Make sure you've set up your documentation files:
```
usethis::use_readme_md()
usethis::use_mit_license(copyright_holder = "Crown copyright")
```
## The fun part
When finished, this pipeline will convert metric penguin measurements to imperial units.
### Load data and functions
This is your actual pipeline. First, the penguins dataset, which will be our dummy data for this exercise:
```{r}
load("data/penguins_data.rda")
head(penguins_data)
```
You also want to load all the functions in your package:
```{r}
devtools::load_all()
```
### Manipulate data
The functions `mm_to_inches()` and `g_to_oz()` don't exist yet! You need to create them and add them to your package. To do this, make a new script in the `R/` folder called "conversions.R" and put the code for these new functions there. Remember to add roxygen documentation (including `@export`) and run `devtools::document()` before re-running this notebook.
Each function needs to take in one parameter - the numbers to convert. This will be a numeric object of any length. It needs to perform the conversion and return the converted values. There are 25.4 millimetres in an inch and 28.349523125 grams in an ounce.
```{r}
penguins_data <- dplyr::mutate(
penguins_data,
bill_length_inches = mm_to_inches(bill_length_mm),
bill_depth_inches = mm_to_inches(bill_depth_mm),
flipperl_depth_inches = mm_to_inches(flipper_length_mm),
body_mass_oz = g_to_oz(body_mass_g)
)
head(penguins_data)
```
### Testing functions
There is a specific layout for unit tests, but here are example tests for the functionality your just wrote. Let's see if you pass the tests!
```{r}
library(testthat)
test_that("length conversions handle zeros", expect_equal(mm_to_inches(0), 0))
test_that("length conversions handle single numbers", expect_equal(mm_to_inches(100), 3.937008, tolerance = 0.0000001))
test_that("length conversions handle negatives numbers", expect_equal(mm_to_inches(-100), -3.937008, tolerance = 0.0000001))
test_that("length conversions handle vectors longer than 1", expect_equal(mm_to_inches(c(100, 1000, 10000)), c(3.937008, 39.37008, 393.7008), tolerance = 0.0000001))
test_that("weight conversions handle zeros", expect_equal(g_to_oz(0), 0))
test_that("weight conversions handle single numbers", expect_equal(g_to_oz(100), 3.527396, tolerance = 0.0000001))
test_that("weight conversions handle negatives numbers", expect_equal(g_to_oz(-100), -3.527396, tolerance = 0.0000001))
test_that("weight conversions handle vectors longer than 1", expect_equal(g_to_oz(c(100, 1000, 10000)), c(3.527396, 35.27396, 352.7396), tolerance = 0.0000001))
```
### Further developments
There is a lot more than can be added to this package. Here are some ideas for further developments:
* Add new modules with functionality for summary stats, visualisations, etc.
* Add dependencies using usethis::use_package()
* set up a test folder with usethis::use_test() and migrate the tests from this file