2

I have a list of kable elements

list1 <- vector(mode = 'list', length = 2L)
a <- data.frame(a = c(1,2,3), b = c('q','w','e'))
b <- data.frame(d = c(3,4,5), e = c('k', 'l', 'm'))
list1[[1]] <- a %>% kable(format = 'html') %>% kable_styling('hover', full_width = F)
list1[[2]] <- b %>% kable(format = 'html') %>% kable_styling('hover', full_width = F)

The only way I am succesfully printing each element to an html file (rmarkdown) is by calling one by one like this:

```{r, echo = FALSE, result = 'asis'}
list1[[1]]
list1[[2]]
```

with the desired output when calling rmarkdown::render(output_format = 'html_document'):

desired output

If I do any other thing, like:

```{r, echo = FALSE, result = 'asis'}    
print(list1)
```

or

```{r, echo = FALSE, result = 'asis'}  
for(i in seq_along(list1)) {print(list1[[i]])}
```

or

```{r, echo = FALSE, result = 'asis'}
purrr::map(c(1,2), ~ print(list1[[.x]]))
```

I get the following output in the html file:

## <table class="table table-hover" style="width: auto !important; margin-left: auto; margin-right: auto;">
##  <thead>
##   <tr>
##    <th style="text-align:right;"> a </th>
##    <th style="text-align:left;"> b </th>
##   </tr>
##  </thead>
## <tbody>
##   <tr>
##    <td style="text-align:right;"> 1 </td>
##    <td style="text-align:left;"> q </td>
##   </tr>
##   <tr>
##    <td style="text-align:right;"> 2 </td>
##    <td style="text-align:left;"> w </td>
##   </tr>
##   <tr>
##    <td style="text-align:right;"> 3 </td>
##    <td style="text-align:left;"> e </td>
##   </tr>
## </tbody>
## </table>
## <table class="table table-hover" style="width: auto !important; margin-left: auto; margin-right: auto;">
##  <thead>
##   <tr>
##    <th style="text-align:right;"> d </th>
##    <th style="text-align:left;"> e </th>
##   </tr>
##  </thead>
## <tbody>
##   <tr>
##    <td style="text-align:right;"> 3 </td>
##    <td style="text-align:left;"> k </td>
##   </tr>
##   <tr>
##    <td style="text-align:right;"> 4 </td>
##    <td style="text-align:left;"> l </td>
##   </tr>
##   <tr>
##    <td style="text-align:right;"> 5 </td>
##    <td style="text-align:left;"> m </td>
##   </tr>
## </tbody>
## </table>

How can I print all those tables without having to call element by element?

daniellga
  • 1,142
  • 6
  • 16

1 Answers1

2

It works with the following command (where kables is a list of kable) in my session.

r knit(text = unlist(kables))
M--
  • 25,431
  • 8
  • 61
  • 93