Hi all,
I am trying to think of the best way to have a DT
render in my shiny app using default data (e.g. iris
), but also allow it up be editable. On the server side I feel like the data should be a reactive component (based on my mental model of observe
vs. reactive
), but I can't seem to get it working. The code below works, but it doesn't "feel" correct. Any experts see issues with my code?
The biggest problem is, since x
isn't reactive, I have to add input$foo_cell_edit
into any observeEvent(...)
I want to update when the table is updated. If x()
was reactive, I feel like that wouldn't be necessary.
library(shiny)
library(DT)
ui <- fluidPage(
textOutput('txt'),
dataTableOutput('foo')
)
server <- function(input, output, session) {
x <- iris
output$foo <- renderDataTable(datatable(x, editable = TRUE))
proxy <- dataTableProxy('foo')
observeEvent(input$foo_cell_edit, {
x <<- editData(x, input$foo_cell_edit, proxy)
})
output$txt <- renderText({
input$foo_cell_edit # necessary or text won't update on table edit
sum(x[, 'Sepal.Length'])
})
}
shinyApp(ui, server)