From 8f9f1faa19afe48b4a26b0038b407ad8e0c5674b Mon Sep 17 00:00:00 2001 From: Emily de la Rua <59304861+edelarua@users.noreply.github.com> Date: Wed, 6 Dec 2023 11:20:59 -0500 Subject: [PATCH] Fix `as_result_df` bug for small tables (#801) --- NEWS.md | 1 + R/tt_export.R | 1 + tests/testthat/test-result_data_frame.R | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/NEWS.md b/NEWS.md index eb47fb389..e287e16eb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -19,6 +19,7 @@ * Fixed a bug in `as_result_df` causing misalignment of column names. * Fixed a bug that was not allowing path indexing as `row_paths()` was giving a different path due to it being made of named values. + * Fixed a bug in `as_result_df` when called on tables with less than 3 rows. ### Miscellaneous * Applied `styler` and resolved package lint. Changed default indentation from 4 spaces to 2. diff --git a/R/tt_export.R b/R/tt_export.R index f149d3122..16216fbfe 100644 --- a/R/tt_export.R +++ b/R/tt_export.R @@ -351,6 +351,7 @@ do_data_row <- function(rdfrow, maxlen) { pth <- pth[-1 * (pthlen - 2)] } pthlen_new <- length(pth) + if (maxlen == 1) pthlen_new <- 3 c( as.list(pth[seq_len(pthlen_new - 2)]), replicate(maxlen - pthlen, list(NA_character_)), diff --git a/tests/testthat/test-result_data_frame.R b/tests/testthat/test-result_data_frame.R index 6a40be997..a67619447 100644 --- a/tests/testthat/test-result_data_frame.R +++ b/tests/testthat/test-result_data_frame.R @@ -112,4 +112,20 @@ test_that("as_result_df works with visual output (as_viewer)", { tbl <- build_table(lyt, ex_adsl) expect_equal(as_result_df(tbl, simplify = TRUE, as_viewer = TRUE)[2, 2][[1]], c(24, 46)) + + # Test for tables with less than 3 rows + tbl <- rtable( + header = rheader(rrow("", "c1", "c2")), + rrow("row 1", 1, c(.8, 1.2)) + ) + expect_equal( + as_result_df(tbl)[, 1:5], + data.frame( + "avar_name" = "row 1", + "row_name" = "row 1", + "row_num" = 1, + "is_group_summary" = FALSE, + "node_class" = "DataRow" + ) + ) })