From 3480c5e40d0511e2c40415f2c2545e47da097fde Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Todor=20Kondi=C4=87?= <todor.kondic@uni.lu>
Date: Thu, 30 Apr 2020 11:11:19 +0200
Subject: [PATCH] Add saving configuration

* R/shiny-ui-base.R(obsrv_e,validate,vol_f): New wrappers.

* R/shiny-ui-config.R(saveConfB): Change default filename and
  filetype.

* R/shiny-ui-config.R(input$saveConfB): New observer.
---
 R/mix.R             |  5 +++++
 R/shiny-ui-base.R   | 10 ++++++++++
 R/shiny-ui-config.R | 14 +++++++++++---
 3 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/R/mix.R b/R/mix.R
index 74b70be..e9ea8fe 100644
--- a/R/mix.R
+++ b/R/mix.R
@@ -592,3 +592,8 @@ read_setid <- function(fn,known,unk) {
     assertthat::assert_that(nrow(natmp)==0,msg=paste("The following IDs from set table have not been found in the compound table:","------",print_table(natmp),"------",sep = "\n"))
     tmp
 }
+
+
+write_conf <- function(conf,fn) {
+    yaml::write_yaml(x=conf,file=fn)
+}
diff --git a/R/shiny-ui-base.R b/R/shiny-ui-base.R
index 62e8c48..70787e1 100644
--- a/R/shiny-ui-base.R
+++ b/R/shiny-ui-base.R
@@ -16,7 +16,11 @@ react_v <- shiny::reactiveValues
 react_f <- shiny::reactive
 react_e <- shiny::eventReactive
 obsrv <- shiny::observe
+obsrv_e <- shiny::observeEvent
 vols <- shinyFiles::getVolumes
+vol_f <- vols()
+validate <- function(expr,msg) shiny::validate(need(expr,msg))
+
 
 path2vol <- function(path) {
     ## This function returns shinyFiles compatible volumes.
@@ -81,3 +85,9 @@ txt_file_input <- function(inputId,input,fileB,label,volumes) {
     }
     
 }
+
+rv_conf2conf <- function(rv) {
+    x <- shiny::reactiveValuesToList(rv$conf)
+    x$compounds<-shiny::reactiveValuesToList(rv$conf$compounds)
+    x
+}
diff --git a/R/shiny-ui-config.R b/R/shiny-ui-config.R
index 8793203..3820798 100644
--- a/R/shiny-ui-config.R
+++ b/R/shiny-ui-config.R
@@ -67,8 +67,8 @@ mk_ui_config <- function() {
                           shinyFiles::shinySaveButton("saveConfB",
                                                       "Save configuration.",
                                                       title="Save",
-                                                      filename = "conf-state.rds",
-                                                      "rds"),
+                                                      filename = "conf-state.yaml",
+                                                      "yaml"),
                           shinyFiles::shinyFilesButton("restoreConfB",
                                                        label="Restore configuration.",
                                                        multiple=F,
@@ -135,7 +135,7 @@ react_conf_f <- function(input,output,session,rv,rf) {
         vls <- vols()() #Ugly! :)
         vol <- path2vol(path)
         sel<-match(vol,vls)
-        validate(need(sel,"Yikes! Unable to detect current project's volume."))
+        validate(sel,msg="Yikes! Unable to detect current project's volume.")
         res<-names(vls)[[sel]]
         res
     })
@@ -174,6 +174,14 @@ server_conf <- function(input,output,session,rv,rf) {
                                 defaultPath=dpath(),roots=vs)
     shinyFiles::shinyDirChoose(input, 'switchProjB',roots=vs)
 
+    obsrv_e(input$saveConfB, {
+        conf<-rv_conf2conf(rv)
+        vol <- vol_f()
+        fn <- shinyFiles::parseSavePath(roots=vol_f,input$saveConfB)[["datapath"]]
+        validate(fn,msg="Invalid file to save config to.")
+        write_conf(conf,fn)
+    })
+
   
     
 }
-- 
GitLab