Max Power
Max Power

Reputation: 8996

Getting R Shiny DataTables Minimal Example to Work in RMarkdown Doc

The DT (datatables) library for R provides the minimal example for working with shiny below at http://rstudio.github.io/DT/extensions.html (this works for me)

library(shiny)
shinyApp(
  ui = fluidPage(DT::dataTableOutput('tbl')),
  server = function(input, output) {
    output$tbl = DT::renderDataTable(
      iris, options = list(lengthChange = FALSE)
    )
  }
)

However, the following code in a shiny RMarkdown document doesn't display any output. Why is that?

```{r}
library(rmarkdown); library(knitr); library(DT)

mydt = DT::renderDataTable(iris)
DT::dataTableOutput('mydt')
```

renderPrint(DT::dataTableOutput('mydt')) also doesn't display the table, though it displays some html info about the table.

I don't understand why defining the datatable with DT::renderDataTable() and displaying it with DT::dataTableOutput() works in a shiny app but not a shiny document. Though I presume I'm misunderstanding something.

Upvotes: 2

Views: 1689

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30184

All you need is DT::renderDataTable(iris).

```{r}
DT::renderDataTable(iris)
```

I don't understand what you mean by 1) assigning DT::renderDataTable(iris) to an R object mydt, 2) printing DT::dataTableOutput('mydt'), and 3) renderPrint(DT::dataTableOutput('mydt')). None of these make sense in this document.

Upvotes: 2

Related Questions