Exie
Exie

Reputation: 466

Dynamic array of tables

I have a need to present N tables in Shiny, where N is based on a user input.

My initial thought was to make something like this:

ui.r

fluidPage(
  fluidRow(
    column(width=4
       ,dataTableOutput("table1")
       ,dataTableOutput("table2")
       ...
       ,dataTableOutput("table10")
    )
  )
)

server.r

shinyServer(function(input, output) {
    output$table1 <- renderDataTable({ ... })
    output$table2 <- renderDataTable({ ... })
    ...
    output$table10 <- renderDataTable({ ... })
}

But as you can see thats really sloppy, and as soon as they ask for up to 20,30 or 40 tables it will fall apart.

I thought about putting something in a for loop like:

for(1:N) {
    ,dataTableOutput("table"+N)
}

But I'm sure there is a better way. Show me the light R/Shiny experts!

Upvotes: 1

Views: 107

Answers (1)

Exie
Exie

Reputation: 466

Update:

Thanks for nongkrong for the awesome link to here: https://gist.github.com/wch/5436415/

I had a little trouble getting it to work reactively, but that wasnt really a requirement.

The trick seemed to be injecting the dataTableOutput() elements inside the renderUI() block. Works really nicely.

It's also pretty neat how you can then direct your renderDataTable() blocks to the aforementioned tags you just defined.

Upvotes: 1

Related Questions