Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reactableUpdate not preserving matrix dimensions #360

Open
dleopold opened this issue Feb 21, 2024 · 1 comment
Open

reactableUpdate not preserving matrix dimensions #360

dleopold opened this issue Feb 21, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@dleopold
Copy link

It looks like the updateReactable function does not properly preserve matrix dimensions. Here is a toy example:

library(shiny)
library(reactable)

ui <- fluidPage(
  reactableOutput("table"),
  actionButton("update", "Update table")
)

server <- function(input, output) {
  
  mx <- matrix(
    nrow = 8,
    ncol = 12,
    dimnames = list(
      LETTERS[1:8],
      1:12
    )
  )
  mx[,] <- sample(c(T,F), 8 * 12, replace = TRUE)
  
  output$table <- renderReactable({
    reactable(
      mx,
      rownames = TRUE,
      columns = list(
        .rownames = colDef(
          cell = function(value) {value}
        )
      )
    )
  })
  
  observeEvent(input$update, {
    mx[,] <- sample(mx, 8 * 12)
    print(dim(mx))
    updateReactable("table", data = mx)
  })
  
}
shinyApp(ui = ui, server = server)

simply changing the update call to:
updateReactable("table", data = as.data.frame(mx))
seems to result in a correct update, but the documentation suggests that the data argument can accept a matrix.

Also, updating the matrix without the colDef specification for the .rownames column in the initial render function causes the row names to be dropped from the rendered table. This happens regardless of whether in input and/or update is a matrix or data.frame.

@glin glin added the bug Something isn't working label Mar 31, 2024
@glin
Copy link
Owner

glin commented Mar 31, 2024

Thanks for the report and repro. Internally, matrices are handled by just running as.data.frame() on them, and row names are done by appending them to the data frame as a new column. Both of these are missing from updateReactable(). Matrices should be fairly straightforward to fix but row names are trickier. updateReactable() might have to take a rownames argument as well, for lack of a way to know whether the original table had rownames. Or always add the row names, which isn't super ideal, hmm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants