johnny
johnny

Reputation: 631

Combining renderUI, dataTableOutput, and renderDataTable

Suppose I have the following shiny app that renders a data table from the package DT:

library(shiny)
ui <- fluidPage(uiOutput("abc"))
server <- function(input, output, session) {
  output$abc <- renderUI({DT::dataTableOutput("dt_output")})               # line 4
  output$dt_output <- DT::renderDataTable({data.table(a = 1:3, b = 4:6)})  # line 5
}
runApp(list(ui = ui, server = server))

How would you combine lines 4 and 5, with the constraint that output$abc must remain a uiOutput?

My attempt at combining (the code below) led to an error, "cannot coerce type closure":

output$abc <- renderUI({DT::dataTableOutput(
    DT::renderDataTable({data.table(a = 1:3, b = 4:6)}))})

Upvotes: 1

Views: 440

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

This should work:

library(shiny)

ui <- fluidPage(
    uiOutput("abc")
)

server <- function(input, output, session) {

    output$abc <- renderUI({
        output$aa <- DT::renderDataTable(head(mtcars))
        DT::dataTableOutput("aa")
    })

}
runApp(list(ui = ui, server = server))

Upvotes: 2

Related Questions