Skip to content

Commit

Permalink
refactor: move to cli package
Browse files Browse the repository at this point in the history
  • Loading branch information
chantelwetzel-noaa committed Nov 5, 2024
1 parent 52b604e commit e393f4e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
8 changes: 6 additions & 2 deletions R/GetN.fn.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ GetN.fn <- function(
}

if (verbose) {
message("\nThe effN sample size is calculated using the ", species, " multiplier of ", n.unq, ". This number is multiplied by the number of tows in each year.\n")
cli::cli_alert_info(
"The effN sample size is calculated using the {species} multiplier of {n.unq}.
This number is multiplied by the number of tows in each year.")
}


Expand Down Expand Up @@ -135,7 +137,9 @@ GetN.fn <- function(

if (sum(ind) > 0) {
if (verbose) {
cat("\nInput sample size exceded the number of fish for", yr[ind], "and has been capped equal to number of fish.\n")
cli::cli_alert_info(
"Input sample size exceded the number of fish for {yr[ind]} and has been
capped equal to number of fish.")
}
}

Expand Down
71 changes: 53 additions & 18 deletions R/SexRatio.fn.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ SexRatio.fn <- function(
maxSizeUnsexed,
bins = NULL,
verbose = TRUE) {

cli::cli_alert_warning(
"Applying sex ratios is no longer best practice. Please consider turning off
sex ratio application in SurveyLFs.fn() or using get_expanded_comps()."
)
if (sexRatioStage == 1) {
# incorporate unsexed fish using sex ratios
if (length(sexRatioUnsexed) == 1 & !is.na(sexRatioUnsexed)) {
if (verbose) {
message("Sex ratio for unsexed fish being applied to the expanded numbers within a tow (stage 1) when possible.
cli::cli_alert_infor("Sex ratio for unsexed fish being applied to the expanded numbers within a tow (stage 1) when possible.
If no data within a tow for bin then the sex ratio for the bin across all years applied to unsexed fish.
If no data for that bin across all years then the sex ratio for nearby bins was applied to unsexed fish.\n")
If no data for that bin across all years then the sex ratio for nearby bins was applied to unsexed fish.")
}

x$sexRatio <- x$expF / (x$expF + x$expM)
Expand All @@ -104,25 +109,38 @@ SexRatio.fn <- function(
check <- round(length(noRatio) / length(x$sexRatio), 3)
if (check > 0.10) {
if (verbose) {
message("\n There are", check, "percent of tows with observations that the sex ratio will be filled based on other tows.
Consider increasing the maxSizeUnsexed or create the comps as unsexed.\n")
cli::cli_alert_info(
"There are {check} percent of tows with observations that the sex ratio
will be filled based on other tows. Consider increasing the maxSizeUnsexed
or create the comps as unsexed.")
}
if (length(noRatio) > 0) {
cli::cli_alert_info(
"These are sex ratios that were filled in using observations from the
same lengths from different strata and years:"
)
}
if (length(noRatio) > 0) cat("\nThese are sex ratios that were filled in using observations from the same lengths from different strata and years:\n")
}
for (i in noRatio) {
inds <- x$allLs == x$allLs[i]
tmpF <- sum(x$expF[inds])
tmpM <- sum(x$expM[inds])
x$sexRatio[i] <- tmpF / (tmpF + tmpM)
if (verbose) {
message(cat("LengthAge:", x[i, c("Length_cm")], "Bin:", x[i, c("allLs")], "Sex Ratio:", x[i, c("sexRatio")]))
len_check <- x[i, "Length_cm"]
bin_check <- x[i, "allLs"]
sex_ratio_check <- x[i, "sexRatio"]
cli::cli_alert_info(
"LengthAge: {len_check}, Bin: {bin_check}, Sex Ratio: {sex_ratio_check}")
}
}

noRatio <- which(is.na(x$sexRatio))
if (length(noRatio) > 0) {
if (verbose) {
message("\nThese are sex ratios that were filled in using observations from nearby lengths\n")
cli::cli_alert_info(
"These are sex ratios that were filled in using observations from
nearby lengths.")
}
}

Expand All @@ -133,18 +151,22 @@ SexRatio.fn <- function(
tmpM <- sum(x$expM[inds])
x$sexRatio[i] <- tmpF / (tmpF + tmpM)
if (verbose) {
message(cat("Length/Age:", x[i, c("Length_cm")], "Bin:", x[i, c("allLs")], "Sex Ratio:", x[i, c("sexRatio")]))
len_check <- x[i, "Length_cm"]
bin_check <- x[i, "allLs"]
sex_ratio_check <- x[i, "sexRatio"]
cli::cli_alert_info(
"Length/Age: {len_check}, Bin: {bin_check}, Sex Ratio: {sex_ratio_check}")
}
}
noRatio <- which(is.na(x$sexRatio))
if (length(noRatio) > 0) {
if (verbose) {
message("Some sex ratios were left unknown and omitted\n\n")
cli::cli_alert_info("Some sex ratios were left unknown and omitted")
}
}
if (length(noRatio) == 0) {
if (verbose) {
message("Done filling in sex ratios\n\n")
cli::cli_alert_info("Done filling in sex ratios")
}
}

Expand All @@ -157,8 +179,9 @@ SexRatio.fn <- function(

if (sexRatioStage == 2) {
if (verbose) {
message("Sex ratio for unsexed fish being applied to the expanded numbers within a strata and year (stage 2).
If no data within a strata and year for bin then the sex ratio for the bin across all years and strata applied to unsexed fish.\n")
cli::cli_alert_info(
"Sex ratio for unsexed fish being applied to the expanded numbers within a strata and year (stage 2).
If no data within a strata and year for bin then the sex ratio for the bin across all years and strata applied to unsexed fish.")
}
# Take everything out of the list into a dataframe
out <- NULL
Expand Down Expand Up @@ -189,14 +212,18 @@ SexRatio.fn <- function(
check <- round(length(noRatio) / length(out$sexRatio), 3)
if (check > 0.10) {
if (verbose) {
message("\n There are", check, "percent of tows with observations that the sex ratio will be filled based on other tows.
Consider increasing the maxSizeUnsexed or create the comps as unsexed.\n")
cli::cli_alert_info(
"There are {check} percent of tows with observations that the sex ratio
will be filled based on other tows. Consider increasing the maxSizeUnsexed
or create the comps as unsexed.")
}
}

if (length(noRatio) > 0) {
if (verbose) {
("\nThese are sex ratios that were filled in using observations from the same lengths from different strata and years\n")
cli::cli_alert_info(
"These are sex ratios that were filled in using observations from the
same lengths from different strata and years.")
}
}
for (i in noRatio) {
Expand All @@ -205,15 +232,20 @@ SexRatio.fn <- function(
tmpM <- sum(out$TotalLjhM[inds])
out$sexRatio[i] <- tmpF / (tmpF + tmpM)
if (verbose) {
message(cat("Length/Age:", out[i, "LENGTH"], "Sex Ratio:", round(out[i, "sexRatio"], 3)))
len_check <- out[i, "LENGTH"]
sex_ratio_check <- round(out[i, "sexRatio"], 3)
cli::cli_alert_info(
"Length/Age: {len_check}, Sex Ratio: {sex_ratio_check}")
}
}

# Calculate the ratio based upon near lengths
noRatio <- which(is.na(out$sexRatio))
if (length(noRatio) > 0) {
if (verbose) {
message("\nThese are sex ratios that were filled in using observations from nearby lengths\n")
cli::cli_alert_info(
"These are sex ratios that were filled in using observations from nearby
lengths.")
}
for (i in noRatio) {
unq.len <- sort(unique(out$LENGTH))
Expand All @@ -228,7 +260,10 @@ SexRatio.fn <- function(
tmpM <- sum(out$TotalLjhM[nearLens])
out$sexRatio[i] <- tmpF / (tmpF + tmpM)
if (verbose) {
message(cat("Length/Age:", out[i, "LENGTH"], "Sex Ratio:", round(out[i, "sexRatio"], 3)))
len_check <- out[i, "LENGTH"]
sex_ratio_check <- round(out[i, "sexRatio"], 3)
cli::cli_alert_info(
"Length/Age: {len_check}, Sex Ratio: {sex_ratio_check}")
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions R/save_rdata.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ save_rdata <- function(x,
)

if (verbose) {
message(
glue::glue(
"{type_message} data file saved to following location:
{dir}"
)
cli::cli_alert_info(
"{type_message} data file saved to following location: {dir}"
)
}
}

0 comments on commit e393f4e

Please sign in to comment.