From e012553cc25de91ef8e8c1a23ffd01cbed62be18 Mon Sep 17 00:00:00 2001 From: Joe Thorley Date: Tue, 27 Oct 2026 04:53:35 -0700 Subject: [PATCH] feat: Copy table and plot .md files into the report folder Co-Authored-By: Claude Opus 5.5 --- R/figures.R | 1 + R/tables.R | 1 + tests/testthat/test-figures.R | 31 ++++++++++++++++--- tests/testthat/test-tables.R | 58 +++++++++++++++++++++++++++++------ 4 files changed, 77 insertions(+), 14 deletions(-) diff --git a/R/figures.R b/R/figures.R index f4b6597..1540934 100644 --- a/R/figures.R +++ b/R/figures.R @@ -99,6 +99,7 @@ sbr_figures <- function( transfer_files(plots, report = report, ext = "csv") transfer_files(plots, report = report, ext = "yaml") transfer_files(plots, report = report, ext = "xlsx") + transfer_files(plots, report = report, ext = "md") plots <- transfer_files(plots, report = report, ext = "png") } colnames(windows)[1] <- "plots" diff --git a/R/tables.R b/R/tables.R index bfe64b3..f5f8162 100644 --- a/R/tables.R +++ b/R/tables.R @@ -97,6 +97,7 @@ sbr_tables <- function( } transfer_files(data, report = report, ext = "yaml") + transfer_files(data, report = report, ext = "md") data <- write_files(data, report = report, ext = ".csv", fun = write_csv) data <- sort_sub(data, sort = sort) diff --git a/tests/testthat/test-figures.R b/tests/testthat/test-figures.R index e6836ad..473c7de 100644 --- a/tests/testthat/test-figures.R +++ b/tests/testthat/test-figures.R @@ -17,7 +17,13 @@ test_that("figures", { ) expect_identical( sort(list.files(sbr_get_report(), recursive = TRUE)), - sort(c("plots/x.csv", "plots/x.png", "plots/x.xlsx", "plots/x.yaml")) + sort(c( + "plots/x.csv", + "plots/x.md", + "plots/x.png", + "plots/x.xlsx", + "plots/x.yaml" + )) ) skip("opens window") @@ -153,7 +159,13 @@ test_that("figures pre_num", { ) expect_identical( sort(list.files(sbr_get_report(), recursive = TRUE)), - sort(c("plots/x.csv", "plots/x.png", "plots/x.xlsx", "plots/x.yaml")) + sort(c( + "plots/x.csv", + "plots/x.md", + "plots/x.png", + "plots/x.xlsx", + "plots/x.yaml" + )) ) txt <- sbr_figures() expect_match( @@ -162,7 +174,13 @@ test_that("figures pre_num", { ) expect_identical( sort(list.files(sbr_get_report(), recursive = TRUE)), - sort(c("plots/x.csv", "plots/x.png", "plots/x.xlsx", "plots/x.yaml")) + sort(c( + "plots/x.csv", + "plots/x.md", + "plots/x.png", + "plots/x.xlsx", + "plots/x.yaml" + )) ) }) @@ -221,7 +239,7 @@ test_that("sbr_figures() lists the correct file names in the md string.", { )) }) -test_that("figures copies yaml and xlsx to report folder", { +test_that("figures copies yaml, xlsx and md to report folder", { withr::local_options(sbr.pre_num_fig = NULL) path <- withr::local_tempdir() subfoldr2::sbf_set_main(path, "output", rm = TRUE, ask = FALSE) @@ -236,10 +254,15 @@ test_that("figures copies yaml and xlsx to report folder", { main <- subfoldr2::sbf_get_main() expect_true(file.exists(file.path(main, "plots/x.yaml"))) expect_true(file.exists(file.path(main, "plots/x.xlsx"))) + expect_true(file.exists(file.path(main, "plots/x.md"))) sbr_figures() report <- sbr_get_report() expect_true(file.exists(file.path(report, "plots/x.png"))) expect_true(file.exists(file.path(report, "plots/x.yaml"))) expect_true(file.exists(file.path(report, "plots/x.xlsx"))) + expect_identical( + readLines(file.path(report, "plots/x.md")), + readLines(file.path(main, "plots/x.md")) + ) }) diff --git a/tests/testthat/test-tables.R b/tests/testthat/test-tables.R index d365f2e..2bd4a53 100644 --- a/tests/testthat/test-tables.R +++ b/tests/testthat/test-tables.R @@ -15,14 +15,14 @@ test_that("tables", { ) expect_identical( list.files(sbr_get_report(), recursive = TRUE), - c("tables/x.csv", "tables/x.yaml") + c("tables/x.csv", "tables/x.md", "tables/x.yaml") ) subfoldr2::sbf_save_table(x, x_name = "y", report = FALSE) expect_identical(sbr_tables(pre_num = 0L), txt) expect_identical( list.files(sbr_get_report(), recursive = TRUE), - c("tables/x.csv", "tables/x.yaml") + c("tables/x.csv", "tables/x.md", "tables/x.yaml") ) x_csv <- utils::read.csv(file.path(sbr_get_report(), "tables/x.csv")) @@ -122,7 +122,7 @@ test_that("tables sub", { ) expect_identical( list.files(sbr_get_report(), recursive = TRUE), - c("tables/A sub/x.csv", "tables/A sub/x.yaml") + c("tables/A sub/x.csv", "tables/A sub/x.md", "tables/A sub/x.yaml") ) }) @@ -143,7 +143,7 @@ test_that("tables missing caption", { ) expect_identical( list.files(sbr_get_report(), recursive = TRUE), - c("tables/x.csv", "tables/x.yaml") + c("tables/x.csv", "tables/x.md", "tables/x.yaml") ) }) @@ -166,7 +166,14 @@ test_that("tables sort sub and name", { ) expect_identical( list.files(sbr_get_report(), recursive = TRUE), - c("tables/a.csv", "tables/a.yaml", "tables/b/a.csv", "tables/b/a.yaml") + c( + "tables/a.csv", + "tables/a.md", + "tables/a.yaml", + "tables/b/a.csv", + "tables/b/a.md", + "tables/b/a.yaml" + ) ) txt <- sbr_tables(sort = c("b", "a"), pre_num = 0L) @@ -193,7 +200,7 @@ test_that("tables with []", { ) expect_identical( list.files(sbr_get_report(), recursive = TRUE), - c("tables/x.csv", "tables/x.yaml") + c("tables/x.csv", "tables/x.md", "tables/x.yaml") ) }) @@ -218,8 +225,10 @@ test_that("tables copies yaml metadata to report folder", { sort(list.files(sbr_get_report(), recursive = TRUE)), sort(c( "tables/x.csv", + "tables/x.md", "tables/x.yaml", "tables/A sub/y.csv", + "tables/A sub/y.md", "tables/A sub/y.yaml" )) ) @@ -242,7 +251,7 @@ test_that("tables pre_num", { ) expect_identical( sort(list.files(sbr_get_report(), recursive = TRUE)), - sort(c("tables/x.csv", "tables/x.yaml")) + sort(c("tables/x.csv", "tables/x.md", "tables/x.yaml")) ) txt <- sbr_tables() expect_identical( @@ -251,7 +260,7 @@ test_that("tables pre_num", { ) expect_identical( sort(list.files(sbr_get_report(), recursive = TRUE)), - sort(c("tables/x.csv", "tables/x.yaml")) + sort(c("tables/x.csv", "tables/x.md", "tables/x.yaml")) ) }) @@ -262,7 +271,12 @@ test_that("tables drop", { sbr_set_report(path, "report", rm = TRUE, ask = FALSE) sbf_reset_sub() - subfoldr2::sbf_save_table(data.frame(x = 1L), "x", sub = "a", caption = "Kept") + subfoldr2::sbf_save_table( + data.frame(x = 1L), + "x", + sub = "a", + caption = "Kept" + ) subfoldr2::sbf_save_table( data.frame(x = 2L), "x", @@ -276,7 +290,31 @@ test_that("tables drop", { ) expect_identical( sort(list.files(sbr_get_report(), recursive = TRUE)), - sort(c("tables/a/x.csv", "tables/a/x.yaml")) + sort(c("tables/a/x.csv", "tables/a/x.md", "tables/a/x.yaml")) ) expect_identical(sbr_tables(drop = c("a", "b")), character(0)) }) + +test_that("tables copies md files to report folder when they exist", { + withr::local_options(sbr.pre_num_tab = NULL) + path <- withr::local_tempdir() + subfoldr2::sbf_set_main(path, "output", rm = TRUE, ask = FALSE) + sbr_set_report(path, "report", rm = TRUE, ask = FALSE) + sbf_reset_sub() + + x <- data.frame(estimate = 1.2345, lower = 0.9, upper = 1.6) + subfoldr2::sbf_save_table(x, caption = "Estimates") + withr::with_options( + list(sbf.save_md = FALSE), + subfoldr2::sbf_save_table(x, x_name = "y", caption = "No md") + ) + + sbr_tables() + report <- sbr_get_report() + expect_identical( + readLines(file.path(report, "tables/x.md")), + readLines(file.path(subfoldr2::sbf_get_main(), "tables/x.md")) + ) + expect_false(file.exists(file.path(report, "tables/y.md"))) + expect_true(file.exists(file.path(report, "tables/y.csv"))) +})