I am trying to capture edits to a datatable (mytable) and then create a second datatable (new_table) which reflects the edits. There is a separate input$rows and input$columns which determines how many rows/columns in the original mytable.
I can't seem to capture the output from the original mytable. Any help much appreciated
library(DT)
shinyApp(
ui = fluidPage(
textInput("rows",
"enter rows",
value = "2"),
textInput("columns",
"enter columns",
value = "3"),
DTOutput('mytable'),
DTOutput("new_table")
),
server = function(input, output) {
data <- reactive({
as.data.frame(matrix(" ",
as.numeric(input$rows),
as.numeric(input$columns)))
})
dfon <- reactiveValues(data = NULL)
output$mytable <- renderDataTable({
datatable(
data(),
editable = 'cell'
)
})
observeEvent(input$mytable_cell_edit, {
info = input$mytable_cell_edit
str(info)
#i = info$row
#j = info$col 1 # offset by 1
#v = info$value
#dfon$top[i, j] <<- DT::coerceValue(v, dfon$top[i, j])
dfon$data <<- editData(dfon$data, info)
})
output$new_table <- renderDataTable({
datatable(dfon$data)
})
}
)
CodePudding user response:
The simplest solution would be this:
library(shiny)
library(DT)
library(tidyverse)
shinyApp(
ui = fluidPage(
textInput("rows",
"enter rows",
value = "2"
),
textInput("columns",
"enter columns",
value = "3"
),
DTOutput("mytable"),
DTOutput("new_table")
),
server = function(input, output) {
data <- reactive({
req(input$rows, input$columns)
as.data.frame(matrix(
" ",
as.numeric(input$rows),
as.numeric(input$columns)
))
})
dfon <- reactiveValues(data = NULL)
observe({
dfon$data <- data()
})
output$mytable <- renderDataTable({
datatable(
data(),
editable = "cell"
)
})
observeEvent(input$mytable_cell_edit, {
info <- input$mytable_cell_edit
i <- info$row
j <- info$col
v <- info$value
dfon$data[i, j] <- v
})
output$new_table <- renderDataTable({
datatable(dfon$data)
})
}
)
We could use dataTableProxy, but it works for new rows, with new columns is trickier.