Skip to content

Commit

Permalink
Merge branch 'main' into r-pkgs-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon authored Nov 12, 2024
2 parents f27a3bd + f07d949 commit 032de73
Show file tree
Hide file tree
Showing 14 changed files with 17,038 additions and 294 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ jobs:

- name: preping
run: |
mkdir _site
cp -R 0* _site
quarto render README.qmd --to=html
mv README.html _site/index.html
mv README.html index.html
- uses: actions/upload-pages-artifact@v3
with:
path: "_site"
path: "./"

website:
if: ${{ github.event_name != 'pull_request' }}
Expand Down
3,599 changes: 3,599 additions & 0 deletions 11-rpkgs-i/lab.html

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions 11-rpkgs-i/lab.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: "Lab 07 - R packages"
format:
html:
embed-resources: true
---

# Learning goals

- Build an R package.
- Use Rcpp in the package.
- Practice your GitHub skills.

# Lab description

Today we will write an R package that uses `Rcpp`. To do so, we will use a
[template package](https://github.com/UofUEpiBio/egpkg) that has the following
set up:

- Has a single Rcpp function.

- Uses `roxygen2`.

- Uses GitHub Actions for continuous integration.

The R package doesn't have any testing in place, needs a license, and doesn't
have coverage checking.

## Question 1: Make sure you can compile the package

Fork the following repository [UofUEpiBio/egpkg](https://github.com/UofUEpiBio/egpkg),
and clone it to your local computer. Once you have a copy, do the following:

1. Inspect its contents and the file located at `.github/workflows`.

2. Make sure you can install it using Rstudio.

3. Run R CMD check using RStudio.

4. RUn R CMD check using the command line (if available).

If you don't fork it and instead downloaded it manually, make sure you
set up a GitHub repository in your account to push your changes.

## Question 2: Adding a C++ function

Add the `ps_match` function [we wrote on Week 5](https://github.com/UofUEpiBio/PHS7045-advanced-programming/issues/8#issuecomment-1424974938).
The function should be named `ps_match`:

1. Add the function in a separate c++ file in the `src` folder. Make sure to document
it using `roxygen`. Once you finish that, ensure it compiles and the function
is visible.[^reminder] (then commit and push)

[^reminder]: Remember to run `roxygen2::roxygenise()`, `devtools::document()`, or Ctr + Shift + D, if using RStudio.

2. Write an example with artificial data passing a random vector with ten elements
distributed U[0, 1]. Add the example to the documentation using the `@examples` tag. (then commit and push)

3. Write a test using the `testthat` R package. The C++ function should match the results of the equivalent R function:

```r
set.seed(1231)
x <- cbind(runif(20))

ps_matchR <- function(x) {

match_expected <- dist(x) |> as.matrix()
diag(match_expected) <- .Machine$integer.max
indices <- apply(match_expected, 1, which.min)

list(
match_id = as.integer(unname(indices)),
match_x = x[indices]
)

}
```

(then commit and push)

## Question 3: Checkout the coverage of the package (if time permits)

Using the `covr` R package, checkout the coverage using the following function

```r
covr::package_coverage()
```
3,471 changes: 3,471 additions & 0 deletions 11-rpkgs-i/slides.html

Large diffs are not rendered by default.

Loading

0 comments on commit 032de73

Please sign in to comment.