0

I'm trying to generate a report from a Shiny application using R-Markdown. I have a series of DT tables created in a for-loop to be rendered in my RMD HTML output file: RMD setup:

    ```{r setup, include=FALSE}
    library(knitr)
    library(DT)
    library(plotly)
    library(htmltools)
    
    knitr::opts_chunk$set(echo = FALSE)
    ```

When I create the data table by itself, there is no problem:

    ```{r table1, echo=FALSE}
    DT::datatable(
      params$table1_data,
      rownames= FALSE,
      class = 'cell-border stripe',
      options = list(searching = FALSE, paging = FALSE, info = FALSE)
    )
    ```
    Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.

The HTML output file came out just right

image here

However, when I create multiple data tables in a for loop:

    ::: {style="margin-bottom:30px;"} 

       
    ```{r dataframes, echo = FALSE}
        # Building list of outputs
        dt_list <- htmltools::tagList()
        
        for(i in 1:params$n_files){
          # Extracting data
          df_input_data = params$langmuir_freundlich[[i]][[1]]
        
          # Creating tables
          dt_list[[i]] <- DT::datatable(
            df_input_data[,1:3],
            caption = params$table1_data[i, 2],
            rownames = FALSE,
            class = 'cell-border stripe',
            options = list(pageLength =15, searching = FALSE, paging = TRUE, info = TRUE)
          )
        }
        
        dt_list
     ```
     :::
    
     ### Next section
     Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.
        
        
        $$
        I = \frac{kC^{b}}{1+kC^{b}}
        $$

The tables look great, BUT for some reason, the last table (even if it´s just one table) is overlapping my NEXT SECTION, I'm actually only able to see the latex equation included.

image here

I have tried many versions of the for loop, but I believe the problem is somewhere else. I even tried adding a div<> with a style to add some space between the tables and the next paragraph with no success. Can anyone please help me to find out where the problem is?

shafee
  • 15,566
  • 3
  • 19
  • 47
drn_svq
  • 1
  • 3
  • Can you validate your version of the `rmarkdown` package? I can see this issue closed (https://github.com/rstudio/rmarkdown/issues/967) with a `1.7` milestone so if you are able to use a version newer than 1.7, does that help? If not, the reporter of the issue noted a workaround at http://stackoverflow.com/questions/42361888/rmarkdown-overlapping-output/42411142#42411142 – jav Oct 15 '22 at 00:31

1 Answers1

0

Solution based on @jav comment and this thread Rmarkdown overlapping output

My rmarkdown package was up to date, so the solution was to add the div styles after the header:

---
title:  "my_solution"
author: "my_name"
output: html_document
---

<style type="text/css">
    div.datatables { height: auto !important;}
</style>

```{r setup, include=FALSE}
library(knitr)
library(DT)
library(plotly)
library(htmltools)

knitr::opts_chunk$set(echo = FALSE)
`

``

Then add the tables in my for-loop

#### 1.1. Data frames
```{r dataframes, echo = FALSE}
# Building a list of outputs
dt_list <- htmltools::tagList()

for(i in 1:params$n_files){
  # Extracting data
  df_input_data = params$langmuir_freundlich[[i]][[1]]

  # Creating tables
  dt_list[[i]] <- DT::datatable(
    df_input_data[,1:3],
    caption = htmltools::tags$caption(
      style = 'caption-side: top; text-align: center;',
      paste0('Table ', n_table,': ', params$table1_data[i, 2], ' data frame')
    ),
    rownames= FALSE,
    class = 'cell-border stripe',
    options = my_options, 
  )
  
  # increment table counter
  n_table <- n_table + 1
}

dt_list
```

Resulting in these beautiful tables that don´t overlap my html document enter image description here

drn_svq
  • 1
  • 3