Skip to content

Commit 723de98

Browse files
fix: handle sex groups with single maturity class in plot_maturity
Guard against "argument is of length zero" crash when a sex group contains only immature or only mature fish. Previously, the comparison tmp[tmp$maturity == 0, ]$mean > tmp[tmp$maturity == 1, ]$mean would return logical(0) if one maturity class was absent, causing an error. Now returns NA with a warning instead.
1 parent 8cda6d8 commit 723de98

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: ggFishPlots
22
Type: Package
33
Title: Visualise and Calculate Life History Parameters for Fisheries Science using 'ggplot2'
4-
Version: 0.4.3
4+
Version: 0.4.4
55
Date: 2026-05-12
66
Authors@R: c(person("Mikko", "Vihtakari", email = "mikko.vihtakari@hi.no",
77
role = c("aut", "cre"),

R/plot_maturity.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,17 @@ plot_maturity <- function(
280280
dplyr::group_by(maturity) %>%
281281
dplyr::summarise(mean = mean(length), .groups = "keep")
282282

283+
if (!all(c(0L, 1L) %in% tmp$maturity)) {
284+
warning(
285+
"Sex group '", k, "' has only one maturity class in the data. ",
286+
"Unable to calculate L50 reliably."
287+
)
288+
return(
289+
tibble::tibble(mean = NA, ci.min = NA, ci.max = NA, sex = k) %>%
290+
dplyr::mutate(dplyr::across(c("mean", "ci.min", "ci.max"), as.numeric))
291+
)
292+
}
293+
283294
if (tmp[tmp$maturity == 0, ]$mean > tmp[tmp$maturity == 1, ]$mean) {
284295
warning(
285296
"Mean size of ",

0 commit comments

Comments
 (0)