Recommended way to initialize JS renderer in 'asis' Markdown chunck

前端 未结 3 496
遇见更好的自我
遇见更好的自我 2020-12-07 02:40

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

However, in the above exam

相关标签:
3条回答
  • 2020-12-07 03:20

    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()
    ```
    
    0 讨论(0)
  • 2020-12-07 03:31

    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.

    0 讨论(0)
  • 2020-12-07 03:32

    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)))})
    ```
    
    0 讨论(0)
提交回复
热议问题