Skip to content

Namespace clash #4

@cl-roberts

Description

@cl-roberts

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions