我在tabsetpanel中渲染了三个数据表,我需要做的是将一个数据表的当前页面保留到其他数据表中。例如,如果我在datatable1的第1页中,并且我转到第2页,我希望datatable2和3位于第2页。
这是我的代码,有什么想法吗?
shinyApp(
ui = fluidPage(
tags$head(
# hides the default search functionality
tags$style(
HTML(".dataTables_filter, .dataTables_info { display: none; }")
)
),
fluidRow(
column(10,
""
),
column(2,
uiOutput("pageFilter")
#numericInput("page", "Page", 1, min = 1)
),
column(12,
tabsetPanel(
tabPanel('pressure',
DT::dataTableOutput("mytable1")),
tabPanel('mtcars',
DT::dataTableOutput("mytable2")),
tabPanel('iris',
DT::dataTableOutput("mytable3"))
)
)
)
)
答案 0 :(得分:1)
重要的是使用stateSave
参数,见下文:
library(DT)
library(shiny)
app <- shinyApp(
ui = fluidPage(
tags$head(
# hides the default search functionality
tags$style(
HTML(".dataTables_filter, .dataTables_info { display: none; }")
)
),
fluidRow(
column(10,
""
),
column(2,
# adding new page filter
uiOutput("pageFilter")
),
column(12,
tabsetPanel(id = "tab",
tabPanel('pressure',
dataTableOutput('table1')
),
tabPanel('mtcars',
dataTableOutput('table2')
)
)
)
)
),
server = function(input, output) {
global <- reactiveValues()
observe({
global$val <- input$table1_state$start / input$table1_state$length + 1
})
observe({
global$val <- input$table2_state$start / input$table2_state$length + 1
})
output$pageFilter <- renderUI({
numericInput("page", "Page", global$val, min = 1)
})
output$table1 <- DT::renderDataTable({
iris
}, options = list(pageLength = 5, stateSave = TRUE))
output$table2 = DT::renderDataTable({
mtcars
}, options = list(pageLength = 5, stateSave = TRUE))
# using new page filter
observeEvent({input$page; input$tab}, {
dataTableProxy("table1") %>% selectPage(global$val)
dataTableProxy("table2") %>% selectPage(global$val)
})
}
)
runApp(app, launch.browser = TRUE)