Rmarkdown:knit_child 环境中闪亮的 DT 服务器上下文未呈现

Rmarkdown: shiny DT server context in knit_child environment not rendering

我有一个父子 Rmarkdown 文件,我试图在子 rmd 文件中嵌入一个闪亮 UI-服务器结构化 DT table。但是 DT 项不会在子项中呈现(但如果放在父项中,它会)。检查 HTML 输出时,dom 中的错误消息说:

shinyapp.js:342 Uncaught Duplicate binding for ID table_diamond
favicon.ico:1 Failed to load resource: the server responded with a status of 
404 (Not Found) 

下面是我的示例代码:

Parent.Rmd:

---
title: "Hello Prerendered Shiny"
output:
  html_document:
    fig_caption: yes
    keep_md: no
    number_sections: no
    theme: cerulean
    toc: yes
    toc_depth: 5
    toc_float:
      collapsed: true
  runtime: shiny_prerendered
---

```{r setup, results=FALSE, eval=TRUE, echo=FALSE, message=FALSE, warning=FALSE}
library(DT)
library(tidyverse)
library(knitr)
library(c3)

```

## Content Listed by diamond color

```{r echo=FALSE, eval=TRUE, include=FALSE, warning=FALSE}

color <- levels(diamonds$color)
out <- NULL

for (c in color){

  colorNum <- 0

 for (ct in 1: length(levels(diamonds[diamonds$color== c, ]$cut ))) {

 this_cut <- levels(diamonds[diamonds$color==c, ]$cut)[ct]
 env = new.env()
 out <- c(out, knit_child('sample_child.Rmd', envir = env))
 colorNum <- colorNum +1
  }
}
```
`r paste(out, collapse='\n')`

儿童路径:

---
output: html_document
runtime: shiny_prerendered
---


```{r eval='TRUE', echo=FALSE, results='asis'}
if(colorNum ==0)  cat('\n##',c,'\n'); #cat('\n'); 

```


### `r this_cut`  

#### Price range on fixed color and cut

```{r eval=TRUE, echo=FALSE, fig.retina=1, dpi = 72,results='asis', warning=FALSE}

data <-subset(diamonds, color == c) %>%
  filter(cut == this_cut) %>%
  as.data.frame()

plot(x = data$clarity, y = data$price, ylab = 'Price', xlab = 'clarity')

```
#### Detail Table

```{r, echo=FALSE}

DT::dataTableOutput("table_diamond")
submitButton("Save")

```

```{r, context="server"}
output$table_diamond <- DT::renderDataTable({

data <-subset(diamonds, color == c) %>%
filter(cut == this_cut) %>%
as.data.frame()

datatable(data)
})
```

有什么见解吗?

找出原因:

正如 dom 错误所说的“shinyapp.js:342 ID table_diamond 的未捕获重复绑定”,循环正在使用相同的输出 ID "table_diamond" 创建输出数据表.

要使此输出 ID 动态,在 UI:

table_id <- paste0('table_', c, this_cut)
dataTableOuput(outputId = table_id)

在服务器中,使用双方括号 [[ ]] 而不是 $:

output[[table_id]] <- DT::renderDataTable({

data <-subset(diamonds, color == c) %>%
filter(cut == this_cut) %>%
as.data.frame()

datatable(data)
})

感谢