Shiny within Rmarkdown not rendering within loop

Cross-posted on SO, without resolution: r - Shiny within Rmarkdown not rendering within loop - Stack Overflow

I would like to render Shiny tabs (and other Shiny features) within an Rmarkdown document. Specifically, I'd like to display a table in each tab, generated with a loop. The tabs are rendering, but no the tables.

We can add {.tabset} tag after a header and subsequent lower level headers will be separate tabs. I tried to make use of this by using cat() and the lower header level and a unique name at each loop for the tab name. That creates the tabs fine. A table is created within each tab when I call the function with arguments separately.

However, when looping through the function, the tables are not being generated on each tab. In the final tab is output
<shiny.render.function> for each loop.

I know if I was generating a table in Rmarkdown without Shiny, I'd print each table within each loop. However, there's no similar function for printing rendered Shiny objects.

---
output: html_document
runtime: shiny
---

## Tables Work {.tabset}

{r works, results='asis'}
# Function to generate a numbered tab and table within that tab
tbl_display_func <- function(tab_number, df){
  cat('### Results', tab_number,' \n\n')
    DT::renderDataTable({
      DT::datatable(
        head(df)
        )
    })
}

# Works fine to call the function separately
tbl_display_func(1, cars)
tbl_display_func(2, iris)

working_tabs_and_tables

Tables Don't Work {.tabset}

However, when I loop through the function, tables do not display.

{r do-not-work, results='asis'}
# Does not work here. Generates tabs, then list of <shiny.render.function> in tab 2
purrr::map2(
  .x = list(1,2),
  .y = list(cars, iris),
  .f = tbl_display_func
) 

I tried to create the tables as renderTableOutput output, as I might if creating multiple widgets just in shiny, and then calling those within a function, but this didn't help

Tables Don't Work {.tabset}

{r generate-tables-then-print, results='asis'}
# Function just to generate table
tbl_func <- function(df){
    DT::renderDataTable({
      DT::datatable(
        head(df)
        )
    })
}

# Store rendered table as output
tbl_func_output <- purrr::map(
  .x = list(cars, iris),
  .f = tbl_func
)

# Loop through each tab and table
display_func <- function(tab_number){
  cat('### Results', tab_number,' \n\n')
  tbl_func_output[[tab_number]]
}

    purrr::map(
      .x = 1:2,
      .f = display_func
    )

How might I loop through and create a table at each tab?

UPDATE: Issue marked as resolved at SO link in question. Solution involves a different approach using do.call(tabsetPanel and separate tabPanels in loop. Also noted is that we can use cat(knit_print(DT::datatable(head(df)))) in loop by calling DT::datatable(matrix()) in an earlier chunk ( datatables generated in a loop in .Rmd file? · Issue #67 · rstudio/DT · GitHub)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.