Skip to content
Open
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
19 changes: 19 additions & 0 deletions R/setFishing.R
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,25 @@ check_gear_params <- function(x) {
". Did you perhaps mis-spell the names?")
}

# Auto-populate missing argument columns for selectivity functions
if ("sel_func" %in% names(x)) {
funcs <- unique(x$sel_func)
funcs <- funcs[!is.na(funcs)]
for (sf in funcs) {
arg <- tryCatch({
args <- names(formals(sf))
args[!(args %in% c("w", "species_params", "..."))]
}, error = function(e) NULL)
if (length(arg) > 0) {
for (a in arg) {
if (!(a %in% names(x))) {
x[[a]] <- NA
}
}
}
}
}

# Validate parameters based on selectivity function
if (nrow(x) > 0 && "sel_func" %in% names(x)) {
for (i in seq_len(nrow(x))) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-calc-selectivity.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test_that("calc_selectivity leaves unspecified gear-species combinations at zero

test_that("calc_selectivity errors for missing or NA selectivity parameters", {
params_missing <- knife_edge_selectivity_params_no_length

class(params_missing@gear_params) <- "data.frame"
params_missing@gear_params$knife_edge_size <- NULL
expect_error(calc_selectivity(params_missing),
"missing in the gear_params dataframe")
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-setFishing.R
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,14 @@ test_that("gear_params reactive validation works", {
df3 <- data.frame(species = "Sprat", gear = "g", sel_func = "knife_edge", knife_edge_size = -5)
expect_warning(gp3 <- gear_params(df3), "knife_edge_size must be non-negative")
})

test_that("gear_params selectivity column auto-population works", {
df <- data.frame(species = "Sprat", gear = "g")
gp <- gear_params(df)

# Setting sel_func to sigmoid_length should add columns l25 and l50
gp$sel_func <- "sigmoid_length"
expect_true(all(c("l25", "l50") %in% colnames(gp)))
expect_true(all(is.na(gp$l25)))
expect_true(all(is.na(gp$l50)))
})