How to edit and save changes made on Shiny dataTable using DT package
Asked Answered
F

2

14

I would like to edit the table and save the changes to the example below. Here's what I have so far.

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)


# Define UI ----
ui <- fluidPage(
  titlePanel("UC Berkley Admissions"),

    mainPanel(
      tabsetPanel(
        id = 'dataset',
        tabPanel("Sample Bank", DT::dataTableOutput("banking.df"),
                 br(),
                 actionButton("saveBtn","Save")))))

# Define server logic ----
server <- function(input, output) {
  d1 = banking.df
  d1$Date = Sys.time() + seq_len(nrow(d1))
  
  output$banking.df=render_dt(d1,'cell')
  
  #edit a single cell
  proxy5=dataTableProxy('banking.df')
  observeEvent(input$banking.df_cell_edit,{
    info=input$banking.df_cell_edit
    str(info)
    d1<<-editData(d1,info)
    replaceData(proxy5,d1,resetPaging = FALSE)
  })
  
  #observeEvent(input$saveBtn,write.csv(input$banking.df),
  #             file="NewData.csv",row.names=FALSE)
}



# Run the app ----
shinyApp(ui = ui, server = server)

I am able to edit the table and it will store the changes. However, whenever I try to make an edit that is new to the particular column, it does not save the changes and it leaves it as blank. For example, if I type "H" on the dept. column that change is not allowed. How can I fix this?

Also, how can I use the "save button" so that it can overwrite the banking.df?

Fortyfive answered 12/11, 2019 at 5:23 Comment(0)
B
25

I tried your code and modified few elements. Few points need to be cleared:

  • You can use DT properties editable=TRUE.
  • To download as csv or excel you can use DT default CSV,Excel button.It can store the edited data.
  • If you want to use separate Button to Download you can simply use write.csv() for that.

Code: UI.R

library(shiny)
library(DT)

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")
               ))))

Server.R

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))

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()
  }
  )
}

Hope this Helps. enter image description here

Blazer answered 12/11, 2019 at 13:49 Comment(2)
This is great. Thank you so much!Fortyfive
d1 <<- editData(d1, input$banking.df_data_cell_edit) within your observeEvent is a succinct way of doing d1[input$banking.df_data_cell_edit$row,input$banking.df_data_cell_edit$col] <<- input$banking.df_data_cell_edit$valuePrerogative
E
0

Just one small comment to Subhasish1315's amazing code: it is working for me with a small correction. Apparently DT starts column number at 0, while R starts at 1, so it should be:

observeEvent(input$banking.df_data_cell_edit, {
    d1[input$banking.df_data_cell_edit$row,input$banking.df_data_cell_edit$col**+1**] <<- input$banking.df_data_cell_edit$value
  })

See Shiny DT editing saves in the wrong column for a discussion of the issue.

Erastus answered 7/9, 2022 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.