简体   繁体   English

在 R Shiny 中结合 DT::datatable、DT::dataTableProxy 和“SearchPanes”扩展

[英]Combining DT::datatable, DT::dataTableProxy and "SearchPanes" Extension in R Shiny

It appears that DT::dataTableProxy is not possible with SearchPanes extension because: SearchPanes扩展似乎无法使用DT::dataTableProxy ,因为:

  • SearchPanes requires Select extension. SearchPanes 需要Select扩展名。
  • Select extension requires DT::renderDT(server = FALSE) option. Select扩展需要DT::renderDT(server = FALSE)选项。
  • DT::dataTableProxy does not work on the client side and throws DT error. DT::dataTableProxy在客户端不起作用并抛出 DT 错误。
library(shiny)
library(shinydashboard)
library(tidyverse)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    shiny::selectInput("rows", label = "Rows", choices = 1:nrow(mtcars)),
    shiny::actionButton("new", label = "New Data")
  ),
  dashboardBody(DT::dataTableOutput("cars"))
)

server <- function(input, output) { 
  rows <- reactive({ input$rows })
  output$cars <- DT::renderDataTable(server = FALSE, {
    expr = DT::datatable(
      data = mtcars |> head(rows()) 
      #,
      #extensions = c("SearchPanes", "Select", "Buttons"),
      #options = list(
      #  dom = "Btip",
      #  buttons = list("searchPanes")
      #)
    )

  })
  
  dtProxy <- DT::dataTableProxy("cars")
  
  observeEvent(input$new, label = "Observe button proxy update", {
    doubledata <- bind_rows(mtcars, mtcars)
    DT::replaceData(proxy = dtProxy, 
                    data = doubledata,
                    resetPaging = FALSE)
  })
}

shinyApp(ui, server)

Try this code using server = FALSE, click New Data , you will receive DT Warning: DataTables warning: table id=DataTables_Table_0 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1使用 server = FALSE 尝试此代码,单击New Data ,您将收到 DT Warning: DataTables warning: table id=DataTables_Table_0 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 DataTables warning: table id=DataTables_Table_0 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

Remove server = FALSE and proxy runs.删除server = FALSE和代理运行。

Remove the commented section, and search panes appear, but with no actual filters represented, and message stating that if we really want to use select extension then set select = 'none' .删除注释部分,搜索窗格出现,但没有显示实际的过滤器,并且消息表明如果我们really want to use select extension then set select = 'none'

Here are some reference materials: RStudio DT Extensions Matt Herman Tutorial这里有一些参考资料: RStudio DT Extensions Matt Herman Tutorial

What I ended up doing was use only the DT::datatableProxy feature, then use a custom button for the search panes.我最终做的是仅使用DT::datatableProxy功能,然后为搜索窗格使用自定义按钮。 Custom button was found here How to add custom button in R Shiny datatable .在此处找到自定义按钮如何在 R Shiny 数据表中添加自定义按钮 This required making a new reactive which was invalidated by the first, and checking if the inputs had any new values.这需要制作一个被第一个无效的新反应,并检查输入是否有任何新值。 Then the proxy received the filtered data.然后代理收到过滤后的数据。

Maybe someday they will add support for search panes.也许有一天他们会添加对搜索窗格的支持。

Add server-side support for SearchPanes extension #877添加对 SearchPanes 扩展的服务器端支持 #877

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM