我有一个父子Rmarkdown文件,我试图在子rmd文件中嵌入一个闪亮的UI服务器结构DT表。但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')`
儿童Rmd:
---
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 Uncaught Duplicate binding for ID table_diamond”,循环使用相同的输出ID“table_diamond”创建输出dataTable。
要在UI中使此输出Id动态化:
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)
})