r Markdown output being written to R studio viewer, not document

This is a weird issue. I am producing GT tables in a loop. The table code is in a markdown document that produces an accessible PDF report using accessr.

When I render the rmarkdown document the tables are being written to the Rstudio viewer pane and not to the document.
Here is some sample code:

names <- unlist(unique(oData10[["data"]]$NAME))

for(a in 1: length(names)) {
income_table <- oData10[["data"]] %>% filter(NAME == names[a]) %>%
  gt() %>%
  tab_options(
    table.width = pct(100),
    table.font.size = "9pt",
    latex.use_longtable = TRUE
  ) %>%
  sub_missing(missing_text = "") %>%
  tab_header(title= paste0("Income Sources: ",names[a])) %>%
  tab_spanner(label=oData10[["header"]][[1,3]],columns=c(Y_HH_EST, Y_HH_MOE)) %>%
  tab_spanner(label=oData10[["header"]][[1,5]],columns=c(HH_EARN_EST_PCT, HH_EARN_MOE_PCT)) %>%
  tab_spanner(label=oData10[["header"]][[1,7]],columns=c(AVG_EST, AVG_MOE)) %>%
  cols_label(NAME = oData10[["header"]][[2,1]],
             INC_TYPE = oData10[["header"]][[2,2]],
             Y_HH_EST = oData10[["header"]][[2,3]],
             Y_HH_MOE = oData10[["header"]][[2,4]],
             HH_EARN_EST_PCT = oData10[["header"]][[2,5]],
             HH_EARN_MOE_PCT = oData10[["header"]][[2,6]],
             AVG_EST = oData10[["header"]][[2,7]],
             AVG_MOE = oData10[["header"]][[2,8]]
             ) %>%
  cols_width(NAME ~ px(100), INC_TYPE ~ px(100), everything() ~ px(70)) %>%
  cols_align(align = "left", columns= c(1:2)) %>%
  cols_align(align="right",
             columns=c(3 : length(oData10[["header"]][[1]]))) %>%
  tab_source_note(oData10[["source"]]) %>%
  tab_style(
    style = cell_text(align = "center"),
    locations = cells_column_labels()
  )
print(income_table)
}

The data frame, header strings, and source strings all come from a list. I am rendering a long document with multiple tables. This example is the first time I am putting the gt code in a loop.
The output is going to the wrong place. Is there a clear solution for this?
TIA --AB

I think the problem is your print() argument inside the loop. You should better use knitr:: or simply return the result without an argument, e.g. instead of print(income_table) you could use income_table or knitr::kable(income_table). Please check out if it works then :slight_smile: