Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create minimal R package #14

Merged
merged 22 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
^renv$
^renv\.lock$
^.*\.Rproj$
^\.Rproj\.user$
^.lintr$
^.github$
1 change: 1 addition & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
35 changes: 35 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check.yaml

permissions: read-all

jobs:
R-CMD-check:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-renv@v2

- name: Install rcmdcheck
run: install.packages("rcmdcheck")
shell: Rscript {0}

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
37 changes: 37 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: lint.yaml

permissions: read-all

jobs:
lint:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-renv@v2

- name: Install lintr
run: install.packages("lintr")
shell: Rscript {0}

- name: Lint
run: |
pkgload::load_all()
lintr::lint_package()
shell: Rscript {0}
env:
LINTR_ERROR_ON_LINT: true
86 changes: 86 additions & 0 deletions .github/workflows/pr-commands.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
issue_comment:
types: [created]

name: pr-commands.yaml

permissions: read-all

jobs:
document:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }}
name: document
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-renv@v2

- name: Install roxygen2
run: install.packages("roxygen2")
shell: Rscript {0}

- name: Document
run: roxygen2::roxygenise()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add man/\* NAMESPACE
git commit -m 'Document'

- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

style:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }}
name: style
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2

- name: Install dependencies
run: install.packages("styler")
shell: Rscript {0}

- name: Style
run: styler::style_pkg()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add \*.R
git commit -m 'Style'

- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters: linters_with_defaults(
line_length_linter(120),
object_name_linter(styles = c("snake_case", "symbols", "camelCase"))
)
encoding: "UTF-8"
21 changes: 21 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Package: omopbundles
Type: Package
Title: Bundles of OMOP Concepts for Ease of Data Selection
Version: 0.1.0
Authors@R: person('Stef', 'Piatek', email = '[email protected]', role = c('cre', 'aut'))
Description: Bundles of OMOP Concepts for Ease of Data Selection.
License: Apache License (>= 2)
Encoding: UTF-8
LazyData: true
Imports:
dplyr (>= 1.0),
purrr (>= 1.0),
readr (>= 2.0)
Suggests:
stefpiatek marked this conversation as resolved.
Show resolved Hide resolved
usethis,
testthat (>= 3.0.0),
lintr,
pak,
pkgload
Config/testthat/edition: 3
RoxygenNote: 7.3.2
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(available_bundles)
export(concept_by_bundle)
importFrom(dplyr,mutate)
importFrom(dplyr,tibble)
importFrom(purrr,map_dfr)
importFrom(readr,read_csv)
56 changes: 56 additions & 0 deletions R/bundles.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' @importFrom dplyr tibble mutate
#' @importFrom purrr map_dfr
#' @importFrom readr read_csv

#' @title Get available bundles for a version
#'
#' @description If a bundle has multiple names, then the id will be duplicated across rows
#'
#' @param version Requested version, if not defined, the latest will be used
#' @return dataframe that contains a "concept_name", "version" and a "domain" column for each available concept
#' @export
#' @examples
#' available_bundles()
#' available_bundles("0.1")
available_bundles <- function(version = "latest") {
raw_dir <- .get_raw_dir()
directories <- list.dirs(raw_dir, full.names = TRUE)
domain_directories <- directories[directories != raw_dir]

purrr::map_dfr(domain_directories, .build_concepts_from_directory) |>
mutate(version = version)
}
stefpiatek marked this conversation as resolved.
Show resolved Hide resolved

.get_raw_dir <- function(...) {
system.file("data-raw", ..., package = "omopbundles", mustWork = TRUE)
}

.build_concepts_from_directory <- function(directory) {
concept_files <- list.files(directory)
concept_name <- NULL

dplyr::tibble(
id = concept_files,
concept_name = concept_files,
domain = basename(directory)
) |>
dplyr::mutate(concept_name = sub("\\.csv$", "", concept_name)) |>
dplyr::mutate(concept_name = gsub("_", " ", concept_name))
}

#' @title Get concepts for a a single bundle row
#'
#' @param bundle_row Single row of a dataframe with a domain and id
#' @return Dataframe with a concept_id and domain column
#' @export
#' @examples
#' available_bundles() |> dplyr::filter(concept_name == "smoking") |> concept_by_bundle()
#' dplyr::tibble(id = "smoking.csv", domain = "observation", version = "latest") |> concept_by_bundle()
concept_by_bundle <- function(bundle_row) {
stopifnot(is.data.frame(bundle_row))
stopifnot(nrow(bundle_row) == 1)

.get_raw_dir(bundle_row$domain, bundle_row$id) |>
readr::read_csv(show_col_types = FALSE) |>
dplyr::mutate(domain = bundle_row$domain)
}
stefpiatek marked this conversation as resolved.
Show resolved Hide resolved
91 changes: 89 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,89 @@
# omop-bundles
R package to provide access to OMOP bundles
# Omop Bundles

<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![R-CMD-check](https://github.com/SAFEHR-data/omop-bundles/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/SAFEHR-data/omop-bundles/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

Catalogue of omop concepts grouped into useful bundles to help researchers select concepts.

## Overview

1. [Installation](#installation)
2. [Development](#development)
- [Set up](#set-up)
- [Updating the `renv` lockfile](#updating-the-renv-lockfile)
- [Design](#design)
- [Coding style](#coding-style)
3. [Deployment](./deploy/README.md)

## Installation

You can install the development version of data-catalogue from within R:

```r
# install.packages("pak")
pak::pak("SAFEHR-data/data-catalogue")
```

## Development

### Set up

Make sure you have a [recent version of R](https://cloud.r-project.org/) (>= 4.4.0) installed.
Though not required, [RStudio](https://www.rstudio.com/products/rstudio/download/) is recommended as an IDE,
as it has good support for R package development and Shiny.

1. Clone this repository

- Either with `git clone [email protected]:SAFEHR-data/omop-bundles.git`
- Or by creating [a new project in RStudio from version control](https://docs.posit.co/ide/user/ide/guide/tools/version-control.html#creating-a-new-project-based-on-a-remote-git-or-subversion-repository)

2. Install [`{renv}`](https://rstudio.github.io/renv/index.html) and restore the project library by running the following from an R console in the project directory:

```r
install.packages("renv")
renv::restore()
```

### Updating the `renv` lockfile

Make sure to regularly run `renv::status(dev = TRUE)` to check if your local library and the lockfile
are up to date.

When adding a new dependency, install it in the `renv` library with

```r
renv::install("package_name")
```

and then use it in your code as usual.
`renv` will pick up the new package if it's installed and used in the project.

To update the lockfile, run

```r
renv::snapshot(dev = TRUE)
```

The `dev = TRUE` argument ensures that development dependencies (e.g. those recorded under
`Suggests` in the `DESCRIPTION` file) are also included in the lockfile.

### Coding style

We are following the [tidyverse style guide](https://style.tidyverse.org/).
The [`{styler}`](https://styler.r-lib.org/index.html) package can be used to automatically format R code to this style,
by regularly running

```r
styler::style_pkg()
```

within the project directory.
It's also recommended to install [`{lintr}`](https://github.com/r-lib/lintr) and regularly run

```r
lintr::lint_package()
```

(or have it [run automatically in your IDE](https://lintr.r-lib.org/articles/editors.html)).
4 changes: 4 additions & 0 deletions inst/data-raw/measurement/HBA1c.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
concept_id,allowed,per_person,min,max
3005673,T,1,0,
40762352,T,1,0,

3 changes: 3 additions & 0 deletions inst/data-raw/measurement/LDH.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
concept_id,allowed,per_person,min,max
3022250,T,1,0,

3 changes: 3 additions & 0 deletions inst/data-raw/measurement/antibodies_to_hiv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
concept_id,allowed,per_person,min,max
40760007,T,1,0,

29 changes: 29 additions & 0 deletions inst/data-raw/measurement/antigens.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
concept_id,allowed,per_person,min,max
3003573,T,0,,
3021714,T,0,,
40761161,T,0,,
3005480,T,0,,
3037821,T,0,,
3033876,T,0,,
3024448,T,0,,
3010024,T,0,,
3002394,T,0,,
3001884,T,0,,
3013356,T,0,,
3013154,T,0,,
3013506,T,0,,
3023848,T,0,,
3026396,T,0,,
3028568,T,0,,
3037596,T,0,,
3007188,T,0,,
3019597,T,0,,
3005186,T,0,,
3005222,T,0,,
3008387,T,0,,
3006768,T,0,,
37062741,T,0,,
37051745,T,0,,
37020250,T,0,,
37021506,T,0,,
37021365,T,0,,
Loading