diff --git a/R/create_figures_doc.R b/R/create_figures_doc.R index f22ee745..99e4c3d6 100644 --- a/R/create_figures_doc.R +++ b/R/create_figures_doc.R @@ -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, @@ -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 @@ -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, @@ -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( @@ -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 <- "" @@ -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 <- "" @@ -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"), @@ -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" + ) +} \ No newline at end of file diff --git a/R/create_tables_doc.R b/R/create_tables_doc.R index 903f9c1e..da6b42ea 100644 --- a/R/create_tables_doc.R +++ b/R/create_tables_doc.R @@ -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 @@ -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 <- "" @@ -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 @@ -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 @@ -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" + ) + +} \ No newline at end of file diff --git a/R/utils.R b/R/utils.R index 6caa2385..0eb5d20b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -372,3 +372,102 @@ gt_split <- function( gt_group } + +#----Fix figures/tables docs with duplicate chunks---- +fix_duplicate_chunks <- function(doc_path, + doc_type){ + + new_figs_doc <- readLines(doc_path) |> + 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_warning("{doc_type} doc contains chunks with identical labels: {repeated_labels}.") + cli::cli_alert_warning("{doc_type} doc will not render if chunks have identical labels.") + cli::cli_alert_info("The duplicate chunks will be commented out.") + + in_chunk <- FALSE + current_chunk_start <- NA + current_chunk_label <- NULL + + chunks_list <- list() + + for (i in seq_along(new_figs_doc)) { + line <- new_figs_doc[i] + + # get code chunk start + if (!in_chunk && grepl("^\\s*```\\s*\\{[a-zA-Z]", line)) { + in_chunk <- TRUE + current_chunk_start <- i + current_chunk_label <- NULL + + # Handle inline label format: e.g., ```{r my-label} + inline_match <- regmatches(line, regexec("^\\s*```\\s*\\{[a-zA-Z]+\\s+([^, }]+)", line))[[1]] + if (length(inline_match) > 1) { + current_chunk_label <- trimws(inline_match[2]) + } + } + # get label + else if (in_chunk && is.null(current_chunk_label) && grepl("^\\s*#\\|\\s*label:", line)) { + # extract everything after "label:" + label_match <- regmatches(line, regexec("^\\s*#\\|\\s*label:\\s*(.*)", line))[[1]] + if (length(label_match) > 1) { + current_chunk_label <- trimws(label_match[2]) + # strip outer quotes + current_chunk_label <- gsub("^['\"]|['\"]$", "", current_chunk_label) + } + } + # find end of code chunk + else if (in_chunk && grepl("^\\s*```\\s*$", line)) { + in_chunk <- FALSE + + # get chunk label + if (!is.null(current_chunk_label)) { + chunks_list[[length(chunks_list) + 1]] <- tidyr::tibble( + start = current_chunk_start, + end = i, + label = current_chunk_label + ) + } + } + } + + # Combine list elements into a df + chunks_df <- dplyr::bind_rows(chunks_list) + + # Find duplicate occurrences + duplicates <- chunks_df |> + dplyr::group_by(label) |> + dplyr::mutate(occurrence = dplyr::row_number()) |> + dplyr::ungroup() |> + dplyr::filter(occurrence > 1) |> + dplyr::arrange(dplyr::desc(start)) + + modified_doc <- new_figs_doc + + # Loop over duplicate chunks and comment out every line in range + for (row_idx in seq_len(nrow(duplicates))) { + label <- duplicates$label[row_idx] + start <- duplicates$start[row_idx] + end <- duplicates$end[row_idx] + + cli::cli_alert_info(sprintf("Commenting out duplicate chunk '%s' (Lines %d to %d)", label, start, end)) + + # comment out each line + modified_doc[start:end] <- paste0("") + } + + writeLines(as.character(unlist(modified_doc)), doc_path) + cli::cli_alert_success(sprintf("Successfully resolved %d duplicate chunk label issues in {doc_type} doc.", nrow(duplicates))) + } +} \ No newline at end of file diff --git a/tests/testthat/_snaps/create_figures_doc.md b/tests/testthat/_snaps/create_figures_doc.md new file mode 100644 index 00000000..62475c0f --- /dev/null +++ b/tests/testthat/_snaps/create_figures_doc.md @@ -0,0 +1,65 @@ +# Adds new figure from figures folder. + + Code + cat(fc_pasted) + Output + # Figures {#sec-figures} + + #| warnings: false + #| eval: true + # load rda + load(file.path(figures_dir, 'biomass_figure.rda')) + + # save rda with plot-specific name + biomass_plot_rda <- rda + + # remove generic rda object + rm(rda) + + # save figure, caption, and alt text as separate objects + biomass_plot <- biomass_plot_rda$figure + biomass_cap <- biomass_plot_rda$caption + biomass_alt_text <- biomass_plot_rda$alt_text + ``` + + ```{r} + #| label: 'fig-biomass' + #| echo: false + #| warning: false + #| fig-cap: !expr biomass_cap + #| fig-alt: !expr biomass_alt_text + biomass_plot + ``` + + {{< pagebreak >}} + + ```{r} + #| label: 'fig-abundance_at_age-setup' + #| warnings: false + #| eval: true + # load rda + load(file.path(figures_dir, 'abundance_at_age_figure.rda')) + + # save rda with plot-specific name + abundance_at_age_plot_rda <- rda + + # remove generic rda object + rm(rda) + + # save figure, caption, and alt text as separate objects + abundance_at_age_plot <- abundance_at_age_plot_rda$figure + abundance_at_age_cap <- abundance_at_age_plot_rda$caption + abundance_at_age_alt_text <- abundance_at_age_plot_rda$alt_text + ``` + + ```{r} + #| label: 'fig-abundance_at_age' + #| echo: false + #| warning: false + #| fig-cap: !expr abundance_at_age_cap + #| fig-alt: !expr abundance_at_age_alt_text + abundance_at_age_plot + ``` + + {{< pagebreak >}} + diff --git a/tests/testthat/_snaps/create_tables_doc.md b/tests/testthat/_snaps/create_tables_doc.md new file mode 100644 index 00000000..5b48e160 --- /dev/null +++ b/tests/testthat/_snaps/create_tables_doc.md @@ -0,0 +1,293 @@ +# Adds new table from tables folder. + + Code + cat(fc_pasted) + Output + # Tables {#sec-tables} + + ```{r} + #| label: 'tab-landings-setup' + #| warnings: false + #| eval: true + # load rda + load(file.path(tables_dir, 'landings_table.rda')) + + # save rda with table-specific name + landings_table_rda <- rda + + # save table and caption as separate objects + landings_table <- landings_table_rda$table + landings_cap <- landings_table_rda$caption + ``` + + ::: {.landscape} + + ```{r} + #| label: 'tbl-landings1' + #| echo: false + #| tbl-cap: !expr paste0(landings_cap, ' (1 of 6)') + #| tbl-pos: 't' + # plot table 1 + landings_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(1) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-landings2' + #| echo: false + #| tbl-cap: !expr paste0(landings_cap, ' (2 of 6)') + #| tbl-pos: 't' + # plot table 2 + landings_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(2) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-landings3' + #| echo: false + #| tbl-cap: !expr paste0(landings_cap, ' (3 of 6)') + #| tbl-pos: 't' + # plot table 3 + landings_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(3) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-landings4' + #| echo: false + #| tbl-cap: !expr paste0(landings_cap, ' (4 of 6)') + #| tbl-pos: 't' + # plot table 4 + landings_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(4) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-landings5' + #| echo: false + #| tbl-cap: !expr paste0(landings_cap, ' (5 of 6)') + #| tbl-pos: 't' + # plot table 5 + landings_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(5) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-landings6' + #| echo: false + #| tbl-cap: !expr paste0(landings_cap, ' (6 of 6)') + #| tbl-pos: 't' + # plot table 6 + landings_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(6) + + ``` + ::: + {{< pagebreak >}} + + ```{r} + #| label: 'tab-land-setup' + #| warnings: false + #| eval: true + # load rda + load(file.path(tables_dir, 'land_table.rda')) + + # save rda with table-specific name + land_table_rda <- rda + + # save table and caption as separate objects + land_table <- land_table_rda$table + land_cap <- land_table_rda$caption + ``` + + ::: {.landscape} + + ```{r} + #| label: 'tbl-land1' + #| echo: false + #| tbl-cap: !expr paste0(land_cap, ' (1 of 6)') + #| tbl-pos: 't' + # plot table 1 + land_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(1) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-land2' + #| echo: false + #| tbl-cap: !expr paste0(land_cap, ' (2 of 6)') + #| tbl-pos: 't' + # plot table 2 + land_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(2) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-land3' + #| echo: false + #| tbl-cap: !expr paste0(land_cap, ' (3 of 6)') + #| tbl-pos: 't' + # plot table 3 + land_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(3) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-land4' + #| echo: false + #| tbl-cap: !expr paste0(land_cap, ' (4 of 6)') + #| tbl-pos: 't' + # plot table 4 + land_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(4) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-land5' + #| echo: false + #| tbl-cap: !expr paste0(land_cap, ' (5 of 6)') + #| tbl-pos: 't' + # plot table 5 + land_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(5) + + ``` + ::: + ::: {.landscape} + + ```{r} + #| label: 'tbl-land6' + #| echo: false + #| tbl-cap: !expr paste0(land_cap, ' (6 of 6)') + #| tbl-pos: 't' + # plot table 6 + land_table |> + gt::tab_options( + table.width = pct(100), + table.layout = 'auto' + ) |> + gt::cols_width( + everything() ~ pct(20) + ) |> + asar::gt_split(row_every_n = 28) |> + gt::grp_pull(6) + + ``` + ::: + {{< pagebreak >}} + diff --git a/tests/testthat/out_new.rda b/tests/testthat/out_new.rda new file mode 100644 index 00000000..cd680945 Binary files /dev/null and b/tests/testthat/out_new.rda differ diff --git a/tests/testthat/test-create_figures_doc.R b/tests/testthat/test-create_figures_doc.R index 0f3d4a7d..adaed641 100644 --- a/tests/testthat/test-create_figures_doc.R +++ b/tests/testthat/test-create_figures_doc.R @@ -28,14 +28,8 @@ test_that("Creates expected start of nearly empty figures doc", { }) test_that("Creates expected start of figures doc with figure", { - # load sample dataset - load(file.path( - "fixtures", "ss3_models_converted", "Hake_2018", - "std_output.rda" - )) - - stockplotr::plot_biomass( - dat = out_new, + stockplotr::plot_biomass( + dat = stockplotr::example_data, make_rda = TRUE, module = "TIME_SERIES" ) @@ -73,14 +67,8 @@ test_that("Formerly empty figures doc renders correctly", { # create empty figures doc create_template() - # load sample dataset - load(file.path( - "fixtures", "ss3_models_converted", "Hake_2018", - "std_output.rda" - )) - stockplotr::plot_biomass( - dat = out_new, + dat = stockplotr::example_data, make_rda = TRUE, module = "TIME_SERIES" ) @@ -108,43 +96,80 @@ test_that("Formerly empty figures doc renders correctly", { ) # erase temporary testing files - file.remove(fs::path(getwd(), "09_figures.qmd")) file.remove(fs::path(getwd(), "captions_alt_text.csv")) file.remove(fs::path(getwd(), "key_quantities.csv")) unlink(fs::path(getwd(), "figures"), recursive = T) unlink(fs::path(getwd(), "report"), recursive = T) }) -test_that("Throws warning if chunks with identical labels", { - # load sample dataset - load(file.path( - "fixtures", "ss3_models_converted", "Hake_2018", - "std_output.rda" - )) - +# TODO: update test and find condition where chunks might have identical labels +# test_that("Throws warning if chunks with identical labels", { +# stockplotr::plot_biomass( +# dat = stockplotr::example_data, +# make_rda = TRUE, +# module = "TIME_SERIES" +# ) +# +# # create figures doc +# create_figures_doc( +# subdir = getwd(), +# figures_dir = getwd() +# ) +# +# expect_message( +# create_figures_doc( +# subdir = getwd(), +# figures_dir = getwd() +# ), +# "Figures doc contains chunks with identical labels:" +# ) +# +# # erase temporary testing files +# file.remove(fs::path(getwd(), "09_figures.qmd")) +# file.remove(fs::path(getwd(), "captions_alt_text.csv")) +# file.remove(fs::path(getwd(), "key_quantities.csv")) +# unlink(fs::path(getwd(), "figures"), recursive = T) +# }) + +# DO NOT RERUN MANUALLY -- snapshot path will not be correct if it adjusts and test will fail +test_that("Adds new figure from figures folder.", { + # Create one figure stockplotr::plot_biomass( - dat = out_new, + dat = stockplotr::example_data, make_rda = TRUE, module = "TIME_SERIES" ) - - # create figures doc + + create_template() + + # Make another figure + stockplotr::plot_abundance_at_age( + dat = stockplotr::example_data, + make_rda = TRUE + ) + + # rerender figures doc, appending new figure create_figures_doc( - subdir = getwd(), + subdir = file.path(getwd(), "report"), figures_dir = getwd() ) - - expect_message( - create_figures_doc( - subdir = getwd(), - figures_dir = getwd() - ), - "Figures doc will not render if chunks have identical labels." + + # read in figures doc + figure_content <- readLines(file.path(getwd(), "report", "09_figures.qmd")) + # Remove the first lines so test doesn't test path differences + # Note: you CAN NOT test rendering with this approach + figure_content <- figure_content[-c(3:11)] + # remove line numbers and collapse + fc_pasted <- paste(figure_content, collapse = "\n") + + # test expectation of start of figures doc + expect_snapshot( + cat(fc_pasted) ) - + # erase temporary testing files - file.remove(fs::path(getwd(), "09_figures.qmd")) file.remove(fs::path(getwd(), "captions_alt_text.csv")) file.remove(fs::path(getwd(), "key_quantities.csv")) unlink(fs::path(getwd(), "figures"), recursive = T) + unlink(fs::path(getwd(), "report"), recursive = T) }) diff --git a/tests/testthat/test-create_tables_doc.R b/tests/testthat/test-create_tables_doc.R index 7a097702..9c7860d2 100644 --- a/tests/testthat/test-create_tables_doc.R +++ b/tests/testthat/test-create_tables_doc.R @@ -27,120 +27,158 @@ test_that("Creates expected start of nearly empty tables doc", { file.remove(fs::path(getwd(), "08_tables.qmd")) }) -# test_that("Creates expected start of tables doc with table", { -# # load sample dataset -# load(file.path( -# "fixtures", "ss3_models_converted", "Hake_2018", -# "std_output.rda" -# )) -# -# stockplotr::table_landings(out_new, -# make_rda = TRUE -# ) -# -# # create tables doc -# create_tables_doc( -# subdir = getwd(), -# tables_dir = getwd() -# ) -# -# # read in tables doc -# table_content <- readLines("08_tables.qmd") -# # extract first 7 lines -# head_table_content <- head(table_content, 7) -# # remove line numbers and collapse -# fc_pasted <- paste(head_table_content, collapse = "") -# -# # expected tables doc head -# expected_head_table_content <- "# Tables {#sec-tables} ```{r} #| label: 'set-rda-dir-tbls'#| echo: false #| warning: false #| include: false" -# -# # test expectation of start of tables doc -# testthat::expect_equal( -# fc_pasted, -# expected_head_table_content -# ) -# -# # erase temporary testing files -# file.remove(fs::path(getwd(), "08_tables.qmd")) -# file.remove(fs::path(getwd(), "captions_alt_text.csv")) -# file.remove(fs::path(getwd(), "key_quantities.csv")) -# unlink(fs::path(getwd(), "tables"), recursive = T) -# }) -# +test_that("Creates expected start of tables doc with table", { + stockplotr::table_landings( + stockplotr::example_data, + make_rda = TRUE, + interactive = FALSE + ) + + # create tables doc + create_tables_doc( + subdir = getwd(), + tables_dir = getwd() + ) + + # read in tables doc + table_content <- readLines("08_tables.qmd") + # extract first 7 lines + head_table_content <- head(table_content, 7) + # remove line numbers and collapse + fc_pasted <- paste(head_table_content, collapse = "") + + # expected tables doc head + expected_head_table_content <- "# Tables {#sec-tables} ```{r} #| label: 'set-rda-dir-tbls'#| echo: false #| warning: false #| include: false" + + # test expectation of start of tables doc + testthat::expect_equal( + fc_pasted, + expected_head_table_content + ) + + # erase temporary testing files + file.remove(fs::path(getwd(), "08_tables.qmd")) + file.remove(fs::path(getwd(), "captions_alt_text.csv")) + file.remove(fs::path(getwd(), "key_quantities.csv")) + unlink(fs::path(getwd(), "tables"), recursive = T) +}) + +# moot bc new checks account for this +# not sure when this might happen # test_that("Throws warning if chunks with identical labels", { -# # load sample dataset -# load(file.path( -# "fixtures", "ss3_models_converted", "Hake_2018", -# "std_output.rda" -# )) -# -# stockplotr::table_landings(out_new, -# make_rda = TRUE +# stockplotr::table_landings( +# stockplotr::example_data, +# make_rda = TRUE, +# interactive = FALSE # ) -# +# # # create tables doc # create_tables_doc( # subdir = getwd(), # tables_dir = getwd() # ) -# +# # expect_message( # create_tables_doc( # subdir = getwd(), # tables_dir = getwd() # ), -# "Tables doc will not render if chunks have identical labels." -# ) -# -# # erase temporary testing files -# file.remove(fs::path(getwd(), "08_tables.qmd")) -# file.remove(fs::path(getwd(), "captions_alt_text.csv")) -# file.remove(fs::path(getwd(), "key_quantities.csv")) -# unlink(fs::path(getwd(), "tables"), recursive = T) -# }) -# -# test_that("Formerly empty tables doc renders correctly", { -# # create empty tables doc -# create_template() -# -# # load sample dataset -# load(file.path( -# "fixtures", "ss3_models_converted", "Hake_2018", -# "std_output.rda" -# )) -# -# stockplotr::table_landings( -# dat = out_new, -# make_rda = TRUE, -# module = "CATCH" -# ) -# -# # rerender tables doc, appending new table -# create_tables_doc( -# subdir = file.path(getwd(), "report"), -# tables_dir = getwd() +# "Tables doc contains chunks with identical labels:" # ) -# -# # read in tables doc -# table_content <- readLines(file.path(getwd(), "report", "08_tables.qmd")) -# # extract first 8 lines -# head_table_content <- head(table_content, 8) -# # remove line numbers and collapse -# fc_pasted <- paste(head_table_content, collapse = "") -# -# # expected tables doc head -# expected_head_table_content <- "# Tables {#sec-tables} ```{r} #| label: 'set-rda-dir-tbls'#| echo: false #| warning: false #| include: false" -# -# # test expectation of start of tables doc -# expect_equal( -# fc_pasted, -# expected_head_table_content -# ) -# +# # # erase temporary testing files # file.remove(fs::path(getwd(), "08_tables.qmd")) # file.remove(fs::path(getwd(), "captions_alt_text.csv")) # file.remove(fs::path(getwd(), "key_quantities.csv")) # unlink(fs::path(getwd(), "tables"), recursive = T) -# unlink(fs::path(getwd(), "report"), recursive = T) # }) + +test_that("Formerly empty tables doc renders correctly", { + # create empty tables doc + create_template() + + # create example table + stockplotr::table_landings( + dat = stockplotr::example_data, + interactive = FALSE, + make_rda = TRUE, + module = "CATCH" + ) + + # rerender tables doc, appending new table + create_tables_doc( + subdir = file.path(getwd(), "report"), + tables_dir = getwd() + ) + + # read in tables doc + table_content <- readLines(file.path(getwd(), "report", "08_tables.qmd")) + # extract first 8 lines + head_table_content <- head(table_content, 8) + # remove line numbers and collapse + fc_pasted <- paste(head_table_content, collapse = "") + + # expected tables doc head + expected_head_table_content <- "# Tables {#sec-tables} ```{r} #| label: 'set-rda-dir-tbls'#| echo: false #| warning: false #| include: falselibrary(gt)" + + # test expectation of start of tables doc + expect_equal( + fc_pasted, + expected_head_table_content + ) + + # erase temporary testing files + # file.remove(fs::path(getwd(), "08_tables.qmd")) + file.remove(fs::path(getwd(), "captions_alt_text.csv")) + file.remove(fs::path(getwd(), "key_quantities.csv")) + unlink(fs::path(getwd(), "tables"), recursive = T) + unlink(fs::path(getwd(), "report"), recursive = T) +}) + +test_that("Adds new table from tables folder.", { + # Create one figure + stockplotr::table_landings( + dat = stockplotr::example_data, + make_rda = TRUE, + module = "CATCH" + ) + + create_template() + + # Make another table + # stockplotr::table_index( + # dat = stockplotr::example_data, + # make_rda = TRUE + # ) + # temporarily create a copy of landings table + # add above index table once ready + file.copy( + from = file.path(getwd(), "tables", "landings_table.rda"), + to = file.path(getwd(), "tables", "land_table.rda") + ) + + # rerender figures doc, appending new figure + create_tables_doc( + subdir = file.path(getwd(), "report"), + tables_dir = getwd() + ) + + # read in figures doc + table_content <- readLines(file.path(getwd(), "report", "08_tables.qmd")) + # Remove the first lines so test doesn't test path differences + # Note: you CAN NOT test rendering with this approach + table_content <- table_content[-c(3:11)] + # remove line numbers and collapse + fc_pasted <- paste(table_content, collapse = "\n") + + # test expectation of start of figures doc + expect_snapshot( + cat(fc_pasted) + ) + + # erase temporary testing files + file.remove(fs::path("captions_alt_text.csv")) + file.remove(fs::path("key_quantities.csv")) + unlink(fs::path("tables"), recursive = T) + unlink(fs::path("report"), recursive = T) +})