Skip to content

Commit

Permalink
Merge pull request #438 from mrc-ide/optional-duckdb
Browse files Browse the repository at this point in the history
Make duckdb an optional dependency
  • Loading branch information
r-ash authored Aug 9, 2024
2 parents 60ad718 + de7fc53 commit 0e4117f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true
extra-repositories: https://mrc-ide.r-universe.dev/bin/linux/jammy/4.3
extra-repositories: https://mrc-ide.r-universe.dev

- uses: r-lib/actions/setup-r-dependencies@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Imports:
brio,
data.tree,
dplyr (>= 1.1.0),
duckdb (>= 0.10.0),
eppasm (>= 0.7.1),
gt,
first90 (>= 1.6.1),
Expand Down Expand Up @@ -60,6 +59,7 @@ Suggests:
DiagrammeR,
covr,
datamodelr,
duckdb (>= 0.10.0),
here,
knitr,
lubridate,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# naomi 2.9.28

* Make duckdb an optional dependency

# naomi 2.9.27

* Show calibration plot ratio values to nearest 0.1.
Expand Down
1 change: 1 addition & 0 deletions R/outputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,7 @@ read_hintr_output <- function(path) {
}

read_duckdb <- function(path) {
assert_package_installed("duckdb")
con <- DBI::dbConnect(duckdb::duckdb(dbdir = path, read_only = TRUE))
on.exit(DBI::dbDisconnect(con, shutdown = TRUE))
DBI::dbGetQuery(con, sprintf("SELECT * from %s", DUCKDB_OUTPUT_TABLE_NAME))
Expand Down
1 change: 1 addition & 0 deletions R/run-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ hintr_save <- function(obj, file) {
stop(paste("Trying to save invalid object as duckdb database.",
"Only data frames can be saved as database."))
}
assert_package_installed("duckdb")
con <- DBI::dbConnect(duckdb::duckdb(dbdir = file))
on.exit(DBI::dbDisconnect(con, shutdown = TRUE))
DBI::dbWriteTable(con, DUCKDB_OUTPUT_TABLE_NAME, obj)
Expand Down
12 changes: 12 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ vlapply <- function(X, FUN, ...) {
is_empty <- function(x) {
length(x) == 0 || is.null(x) || is.na(x) || !nzchar(x)
}


assert_package_installed <- function(package_name) {
if (!requireNamespace(package_name, quietly = TRUE)) {
stop(
sprintf("Package '%s' must be installed to use this function.",
package_name),
call. = FALSE
)
}
invisible(TRUE)
}
1 change: 1 addition & 0 deletions tests/testthat/test-01-run-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ test_that("trying to calibrate incompatible model output returns error", {
})

test_that("calibration plot data can be saved as duckdb database", {
testthat::skip_if_not_installed("duckdb")

## Calibration makes no modification of existing files.
output_hash <- tools::md5sum(a_hintr_output$model_output_path)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-hintr-plot-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ test_that("there is metadata for every indicator in comparison data", {
})

test_that("hintr data can be saved and read as qs or duckdb type", {
testthat::skip_if_not_installed("duckdb")

t_qs <- tempfile(fileext = ".qs")
t_db <- tempfile(fileext = ".duckdb")
t_rds <- tempfile(fileext = ".rds")
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ test_that("is_empty", {
expect_true(is_empty(c()))
expect_false(is_empty(c("things")))
})

test_that("can assert optional package is installed", {
expect_true(assert_package_installed("testthat"))

expect_error(
assert_package_installed("my.fake.pkg"),
"Package 'my.fake.pkg' must be installed to use this function.",
fixed = TRUE)
})

0 comments on commit 0e4117f

Please sign in to comment.