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

feat: ✨ update algorithm logic to include regex in parentheses #159

Merged
merged 13 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 3 deletions R/get-algorithm.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#' get_algorithm_logic("hba1c")
#' get_algorithm_logic("gld")
#' }
get_algorithm_logic <- function(criteria) {
algorithm |>
get_algorithm_logic <- function(criteria, algorithm_logic = algorithm) {
algorithm_logic |>
dplyr::filter(.data$name == criteria) |>
dplyr::pull(.data$logic) |>
stringr::str_replace_all("AND", "&") |>
stringr::str_replace_all("OR", "|") |>
# regex are defined with '=~', so convert them into a stringr function.
stringr::str_replace_all("(.*) \\=\\~ (.*)", "stringr::str_detect(\\1, \\2)")
stringr::str_replace_all("(\\(?)([a-zA-Z0-9_]+)\\)? \\=\\~ ('.*')", "\\1stringr::str_detect(\\2, \\3)")
}
24 changes: 24 additions & 0 deletions tests/testthat/test-get-algorithm-logic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
test_algorithm_logic <- data.frame(
name = c("hba1c", "podiatrist_services", "gld"),
logic = c(
"(analysiscode == 'NPU27300' AND value >= 48) OR (analysiscode == 'NPU03835' AND value >= 6.5)",
"(speciale =~ '^54') AND (barnmak != 0)",
"atc =~ '^A10'"
)
)

test_that("`or` logic is converted to R logic", {
get_algorithm_logic("hba1c", test_algorithm_logic) |>
expect_equal("(analysiscode == 'NPU27300' & value >= 48) | (analysiscode == 'NPU03835' & value >= 6.5)")
})

test_that("single regex is converted to R logic", {
get_algorithm_logic("gld", test_algorithm_logic) |>
expect_equal("stringr::str_detect(atc, '^A10')")
})

test_that("`and` logic and regex within parentheses are converted to R logic", {
# i.e., the regex is within a parenthesis
get_algorithm_logic("podiatrist_services", test_algorithm_logic) |>
expect_equal("(stringr::str_detect(speciale, '^54')) & (barnmak != 0)")
})
Loading