From 5cc7e60f3dc3c3681d82bef5d01104ab1951c177 Mon Sep 17 00:00:00 2001 From: Ty Garber Date: Mon, 4 Nov 2024 08:53:11 -0800 Subject: [PATCH 1/4] run copier --- R/copy.R | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 2 deletions(-) diff --git a/R/copy.R b/R/copy.R index 0cac428..8180b8a 100644 --- a/R/copy.R +++ b/R/copy.R @@ -8,8 +8,6 @@ #' @examples #' \dontrun{framdb |> copy_fishery_scalers(132, 133, 87)} #' - - copy_fishery_scalers <- function(fram_db, from_run, to_run, fishery_id = NULL){ validate_fram_db(fram_db) validate_run_id(fram_db, c(to_run, from_run)) @@ -88,3 +86,136 @@ copy_fishery_scalers <- function(fram_db, from_run, to_run, fishery_id = NULL){ } + +#' Copies a run a number of times +#' @param fram_db FRAM database object +#' @param target_run Run ID to be copied from +#' @param times Number of copies +#' @export +#' @examples +#' \dontrun{framdb |> copy_run(target_run = 141, times = 1) +#' +copy_run <- function(fram_db, target_run, times = 1){ + + + # target_run = 139 + # times = 15 + run_table <- DBI::dbReadTable(fram_db$fram_db_connection, 'RunID') + + max_run_id <- run_table |> + dplyr::pull(.data$RunID) |> + max() + + # collect tables + stock_recruit <- DBI::dbGetQuery(fram_db$fram_db_connection, + glue::glue( + "SELECT * FROM StockRecruit + WHERE RunID = {target_run} + ")) + + fishery_scalers <- DBI::dbGetQuery(fram_db$fram_db_connection, + glue::glue( + "SELECT * FROM FisheryScalers + WHERE RunID = {target_run} + ")) + + sfrs <- DBI::dbGetQuery(fram_db$fram_db_connection, + glue::glue( + "SELECT * FROM StockFisheryRateScaler + WHERE RunID = {target_run} + ")) + + non_retention <- DBI::dbGetQuery(fram_db$fram_db_connection, + glue::glue( + "SELECT * FROM NonRetention + WHERE RunID = {target_run} + ")) + + run_target <- run_table |> + dplyr::filter(.data$RunID == .env$target_run) + + + cli::cli_progress_bar(total = times, + format = 'Copying run {i}/{times} | {cli::pb_bar} {cli::pb_percent} {cli::pb_eta}' + ) + + for(i in seq_along(1:times)) { + # RunID Table + run_insert <- run_target |> + dplyr::mutate( + RunID = .env$max_run_id + .env$i + ) |> + dplyr::select(-.data$PrimaryKey) + + + # send to db + DBI::dbAppendTable(fram_db$fram_db_connection, + name = 'RunID', + value = run_insert, + batch_rows = 1) + + + # stock recruit table + stock_recruit_insert <- stock_recruit |> + dplyr::mutate( + RunID = .env$max_run_id + .env$i + ) |> + dplyr::select(-.data$PrimaryKey) + + # send to db + DBI::dbAppendTable(fram_db$fram_db_connection, + name = 'StockRecruit', + value = stock_recruit_insert, + batch_rows = 1) + + # fishery scalers + fishery_scalers_insert <- fishery_scalers |> + dplyr::mutate( + RunID = .env$max_run_id + .env$i + ) + + # send to db + DBI::dbAppendTable(fram_db$fram_db_connection, + name = 'FisheryScalers', + value = fishery_scalers_insert, + batch_rows = 1) + + # stock fishery rate scalers + if(nrow(sfrs) > 0){ + sfrs_insert <- sfrs |> + dplyr::mutate( + RunID = .env$max_run_id + .env$i + ) + + # send to db + DBI::dbAppendTable(fram_db$fram_db_connection, + name = 'StockFisheryRate Scaler', + value = sfrs_insert, + batch_rows = 1) + } + + + # non retention + non_retention_insert <- non_retention |> + dplyr::mutate( + RunID = .env$max_run_id + .env$i + ) + + # send to db + DBI::dbAppendTable(fram_db$fram_db_connection, + name = 'NonRetention', + value = non_retention_insert, + batch_rows = 1) + + + + cli::cli_progress_update() + } + + cli::cli_process_done() +} + + + + + From 85e8b9cc3f42e1740bdc24b0f0ddd5da6d93187f Mon Sep 17 00:00:00 2001 From: Ty Garber Date: Mon, 4 Nov 2024 08:55:16 -0800 Subject: [PATCH 2/4] docs --- NAMESPACE | 1 + R/copy.R | 2 +- framrsquared.Rproj | 1 + man/copy_run.Rd | 22 ++++++++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 man/copy_run.Rd diff --git a/NAMESPACE b/NAMESPACE index 9bd242f..1c651f4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ export(compare_runs) export(compare_stock_fishery_rate_scalers) export(connect_fram_db) export(copy_fishery_scalers) +export(copy_run) export(disconnect_fram_db) export(error_check_code) export(fetch_quarto_templates) diff --git a/R/copy.R b/R/copy.R index 8180b8a..8f99401 100644 --- a/R/copy.R +++ b/R/copy.R @@ -93,7 +93,7 @@ copy_fishery_scalers <- function(fram_db, from_run, to_run, fishery_id = NULL){ #' @param times Number of copies #' @export #' @examples -#' \dontrun{framdb |> copy_run(target_run = 141, times = 1) +#' \dontrun{framdb |> copy_run(target_run = 141, times = 1)} #' copy_run <- function(fram_db, target_run, times = 1){ diff --git a/framrsquared.Rproj b/framrsquared.Rproj index 497f8bf..270314b 100644 --- a/framrsquared.Rproj +++ b/framrsquared.Rproj @@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/man/copy_run.Rd b/man/copy_run.Rd new file mode 100644 index 0000000..eba8955 --- /dev/null +++ b/man/copy_run.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/copy.R +\name{copy_run} +\alias{copy_run} +\title{Copies a run a number of times} +\usage{ +copy_run(fram_db, target_run, times = 1) +} +\arguments{ +\item{fram_db}{FRAM database object} + +\item{target_run}{Run ID to be copied from} + +\item{times}{Number of copies} +} +\description{ +Copies a run a number of times +} +\examples{ +\dontrun{framdb |> copy_run(target_run = 141, times = 1)} + +} From 0da4b864ae7bf7f61a0e7efb0a6043a1b194a0c3 Mon Sep 17 00:00:00 2001 From: Ty Garber Date: Mon, 4 Nov 2024 09:48:33 -0800 Subject: [PATCH 3/4] fixing chinook runs --- R/copy.R | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/R/copy.R b/R/copy.R index 8f99401..c33c519 100644 --- a/R/copy.R +++ b/R/copy.R @@ -95,11 +95,11 @@ copy_fishery_scalers <- function(fram_db, from_run, to_run, fishery_id = NULL){ #' @examples #' \dontrun{framdb |> copy_run(target_run = 141, times = 1)} #' -copy_run <- function(fram_db, target_run, times = 1){ - +copy_run <- function(fram_db, target_run, times = 1, identifier = 'copy'){ # target_run = 139 - # times = 15 + # times = 1 + run_table <- DBI::dbReadTable(fram_db$fram_db_connection, 'RunID') max_run_id <- run_table |> @@ -131,6 +131,14 @@ copy_run <- function(fram_db, target_run, times = 1){ WHERE RunID = {target_run} ")) + if(fram_db$fram_db_species == 'CHINOOK') { + size_limits <- DBI::dbGetQuery(fram_db$fram_db_connection, + glue::glue( + "SELECT * FROM SizeLimits + WHERE RunID = {target_run} + ")) + } + run_target <- run_table |> dplyr::filter(.data$RunID == .env$target_run) @@ -143,7 +151,8 @@ copy_run <- function(fram_db, target_run, times = 1){ # RunID Table run_insert <- run_target |> dplyr::mutate( - RunID = .env$max_run_id + .env$i + RunID = .env$max_run_id + .env$i, + RunName = glue::glue(RunName, ' {identifier} {i}') ) |> dplyr::select(-.data$PrimaryKey) @@ -189,7 +198,7 @@ copy_run <- function(fram_db, target_run, times = 1){ # send to db DBI::dbAppendTable(fram_db$fram_db_connection, - name = 'StockFisheryRate Scaler', + name = 'StockFisheryRateScaler', value = sfrs_insert, batch_rows = 1) } @@ -207,6 +216,21 @@ copy_run <- function(fram_db, target_run, times = 1){ value = non_retention_insert, batch_rows = 1) + if (fram_db$fram_db_species == 'CHINOOK') { + size_limits_insert <- size_limits |> + dplyr::mutate(RunID = .env$max_run_id + .env$i) |> + dplyr::select(-.data$PrimaryKey) + + + # send to db + DBI::dbAppendTable( + fram_db$fram_db_connection, + name = 'SizeLimits', + value = size_limits_insert, + batch_rows = 1 + ) + + } cli::cli_progress_update() From ff73347438b61bf1217521059af79a6530a4ad16 Mon Sep 17 00:00:00 2001 From: Ty Garber Date: Mon, 4 Nov 2024 09:54:51 -0800 Subject: [PATCH 4/4] working out bugs --- R/copy.R | 5 +++-- man/copy_run.Rd | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/R/copy.R b/R/copy.R index c33c519..db76bfb 100644 --- a/R/copy.R +++ b/R/copy.R @@ -91,11 +91,12 @@ copy_fishery_scalers <- function(fram_db, from_run, to_run, fishery_id = NULL){ #' @param fram_db FRAM database object #' @param target_run Run ID to be copied from #' @param times Number of copies +#' @param label Label of each copy e.g. copy 1, copy 2 #' @export #' @examples #' \dontrun{framdb |> copy_run(target_run = 141, times = 1)} #' -copy_run <- function(fram_db, target_run, times = 1, identifier = 'copy'){ +copy_run <- function(fram_db, target_run, times = 1, label = 'copy'){ # target_run = 139 # times = 1 @@ -152,7 +153,7 @@ copy_run <- function(fram_db, target_run, times = 1, identifier = 'copy'){ run_insert <- run_target |> dplyr::mutate( RunID = .env$max_run_id + .env$i, - RunName = glue::glue(RunName, ' {identifier} {i}') + RunName = glue::glue(.data$RunName, ' {label} {i}') ) |> dplyr::select(-.data$PrimaryKey) diff --git a/man/copy_run.Rd b/man/copy_run.Rd index eba8955..8138574 100644 --- a/man/copy_run.Rd +++ b/man/copy_run.Rd @@ -4,7 +4,7 @@ \alias{copy_run} \title{Copies a run a number of times} \usage{ -copy_run(fram_db, target_run, times = 1) +copy_run(fram_db, target_run, times = 1, label = "copy") } \arguments{ \item{fram_db}{FRAM database object} @@ -12,6 +12,8 @@ copy_run(fram_db, target_run, times = 1) \item{target_run}{Run ID to be copied from} \item{times}{Number of copies} + +\item{label}{Label of each copy e.g. copy 1, copy 2} } \description{ Copies a run a number of times