题意:
在重新渲染表格后保留列过滤器
问题背景:
I have a datatable that uses both interactive shiny filters via action buttons which subset my data, and also the built in datatable filters. The action buttons perform bulk filtering by subsetting the dataframe. The problem I'm having is that whenever one of these bulk filters is applied, the datatable is re-rendered and all the individual column filters are cleared. I'd like to be able to keep the individual column filters active whenever the data is subsetted and the table re-rendered.
我有一个数据表,它使用交互式的 Shiny 过滤器(通过操作按钮实现,可以对我的数据进行子集划分)以及内置的数据表过滤器。操作按钮通过划分数据框的子集来执行批量过滤。我遇到的问题是,每当应用这些批量过滤器之一时,数据表都会重新渲染,并且所有单独的列过滤器都会被清除。我希望在数据被子集划分并且表格重新渲染时,能够保持单独的列过滤器处于活动状态。
I've managed to find that I can output and isolate this information from the datatable using input$mytable_search_columns but I have no idea how to write that javascript that will apply this criteria upon re-rendering the table.
我已经设法发现,我可以使用 input$mytable_search_columns
从数据表中输出和隔离这些信息,但我不知道如何编写 JavaScript 来在表格重新渲染时应用这些条件。
library(shinyBS)
library(DT)
server <- function(input, output, session) {
df <- reactive({iris})
df.sub <- reactive({
if(input$buttonfilter == 0){
df.sub <- df()
}
if(input$buttonfilter == 1){
df.sub <- subset(df(), subset = Species == 'setosa')
}
df.sub
})
output$mytable <- DT::renderDataTable(df.sub(),
filter = 'top')
output$filters <- renderText({input$mytable_search_columns})
}
ui <- fluidPage(
h3('Button Toggle Filter'),
bsButton("buttonfilter","Show only Setosa", type = 'toggle'),
br(),
br(),
h3('Current filters'),
textOutput('filters'),
br(),
br(),
DT::dataTableOutput('mytable')
)
shinyApp(ui = ui, server = server)
What I'm trying to do is find a way to maintain the current DT filters when the table is re-rendered based on the subset initiated by the action button. In this example you can see the filters are cleared once the table is re-rendered.
我想要做的是找到一种方法,当表格根据操作按钮触发的子集重新渲染时,能够保持当前的 DT(DataTables)过滤器。在这个例子中,你可以看到一旦表格重新渲染,过滤器就会被清除。
问题解决:
I found a way without using JavaScript. I think this is what you want:
我找到了一个不使用JavaScript的方法。我想这就是你想要的:
library(shinyBS)
library(DT)
server <- function(input, output, session) {
df <- reactive({
if(input$buttonfilter %% 2 == 0){
df.sub <- iris
} else {
df.sub <- subset(iris, subset = Species == 'setosa')
}
df.sub
})
output$mytable <- DT::renderDataTable(isolate(df()), filter = 'top')
proxy <- dataTableProxy('mytable')
observe({
replaceData(proxy, df(), resetPaging = FALSE)
})
}
ui <- fluidPage(h3('Button Toggle Filter'),
bsButton("buttonfilter","Show only Setosa", type = 'toggle'),
br(),br(),
DT::dataTableOutput('mytable')
)
shiny::shinyApp(ui=ui,server=server)
We basically create a proxy for our table and just replace the data for the rendered table. For details check the very bottom of this page: Using DT in Shiny
我们基本上为我们的表格创建了一个代理,并只替换已渲染表格的数据。有关详细信息,请查看此页面底部的“在Shiny中使用DT”部分。
I did not find the example mentioned there on my computer but you can go to GitHub and copy and paste it:
我在我的电脑上没有找到那里提到的示例,但你可以访问GitHub并复制粘贴它: