From 6db7ace8e36c1a848159e8e3d81d1917d68fe925 Mon Sep 17 00:00:00 2001 From: Dirk Schumacher Date: Thu, 12 Mar 2026 15:56:38 +0100 Subject: [PATCH] Fix a bug for indicators with NULL estimates --- R/indicators.R | 6 +++++- R/prevalence.R | 3 +++ tests/testthat/test-prevalence.R | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/R/indicators.R b/R/indicators.R index 0bc44b9..a9e8f3f 100644 --- a/R/indicators.R +++ b/R/indicators.R @@ -396,7 +396,11 @@ indicators_compute_all <- function(indicators, values, concepts) { } else { tibble::tibble( result = result, - input_value = indicator_adjust_value(indicator, base_values[[i]], concepts) + input_value = indicator_adjust_value( + indicator, + base_values[[i]], + concepts + ) ) } } diff --git a/R/prevalence.R b/R/prevalence.R index 5a05deb..5574208 100644 --- a/R/prevalence.R +++ b/R/prevalence.R @@ -404,6 +404,9 @@ combine_and_format_estimates <- function( estimates <- list(...) result <- Reduce( function(acc, el) { + if (is.null(el) || nrow(el) == 0) { + return(acc) + } dplyr::inner_join( acc, el, diff --git a/tests/testthat/test-prevalence.R b/tests/testthat/test-prevalence.R index 63bea66..7f2557d 100644 --- a/tests/testthat/test-prevalence.R +++ b/tests/testthat/test-prevalence.R @@ -1,5 +1,5 @@ test_that("mn_stats", { - testdata <- random_datset(100) + testdata <- random_datset(10) res <- mn_stats( indicators = list( indicator_iodine(), @@ -113,3 +113,19 @@ test_that("mn_stats", { ) ) }) + +test_that("estimates with NULL values work", { + testdata <- random_datset(5) + expect_silent( + res <- mn_stats( + indicators = list( + indicator_iodine() + ), + age = testdata$age_years, + sex = testdata$sex, + iodine = testdata$iodine, + cluster = 1:nrow(testdata), + strata = rep.int(1, nrow(testdata)) + ) + ) +})