Skip to content

Commit 9955ed2

Browse files
authored
R pkgs 2024 (#38)
* Updating slides * Updating R packages slides * Renaming the week
1 parent f07d949 commit 9955ed2

7 files changed

Lines changed: 7892 additions & 2 deletions

File tree

13-rpkgs-i/lab.html

Lines changed: 3599 additions & 0 deletions
Large diffs are not rendered by default.

13-rpkgs-i/lab.qmd

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: "Lab 07 - R packages"
3+
format:
4+
html:
5+
embed-resources: true
6+
---
7+
8+
# Learning goals
9+
10+
- Build an R package.
11+
- Use Rcpp in the package.
12+
- Practice your GitHub skills.
13+
14+
# Lab description
15+
16+
Today we will write an R package that uses `Rcpp`. To do so, we will use a
17+
[template package](https://github.com/UofUEpiBio/egpkg) that has the following
18+
set up:
19+
20+
- Has a single Rcpp function.
21+
22+
- Uses `roxygen2`.
23+
24+
- Uses GitHub Actions for continuous integration.
25+
26+
The R package doesn't have any testing in place, needs a license, and doesn't
27+
have coverage checking.
28+
29+
## Question 1: Make sure you can compile the package
30+
31+
Fork the following repository [UofUEpiBio/egpkg](https://github.com/UofUEpiBio/egpkg),
32+
and clone it to your local computer. Once you have a copy, do the following:
33+
34+
1. Inspect its contents and the file located at `.github/workflows`.
35+
36+
2. Make sure you can install it using Rstudio.
37+
38+
3. Run R CMD check using RStudio.
39+
40+
4. RUn R CMD check using the command line (if available).
41+
42+
If you don't fork it and instead downloaded it manually, make sure you
43+
set up a GitHub repository in your account to push your changes.
44+
45+
## Question 2: Adding a C++ function
46+
47+
Add the `ps_match` function [we wrote on Week 5](https://github.com/UofUEpiBio/PHS7045-advanced-programming/issues/8#issuecomment-1424974938).
48+
The function should be named `ps_match`:
49+
50+
1. Add the function in a separate c++ file in the `src` folder. Make sure to document
51+
it using `roxygen`. Once you finish that, ensure it compiles and the function
52+
is visible.[^reminder] (then commit and push)
53+
54+
[^reminder]: Remember to run `roxygen2::roxygenise()`, `devtools::document()`, or Ctr + Shift + D, if using RStudio.
55+
56+
2. Write an example with artificial data passing a random vector with ten elements
57+
distributed U[0, 1]. Add the example to the documentation using the `@examples` tag. (then commit and push)
58+
59+
3. Write a test using the `testthat` R package. The C++ function should match the results of the equivalent R function:
60+
61+
```r
62+
set.seed(1231)
63+
x <- cbind(runif(20))
64+
65+
ps_matchR <- function(x) {
66+
67+
match_expected <- dist(x) |> as.matrix()
68+
diag(match_expected) <- .Machine$integer.max
69+
indices <- apply(match_expected, 1, which.min)
70+
71+
list(
72+
match_id = as.integer(unname(indices)),
73+
match_x = x[indices]
74+
)
75+
76+
}
77+
```
78+
79+
(then commit and push)
80+
81+
## Question 3: Checkout the coverage of the package (if time permits)
82+
83+
Using the `covr` R package, checkout the coverage using the following function
84+
85+
```r
86+
covr::package_coverage()
87+
```

13-rpkgs-i/slides.html

Lines changed: 3471 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)