在"asis" Markdown块中初始化JS渲染器的推荐方法 [英] Recommended way to initialize JS renderer in 'asis' Markdown chunck

查看:64
本文介绍了在"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

结果='asis'现在更多记录在 https://bookdown.org/yihui/rmarkdown-cookbook/results-asis.html#results-asis .它旨在从R块生成原始的mardown内容.任何事情都必须导致文本输出,这隐含地意味着这些块中的任何R对象都不会真正发生编织魔术,因为在编织内容时,knitr不会进行调整(因为它突出了文本)

我想我不会使用result ='asis'来cat()一个复杂的R对象,例如htmlwidget.您找到了一种解决方法,但可能会遇到其他问题.

由于@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

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆