Skip to content

Commit

Permalink
Remove comment arguments from setResource() as proposed in #214
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavdelius committed Aug 24, 2021
1 parent 00a78fb commit 06504ba
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 104 deletions.
2 changes: 1 addition & 1 deletion R/newMultispeciesParams.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ newMultispeciesParams <- function(
#' @inheritDotParams setExtMort -reset
#' @inheritDotParams setReproduction -reset
#' @inheritDotParams setFishing
#' @inheritDotParams setResource
#' @inheritDotParams setResource -reset
#'
#' @return A \linkS4class{MizerParams} object
#'
Expand Down
47 changes: 35 additions & 12 deletions R/setResource.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@
#'
#' @param params A MizerParams object
#' @param resource_rate Optional. Vector of resource intrinsic birth rates
#' @param comment_rate `r lifecycle::badge("experimental")`
#' A string describing how the value for 'resource_rate' was obtained. This is
#' ignored if 'resource_rate' is not supplied or already has a comment
#' attribute.
#' @param resource_capacity Optional. Vector of resource intrinsic carrying
#' capacity
#' @param comment_capacity `r lifecycle::badge("experimental")`
#' A string describing how the value for 'resource_capacity' was obtained. This is
#' ignored if 'resource_capacity' is not supplied or already has a comment
#' attribute.
#' @param reset If set to TRUE, then both `resource_rate` and
#' `resource_capacity` will be reset to the value calculated from the resource
#' parameters, even if they were previously overwritten with custom values. If
#' set to FALSE (default) then a recalculation from the resource parameters
#' will take place only if no custom values have been set.
#' @param r_pp Coefficient of the intrinsic resource birth rate
#' @param n Allometric growth exponent for resource
#' @param kappa Coefficient of the intrinsic resource carrying capacity
Expand All @@ -62,9 +59,8 @@
#' @family functions for setting parameters
setResource <- function(params,
resource_rate = NULL,
comment_rate = "set manually",
resource_capacity = NULL,
comment_capacity = "set manually",
reset = FALSE,
r_pp = resource_params(params)[["r_pp"]],
kappa = resource_params(params)[["kappa"]],
lambda = resource_params(params)[["lambda"]],
Expand All @@ -73,6 +69,7 @@ setResource <- function(params,
resource_dynamics = NULL,
...) {
assert_that(is(params, "MizerParams"),
is.logical(reset),
is.number(kappa), kappa > 0,
is.number(lambda),
is.number(r_pp), r_pp > 0,
Expand All @@ -83,10 +80,32 @@ setResource <- function(params,
params@resource_params[["r_pp"]] <- r_pp
params@resource_params[["n"]] <- n
params@resource_params[["w_pp_cutoff"]] <- w_pp_cutoff

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

# weight specific resource growth rate
if (!is.null(resource_rate)) {
if (is.null(comment(resource_rate))) {
comment(resource_rate) <- comment_rate
if (is.null(comment(params@rr_pp))) {
comment(resource_rate) <- "set manually"
} else {
comment(resource_rate) <- comment(params@rr_pp)
}
}
assert_that(is.numeric(resource_rate),
identical(length(resource_rate), length(params@rr_pp)))
Expand All @@ -105,7 +124,11 @@ setResource <- function(params,
# the resource carrying capacity
if (!is.null(resource_capacity)) {
if (is.null(comment(resource_capacity))) {
comment(resource_capacity) <- comment_capacity
if (is.null(comment(params@cc_pp))) {
comment(resource_capacity) <- "set manually"
} else {
comment(resource_capacity) <- comment(params@cc_pp)
}
}
assert_that(is.numeric(resource_capacity),
identical(length(resource_capacity), length(params@cc_pp)))
Expand Down
33 changes: 0 additions & 33 deletions docs/dev/reference/setParams.html

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

29 changes: 13 additions & 16 deletions docs/dev/reference/setResource.html

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

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

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

17 changes: 6 additions & 11 deletions man/setResource.Rd

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

Loading

0 comments on commit 06504ba

Please sign in to comment.