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..914cb4e --- /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{framdb |> copy_fishery_scalers(132, 133, 87)} +#' + + +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..70cbcb0 --- /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{framdb |> copy_fishery_scalers(132, 133, 87)} + +}