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

标签 r json shiny shiny-server dt

我有一个 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() 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.

关于r - 使用客户端处理(服务器 = F)在 Shiny 应用程序中进行 DT 编辑会抛出 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52519367/

相关文章:

javascript - 将 JSON 的 php 字符串(带引号)传递给 onclick 函数

r - 更改 Shiny 的谷歌地图热图选项

css - 在 Shiny 中,如何为范围 slider 的两个按钮使用不同的颜色?

r - randomForest、randomForestSRC 或 cforest 中单棵树的重要性可变吗?

r - 如何更改 visreg 中交互图的颜色和线型

r - 如何将多行Excel单元格读入R

android - 使用 json 将一些数据从 android 发布并返回到 php 时出错

javascript - 在 React 中,如何检查输入单选是否已定义?

r - 如何渲染 HTML 风格的传单标签?

r - 为什么从 Map 返回时 data.table 会被复制