Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use the same dataTableOutput in different tabs

Tags:

r

shiny

Is there a possibility to reuse a dataTableOutput in several tabs? The only possibility I found was using a layout where the dataTableOutput gets its own row but I don't want it above all tabs.

If I just call the dataTableOutput multiple times, none of the tables get printed.

EDIT:

Thanks to the answer of daattali I got this almost done. The only thing I didn't mentioned before was, I need the two tables synchronised in a way. At the moment, when I try to update each other via proxy, the whole system gets buggy when selecting to many rows in a short time...

like image 269
drmariod Avatar asked Nov 21 '25 08:11

drmariod


1 Answers

You can't use the same id (since you can't have two elements on the same page with the same id), but what you can do is generate the table once as a reactive value and then simply return that value inside the render table functions. This has the benefit of only running the code for generating the table once, and re-using the table in as many outputs as you want.

Example:

library(shiny)

ui <- fluidPage(
  tabsetPanel(
    tabPanel("tab1", "tab 1", DT::dataTableOutput("table1")),
    tabPanel("tab2", "tab 2", DT::dataTableOutput("table2"))
  )
)

server <- function(input, output, session) {
  table_data <- reactive({
    DT::datatable(iris)
  })
  output$table1 <- DT::renderDataTable(table_data())
  output$table2 <- DT::renderDataTable(table_data())
}

shinyApp(ui = ui, server = server)
like image 131
DeanAttali Avatar answered Nov 22 '25 21:11

DeanAttali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!