从R Shiny Datatable中提取过滤器

aww*_*smm 5 r shiny shinydashboard dt

我有一个DT数据表中的R闪亮和我已经通过设置使能列过滤filter="top"renderDT().我现在想要提取用户应用的过滤器,以便我可以将它们保存在服务器端的变量中,并在例如更新数据库时重新应用它们,这需要更新表.

这是使用Shiny Dashboard的MWE:

library(shiny)           #  Shiny web app
library(shinydashboard)  #  Dashboard framework for Shiny
library(plotly)          #  Plotly interactive plots
library(DT)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(column(12, DTOutput("table")))
  )
)

server <- function(input, output, session) {
  fileData <- reactiveFileReader(1000, session, 'test.csv', read.csv)
  output$table <- renderDT(fileData(), filter = "top")
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

重申一下,我想保存过滤器(例如,用户可能会选择一系列数值或来自其中一个过滤器框的特定因子)作为input$变量,因此我可以在服务器端使用它们.

aww*_*smm 6

我认为最简单的方法是添加

options = list(stateSave = TRUE)
Run Code Online (Sandbox Code Playgroud)

renderDT()函数内部。然后,在 中server,可以随时访问表的状态input$<tableID>_state(我的表只是称为“表”,因此变为input$table_state

observeEvent(input$table_state, {
  str(input$table_state)
})
Run Code Online (Sandbox Code Playgroud)

那么整个解决方案是:

library(shiny)
library(shinydashboard)
library(plotly)
library(DT)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(DTOutput("table"))
  )
)

server <- function(input, output, session) {
  fileData <- reactiveFileReader(1000, session, 'www/test.csv', read.csv)
  output$table <- renderDT(fileData(), filter = "top",
    options = list(stateSave = TRUE))

  observeEvent(input$table_state, {
    str(input$table_state)
  })

}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

RStudio 控制台中的示例输出:

List of 6
 $ time   : num 1.54e+12
 $ start  : int 0
 $ length : int 10
 $ order  : list()
 $ search :List of 4
  ..$ search         : chr ""
  ..$ smart          : logi TRUE
  ..$ regex          : logi FALSE
  ..$ caseInsensitive: logi TRUE
 $ columns:List of 5
  ..$ :List of 2
  .. ..$ visible: logi TRUE
  .. ..$ search :List of 4
  .. .. ..$ search         : chr ""
  .. .. ..$ smart          : logi TRUE
  .. .. ..$ regex          : logi FALSE
  .. .. ..$ caseInsensitive: logi TRUE
  ..$ :List of 2
  .. ..$ visible: logi TRUE
  .. ..$ search :List of 4
  .. .. ..$ search         : chr "[\"0\"]"
  .. .. ..$ smart          : logi TRUE
  .. .. ..$ regex          : logi FALSE
  .. .. ..$ caseInsensitive: logi TRUE
  ..$ :List of 2
  .. ..$ visible: logi TRUE
  .. ..$ search :List of 4
  .. .. ..$ search         : chr "[\"8\"]"
  .. .. ..$ smart          : logi TRUE
  .. .. ..$ regex          : logi FALSE
  .. .. ..$ caseInsensitive: logi TRUE
  ..$ :List of 2
  .. ..$ visible: logi TRUE
  .. ..$ search :List of 4
  .. .. ..$ search         : chr ""
  .. .. ..$ smart          : logi TRUE
  .. .. ..$ regex          : logi FALSE
  .. .. ..$ caseInsensitive: logi TRUE
  ..$ :List of 2
  .. ..$ visible: logi TRUE
  .. ..$ search :List of 4
  .. .. ..$ search         : chr ""
  .. .. ..$ smart          : logi TRUE
  .. .. ..$ regex          : logi FALSE
  .. .. ..$ caseInsensitive: logi TRUE
Run Code Online (Sandbox Code Playgroud)

请注意search显示应用于每列的过滤器的列表。

奖金

对于超级简单的过滤器提取,请使用input$table_search_columns. 这给出了与使用相同的结果sapply

sapply(input$table_state$columns, function(x) x$search$search)
Run Code Online (Sandbox Code Playgroud)

这将给出类似

[1] ""        "[\"0\"]" "[\"8\"]" ""        ""      
Run Code Online (Sandbox Code Playgroud)

对于上面的例子。

  • 比我的答案更好! (2认同)