rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
587 stars 184 forks source link

Issue using `replaceData()` with `dataTableProxy()` #1101

Closed gadenbuie closed 7 months ago

gadenbuie commented 7 months ago

AFAICT, this issue was introduced in #1096. We found this in the nightly shiny tests, which started to fail the day after the day #1096 was merged (and then it took me a little bit to triage and track down the issue).

Here's a small reprex app:

library(shiny)
library(DT)

ui <- fluidPage(
  DTOutput("table"),
  actionButton("inc", "Increment")
)

server <- function(input, output, session) {
  x <- reactiveVal(0L)

  df <- reactive({
    data.frame(value = x())
  })

  output$table <- renderDT({
    DT::datatable(
      isolate(df()),
      rownames = FALSE,
      options = list(dom = "t", ordering = FALSE)
    )
  })

  observeEvent(input$inc, {
    message("incrementing x: ", x(), " -> ", x() + 1L)
    x(x() + 1L)
  }, ignoreNULL = TRUE)

  observeEvent(x(), {
    req(x() > 0)
    message("replacing table data")
    replaceData(dataTableProxy("table"), df())
  })
}

shinyApp(ui, server)

The expected behavior is for the table to be a single column (value) with a single row. When clicking on the Increment button, the value should increment by one, using replaceData(dataTableProxy("table"), df()).

The current behavior I'm seeing is that the app starts as expected

image

but then after clicking the increment button to update the data, the table shows no rows.

image

stla commented 7 months ago

It works if we set rownames=FALSE:

replaceData(dataTableProxy("table"), df(), rownames = FALSE)
yihui commented 7 months ago

Right, if the original table was rendered with rownames = FALSE, the new data should also use rownames = FALSE when replacing the old data.

I've tested CRAN releases 0.30 and 0.29. The problem doesn't seem to be new, i.e., both CRAN versions can reproduce the problem. I don't know why it didn't happen before...

yihui commented 7 months ago

@gadenbuie Could you check if rownames = FALSE works? Thanks!

gadenbuie commented 7 months ago

@yihui it resolves the issue in the reprex! It didn't resolve the new issue in our test apps, but I realized the test app used replaceData(dataTableProxy("table"), x()) (replacing the table with a value, not a data frame), so maybe that was unintentionally supported until recently. Anyway, I've updated our test app with both fixes and I think we can consider this resolved. Thanks all!