Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0e87178
Create function that checks for duplicated chunk labels in fig/table …
sbreitbart-NOAA May 28, 2026
99eea88
Update tests
sbreitbart-NOAA May 28, 2026
52836c5
add check for current figures and remove from loop if present already…
Schiano-NOAA Jun 16, 2026
9653ebe
remove test and add comment for duplicate chunks
Schiano-NOAA Jun 16, 2026
e7e98c2
add check and adjust conditions for messages when checking for previo…
Schiano-NOAA Jun 17, 2026
2d7b6d8
adjust the condition when the set up chunk gets added
Schiano-NOAA Jun 17, 2026
0dd3fd4
adjust create_figures_docs to work and pass checks
Schiano-NOAA Jun 22, 2026
2b3b253
change message when figures/tables doc doesn't have any objects to add
Schiano-NOAA Jun 22, 2026
a430d8f
update messages when new figures are added vs initial creation with figs
Schiano-NOAA Jun 22, 2026
3d30b54
add new test for duplicate figs
Schiano-NOAA Jun 22, 2026
adb3b18
comment out dup chunk check since no longer applies and add TODO; add…
Schiano-NOAA Jun 22, 2026
05d0a5e
update create_tables_doc to reflect the same changes done for create_…
Schiano-NOAA Jun 22, 2026
f348455
correct tests and comment out moot test
Schiano-NOAA Jun 22, 2026
9fdedca
make new test testing capability of create_tables_doc adding another …
Schiano-NOAA Jun 22, 2026
0c256ad
adjust test to create snapshot instead of expect equal
Schiano-NOAA Jun 22, 2026
be9c145
move create_tab_chunks into utils
Schiano-NOAA Jun 22, 2026
1f9ae42
move create_fig_chunks out of fxn and into utils
Schiano-NOAA Jun 22, 2026
d9b8c65
Revert "move create_fig_chunks out of fxn and into utils"
Schiano-NOAA Jun 23, 2026
616e7e4
Revert "move create_tab_chunks into utils"
Schiano-NOAA Jun 23, 2026
f860db7
update tests for figures and tables docs fxns to create snapshot and …
Schiano-NOAA Jun 24, 2026
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
107 changes: 66 additions & 41 deletions R/create_figures_doc.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ create_figures_doc <- function(subdir = getwd(),
updated_content <- gsub(empty_doc_text, "", figure_content, fixed = TRUE)
writeLines(updated_content, existing_figs_doc)
}
} else {
# existing_figs_doc <- NULL
figure_content <- ""
}

figures_doc_header <- ifelse(append,
Expand All @@ -44,14 +47,23 @@ create_figures_doc <- function(subdir = getwd(),
)

# add chunk that creates object as the directory of all rdas
figures_doc_setup <- paste0(
add_chunk(
glue::glue("figures_dir <- fs::path('{figures_dir}', 'figures')"),
label = "set-rda-dir-figs"
),
"\n"
)

# check if the current setup already has the setup chunk
if (!(any(grepl(
"#| label: 'set-rda-dir-figs'",
figure_content,
fixed = TRUE
)))) {
figures_doc_setup <- paste0(
add_chunk(
glue::glue("figures_dir <- fs::path('{figures_dir}', 'figures')"),
label = "set-rda-dir-figs"
),
"\n"
)
} else {
figures_doc_setup <- ""
}

figures_doc <- ""

# list all files in figures
Expand All @@ -61,6 +73,37 @@ create_figures_doc <- function(subdir = getwd(),
rda_fig_list <- file_list[grepl("_figure.rda", file_list)]
# create sublist of only non-rda figure files
non.rda_fig_list <- file_list[!grepl(".rda", file_list)]

# Check if rda or non-rda already exists and remove from list
new_rda <- FALSE
new_non.rda <- FALSE
if (length(file.path(subdir, list.files(subdir, pattern = "figures.qmd"))) == 1) {
existing_figs_doc <- file.path(subdir, list.files(subdir, pattern = "figures.qmd"))
figure_content <- readLines(existing_figs_doc) |>
suppressWarnings()
# find all instances of figures
existing_rda_figs <- vapply(rda_fig_list, function(x) {
any(grepl(x, figure_content, fixed = TRUE))
}, FUN.VALUE = logical(1))
rda_fig_list <- rda_fig_list[!existing_rda_figs]
# add condition for message to add "new" into message
new_rda <- ifelse(
length(existing_rda_figs) > 0,
TRUE,
FALSE
)
# find instances of non-rda and remove
existing_non.rda_figs <- vapply(non.rda_fig_list, function(x) {
any(grepl(x, figure_content, fixed = TRUE))
}, FUN.VALUE = logical(1))
non.rda_fig_list <- non.rda_fig_list[!existing_non.rda_figs]
# add condition for message to add "new" into message
new_non.rda <- ifelse(
length(existing_non.rda_figs) > 0,
TRUE,
FALSE
)
}

# create two-chunk system to plot each rda figure
create_fig_chunks <- function(fig = NA,
Expand Down Expand Up @@ -119,7 +162,7 @@ rm(rda)\n
cli::cli_alert_warning("Found zero figure files in {fs::path(figures_dir, 'figures')}.",
wrap = TRUE
)
cli::cli_alert_info("For `create_figures_doc` to run properly, there must be:",
cli::cli_alert_info("For `create_figures_doc` to incorporate figures, there must be:",
wrap = TRUE
)
cli::cli_ol(c(
Expand All @@ -133,7 +176,7 @@ rm(rda)\n
} else {
# paste rda figure code chunks into one object
if (length(rda_fig_list) > 0) {
cli::cli_alert_success("Found {length(rda_fig_list)} figure{?s} in an rda format (i.e., .rda) in {fs::path(figures_dir, 'figures')}.",
cli::cli_alert_success("Found {length(rda_fig_list)}{ifelse(new_rda, ' new ', ' ')}figure{?s} in an rda format (i.e., .rda) in {fs::path(figures_dir, 'figures')}.",
wrap = TRUE
)
rda_figures_doc <- ""
Expand All @@ -154,7 +197,7 @@ rm(rda)\n
)
}
if (length(non.rda_fig_list) > 0) {
cli::cli_alert_success("Found {length(non.rda_fig_list)} figure{?s} in a non-rda format (e.g., .jpg, .png) in {fs::path(figures_dir, 'figures')}.",
cli::cli_alert_success("Found {length(non.rda_fig_list)}{ifelse(new_non.rda, ' new ', ' ')}figure{?s} in a non-rda format (e.g., .jpg, .png) in {fs::path(figures_dir, 'figures')}.",
wrap = TRUE
)
non.rda_figures_doc <- ""
Expand Down Expand Up @@ -188,6 +231,7 @@ rm(rda)\n
# combine figures_doc setup with figure chunks
figures_doc <- paste0(
figures_doc_header,
# ifelse(!append, figures_doc_setup, ""),
figures_doc_setup,
ifelse(
exists("rda_figures_doc"),
Expand Down Expand Up @@ -215,33 +259,14 @@ rm(rda)\n
)

# Read through figures doc and warn about identical labels
new_figs_doc <- readLines(
ifelse(
any(grepl("_figures.qmd$", list.files(subdir))),
fs::path(subdir, list.files(subdir)[grep("_figures.qmd", list.files(subdir))]),
fs::path(subdir, "09_figures.qmd")
)
) |>
suppressWarnings() |>
as.list()

label_line_nums <- grep("\\label", new_figs_doc)
labels <- new_figs_doc[label_line_nums]
names(labels) <- label_line_nums
labels <- lapply(labels, function(x) {
gsub("#\\| label: ", "", x)
})

repeated_labels <- labels[duplicated(labels)]
repeated_labels <- as.vector(unlist(repeated_labels))

if (length(repeated_labels) > 0) {
cli::cli_alert_danger("Figures doc contains chunks with identical labels: {repeated_labels}.")
cli::cli_alert_info("Open figures doc and check for:")
cli::cli_bullets(c(
"*" = "Identical, repeated figures",
"*" = "Different figures with identical labels"
))
cli::cli_alert_warning("Figures doc will not render if chunks have identical labels.")
}
}
doc_path <- ifelse(
any(grepl("_figures.qmd$", list.files(subdir))),
fs::path(subdir, list.files(subdir)[grep("_figures.qmd", list.files(subdir))]),
fs::path(subdir, "09_figures.qmd")
)

fix_duplicate_chunks(
doc_path = doc_path,
doc_type = "Figures"
)
}
135 changes: 76 additions & 59 deletions R/create_tables_doc.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ create_tables_doc <- function(subdir = getwd(),
updated_content <- gsub(empty_doc_text, "", table_content, fixed = TRUE)
writeLines(updated_content, existing_tables_doc)
}
} else {
# existing_figs_doc <- NULL
table_content <- ""
}

# add header
Expand All @@ -74,22 +77,30 @@ create_tables_doc <- function(subdir = getwd(),
)

# add chunk that creates object as the directory of all rdas
tables_doc_setup <- paste0(
add_chunk(
glue::glue(
"library(gt)
if (!(any(grepl(
"#| label: 'set-rda-dir-tbls'",
table_content,
fixed = TRUE
)))) {
tables_doc_setup <- paste0(
add_chunk(
glue::glue(
"library(gt)
tables_dir <- fs::path('{tables_dir}', 'tables')"
),
label = "set-rda-dir-tbls",
# add_option = TRUE,
chunk_option = c(
"echo: false",
"warning: false",
"include: false"
)
),
label = "set-rda-dir-tbls",
# add_option = TRUE,
chunk_option = c(
"echo: false",
"warning: false",
"include: false"
)
),
"\n"
)
"\n"
)
} else {
tables_doc_setup <- ""
}

tables_doc <- ""

Expand All @@ -98,6 +109,26 @@ create_tables_doc <- function(subdir = getwd(),

# create sublist of only rda table files
rda_tab_list <- file_list[grepl(".rda", file_list)]

# Check if rda already exists and remove from list
# Check if rda or non-rda already exists and remove from list
new_rda <- FALSE
if (length(file.path(subdir, list.files(subdir, pattern = "tables.qmd"))) == 1) {
existing_tbls_doc <- file.path(subdir, list.files(subdir, pattern = "tables.qmd"))
table_content <- readLines(existing_tbls_doc) |>
suppressWarnings()
# find all instances of figures
existing_rda_tabs <- vapply(rda_tab_list, function(x) {
any(grepl(x, table_content, fixed = TRUE))
}, FUN.VALUE = logical(1))
rda_tab_list <- rda_tab_list[!existing_rda_tabs]
# add condition for message to add "new" into message
new_rda <- ifelse(
length(existing_rda_tabs) > 0,
TRUE,
FALSE
)
}

# remove rda table files that have an associated "split" version
# remove "_split" from filenames
Expand Down Expand Up @@ -474,22 +505,26 @@ load(file.path(tables_dir, '", stringr::str_remove(tab, "_split"), "'))\n
}

if (length(rda_tab_list) == 0) {
cli::cli_alert_warning("Found zero tables in an rda format (i.e., .rda) in {fs::path(tables_dir, 'tables')}.",
wrap = TRUE
)
cli::cli_alert_info("For `create_tables_doc` to run properly, there must be:",
wrap = TRUE
)
cli::cli_ol(c(
"a 'tables' folder in {fs::path(tables_dir)}",
".rda files in the 'tables' folder"
))
tables_doc <- paste0(
tables_doc_header,
empty_doc_text
)
if (length(file.path(subdir, list.files(subdir, pattern = "tables.qmd"))) != 1) {
cli::cli_alert_warning("Found zero tables in an rda format (i.e., .rda) in {fs::path(tables_dir, 'tables')}.",
wrap = TRUE
)
cli::cli_alert_info("For `create_tables_doc` to incorporate tables, there must be:",
wrap = TRUE
)
cli::cli_ol(c(
"a 'tables' folder in {fs::path(tables_dir)}",
".rda files in the 'tables' folder"
))
tables_doc <- paste0(
tables_doc_header,
empty_doc_text
)
} else {
cli::cli_alert("No new tables detected.")
}
} else {
cli::cli_alert_success("Found {length(final_rda_tab_list)} table{?s} in an rda format (i.e., .rda) in {fs::path(tables_dir, 'tables')}.",
cli::cli_alert_success("Found {length(final_rda_tab_list)}{ifelse(new_rda, ' new ', ' ')}table{?s} in an rda format (i.e., .rda) in {fs::path(tables_dir, 'tables')}.",
wrap = TRUE
)
# paste rda table code chunks into one object
Expand Down Expand Up @@ -554,33 +589,15 @@ load(file.path(tables_dir, '", stringr::str_remove(tab, "_split"), "'))\n
)

# Read through tables doc and warn about identical labels
new_tables_doc <- readLines(
ifelse(
any(grepl("_tables.qmd$", list.files(subdir))),
fs::path(subdir, list.files(subdir)[grep("_tables.qmd", list.files(subdir))]),
fs::path(subdir, "08_tables.qmd")
)
) |>
suppressWarnings() |>
as.list()

label_line_nums <- grep("\\label", new_tables_doc)
labels <- new_tables_doc[label_line_nums]
names(labels) <- label_line_nums
labels <- lapply(labels, function(x) {
gsub("#\\| label: ", "", x)
})

repeated_labels <- labels[duplicated(labels)]
repeated_labels <- as.vector(unlist(repeated_labels))

if (length(repeated_labels) > 0) {
cli::cli_alert_danger("Tables doc contains chunks with identical labels: {repeated_labels}.")
cli::cli_alert_info("Open tables doc and check for:")
cli::cli_bullets(c(
"*" = "Identical, repeated tables",
"*" = "Different tables with identical labels"
))
cli::cli_alert_warning("Tables doc will not render if chunks have identical labels.")
}
}
doc_path <- ifelse(
any(grepl("_tables.qmd$", list.files(subdir))),
fs::path(subdir, list.files(subdir)[grep("_tables.qmd", list.files(subdir))]),
fs::path(subdir, "08_tables.qmd")
)

fix_duplicate_chunks(
doc_path = doc_path,
doc_type = "Tables"
)

}
Loading
Loading