本文介绍了在"asis" Markdown块中初始化JS渲染器的推荐方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'asis'块对于在Markdown文档中输出对象列表非常有用,请参见以下示例: Highcharter DT 传单,...

但是,在上述示例中,如果在上一个块中未调用渲染器一次,则对象列表将不会打印,因此将其初始化:这是一个棘手的解决方法,而我找到解决方案的更多原因是尝试/错误,而不是在文档中找到它.

这是一个可复制的问题,也发布在 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() })

```



解决方案

@cderv给出了我的问题的答案:
https://github.com/rstudio/rmarkdown/issues/1877#issuecomment- 679864674

由于@yihui喜欢此答案,因此暗示使用htmlwidget上的cat + asis应自担风险.

但是,我将继续使用问题中提到的解决方法,因为只要可行,我就会发现它非常实用.

感谢@atusi& @cderv的宝贵意见.

'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 examples, the list of object won't print if the renderer hasn't been called once in a previous chunck, 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() })

```



解决方案

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

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.

这篇关于在"asis" Markdown块中初始化JS渲染器的推荐方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 17:56
查看更多