Persistent data r shiny to access db

345 Views Asked by At

I am trying to find a way to load and save data to a Microsoft Access File which needs to have persistent data. When click load, the input ideally will load all user saved data before beginning updates and save can be used to save the data before closing the app. I have been trying for a day to solve this so any help is greatly appreciated.

Also if a team was running this application and multiple instances of this were open, would a Access file be best or will a excel or csv work too (since technically no one is opening the xl or csv)

I am super new to R and just started my journey with this language. Any help is greatly appreciated. Thank you in advance again!

library(shinydashboard)
library(shinydashboardPlus)
library(DT)
library(tidyverse)
library(fontawesome)

Input <-  structure(list(`LOT ID` = c("", "", ""), StepSeq = c("", "", ""), `Todays Date` = structure(c(
    16070,
    17084, 17084
  ), class = "Date"), `Due Date` = structure(c(
    18627,
    NA, 18545
  ), class = "Date"), `Name` = c(
    "",
    "", "")),
  class = c(
    "spec_tbl_df", "tbl_df", "tbl",
    "data.frame"
  ), row.names = c(NA, -3L))
# 
# Input <- read.csv("\\***\***\***\\***\**\**SM\**\Mohit\WikiStepSkip\WikiStepData\Book.accda")

shinyApp(
  ui <- tags$body(class = "skin-blue sidebar-mini control-sidebar-open", 
  dashboardPage(
    options = list(sidebarExpandOnHover = TRUE),
    header = dashboardHeader(title = "Wiki Step Skip Waitlist", titleWidth = 300),
    sidebar = dashboardSidebar(
      minified = F, collapsed = F,
      textInput(
        "LOTID", "LOT.ID",
        c(unique(Input$`LOT ID`))
      ),
      textInput(
        "STEPSEQ", "STEP SEQ",
        c(unique(Input$StepSeq))
      ),
      dateInput("TODDATE", "Today's Date", value = as.Date("2022-05-22")),
      dateInput("DUEDATE", "Complete By Date", value = as.Date("2022-05-22")),
      textInput(
        "nm", "Name (mandatory)",
        c(unique(Input$`Name`))
      ),
      
      actionButton("add", "Add"),
      actionButton("save", "save"),
      actionButton("load", "load"),
      actionButton("deleteRows", "Delete Rows")
      
    ),
    
    body = dashboardBody(
      h3("Wiki Step Skip Waitlist"),
      tabsetPanel(id = "tabs",
        tabPanel("Current Steps in Waitlist",
          dataTableOutput("TBL1")),
        tabPanel("All Wiki Steps"),
          
      )
    ),
    controlbar = dashboardControlbar(width = 50),
    title = "DashboardPage"
  )), 
  
  ###### SERVER #########
  server <- function(input, output, session) {
    
    #Load
    observeEvent(input$Load, {
      Input <- load("Book.xlsx")
      Load_done <- showNotification(paste("Message", "Data Has been loaded"), duration = NULL)
    })

    # Init with some example data
    data <- reactiveVal(Input)
    rv <- reactiveValues(df = Input, row_selected = NULL)
    formData <- reactive({
      data <- sapply(fields, function(x) input[[x]])
      data
    })
    
    observeEvent(
      input$add,
      {
        #start with current data
        rv$df <- rv$df %>%
          add_row(
            `LOT ID` = isolate(input$LOTID),
            StepSeq = isolate(input$STEPSEQ),
            `Todays Date` = isolate(input$TODDATE),
            `Due Date` = isolate(input$DUEDATE),
            `Name` = isolate(input$nm)
          )#  %>%
        # update data value
        #data()
        
      }
    )
    
    observeEvent(input$deleteRows,{
      if (!is.null(input$TBL1_rows_selected)) {
        #data(data()[-as.numeric(input$TBL1_rows_selected),])
        rv$df <- rv$df[-as.numeric(input$TBL1_rows_selected), ]
      }
    })
    
    observeEvent(input$load,{
      if (!is.null(input$TBL1_rows_selected)) {
        cols_to_save <- c('LOTID', 'STEPSEQ', 'TODDATE', 'DUEDATE', 'nm')
        colnms <- c('LOT.ID', 'StepSeq', 'Today Date', 'Sale Date', 'Step Seq')
        "remember the row selected"
        rv$row_selected <- input$TBL1_rows_selected
        
        walk2(cols_to_save, colnms, ~{rv$df[input$TBL1_rows_selected, ..2] <<- input[[..1]]}) 
        
      }
      
    })
    
    output$TBL1 <- renderDataTable(
      rv$df,selection="multiple"
    
    )
    
    #Saving data
    observeEvent(input$add, {
      savedata <<- input$data
      save(savedata,  file = "Book.accda")
      Save_done <- showNotification(paste("Message", "Data Has been saved"), duration = NULL)
    })

  }
)
0

There are 0 best solutions below