DT 和 Plotly 之间的冲突以在 table 中显示 NA

Conflict between DT and Plotly to show NA in table

有一个解决方案可以在 table 中显示 NAInf 值,使用 options(htmlwidgets.TOJSON_ARGS = list(na = 'string')) 创建的 DT,如 here 所述。

问题是htmlwidgets.TOJSON_ARGS也影响了plotly。下面是问题的简单示例。

library(shiny)
library(DT)
library(plotly)

options(htmlwidgets.TOJSON_ARGS = list(na = 'string'))
dat <- data.frame(x = c(1, NA, NA, 4, 5), y = 1:5)
shinyApp(
  ui = fluidPage(
    plotlyOutput("plot"),
    DTOutput('tbl')
  ),
  server = function(input, output) {
    output$plot <- renderPlotly({
      plot_ly(data = dat, x =~x, y =~y)
    })
    output$tbl = renderDataTable(dat)
  }
)

table 正确显示了 NA 值,但绘图不起作用并显示 Error: formal argument "na" matched by multiple actual arguments

可以设置htmlwidgets.TOJSON_ARGS只影响table而不影响情节吗?或者为了避免 plotly 的错误而使用的正确参数是什么?

上面的代码是一个非常基本的示例,该解决方案应该适用于多个 tables 和用户与数据交互时动态创建的绘图。因此,删除 options(htmlwidgets.TOJSON_ARGS = list(na = 'string')) 并将数据转换为字符串不是一种选择,因为它会破坏 table 的排序功能。在table的columnDefs参数中也不使用自定义JS代码来显示NA,因为它对于大型数据集来说效率低下。

如有任何建议或意见,我们将不胜感激。

DT 的开发版本现在有一个新选项 DT.TOJSON_ARGS,应该可以解决您的问题。参见 rstudio/DT#536