Skip to content

Commit

Permalink
Remove comment argument from setMetabolicRate() 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 615419a commit f897d4c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 44 deletions.
34 changes: 25 additions & 9 deletions R/setMetabolicRate.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
#' @param p The allometric metabolic exponent. This is only used if `metab`
#' is not given explicitly and if the exponent is not specified in a `p`
#' column in the `species_params`.
#' @param comment_metab `r lifecycle::badge("experimental")`
#' A string describing how the value for 'metab' was obtained. This is
#' ignored if 'metab' is not supplied or already has a comment
#' attribute.
#' @param reset If set to TRUE, then the metabolic 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 MizerParams object with updated metabolic rate. Because of the way
Expand All @@ -40,18 +41,33 @@
#' `params <- setMetabolicRate(params, ...)`.
#' @export
#' @family functions for setting parameters
setMetabolicRate <- function(params,
metab = NULL, p = NULL,
comment_metab = "set manually", ...) {
assert_that(is(params, "MizerParams"))
setMetabolicRate <- function(params, metab = NULL, p = NULL,
reset = FALSE, ...) {
assert_that(is(params, "MizerParams"),
is.logical(reset))
if (!is.null(p)) {
assert_that(is.numeric(p))
params <- set_species_param_default(params, "p", p)
}
species_params <- params@species_params

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

if (!is.null(metab)) {
if (is.null(comment(metab))) {
comment(metab) <- comment_metab
if (is.null(comment(params@metab))) {
comment(metab) <- "set manually"
} else {
comment(metab) <- comment(params@metab)
}
}
assert_that(is.array(metab),
identical(dim(metab), dim(params@metab)))
Expand Down
17 changes: 6 additions & 11 deletions man/setMetabolicRate.Rd

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

9 changes: 5 additions & 4 deletions man/setParams.Rd

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

46 changes: 26 additions & 20 deletions tests/testthat/test-setMetabolicRate.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
params <- NS_params
no_sp <- nrow(params@species_params)

## setMetabolicRate ----
# setMetabolicRate ----
test_that("setMetabolicRate works", {
expect_identical(setMetabolicRate(params, params@metab,
comment_metab = NULL), params)
params@species_params$ks <- 2 * params@species_params$ks
p2 <- setMetabolicRate(params)
expect_identical(2 * params@metab, p2@metab)
# only metab changed
p2@metab <- params@metab
expect_identical(p2, params)
})
test_that("setMetabolicRate can set exponent p", {
# no change where p is already set in species_params
Expand All @@ -19,32 +19,38 @@ test_that("setMetabolicRate can set exponent p", {
expect_identical(params@species_params$p, c(1, rep(0.7, 11)))
})
test_that("Comment works on metab", {
params <- NS_params
# if no comment, it is set automatically
metab <- params@metab
params <- setMetabolicRate(params, metab = metab)
expect_identical(comment(params@metab), "set manually")

# comment is stored
comment(metab) <- "test"
params <- setMetabolicRate(params, metab = metab)
expect_identical(comment(params@metab), "test")

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

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

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

# getMetabolicRate ----
test_that("getMetabolicRate works", {
p <- setMetabolicRate(params, metab = getMetabolicRate(params),
comment_metab = NULL)
expect_identical(params, p)
expect_identical(getMetabolicRate(NS_params),
NS_params@metab)
})

0 comments on commit f897d4c

Please sign in to comment.