从R Shiny Datatable中提取过滤器

编程入门 行业动态 更新时间:2024-10-22 17:25:49
本文介绍了从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中提取过滤器

本文发布于:2023-11-09 21:12:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   Shiny   Datatable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!