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

时间:2019-03-12 16:18:41

标签: r shiny r-markdown dt

我有一个父子Rmarkdown文件,并且我试图将Shiny UI服务器结构的DT表嵌入子rmd文件中。但是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)
})
```

有什么见解?

1 个答案:

答案 0 :(得分:0)

找出原因:

就像dom错误说的那样:“ shinyapp.js:342 ID表_钻石的未捕获重复绑定”,循环使用相同的输出ID“ table_diamond”创建输出dataTable。

要在用户界面中动态生成此输出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)
})

感谢R Shiny dynamic tab number and input generation