From 8632f5efb0ef96e621702567df3779adea4f8cc1 Mon Sep 17 00:00:00 2001 From: Ty Garber Date: Fri, 22 Mar 2024 13:09:20 -0700 Subject: [PATCH 1/2] copy fishery scalers function --- NAMESPACE | 1 + R/copy.R | 64 +++++++++++++++++++++++++++++++++++++ man/copy_fishery_scalers.Rd | 26 +++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 R/copy.R create mode 100644 man/copy_fishery_scalers.Rd diff --git a/NAMESPACE b/NAMESPACE index 72b439e..7ba36db 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,7 @@ export(compare_inputs) export(compare_inputs_chart) export(compare_runs) export(connect_fram_db) +export(copy_fishery_scalers) export(disconnect_fram_db) export(fetch_table) export(filter_ak) diff --git a/R/copy.R b/R/copy.R new file mode 100644 index 0000000..9c178b2 --- /dev/null +++ b/R/copy.R @@ -0,0 +1,64 @@ +#' Experimental copying scaler inputs from +#' one run to another DANGEROUS +#' @param fram_db FRAM database object +#' @param from_run Run ID to be copied from +#' @param to_run Run ID to be copied to +#' @param fishery_id Specific fishery's scalers to be copied +#' @export +#' @examples +#' \dontrun{truns <- truns_stocks(fram_db)} +#' + + +copy_fishery_scalers <- function(fram_db, from_run, to_run, fishery_id = NULL){ + if (is.null(fishery_id)) { + cli::cli_alert_warning('A fishery ID is not set, this will copy all the fishery scalers!') + input <- tolower(readline(prompt = ('Continue? (y/n): '))) + if (input != 'y') { + stop('Aborting') + } + } + + copy_scalers <- fram_db |> + fetch_table('FisheryScalers') |> + dplyr::filter(.data$run_id == .env$from_run) + + if (!is.null(fishery_id)) { + copy_scalers <- + copy_scalers |> dplyr::filter(.data$fishery_id == .env$fishery_id) + } + + updated_inputs <- copy_scalers |> + dplyr::rowwise() |> + dplyr::mutate(rows_affected = DBI::dbExecute( + fram_db$fram_db_connection, + glue::glue( + 'UPDATE FisheryScalers + SET FisheryFlag = {fishery_flag}, + FisheryScaleFactor = {fishery_scale_factor}, + Quota = {quota}, + MSFFisheryScaleFactor = {msf_fishery_scale_factor}, + MSFQuota = {msf_quota}, + MarkReleaseRate = {mark_release_rate}, + MarkMisIDRate = {mark_mis_id_rate}, + UnMarkMisIDRate = {un_mark_mis_id_rate}, + MarkIncidentalRate = {mark_incidental_rate} + WHERE RunID = {.env$to_run} AND + TimeStep = {time_step} AND + FisheryID = {fishery_id};' + ) + )) + + if (nrow(updated_inputs |> dplyr::filter(.data$rows_affected == 0)) > 0) { + cli::cli_alert_warning('Some rows were not changed.') + updated_inputs |> dplyr::filter(.data$rows_affected == 0) + } else if (nrow(updated_inputs |> dplyr::filter(.data$rows_affected > 1 )) > 0) { + cli::cli_alert_danger('Multiple rows were effected by one query... DON\'T USE') + updated_inputs |> dplyr::filter(.data$rows_affected > 1) + } else { + rows <- updated_inputs |> dplyr::filter(.data$rows_affected == 1) |> nrow() + cli::cli_alert_success('Successfully updated {rows} row{?s}') + } + + +} diff --git a/man/copy_fishery_scalers.Rd b/man/copy_fishery_scalers.Rd new file mode 100644 index 0000000..be0932f --- /dev/null +++ b/man/copy_fishery_scalers.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/copy.R +\name{copy_fishery_scalers} +\alias{copy_fishery_scalers} +\title{Experimental copying scaler inputs from +one run to another DANGEROUS} +\usage{ +copy_fishery_scalers(fram_db, from_run, to_run, fishery_id = NULL) +} +\arguments{ +\item{fram_db}{FRAM database object} + +\item{from_run}{Run ID to be copied from} + +\item{to_run}{Run ID to be copied to} + +\item{fishery_id}{Specific fishery's scalers to be copied} +} +\description{ +Experimental copying scaler inputs from +one run to another DANGEROUS +} +\examples{ +\dontrun{truns <- truns_stocks(fram_db)} + +} From f727092aaea4251403b06cc825a411db8cce8d51 Mon Sep 17 00:00:00 2001 From: Ty Garber Date: Wed, 3 Apr 2024 09:17:36 -0700 Subject: [PATCH 2/2] fix to docs --- R/copy.R | 2 +- man/copy_fishery_scalers.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/copy.R b/R/copy.R index 9c178b2..914cb4e 100644 --- a/R/copy.R +++ b/R/copy.R @@ -6,7 +6,7 @@ #' @param fishery_id Specific fishery's scalers to be copied #' @export #' @examples -#' \dontrun{truns <- truns_stocks(fram_db)} +#' \dontrun{framdb |> copy_fishery_scalers(132, 133, 87)} #' diff --git a/man/copy_fishery_scalers.Rd b/man/copy_fishery_scalers.Rd index be0932f..70cbcb0 100644 --- a/man/copy_fishery_scalers.Rd +++ b/man/copy_fishery_scalers.Rd @@ -21,6 +21,6 @@ Experimental copying scaler inputs from one run to another DANGEROUS } \examples{ -\dontrun{truns <- truns_stocks(fram_db)} +\dontrun{framdb |> copy_fishery_scalers(132, 133, 87)} }