Hello I use R shiny DT library to implement table input. I find that I need to double click the cells, then I can fill in the cells. I want to ask how can I change the code to make it input the data directly without double clicking.
I asked ChatGPT, it provide the code. But it does not work.
library(shiny)
library(DT)
jscode <- "
function cell_editCallback(table) {
table.on('click.dt', 'td', function() {
var index = table.cell(this).index();
Shiny.setInputValue('table_cell_edit', {row: index[0], col: index[1]});
});
}
"
shinyApp(
ui = fluidPage(
DTOutput("table")
),
server = function(input, output, session) {
data <- iris
# Define callback for direct editing
session$onFlushed(function() {
shinyjs::runjs(jscode)
})
output$table <- renderDT(
data,
callback = JS("cell_editCallback", "table"),
selection = 'none', # Prevent row selection
server = FALSE # Disable server-side processing
)
observeEvent(input$table_cell_edit, {
row <- input$table_cell_edit$row
col <- input$table_cell_edit$col
data[row, col] <- isolate(input$table_cell_edit$value)
})
}
)