Skip to content

Update functions that relied on 'flextable' and convert to 'gt'#429

Open
sbreitbart-NOAA wants to merge 21 commits intodevfrom
ft-to-gt
Open

Update functions that relied on 'flextable' and convert to 'gt'#429
sbreitbart-NOAA wants to merge 21 commits intodevfrom
ft-to-gt

Conversation

@sbreitbart-NOAA
Copy link
Collaborator

@sbreitbart-NOAA sbreitbart-NOAA commented Feb 25, 2026

What is the feature?

  • Update functions that relied on 'flextable' and convert to 'gt'

How have you implemented the solution?

  • Update ID_tbl_width_class() to work with gt tables, vs flextables
  • Create ID_tbl_length_class() to ID table length, to decide whether to split them across pages lengthwise
  • Update create_tables_doc() to properly format and split tables with varying widths and lengths in tables qmd
  • Update/create tests
  • Update render_lg_table()
  • Update captions for split tables by removing individual column names shown, and instead state number of table vs total number of tables
  • Edit add_child() to add line to tables chunk in skeleton to add "clearpage" latex command, which ensures all tables will be plotted before any other section starts (otherwise, Figures and Appendix titles appeared between tables)
  • Other minor fixes like removing stale, duplicate license

Does the PR impact any other area of the project, maybe another repo?

  • Now it better connects with stockplotr's table exporting functionality

These reports can be tagged. ✅

…lextable-based pipeline with gt (for identifying table width, and splitting/rotating where needed). Next step: split super-long tables where necessary
… orientation and 1 table shown on landscape orientation
…shown, and instead state number of table vs total number of tables
…, and redesign to better work with gt tables vs flextables; update documentation and tests
…ce tab_options(table.width) is sufficient; add "tbl-pos: 't'" to table chunk options to anchor tables
@github-actions
Copy link
Contributor

github-actions bot commented Feb 25, 2026

Checklist

  • PR base branch is accurate
  • Is the code concise?
  • Comments are clear and useful.
  • Can you remove or combine any arguments?
  • Do argument contain defaults (if appliable)?
  • Code is documented and example provided (Roxygen).
  • Did you make a test (testthat)?
  • Was this tested under multiple scenarios?
  • Did you run devtools::check()?

@github-actions
Copy link
Contributor

Code Metrics Report

Coverage Code to Test Ratio Test Execution Time
40.0% 1:0.3 30s

Code coverage of files in pull request scope (67.6%)

Files Coverage
R/ID_tbl_length_class.R 93.7%
R/ID_tbl_width_class.R 94.7%
R/add_child.R 95.2%
R/create_tables_doc.R 58.1%
R/export_split_tbls.R 100.0%
R/render_lg_table.R 96.4%

Reported by octocov

Copy link
Collaborator

@Schiano-NOAA Schiano-NOAA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On initial review, the code looked good. It was really cool seeing one of the functions at 100% testing coverage. I'm not sure what, but something is going wrong in the code for splitting the tables. I tested this out on a table_landings that I exported which looked fine in the view, but when it was added to a report and rendered, it resulted in the table plus split tables containing all NA.

Here is what the table looks like straight from stockplotr::table_landings()

Image [break in rows because it was a long table] Image

and here is the issue in the rendered report:

Image

The above table in the rendered report only displays the values up to 1917 while the data goes until 2024

the table continues with all NAs for an additional 3 pages.

Here is the workflow I used:

stockplotr::convert_output(file = "C:/Users/samantha.schiano/Documents/Stock Assessment Workflow/SS_output_examples/Sablefish/Results-2025/Report.sso", save_dir = file.path(getwd(), "conout.rda"))

load(file.path(getwd(),"conout.rda"))
stockplotr::table_landings(out_new, make_rda = TRUE)

create_template(model_results = file.path(getwd(), "conout.rda"))

@Schiano-NOAA
Copy link
Collaborator

I got a new error when running the code with your recent change

processing file: sar_Petrale_skeleton.qmd
  |....................................       |  85% [tables]                  

Error in `gt::gt_split()`:
! argument "data" is missing, with no default
Backtrace:
    x
 1. +-gt::grp_pull(gt::gt_split(row_every_n = 38), 1)
 2. | \-gt:::extract_gt_tbl_from_gt_group(data = data, which = which)
 3. |   \-gt:::get_indices_gt_tbls(data = data)
 4. |     \-base::nrow(data[["gt_tbls"]])
 5. \-gt::gt_split(row_every_n = 38)
 6.   \-gt:::stop_if_not_gt_tbl(data = data)
 7.     \-gt:::is_gt_tbl(data = data)

Quitting from sar_Petrale_skeleton.qmd:225-234 [tables]
                                                                                                                     
Execution halted
WARN: Error encountered when rendering files

I am not sure what could be wrong with my system. I think it's got to be something with the code, but I guess I will have to debug this issue if you can't replicate it :/

@sbreitbart-NOAA
Copy link
Collaborator Author

I got a new error when running the code with your recent change

processing file: sar_Petrale_skeleton.qmd
  |....................................       |  85% [tables]                  

Error in `gt::gt_split()`:
! argument "data" is missing, with no default
Backtrace:
    x
 1. +-gt::grp_pull(gt::gt_split(row_every_n = 38), 1)
 2. | \-gt:::extract_gt_tbl_from_gt_group(data = data, which = which)
 3. |   \-gt:::get_indices_gt_tbls(data = data)
 4. |     \-base::nrow(data[["gt_tbls"]])
 5. \-gt::gt_split(row_every_n = 38)
 6.   \-gt:::stop_if_not_gt_tbl(data = data)
 7.     \-gt:::is_gt_tbl(data = data)

Quitting from sar_Petrale_skeleton.qmd:225-234 [tables]
                                                                                                                     
Execution halted
WARN: Error encountered when rendering files

I am not sure what could be wrong with my system. I think it's got to be something with the code, but I guess I will have to debug this issue if you can't replicate it :/

Which data did you use / can you add the code here? I'll try to reproduce your error. If I can't let's cowork on this next week

@Schiano-NOAA
Copy link
Collaborator

@sbreitbart-NOAA it's happened with multiple examples. The most current one the code was

stockplotr::table_landings(stockplotr::example_data, make_rda = TRUE)
create_template()

then render to get this error

==> quarto preview sar_species_skeleton.qmd --to pdf --no-watch-inputs --no-browse



processing file: sar_species_skeleton.qmd
  |....................................       |  85% [tables]                  

Error in `gt::gt_split()`:
! argument "data" is missing, with no default
Backtrace:
    x
 1. +-gt::grp_pull(gt::gt_split(row_every_n = 38), 1)
 2. | \-gt:::extract_gt_tbl_from_gt_group(data = data, which = which)
 3. |   \-gt:::get_indices_gt_tbls(data = data)
 4. |     \-base::nrow(data[["gt_tbls"]])
 5. \-gt::gt_split(row_every_n = 38)
 6.   \-gt:::stop_if_not_gt_tbl(data = data)
 7.     \-gt:::is_gt_tbl(data = data)

Quitting from sar_species_skeleton.qmd:225-234 [tables]
                                                                                                                     
Execution halted
WARN: Error encountered when rendering files

I will upload my document I made to a github repo. Maybe see if you can pinpoint the error or render it?

@sbreitbart-NOAA
Copy link
Collaborator Author

@Schiano-NOAA I noticed that my last commit missed adding in a few pipes, which broke the code. I've fixed that now and am able to produce reports with all kinds of tables (per width and length). Here's my code, if you want to try it. I understand if you'd rather wait to discuss this before trying it once more!

# All table scenarios (length, width)
scenarios <- list(
  list(name = "reg_reg",    rows = 25, cols = 2),
  list(name = "reg_long",   rows = 45, cols = 2),
  list(name = "wide_reg",   rows = 25, cols = 5),
  list(name = "wide_long",  rows = 45, cols = 5),
  list(name = "ewide_reg",  rows = 25, cols = 10),
  list(name = "ewide_long", rows = 45, cols = 10)
)

# Locations to sample from
location_pool <- c("Providence Rhode Island", "Narragansett Bay", "Point Judith Pond", "Block Island Sound",
  "Cape Cod Bay", "Georges Bank", "Gulf of Maine", "Montauk New York", "Hudson Canyon",
  "Long Island Sound", "Chesapeake Bay", "Outer Banks NC", "Florida Keys", "Gulf of Mexico",
  "Monterey Bay CA", "Puget Sound WA", "Kodiak Alaska", "Bering Sea", "Hawaiian Islands",
  "Sargasso Sea", "Buzzards Bay MA", "Stellwagen Bank", "Vineyard Sound", "Great South Channel",
  "Casco Bay Maine", "Grand Banks", "Scotian Shelf", "Delaware Bay", "Pamlico Sound NC",
  "Charleston Bump SC", "Gray Reef GA", "St Johns River FL", "Mobile Bay AL", 
  "Flower Garden Banks", "Channel Islands CA", "Columbia River Estuary", 
  "Gulf of the Farallones", "Aleutian Islands", "Chukchi Sea", "Beaufort Sea")

tbl_path <- fs::path(getwd(), "tables")
dir.create("tables")
if (!dir.exists(tbl_path)) dir.create(tbl_path)

# Iterate through scenarios and save tables as rdas
for (s in scenarios) {
  
  mat <- matrix(
    sample(location_pool, s$rows * s$cols, replace = TRUE), 
    nrow = s$rows, 
    ncol = s$cols
  )
  
  df <- as.data.frame(mat)
  colnames(df) <- paste0("Col_", 1:s$cols)
  
  test_table <- df |> gt::gt()
  
  rda <- list(
    "table" = test_table,
    "caption" = paste("Test caption for", s$name)
  )
  
  file_name <- paste0(s$name, "_table.rda")
  save(rda, file = fs::path(tbl_path, file_name))
  }

rm(test_table, rda, s, scenarios, mat, df, location_pool, tbl_path)

create_template()
quarto::quarto_render(here::here("report", "sar_species_skeleton.qmd"))

@Schiano-NOAA
Copy link
Collaborator

Awesome! I tried it out with the fixes you pushed and I am still getting the NA tables. I made a repo if you want to try and fork it then render what I made.

I will try the code you added above.

If you are able to render/make it fine based on my example, then it must be my computer, but we will still need to figure out next week what is going on to advise users when this happens. Thanks for keeping at it!

@Schiano-NOAA
Copy link
Collaborator

Ugh yeah still getting the same issue with your code too. We can go through the source code on my machine next Tuesday to see what is going on

@sbreitbart-NOAA
Copy link
Collaborator Author

@Schiano-NOAA I forked your repo, downloaded the ft-to-gt branch, updated the tables_dir in the tables doc, and was able to render both the tables qmd and the skeleton without issue. Hopefully we can sort this out next week!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants