Daisywang
Daisywang

Reputation: 307

Rmarkdown: shiny DT server context in knit_child environment not rendering

I have a parent-child Rmarkdown file and I am trying to embed a Shiny UI-server structured DT table in the child rmd file. But the DT item won't render in child(but if put in parent, it will). When inspecting the HTML output, error message in dom saying:

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) 

Below is the sampled code I have:

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')`

Child 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)
})
```

Any insights?

Upvotes: 0

Views: 243

Answers (1)

Daisywang
Daisywang

Reputation: 307

Figured out why:

Just as dom error said “shinyapp.js:342 Uncaught Duplicate binding for ID table_diamond”,the loop is creating output dataTable using the same output ID "table_diamond".

To make this output Id dynamic, in UI:

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

in Server, use double square brackets [[ ]] instead of $:

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

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

datatable(data)
})

Thanks to R Shiny dynamic tab number and input generation

Upvotes: 0

Related Questions