I am stealing the code off another forum How to edit and save changes made on Shiny dataTable using DT package. The solution from that forum (below) works fine. I notice there seems to be a bug in the "editable = TRUE" feature of the DataTable. Run the code below, and on the interface, change a cell, click on page 2 and back to page 1 again, your changes will be gone.
Can I know what's the issue here? Or is there a bug in the DataTable?
library(shiny)
library(DT)
Admit<-c("Admitted","Rejected","Admitted", "Rejected", "Admitted", "Rejected", "Admitted",
"Rejected","Admitted", "Rejected", "Admitted","Rejected","Admitted", "Rejected","Admitted","Rejected", "Admitted", "Rejected",
"Admitted","Rejected", "Admitted" ,"Rejected","Admitted", "Rejected")
Gender<-c("Male","Male","Female","Female", "Male", "Male", "Female", "Female", "Male","Male","Female","Female",
"Male","Male","Female","Female","Male", "Male", "Female", "Female","Male","Male","Female","Female")
Dept<-c( "A","A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F")
Freq<-c("512", "313", "89", "19", "353", "207", "17", "8", "120", "205", "202", "391", "138", "279", "131", "244", "53", "138",
"94", "299", "22", "351", "24", "317")
banking.df<-data.frame(Admit,Gender,Dept, Freq,stringsAsFactors = FALSE)
d1 = banking.df
d1$Date = Sys.time() + seq_len(nrow(d1))
# Define UI ----
ui <- fluidPage(
titlePanel("UC Berkley Admissions"),
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("Sample Bank",
DT::dataTableOutput("banking.df_data"),
br(),
actionButton("viewBtn","View"),
br(),
actionButton("saveBtn","Save"),
br(),
DT::dataTableOutput("updated.df")
))))
# Define server logic ----
server <- function(input, output) {
output$banking.df_data<-renderDataTable(
d1,selection = 'none', editable = TRUE,
rownames = TRUE,
extensions = 'Buttons',
options = list(
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bfrtip',
buttons = c('csv', 'excel')
),
class = "display"
)
observeEvent(input$banking.df_data_cell_edit, {
d1[input$banking.df_data_cell_edit$row,input$banking.df_data_cell_edit$col] <<- input$banking.df_data_cell_edit$value
})
view_fun<-eventReactive(input$viewBtn,{
if(is.null(input$saveBtn)||input$saveBtn==0)
{
returnValue()
}
else
{
DT::datatable(d1,selection = 'none')
}
})
observeEvent(input$saveBtn,{
write.csv(d1,'test.csv')
})
output$updated.df<-renderDataTable({
view_fun()
}
)
}
# Run the app ----
shinyApp(ui = ui, server = server)
Your original program was intended to display the changes in a second table below which is displayed when you click the view button. To maintain the changes in the top table and not display the second table, try the code below. Reset button is to go back to the initial data, if necessary.
Admit<-c("Admitted","Rejected","Admitted", "Rejected", "Admitted", "Rejected", "Admitted",
"Rejected","Admitted", "Rejected", "Admitted","Rejected","Admitted", "Rejected","Admitted","Rejected", "Admitted", "Rejected",
"Admitted","Rejected", "Admitted" ,"Rejected","Admitted", "Rejected")
Gender<-c("Male","Male","Female","Female", "Male", "Male", "Female", "Female", "Male","Male","Female","Female",
"Male","Male","Female","Female","Male", "Male", "Female", "Female","Male","Male","Female","Female")
Dept<-c( "A","A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F")
Freq<-c("512", "313", "89", "19", "353", "207", "17", "8", "120", "205", "202", "391", "138", "279", "131", "244", "53", "138",
"94", "299", "22", "351", "24", "317")
banking.df<-data.frame(Admit,Gender,Dept, Freq,stringsAsFactors = FALSE)
d1 = banking.df
d1$Date = Sys.time() + seq_len(nrow(d1))
library(shiny)
library(DT)
ui <- fluidPage(
titlePanel("UC Berkley Admissions"),
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("Sample Bank",
DT::DTOutput("banking.df_data"),
br(),
actionButton("saveBtn","Save"),
br(),
actionButton(inputId = "reset", label = "Reset")
))))
server <- function(input, output) {
DF1 <- reactiveValues(data=d1)
orig_d1 <- d1
output$banking.df_data<-renderDT(
DF1$data,selection = 'none', editable = TRUE,
rownames = TRUE,
extensions = 'Buttons',
options = list(
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bfrtip',
buttons = c('csv', 'excel')
)#, class = "display"
)
observeEvent(input$banking.df_data_cell_edit, {
req(input$banking.df_data_cell_edit)
d1[input$banking.df_data_cell_edit$row,input$banking.df_data_cell_edit$col] <<- input$banking.df_data_cell_edit$value
DF1$data <- d1
})
observeEvent(input$saveBtn,{
write.csv(d1,file='C:/My Disk Space/_My Work/RStuff/GWS/test.csv')
})
observeEvent(input$reset, {
DF1$data <- orig_d1
})
}
shinyApp(ui = ui, server = server)