Skip to content

Commit

Permalink
Remove comment argument from setMaxIntakeRate() as proposed in #214
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavdelius committed Aug 21, 2021
1 parent 835c6fa commit 084bbe8
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 65 deletions.
2 changes: 1 addition & 1 deletion R/newMultispeciesParams.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ newMultispeciesParams <- function(
#' @inheritParams setInteraction
#' @inheritDotParams setPredKernel
#' @inheritDotParams setSearchVolume
#' @inheritDotParams setMaxIntakeRate
#' @inheritDotParams setMaxIntakeRate -reset
#' @inheritDotParams setMetabolicRate
#' @inheritDotParams setExtMort
#' @inheritDotParams setReproduction
Expand Down
33 changes: 24 additions & 9 deletions R/setMaxIntakeRate.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
#' @param intake_max Optional. An array (species x size) holding the maximum
#' intake rate for each species at size. If not supplied, a default is set as
#' described in the section "Setting maximum intake rate".
#' @param comment_intake_max `r lifecycle::badge("experimental")`
#' A string describing how the value for 'intake_max' was obtained. This is
#' ignored if 'intake_max' is not supplied or already has a comment
#' attribute.
#' @param reset If set to TRUE, then the intake rate will be reset to the value
#' calculated from the species parameters, even if it was previously
#' overwritten with a custom value. If set to FALSE (default) then a
#' recalculation from the species parameters will take place only if no
#' custom value has been set.
#' @param ... Unused
#'
#' @return A `MizerParams` object with updated maximum intake rate. Because
Expand All @@ -33,16 +34,29 @@
#' `params <- setMaxIntakeRate(params, ...)`.
#' @export
#' @family functions for setting parameters
setMaxIntakeRate <- function(params,
intake_max = NULL,
comment_intake_max = "set manually", ...) {
assert_that(is(params, "MizerParams"))
setMaxIntakeRate <- function(params, intake_max = NULL, reset = FALSE, ...) {
assert_that(is(params, "MizerParams"),
is.logical(reset))
species_params <- params@species_params

if (reset) {
if (!is.null(intake_max)) {
warning("Because you set `reset = TRUE`, the value you provided ",
"for `intake_max` will be ignored and a value will be ",
"calculated from the species parameters.")
intake_max <- NULL
}
comment(params@intake_max) <- NULL
}

# If intake_max array is supplied, check it, store it and return
if (!is.null(intake_max)) {
if (is.null(comment(intake_max))) {
comment(intake_max) <- comment_intake_max
if (is.null(comment(params@intake_max))) {
comment(intake_max) <- "set manually"
} else {
comment(intake_max) <- comment(params@intake_max)
}
}
assert_that(is.array(intake_max),
identical(dim(intake_max), dim(params@intake_max)))
Expand All @@ -58,6 +72,7 @@ setMaxIntakeRate <- function(params,
return(params)
}

# Else recalculate from species params
params@species_params$h <- get_h_default(params)

intake_max <- sweep(outer(params@species_params[["n"]],
Expand Down
26 changes: 13 additions & 13 deletions docs/dev/reference/setMaxIntakeRate.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions docs/dev/reference/setParams.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions man/setMaxIntakeRate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions man/setParams.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 27 additions & 22 deletions tests/testthat/test-setMaxIntakeRate.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
params <- NS_params
no_sp <- nrow(params@species_params)

## setMaxIntakeRate ----
test_that("ssetMaxIntakeRate works", {
expect_identical(setMaxIntakeRate(params, params@intake_max,
comment_intake_max = NULL), params)
# setMaxIntakeRate ----
test_that("setMaxIntakeRate works", {
params <- NS_params
params@species_params$h <- 2 * params@species_params$h
p2 <- setMaxIntakeRate(params)
expect_identical(2 * params@intake_max, p2@intake_max)
# only intake max changed
p2@intake_max <- params@intake_max
expect_identical(p2, params)
})
test_that("Comment works on intake_max", {
params <- NS_params
# if no comment, it is set automatically
intake_max <- params@intake_max
params <- setMaxIntakeRate(params, intake_max = intake_max)
expect_identical(comment(params@intake_max), "set manually")

# comment is stored
comment(intake_max) <- "test"
params <- setMaxIntakeRate(params, intake_max = intake_max)
expect_identical(comment(params@intake_max), "test")

# if no comment, previous comment is kept
comment(intake_max) <- NULL
params <- setMaxIntakeRate(params, intake_max = intake_max)
expect_identical(comment(params@intake_max), "test")

# no message when nothing changes
expect_message(setMaxIntakeRate(params), NA)
# but message when a change is not stored due to comment
params@species_params$h <- 1
expect_message(setMaxIntakeRate(params),
"has been commented")

# comment argument is ignored when there is a comment on intake_max
params <- setMaxIntakeRate(params, intake_max = intake_max,
comment_intake_max = "overwrite")
expect_identical(comment(params@intake_max), "test")
# but it is used otherwise
comment(intake_max) <- NULL
params <- setMaxIntakeRate(params, intake_max = intake_max,
comment_intake_max = "overwrite")
expect_identical(comment(params@intake_max), "overwrite")
expect_message(setMaxIntakeRate(params), "has been commented")
# Can reset
p <- setMaxIntakeRate(params, reset = TRUE)
expect_equal(p@intake_max[, 1], params@w[1]^params@species_params$n,
check.attributes = FALSE)
expect_warning(setMaxIntakeRate(params, intake_max = intake_max,
reset = TRUE),
"Because you set `reset = TRUE`, the")
})

# getMaxIntakeRate ----
test_that("getMaxIntakeRate works", {
p <- setMaxIntakeRate(params, intake_max = getMaxIntakeRate(params),
comment_intake_max = NULL)
expect_identical(params, p)
expect_identical(getMaxIntakeRate(NS_params),
NS_params@intake_max)
})

0 comments on commit 084bbe8

Please sign in to comment.