从R Shiny Datatable中提取过滤器 [英] Extract filters from R Shiny Datatable
问题描述
我在R Shiny中有一个 DT数据表,并且通过设置< renderDT()
中的code> filter = top 。现在,我想提取用户应用的过滤器,以便将它们保存在服务器端变量中,并在(例如)数据库更新,需要更新表时重新应用它们。
I have a DT data table in R Shiny and I have enabled column filtering by setting filter="top"
within renderDT()
. I now want to extract the user-applied filters so I can save them in variables server-side and reapply them when -- for instance -- a database is updated, requiring an update of the table.
这里是使用Shiny Dashboard的MWE:
Here's a MWE using Shiny Dashboard:
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)
重申一下,我想保存过滤器(例如,用户可以选择一个数值范围s或来自其中一个过滤器框的特定因子)作为 input $
变量,以便可以在服务器端使用它们。
To reiterate, I'd like to save the filters (for instance, a user might select a range of numeric values or a specific factor from one of the filter boxes) as input$
variables so I can use them on the server side.
推荐答案
我认为最简单的方法是添加
I think the simplest way to do this is to just add
options = list(stateSave = TRUE)
在 renderDT()内部
函数。然后,在服务器
内,可以随时通过 input $< tableID> _state
(我的表仅被称为表,因此它变为 input $ table_state
:
inside the renderDT()
function. Then, within the server
, the state of the table can be accessed at any time with input$<tableID>_state
(my table is just called "table" so this becomes input$table_state
:
observeEvent(input$table_state, {
str(input$table_state)
})
然后整个解决方案是:
The whole solution is then:
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)
RStudio控制台中的示例输出:
Sample output within the RStudio console:
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
请注意搜索
列表,其中显示了应用于各列的过滤器。
Note the search
lists which show the filters applied to each column.
对于超级简单的过滤器提取,请使用 input $ table_search_columns
。产生与使用 sapply
相同的结果:
For super-easy filter extraction, use input$table_search_columns
. This gives the same result as using sapply
:
sapply(input$table_state$columns, function(x) x$search$search)
这将给出类似的内容
[1] "" "[\"0\"]" "[\"8\"]" "" ""
这篇关于从R Shiny Datatable中提取过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!