title: rmarkdown using kable with list of data.tables loses table formatting
body: <p>I have a <code>rmarkdown</code> document where I want to use a list of <code>data.table</code>s and include them in a PDF/HTML/MS Word document. Each component of the list contains one <code>data.table</code>.</p>
  
  <p>What I'm trying to achieve is to use <code>kable</code> to add all tables in a document with captions.</p>
  
  <p>When I index the <code>data.table</code>s one by one like this</p>
  
  <pre><code>```{r}
   kable(list.of.dts[[1]], caption = paste0("Frequency table for the '", colnames(list.of.dts[[1]])[2], "' variable."))
  ```
  </code></pre>
  
  <p>everything is fine and the table is nicely formatted as on the screenshot below.</p>
  
  <p><a href="https://i.stack.imgur.com/DqSOB.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/DqSOB.jpg" alt="enter image description here"></a></p>
  
  <p>However, if I try to use <code>lapply</code> to <code>kable</code> all the <code>data.tables</code> in the list like this</p>
  
  <pre><code>```{r}
  lapply(X = list.of.dts, FUN = function(i) {
    kable(x = i, caption = paste0("Frequency table for the '", colnames(i)[2], "' variable."))
  })
  ```
  </code></pre>
  
  <p>to process all <code>data.tables</code> and include them in the document, I get quite basic and crude output like on the screenshot below.</p>
  
  <p><a href="https://i.stack.imgur.com/9Oj4L.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/9Oj4L.jpg" alt="enter image description here"></a></p>
  
  <p>Further, if I just pass the list of <code>data.table</code>s to <code>kable</code> like this</p>
  
  <pre><code>```{r}
  kable(list.of.dts)
  ```
  </code></pre>
  
  <p>I get all the tables with a better formatting (although not as in the first example), but stacked on top of each other, centered in the middle of the page and I can't add captions:</p>
  
  <p><a href="https://i.stack.imgur.com/sAz6u.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/sAz6u.jpg" alt="enter image description here"></a></p>
  
  <p>Does anyone know why is this happening and how can I overcome this issue?</p>
  
  <p><strong>EDIT1:</strong></p>
  
  <p>I have put <code>result="asis"</code> in the chunk of the first example and saved the output to an object, then returned the object and it did the trick:</p>
  
  <pre><code>```{r, echo = FALSE, results='asis'}
  my.list2 &lt;- lapply(X = list.of.dts, FUN = function(i) {
    kable(x = i, caption = paste0("Frequency table for the '", colnames(i)[2], "' variable."))
  })
  my.list2
  ```
  </code></pre>
  
  <p>If I don't save the output to a new object and then return it, then I get a blank document. The above works (see the screenshot below). The only problems are that I see the names of the list components in between the tables and the tables and their captions are aligned in the middle of the page:</p>
  
  <p><a href="https://i.stack.imgur.com/FTnoI.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/FTnoI.jpg" alt="enter image description here"></a></p>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>As user20650 (thank you) noted, explicit loop would work better because none of the list components' names will be shown. Besides that, there is no need to assign the results to a new object and then return it. Here is how it looks:</p>
  
  <pre><code>```{r, echo = FALSE, results='asis'}
  for(i in list.of.dts) {
    print(kable(x = i, caption = paste0("Frequency table for the '", colnames(i)[2], "' variable.")))
  }
  ```
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/nc0ob.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/nc0ob.jpg" alt="enter image description here"></a></p>
  
  <p>The only remaining issue is that the tables are still centered in the page.</p>
  
  <p><strong>EDIT3</strong></p>
  
  <p>The last issue (see above) was to align tables and their captions to the left. user20650 made a suggestion that worked for the captions. After I found <a href="https://groups.google.com/forum/#!topic/pandoc-discuss/X_1tpGufN_w" rel="noreferrer">this email thread</a>, I modified the <code>yaml</code> header as follow, adding these <code>LaTeX</code> options to the <code>header-includes:</code> section:</p>
  
  <pre><code>---
  title: "My report"
  author: "John Doe"
  output: pdf_document
  toc: true
  header-includes:
    - \usepackage{caption}
    - \captionsetup{justification=raggedright,singlelinecheck=false}
    - \usepackage[margins=raggedright]{floatrow}
  ---
  </code></pre>
  
  <p>The first two lines of <code>header-includes:</code> (load the <code>caption</code> package and define the cations' settings) fix the captions' position to left. The last line loads the <code>floatrow</code> package and and fixes the margins.</p>
  
  <p>Case closed.</p>