Rmarkdown:knit_child环境中闪亮的DT服务器上下文无法呈现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rmarkdown:knit_child环境中闪亮的DT服务器上下文无法呈现相关的知识,希望对你有一定的参考价值。
我有一个父子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='
')`
儿童Rmd:
---
output: html_document
runtime: shiny_prerendered
---
```{r eval='TRUE', echo=FALSE, results='asis'}
if(colorNum ==0) cat('
##',c,'
'); #cat('
');
```
### `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)
})
感谢R Shiny dynamic tab number and input generation
以上是关于Rmarkdown:knit_child环境中闪亮的DT服务器上下文无法呈现的主要内容,如果未能解决你的问题,请参考以下文章
使用 shinyAppDir 在 Rmarkdown 中嵌入超过 1 个闪亮的应用程序
改变图形大小(Rstudio、Rmarkdown、Shiny)