我有一个 Shiny 的服务器应用程序,用户可以在其中编辑数据表,然后一些 react 性摘要统计信息会相应更新。我将此应用程序托管在一个相当慢的框架上,这就是为什么我想使用客户端处理进行 DT 渲染,即将 server = F
传递给 DT::renderDataTable
。让我分解一下我的问题的要点:
当传递
server = T
时,代码即可完全运行。当传递
server = F
时,当用户编辑DT中的单元格时,浏览器会抛出以下错误消息:
DataTables warning: table id=DataTables_Table_5 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
有趣的是,当此错误窗口关闭时,依赖的摘要统计信息会根据编辑正确更新,并且 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()
callsreloadData()
, which requires the server-side processing mode. See?reloadData()
.block 引用>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.
关于r - 使用客户端处理(服务器 = F)在 Shiny 应用程序中进行 DT 编辑会抛出 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52519367/