【问题标题】:Create html div's in a loop in RMarkdown在 RMarkdown 中循环创建 html div
【发布时间】:2020-10-28 09:30:11
【问题描述】:

我希望能够在rmarkdown 中循环创建多个 HTML div。但是,它似乎不太好用。我要创建的是bsplus::bs_modal(),然后将这些模式附加到按钮上。

具有期望结果的最小.Rmd reprex:

---
title: "Untitled"
output: html_document
---
  
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(magrittr)
```
  
```{r}
bsplus::bs_modal(id = paste0("button_", 1), title = paste0("modal ", 1), body = NULL)
bsplus::bs_modal(id = paste0("button_", 2), title = paste0("modal ", 2), body = NULL)
```
  
```{r}
bsplus::bs_button("Modal 1") %>% 
  bsplus::bs_attach_modal("button_1")
```
  
```{r}
bsplus::bs_button("Modal 2") %>% 
  bsplus::bs_attach_modal("button_2")
```

最小的.Rmd reprex 与我正在使用的方法,但它不起作用:

---
title: "Untitled"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(magrittr)
```

```{r results='asis'}
lapply(1:2, function(i) {
  bsplus::bs_modal(id = paste0("button_", i), title = paste0("modal ", i), body = NULL)
})
```

```{r results='asis'}
lapply(1:2, function(i) {
  bsplus::bs_button(paste0("Modal ", i)) %>% 
    bsplus::bs_attach_modal(paste0("button_", i))
})
```

有什么想法吗?

【问题讨论】:

    标签: html r r-markdown


    【解决方案1】:

    你可以试试htmltools::tagList()

    ---
    title: "Untitled"
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    library(magrittr)
    ```
    
    ```{r results='asis'}
    htmltools::tagList(lapply(1:2, function(i) {
      bsplus::bs_modal(id = paste0("button_", i), title = paste0("modal ", i), body = NULL)
    }))
    ```
    
    ```{r results='asis'}
    htmltools::tagList(lapply(1:2, function(i) {
      bsplus::bs_button(paste0("Modal ", i)) %>% 
        bsplus::bs_attach_modal(paste0("button_", i))
    }))
    ```
    

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多