Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace clash #4

Open
cl-roberts opened this issue Mar 14, 2023 · 0 comments
Open

Namespace clash #4

cl-roberts opened this issue Mar 14, 2023 · 0 comments

Comments

@cl-roberts
Copy link
Collaborator

Issue

The function compute.catch.biomass() is created twice; both instances produce the same outputs but take different inputs. In plotting/compute_plot_products.R the function takes a file path as input...

compute.catch.biomass <- function(model.dir, nyr, years){
data <- read.data.files(model.dir)
weight.at.age <- data$waa[1:nyr,]
fb.nya <- data$foodbait_catch[1:nyr,]
pound.nya <- data$pound_catch[1:nyr,]
gillnet.nya <- data$gillnet_catch[1:nyr,]
seine.yield <- data$seine_yield[1:nyr]
fb.nya.biomass <- weight.at.age * fb.nya
pound.nya.biomass <- weight.at.age * pound.nya
gillnet.nya.biomass <- weight.at.age * gillnet.nya
fb.biomass.annual <- rowSums(fb.nya.biomass) # Now sum the biomass over all age classes for each year
pound.biomass.annual <- rowSums(pound.nya.biomass)
gillnet.biomass.annual <- rowSums(gillnet.nya.biomass)
fb.biomass.annual <- replace(fb.biomass.annual, fb.biomass.annual == 0, NA)
pound.biomass.annual <- replace(pound.biomass.annual, pound.biomass.annual == 0, NA)
gillnet.biomass.annual <- replace(gillnet.biomass.annual, gillnet.biomass.annual == 0, NA)
seine.yield <- replace(seine.yield, seine.yield == 0, NA)
# Matrix of catches by gear type in mt
total.catch <- cbind(fb.biomass.annual, pound.biomass.annual, gillnet.biomass.annual, seine.yield)
total.catch[is.na(total.catch)] <- 0
total.catch.biomass <- rowSums(total.catch) # total catches by year in mt
return(total.catch.biomass)
}

...whereas the compute.catch.biomass() created in functions/fun_read_dat.R takes a data object.

compute.catch.biomass <- function(data, nyr){
weight.at.age <- data$waa[1:nyr,]
fb.nya <- data$foodbait_catch[1:nyr,]
pound.nya <- data$pound_catch[1:nyr,]
gillnet.nya <- data$gillnet_catch[1:nyr,]
seine.yield <- data$seine_yield[1:nyr]
fb.nya.biomass <- weight.at.age * fb.nya
pound.nya.biomass <- weight.at.age * pound.nya
gillnet.nya.biomass <- weight.at.age * gillnet.nya
fb.biomass.annual <- rowSums(fb.nya.biomass) # Now sum the biomass over all age classes for each year
pound.biomass.annual <- rowSums(pound.nya.biomass)
gillnet.biomass.annual <- rowSums(gillnet.nya.biomass)
fb.biomass.annual <- replace(fb.biomass.annual, fb.biomass.annual == 0, NA)
pound.biomass.annual <- replace(pound.biomass.annual, pound.biomass.annual == 0, NA)
gillnet.biomass.annual <- replace(gillnet.biomass.annual, gillnet.biomass.annual == 0, NA)
seine.yield <- replace(seine.yield, seine.yield == 0, NA)
# Matrix of catches by gear type in mt
total.catch <- cbind(fb.biomass.annual, pound.biomass.annual, gillnet.biomass.annual, seine.yield)
total.catch[is.na(total.catch)] <- 0
total.catch.biomass <- rowSums(total.catch) # total catches by year in mt
return(total.catch.biomass)
}

This throws in error when executing plotting/plot_management_outputs.R because the former script is sourced in line 4 but then supplies a data object to the function in line 52.

source(file=paste0(here::here("plotting/", "compute_plot_products.R")))

raw.data <- read.data.files(model.dir)
total.catch.biomass <- compute.catch.biomass(raw.data$PWS_ASA.dat, nyr) %>% round(., 2)

Also note that plotting/compute_plot_products.R sources functions/fun_read_dat.R in line 3.

Solution

Resolve namespace clash. I commented out lines 61-92 in plotting/compute_plot_products.R to accommodate the use case in plotting/plot_management_outputs.R.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant