From 741e248211fdd3661dfb37364dbea14f1b3a832b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Todor=20Kondi=C4=87?= <todor.kondic@uni.lu>
Date: Mon, 2 Mar 2020 15:12:33 +0100
Subject: [PATCH] Add reset state

---
 R/shinyUI.R | 53 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 45 insertions(+), 8 deletions(-)

diff --git a/R/shinyUI.R b/R/shinyUI.R
index 5bb0b7b..c1f658f 100644
--- a/R/shinyUI.R
+++ b/R/shinyUI.R
@@ -122,6 +122,9 @@ mkUI <- function(fnStyle) {
                                                        label="Restore configuration.",
                                                        multiple=F,
                                                        title="Restore"),
+                          shiny::actionButton(inputId="resetConfB",
+                                              label="Reset config (CAUTION!)",
+                                              icon=shiny::icon("trash")),
                           width=NULL)
 
 
@@ -676,7 +679,11 @@ mk_shinyscreen <- function(projDir=getwd(),
     }
 
     proc_read<-function(wd) {
-    readRDS(file.path(wd,FN_SPEC))}
+        readRDS(file.path(wd,FN_SPEC))}
+
+    bak_fstate_pref <- function() {
+        format(Sys.time(),'%Y%m%d_%H_%M_%S_')
+    }
     server <- function(input,output,session) {
 
         ## ***** reactive values *****
@@ -1062,29 +1069,38 @@ mk_shinyscreen <- function(projDir=getwd(),
             df
         })
 
-        get_mtr<-shiny::reactive({
+        mtr_from_inps <- shiny::reactive({
             fnFT<-rvConf$fnFT
-            mtr<-rvTab$mtr
-            if (!is.null(mtr)) {
-                message("Grabbing existing mtr")
-                mtr
-            } else if (!file.exists(fnFT)) {
+            if (!file.exists(fnFT)) {
                 message("Generating the first instance of the state file table")
                 bdf <- gen_base_ftab()
                 df<-gen_clean_state_ftab(bdf)
                 tab2file(tab=df,file=fnFT)
                 message("Done generating the first instance of the state file table.")
-                rvTab$mtr<-df
                 df
             } else {
                 message("Reading in the state file table.")
                 df<-file2tab(fnFT,colClasses=c("rt"="numeric",
                                                "MS2rt"="numeric",
                                                "iMS2rt"="numeric"))
+                message("Done reading in the state file table.")
                 df
             }
         })
 
+        get_mtr<-shiny::reactive({
+            mtr<-rvTab$mtr
+            str(mtr)
+            if (!is.null(mtr)) {
+                message("Grabbing existing mtr")
+                return(mtr)
+            } else {
+                mtr <- mtr_from_inps()
+                rvTab$mtr <- mtr
+                return(mtr)
+            }
+        })
+
         get_comp_tab<-shiny::reactive({
 
             post_note("Started assembling the lists of knowns and unknowns into the `comprehensive' table.")
@@ -1461,6 +1477,27 @@ mk_shinyscreen <- function(projDir=getwd(),
             saveConf()
         })
 
+        shiny::observeEvent(input$resetConfB,{
+            pDir <- rvConf$projDir
+            shiny::req(rvTab$mtr,pDir,rvConf$fnFT)
+            post_note('Started cleaning up state.')
+            pref<-bak_fstate_pref()
+            fnCurr <- paste0(file.path(pDir,pref),
+                             rvConf$fnFT,'.current.bak.csv')
+            fnLast <- paste0(file.path(pDir,pref),
+                             rvConf$fnFT,'.prev.bak.csv')
+            tab2file(tab=rvTab$mtr,file=fnCurr)
+            post_note(paste('Current state backed up to ',fnCurr,' .',sep=''))
+            maybeSaved <- file.path(pDir,rvConf$fnFT)
+            if (isThingFile(maybeSaved)) {
+                file.copy(maybeSaved,fnLast)
+                post_note(paste('Also, last saved state backed up to ',fnLast,' .',sep=''))
+                unlink(maybeSaved,force = T)
+            }
+            rvTab$mtr<-NULL
+            post_note('State is now less dirty.')
+        })
+
         shiny::observeEvent(input$restoreConfB,{
             message("Restore event observed.")
             restoreConf()
-- 
GitLab