使用客户端处理(服务器 = F)在 Shiny 应用程序中进行 DT 编辑会抛出 JSON 错误

DT Editing in Shiny application with client-side processing (server = F) throws JSON Error

我有一个 Shiny Server 应用程序,用户可以在其中编辑数据表,之后一些反应性摘要统计信息会相应更新。我在一个相当慢的框架上托管这个应用程序,这就是为什么我想使用客户端处理进行 DT 渲染,即 server = F 传递给 DT::renderDataTable。让我分解一下我的问题的要点:

DataTables warning: table id=DataTables_Table_5 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

一件有趣的事情是,当这个错误 window 被消除时,相关摘要统计信息会根据编辑正确更新,并且 Shiny 应用程序会继续 。因此,除错误外,一切正常。我应该注意到,我访问了错误中提到的站点,但没有变得更聪明。

下面的可重现示例:

library(shiny)
library(DT)

dt = data.frame(V1 = c(1,2), V2 = c(3,4))

server <- function(input, output, session) {

val = reactiveValues(mat = data.table(dt))

output$testDT = renderDataTable({
            DT::datatable(val$mat, editable = TRUE)
}, server = FALSE)

proxy = dataTableProxy('testDT')

observeEvent(input$testDT_cell_edit, {

  info = input$testDT_cell_edit

  str(info)

  i = info$row
  j = info$col
  v = info$val

  if (j == 1){

  val$mat$V1[i] = DT::coerceValue(v, val$mat$V1[i])
  replaceData(proxy, val$mat, rownames = FALSE)

  }

})


}

ui <- fluidPage(
  dataTableOutput('testDT') 
)

shinyApp(ui, server)

谢谢!

已在 the Github thread 上回答,我在这里分享我的回答。

Probably it's not documented clearly. It has nothing to do with the editing. It's because replaceData() calls reloadData(), which requires the server-side processing mode. See ?reloadData().

reloadData() only works for tables in the server-side processing mode, e.g. tables rendered with renderDataTable(server = TRUE). The data to be reloaded (i.e. the one you pass to dataTableAjax()) must have exactly the same number of columns as the previous data object in the table.