Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub area models #91

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
34 changes: 31 additions & 3 deletions R/es_table_tex.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#' @param save_loc optional input that requires a full path which will
#' allow users to save the tex file to a specific location (e.g. inside doc folder)
#' other than the \code{file.path(dir, table_folder)}. Default NULL
#' @param add_prefix Optional input that should be continous text if used that
#' allows users to append a specific identifier to the tex table labels and the
#' created tex files save the tex file. This option supports the ability to create
#' a joint executive summary for a stock with multiple sub-area models. Default NULL
#' @param csv_name CSV file name to create tex table scripts for. This file should have
#' the following columns: caption, altcaption, label, filenem, loc (optional) where caption will
#' be the table caption, altcapation is the accessibility text for the table (can be
Expand All @@ -35,11 +39,23 @@
#' save_loc = "C:/model/tex_tables",
#' csv_name = "non_es_document_tables.csv"
#' )
#'
#' #example of creating tex files for those created by the
#' # r4ss function SSexecutivesummary for a sub-area model
#' es_table_tex(dir = mod_loc)
#' #example of using a user created csv file:
#' es_table_tex(dir = "C:/models/model_to_reference",
#' save_loc = "C:/model/tex_tables",
#' add_prefix = "oregon",
#' csv_name = "table_labels.csv")
#'
#' }
#' @export
es_table_tex <- function(dir,
table_folder = NULL,
add_prefix = NULL,
save_loc = NULL,
add_text = NULL,
csv_name = "table_labels.csv") {
# Function to round data-frame with characters and numeric values
round_df <- function(df, digits) {
Expand Down Expand Up @@ -81,12 +97,18 @@ es_table_tex <- function(dir,
if (is.character(tab[, 1])) {
tab[, 1] <- gsub("\\_", " ", tab[, 1])
}

if(!is.null(add_prefix)) {
label <- paste0(add_prefix, "-", df$label[i])
} else {
label <- df$label[i]
}

if (col_names[1] == "Year") {
t <- table_format(
x = as.data.frame(tab),
caption = df$caption[i],
label = df$label[i],
label = label,
digits = c(0, rep(2, n)),
longtable = TRUE,
col_names = col_names,
Expand All @@ -96,15 +118,21 @@ es_table_tex <- function(dir,
t <- table_format(
x = round_df(df = data.frame(tab), digits = 2),
caption = df$caption[i],
label = df$label[i],
label = label,
longtable = TRUE,
col_names = col_names,
align = c("r", rep("c", n))
)
}

if(!is.null(add_prefix)) {
save_name <- paste0(add_prefix, "_", tex_name, ".tex")
} else {
save_name <- paste0(tex_name, ".tex")
}

kableExtra::save_kable(t,
file = file.path(save_loc, paste0(tex_name, ".tex"))
file = file.path(save_loc, save_name)
)
}
}
21 changes: 16 additions & 5 deletions R/read_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
#' @param plotfolder A file path relative to `mod_loc` where
#' the figures will be saved. The argument is passed to `printfolder` in
#' [r4ss::SS_plots()]. The default is `plots`.
#' @param add_prefix Optional input that should be continous text if used that
#' allows users to append a specific identifier to the tex table labels and the
#' created tex files save the tex file. This option supports the ability to create
#' a joint executive summary for a stock with multiple sub-area models. Default NULL
#' @param add_text Optional input that should be text and can have spaces. Any input will
#' be incorporated into table captions created for the executive summary using r4ss:SSexecutivesummary
#' function. For example, add_text = "South of Point Conception" will be appended to all captions
#' to indicate the sub-area model. Default NULL.
#' @param printstats `r lifecycle::badge("deprecated")`
#' `printstats = FALSE` is no longer supported;
#' this function will always suppress printing output to the screen with
Expand Down Expand Up @@ -109,8 +117,10 @@
read_model <- function(mod_loc,
save_loc = NULL,
plotfolder = "plots",
add_prefix = NULL,
add_text = NULL,
printstats = lifecycle::deprecated(),
fecund_mult = "million eggs",
fecund_mult = "millions of eggs",
create_plots = TRUE,
png = lifecycle::deprecated(),
html = lifecycle::deprecated(),
Expand Down Expand Up @@ -189,8 +199,7 @@ read_model <- function(mod_loc,
}
model <- r4ss::SS_output(
dir = mod_loc,
printstats = FALSE, verbose = FALSE,
...
printstats = FALSE, verbose = FALSE
)

plot_dir <- file.path(mod_loc, plotfolder)
Expand Down Expand Up @@ -219,7 +228,7 @@ read_model <- function(mod_loc,
bio_target <- fore$Btarget
msst <- ifelse(bio_target == 0.40, 0.25, 0.125)

# Determine the minumum summary age
# Determine the minimum summary age
starter <- r4ss::SS_readstarter(file = file.path(mod_loc, "starter.ss"), verbose = FALSE)
min_sum_age <- paste0(starter$min_age_summary_bio, "+")

Expand Down Expand Up @@ -247,6 +256,8 @@ read_model <- function(mod_loc,
if (!is.null(tables)) {
r4ss::SSexecutivesummary(
replist = model,
add_text = add_text,
so_units = fecund_unit,
ci_value = ci_value,
es_only = es_only,
tables = tables,
Expand All @@ -265,7 +276,7 @@ read_model <- function(mod_loc,

dir.create(file.path(mod_loc, "tex_tables"), showWarnings = FALSE)
dir.create(save_loc, showWarnings = FALSE)
es_table_tex(dir = mod_loc, table_folder = "tables", save_loc = save_loc)
es_table_tex(dir = mod_loc, add_prefix = add_prefix, table_folder = "tables", save_loc = save_loc)

save(mod_loc,
plot_dir,
Expand Down
17 changes: 17 additions & 0 deletions man/es_table_tex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions man/read_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.