Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using renderDataTable within renderUi in Shiny

Tags:

r

shiny

renderer

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

like image 200
curious Avatar asked Aug 04 '15 15:08

curious


1 Answers

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")
            })
    }
))
like image 198
Thomas Avatar answered Oct 16 '22 15:10

Thomas