Edit and Save Button in Shiny dashboard - Not working properly

385 views
Skip to first unread message

Venkatachalam Mahadevan

unread,
Jun 7, 2021, 2:26:44 AM6/7/21
to Shiny - Web Framework for R
Hi guys,

I am trying to create a shiny dashboard with few buttons (save, edit, download and view)

I did not create a button for edit. Instead, I used the following code,
output$initial_data <- DT::renderDataTable({
    DT::datatable(d1,  
                  editable = list(target = 'cell', 
                                  disable = list(columns = c(0,1)))
    ) 
  }) 

I created a proxy table for the edits to save temporarily because without proxy tables I do not know to capture the values being edited
I created a save button to save the edits permanently.

The issues:
  • Along with the save button, I created a pop-up window asking if I want to save the data. If yes then save the edits permanently else don't save the edits being done in that session.
  • The code which I have attached is not working perfectly. So, to check multiple scenarios I reran the session multiple times and a new index named x1, x2, x3 and so on are generating for some reason each time I rerun it.
  • By rerunning the same  shinyApp(ui, server) 4 to 5 times the app starts to malfunction.
It would be great if someone guides me in fixing these.

Thanks for your time!

My code:

library(shiny)
library(shinydashboard)
library(DT)
library(readxl)
library(shinyWidgets)


ui <- dashboardPage(
  dashboardHeader(title = "Validation Report"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Conversions",tabName = "conversions",icon = icon("check"))
      
    )
  ),
  
  dashboardBody(
    
      tabItem(tabName = "conversions",
              h2("sample data"),
              DT::dataTableOutput("sample_data"),
              br(),
              actionButton("viewBtn","View"),
              br(),
              
              verbatimTextOutput(outputId = "res1"),
              tags$br(),
              actionButton(
                inputId = "saveBtn",
                label = "Save"),
              
              
              downloadButton("download1", "Download CSV"), # no label: this button will be hidden
              br(),
              
              DT::dataTableOutput("updated.df")
      )
    
  )
)

server <- function(input,output,session) {
  d1 <- read.csv("work.csv")
  output$sample_data <- DT::renderDataTable({
    DT::datatable(d1,
                  editable = list(target = 'cell', 
                                  disable = list(columns = c(0,1)))
    )
  })
  
  
  output$download1 <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      #d1$Value <- format(d1$Value,big.mark = ",")
      write.csv(d1, file)
    }
  )
  
  
  proxy5 = dataTableProxy('sample_data')
  observeEvent(input$sample_data_cell_edit, {
    info = input$sample_data_cell_edit
    #str(info)  # check what info looks like (a data frame of 3 columns)
    observeEvent(input$myconfirmation2, {
      if (isTRUE(input$myconfirmation2)) {
        d1 <<- editData(d1, info)
        replaceData(proxy5, d1, resetPaging = FALSE) 
        unlink("work.csv")
        write.csv(d1,'work.csv')
      }
      else
      {
        last_df <- read.csv("work.csv")
        output$sample_data <- DT::renderDataTable({
          DT::datatable(last_df,
                        editable = list(target = 'cell',
                                        disable = list(columns = c(0,1)))
          )
        })
        
      }
    })
    
  })
  
  
  
  
  view_fun<-eventReactive(input$myconfirmation2,{
    if(is.null(input$myconfirmation2)||input$myconfirmation2==0)
    {
      returnValue()
    }
    else
    {
      DT::datatable(d1,selection = 'none')
    }
    
  })
  
  
  observeEvent(input$myconfirmation2, {
    if (isTRUE(input$myconfirmation2)) {
      output$res1 <- renderText(invisible("Saved Successfully"))
    }
    else {
      output$res1 <- renderText(invisible(" Unsuccessfully"))
    } 
  })
  
  
  observeEvent(input$saveBtn, {
    
    confirmSweetAlert(
      session = session,
      inputId = "myconfirmation2",
      type = "warning",
      title = "Are you sure you want to save?",
      btn_labels = c("No", "Yes"),
      btn_colors = c("#FE642E", "#04B404")
    )
    
    
  })
  observeEvent(input$myconfirmation2, {
    if (isTRUE(input$myconfirmation2)) {
      write.csv(d1,'work.csv')
    }
    else
    {
      last_df <- read.csv("work.csv")
      output$sample_data <- DT::renderDataTable({
        DT::datatable(last_df,
                      editable = list(target = 'cell',
                                      disable = list(columns = c(0,1)))
        )
      })
      
    }
    
  })
  
  output$updated.df<-renderDataTable({
    view_fun()
  }
  )
  
  
  
}
shinyApp(ui,server)


Reply all
Reply to author
Forward
0 new messages