curious
curious

Reputation: 221

Using renderDataTable within renderUi in Shiny

I'm experimenting a Shiny App to show dynamic contexts, but I cannot get renderDataTable working into a renderUi component. Below two simple replicable tests: the first one is not working, the second one without renderUi works fine, of course.

What is the conceptually difference between this two, and why the first one cannot work in Shiny?

This one not works: note that the uiOutput myTable, contains two reactive component, a selectInput and a renderDataTable, but only the selectInput is rendered.

library(shiny)
runApp(list(
    ui = fluidPage(
            fluidRow(h2("where is the table?")),
            uiOutput('myTable')
    ),
    server = function(input, output) {
            output$myTable <- renderUI({
                    fluidPage(
                            fluidRow(selectInput("test", "test", c(1,2,3))),
                            fluidRow(renderDataTable(iris))
                    )
            })
    }
))

This is fine, both selectInput and renderDataTable are rendered:

library(shiny)
runApp(list(
    ui = fluidPage(
            fluidRow(h2("where is the table?")),
            fluidRow(selectInput("test", "test", c(1,2,3))),                
            fluidRow(dataTableOutput('myTable'))
    ),
    server = function(input, output) {
            output$myTable = renderDataTable(iris)
    }
))

How to get the first scenario working?

Thanks.

EDITING after Yihui comment (thanks Yihui):

In renderUi has to be used some ui function, and not some render function: changed the sample code in the correct way, the result does not change: still no data is shown.

library(shiny)
runApp(list(
    ui = basicPage(
            uiOutput('myTable')
    ),
    server = function(input, output) {
            output$myTable <- renderUI({dataTableOutput(iris)
            })
    }
))

EDIT n.2

Just solved, got it working so:

library(shiny)
runApp(list(
    ui = fluidPage(
            mainPanel(

                    uiOutput('myTable')
            )
    ),
    server = function(input, output) {
            output$myTable <- renderUI({
                    output$aa <- renderDataTable(iris)
                    dataTableOutput("aa")
            })
    }
))

I have to save the renderTableOutput in a output variable first, and then feeding it to dataTableOutput.

Thanks for pointing me to: here

Upvotes: 22

Views: 7734

Answers (1)

Thomas
Thomas

Reputation: 1219

It would be clearer if you split the part of datatable generation and ui generation :

library(shiny)
runApp(list(
    ui = fluidPage(
            mainPanel(
                    uiOutput('myTable')
            )
    ),
    server = function(input, output) {
            output$aa <- renderDataTable({iris})
            output$myTable <- renderUI({
                    dataTableOutput("aa")
            })
    }
))

Upvotes: 6

Related Questions