Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/save_all_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ save_all_plots <- function(
# imported from plot_landings
landings_unit_label = "mt",
# imported from plot_recruitment_deviations- zero unique arguments
# imported from plot_stock_recruitment
# imported from plot_stock_recruitment
spawning_biomass_label = "mt",
spawning_biomass_scale_amount = 1,
# imported from plot_spawning_biomass
Expand Down
56 changes: 28 additions & 28 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ check_year <- function(
# Sourced from r4ss
get_ncol <- function(file, skip = 0) {
nummax <- max(utils::count.fields(file,
skip = skip, quote = "",
comment.char = ""
skip = skip, quote = "",
comment.char = ""
)) + 1
nummax
}
Expand Down Expand Up @@ -87,41 +87,41 @@ SS3_extract_df <- function(dat, label) {
if (!inherits(dat, "data.table")) {
dat <- data.table::as.data.table(dat)
}

# Identify all values to be treated as NA
na_values <- c("", "-", "#")

# Find the row indices using a vectorized approach
start_row <- which(apply(dat, 1, function(row) any(row == label)))[2]

# If the label is not found or is not the second instance, return NA
if (is.na(start_row)) {
cli::cli_alert_warning("Label not found in data frame or less than two instances were found.")
return(NA)
}

# Find the next blank row after the starting row
next_blank_rows <- which(apply(dat, 1, function(row) all(is.na(row) | row %in% na_values)))

# Find the first blank row that appears after the start_row
end_row <- next_blank_rows[which(next_blank_rows > start_row)[1]]
if (is.na(end_row) || length(end_row) == 0) {
end_row <- nrow(dat)
}

# Extract the subset
clean_dt <- data.table::as.data.table(dat[start_row:(end_row - 1), ])

# Efficiently replace specified values with NA
for (j in names(clean_dt)) {
data.table::set(clean_dt, i = which(clean_dt[[j]] %in% na_values), j = j, value = NA)
}

# Identify columns to remove and subset the data.table
# This is the key optimization for removing NA columns
cols_to_keep <- which(sapply(clean_dt, function(x) !all(is.na(x))))
clean_dt <- clean_dt |> dplyr::select(dplyr::all_of(c(names(cols_to_keep))))

# Return data
as.data.frame(clean_dt)
}
Expand All @@ -138,10 +138,10 @@ SS3_extract_fleet <- function(dat, vers) {
}
# Locate the row containing the specified value from the df
value_row <- switch(i,
"1" = which(apply(dat, 1, function(row) any(row == "Fleet")))[1],
"2" = which(apply(dat, 1, function(row) any(row == "Fleet_name")))[1]
"1" = which(apply(dat, 1, function(row) any(row == "Fleet")))[1],
"2" = which(apply(dat, 1, function(row) any(row == "Fleet_name")))[1]
)

# If the parameter value is not found, return NA
if (is.na(value_row)) {
cli::cli_alert_warning("Label not found in data frame.")
Expand All @@ -154,25 +154,25 @@ SS3_extract_fleet <- function(dat, vers) {
}
# Combine the rows surrounding the selected metric from the output table
rows <- c(value_row, next_blank)

# Extract the metric using the rows from above as a guide and clean up empty columns
clean_df <- dat[rows[1]:(rows[2] - 1), ] |>
naniar::replace_with_na_all(condition = ~ .x == "")
fleets <- switch(i,
"1" = {
fleet_names <- Filter(function(x) !all(is.na(x)), clean_df)[-1, 9]$X9
fleet_length <- seq_along(fleet_names)
names(fleet_names) <- fleet_length
fleet_length
},
{
clean_df <- dat[rows[1]:(rows[2] - 1), ] |>
naniar::replace_with_na_all(condition = ~ .x == "")
fleet_info <- Filter(function(x) !all(is.na(x)), clean_df)[-1, ]
stats::setNames(fleet_info[[ncol(fleet_info)]], fleet_info[[1]])
}
"1" = {
fleet_names <- Filter(function(x) !all(is.na(x)), clean_df)[-1, 9]$X9
fleet_length <- seq_along(fleet_names)
names(fleet_names) <- fleet_length
fleet_length
},
{
clean_df <- dat[rows[1]:(rows[2] - 1), ] |>
naniar::replace_with_na_all(condition = ~ .x == "")
fleet_info <- Filter(function(x) !all(is.na(x)), clean_df)[-1, ]
stats::setNames(fleet_info[[ncol(fleet_info)]], fleet_info[[1]])
}
)

if (any(grepl("[0-9]+", fleets))) {
# Set fleets to another object to use as names of new vector
# This follows the same naming convention of i = 2
Expand Down