Skip to content
Merged
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
43 changes: 9 additions & 34 deletions R/create_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -586,40 +586,7 @@ create_template <- function(
}
} # close check for previous files & respective copying

# Handle legacy document order and migration
# TODO: Not sure how setting this to rerender = FALSE always impacts this feature
# Maybe this should go through deprecation in x amnt of time?
fig_info <- migrate_legacy_docs(subdir, doc_type = "figures", rerender_skeleton = FALSE)
tbl_info <- migrate_legacy_docs(subdir, doc_type = "tables", rerender_skeleton = FALSE)

renamed_tables_doc <- FALSE
if (can_rename_legacy_doc(tbl_info)) {
from <- fs::path(subdir, tbl_info$legacy_name)
to <- fs::path(subdir, tbl_info$current_name)
if (!identical(from, to) && file.exists(from)) {
renamed_tables_doc <- file.rename(from = from, to = to)
}
}

renamed_figures_doc <- FALSE
if (can_rename_legacy_doc(fig_info)) {
from <- fs::path(subdir, fig_info$legacy_name)
to <- fs::path(subdir, fig_info$current_name)
if (!identical(from, to) && file.exists(from)) {
renamed_figures_doc <- file.rename(from = from, to = to)
}
}

if (renamed_figures_doc || renamed_tables_doc) {
renamed_docs <- c(
if (renamed_figures_doc) paste0("{.file ", fig_info$current_name, "}"),
if (renamed_tables_doc) paste0("{.file ", tbl_info$current_name, "}")
)
cli::cli_alert_info("Detected legacy figure/table document order in the skeleton.")
cli::cli_alert_info("asar switched to {toString(renamed_docs)}.")
}

# Created tables doc
# Create tables doc
tables_doc_name <- switch(
type,
"nemt" = "06_tables.qmd",
Expand Down Expand Up @@ -651,6 +618,14 @@ create_template <- function(
to = fs::path(subdir, figures_doc_name)
)
}

# rename tables doc
Comment thread
sbreitbart-NOAA marked this conversation as resolved.
if (tables_doc_name != "09_tables.qmd") {
file.rename(
from = fs::path(subdir, "09_tables.qmd"),
to = fs::path(subdir, tables_doc_name)
)
}

# Part I
# Create a report template file to render for the region and species
Expand Down
31 changes: 31 additions & 0 deletions R/rerender_skeleton.R
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,35 @@ rerender_skeleton <- function(
}
# Print message
cli::cli_alert_success("Updated report skeleton in directory {file_dir}.")


# Handle legacy document order and migration
fig_doc <- list.files(file_dir, pattern = "figures.qmd")
tbl_doc <- list.files(file_dir, pattern = "tables.qmd")

# if the number preceding the figures.qmd is higher than the number preceding the tables.qmd, then rename the files to match the new order
fig_num <- as.numeric(stringr::str_extract(fig_doc, "(?<=^)[0-9]+"))
tbl_num <- as.numeric(stringr::str_extract(tbl_doc, "(?<=^)[0-9]+"))

if (!is.na(fig_num) && !is.na(tbl_num) && fig_num > tbl_num) {
cli::cli_alert_info("Detected legacy figure/table document order in the skeleton.")

# Rename the files to match the new order
file.rename(
from = fs::path(file_dir, fig_doc),
to = fs::path(file_dir,
paste0(stringi::stri_pad_left(tbl_num, 2, "0"),
"_figures.qmd")
)
)
file.rename(
from = fs::path(file_dir, tbl_doc),
to = fs::path(file_dir,
paste0(stringi::stri_pad_left(fig_num, 2, "0"),
"_tables.qmd")
)
)
cli::cli_alert_success("Changed order to match the new skeleton.")

}
}
Loading