我正在尝试做一个
shiny
应用程序来下载过滤器Datatable
:
search
delete button
(下载部分按预期工作)
问题:当我第一次使用数据表中的搜索区域进行过滤时,如果我使用按钮删除一行,它会重置第一个过滤器
我的可重现示例:编辑工作解决方案
library(shinydashboard)
library(DT)
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Info boxes"),
dashboardSidebar(),
dashboardBody(
fluidRow(DT::dataTableOutput('data')),
fluidRow(p(class = 'text-center', downloadButton('x3', 'Download Filtered Data')))
)
)
server <- function(input, output) {
df <- reactiveValues(data = data.frame(
Value1 = 1:10,
Value2 = c("A", "B", "C", "D", "E"),
stringsAsFactors = FALSE,
row.names = 1:10
))
output$data <- DT::renderDataTable(
df$data, server = TRUE, filter = 'top', escape = FALSE, selection = 'none')
# download the filtered data
output$x3 = downloadHandler('emergence filtré.csv', content = function(file) {
s = input$data_rows_all
write.table(df$data[s,], file ,sep=";",row.names = F)
})
}
shinyApp(ui = ui, server = server)
谢谢你
我认为这是因为搜索过滤器是在客户端(在网络浏览器内)完成的,并且它并没有真正改变底层数据框架。一种可能的替代方案是向表添加过滤器并将其用作搜索功能,并设置服务器端处理。但这不会在一个盒子里进行全局搜索。
output$data <- DT::renderDataTable(
df$data, server = TRUE, filter = 'top', escape = FALSE, selection = 'none'
)