7

'asis' chunks are very useful to output a list of objects in a Markdown document, see following examples : Highcharter, DT, Leaflet, ...

However, in the above examples, the list of object won't print if the renderer hasn't been called once in a previous chunk, so that it gets initialized : this is a tricky workaround, and I found the solution more by trial / error than by finding it in documentation.

This is a reproducible issue also posted on https://github.com/rstudio/rmarkdown/issues/1877 :

---
title: "Test"
output:
  html_document
---



```{r,echo=F}
library(DT)
library(rmarkdown)
library(purrr)
library(knitr)

df_list <- list("cars" = mtcars, "flowers" = iris)

knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```

```{r}
# If this first initialization isn't run, tabs won't print
DT::datatable(data.frame())
```

# Test tabs {.tabset}

```{r, results='asis' }
imap(df_list, ~{
  cat('## Subtab ',.y,'\n')
  cat('\n')
  DT::datatable(.x) %>%
    htmltools::tagList() %>% as.character() %>% cat() })

```
 

  
ViviG
  • 1,613
  • 1
  • 9
  • 23
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • I'm not sure if that fits here (I think it's a bit different use case and I'm not overly familiar with the Rmarkdown), but for `DT` I used the option to define my own `knit_print.data.frame` method to use `DT` for data.frames following this isse: https://github.com/yihui/printr/issues/33 However, I haven't tested it with lists of data.frames yet – starja Aug 22 '20 at 11:35
  • Thanks @Starja for the link. I tried to include the print method, but it didn't initialize the JS renderer. – Waldi Aug 22 '20 at 16:12
  • I'm not sure if this is related, you need to call `registerS3method` in order to use it: https://cran.r-project.org/web/packages/knitr/vignettes/knit_print.html – starja Aug 22 '20 at 16:29
  • Thanks @ Starja for this vignette. The `meta` parameter from `asis_output` looks promising to setup js library, however I didn't figure out how to use it properly in order to answer my question. – Waldi Aug 23 '20 at 14:29
  • yes, I also find this argument poorly documented and couldn't really make sense of it – starja Aug 23 '20 at 14:49

3 Answers3

5

If you really need 'asis', then you add extra dependencies manually such as JavaScript and CSS. You can do it with knitr::knit_meta_add().

```{r, results='asis'}
library(purrr)
data.frame() %>%
  DT::datatable() %>%
  knitr::knit_print() %>%
  attr('knit_meta') %>%
  knitr::knit_meta_add() %>%
  invisible()

df_list <- list("cars" = mtcars, "flowers" = iris)
imap(df_list, ~{
      cat('## Subtab ',.y,'\n')
      cat('\n')
      cat(knitr::knit_print(DT::datatable(.x)))})
```
atusy
  • 646
  • 3
  • 5
  • Thanks @atusy for the `knitr::knit_meta_add()` suggestion. I think we're on the right way. However if I copy straight away this code in the example code I provided (see edit of my post), tabs aren't created properly. From your answer, the easiest working way seems `DT:datatable(data.frame()` in the previous non `asis` chunck to force initialization. – Waldi Aug 25 '20 at 06:43
3

You can output multiple datatables without asis. Just put the list of widgets into tagList()

```{r}
library(purrr)
list("cars" = mtcars, "flowers" = iris) %>%
  map(~DT::datatable(.x)) %>%
  htmltools::tagList()
```
atusy
  • 646
  • 3
  • 5
  • Thanks for your answer, this is correct in this case! However in two of the examples I sent, 'asis' is needed to be able to create sub tabs following a {.tabset} Markdown option. So the question relates specifically to 'asis' – Waldi Aug 24 '20 at 06:01
3

The answer to my question has been given by @cderv :
https://github.com/rstudio/rmarkdown/issues/1877#issuecomment-679864674

The results = 'asis' is now more documented in https://bookdown.org/yihui/rmarkdown-cookbook/results-asis.html#results-asis . It is aimed at generating raw mardown content from a R chunk. Anything must result in a text output, and implicitly that means no knitr magic really happens for any R object in those chunk, because knitr does no adjustment when knit_printing the content (as it is juts text)

I think I would not use result = 'asis' to cat() a complex R object like an htmlwidget. You found a workaround but you may encounter other issues.

As this answer has been liked by @yihui, it gives a hint that cat + asis on htmlwidget should be used at one's own risk.

However, I'll personnaly continue to use the workarounds mentioned in the question, because as long as it works I find it very practical.

Thanks @atusi & @cderv for their valuable input.

Waldi
  • 39,242
  • 6
  • 30
  • 78