From f339e61db788c6ee71930bf74a11cd2e1034c5db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Todor=20Kondi=C4=87?= <todor.kondic@uni.lu> Date: Thu, 30 Apr 2020 07:18:48 +0200 Subject: [PATCH] Introduce modular reactive values/functions concept Because we decided to split the server and UI definitions into separate "modules", we also needed to create a way for different modules to exchange their reactive values and functions. In this regard, each module is going to have two server-like functions, react_module-name_f and react_module-name_v, which will define and return reactive functions and objects, respectively. Each such function is server-like, i.e. it will take input, output and session arguments. The two remaining arguments are rv and rf, for the input reactive values and functions. * R/shiny-ui-config.R(react_conf_v): New function. Add conf-related reactive values. (react_vonf_f): New function. Add conf-related reactive functions. (server_conf): Update observers. * R/shiny-ui-top.R(server): Adapt to the new concept. --- R/shiny-ui-config.R | 59 ++++++++++++++++++++++++++++++++++++++++++++- R/shiny-ui-top.R | 12 ++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/R/shiny-ui-config.R b/R/shiny-ui-config.R index b4ed132..8793203 100644 --- a/R/shiny-ui-config.R +++ b/R/shiny-ui-config.R @@ -118,5 +118,62 @@ mk_ui_config <- function() { side=confSideItem)) } +react_conf_v <- function(input,output,session,rv,rf) { + rv$conf <- react_v(data=CONF$data, + project=CONF$project, + compounds=react_v(known=CONF$compounds$known, + unknown=CONF$compounds$unknown, + sets=CONF$compounds$sets)) + + rv +} + +react_conf_f <- function(input,output,session,rv,rf) { + rf$get_proj_vol <- react_e(rv$conf$project,{ + ## For shinyfiles dialogs. + path <- normalizePath(rv$conf$project, winslash = '/') + vls <- vols()() #Ugly! :) + vol <- path2vol(path) + sel<-match(vol,vls) + validate(need(sel,"Yikes! Unable to detect current project's volume.")) + res<-names(vls)[[sel]] + res + }) + + rf$get_proj_path <- react_e(rv$conf$project,{ + ## For shinyfiles dialogs. + wd <- rv$conf$project + vol <- rf$get_proj_vol() + v <- vols()() + pref<-v[[vol]] + res<-wd + message('Relative project path is: ',res) + res + }) + + rf +} -server_conf <- function(input,output,session) {} +server_conf <- function(input,output,session,rv,rf) { + ## ***** shinyFiles observers ***** + droot <- rf$get_proj_vol + dpath <- rf$get_proj_path + vs <- vols() + shinyFiles::shinyFileChoose(input, 'impKnownListB',defaultRoot=droot(), + defaultPath=dpath(),roots=vs) + shinyFiles::shinyFileChoose(input, 'impUnkListB',defaultRoot=droot(), + defaultPath=dpath(),roots=vs) + shinyFiles::shinyFileChoose(input, 'impSetIdB',defaultRoot=droot(), + defaultPath=dpath(),roots=vs) + + shinyFiles::shinyFileSave(input, 'saveConfB',defaultRoot=droot(), + defaultPath=dpath(),roots=vs) + shinyFiles::shinyFileChoose(input, 'restoreConfB',defaultRoot=droot(), + defaultPath=dpath(),roots=vs) + shinyFiles::shinyFileChoose(input, 'mzMLB',defaultRoot=droot(), + defaultPath=dpath(),roots=vs) + shinyFiles::shinyDirChoose(input, 'switchProjB',roots=vs) + + + +} diff --git a/R/shiny-ui-top.R b/R/shiny-ui-top.R index c92b56b..241a68d 100644 --- a/R/shiny-ui-top.R +++ b/R/shiny-ui-top.R @@ -46,7 +46,17 @@ mk_ui <- function (fn_style) { mk_shinyscreen <- function(fn_style=system.file('www/custom.css',package = 'shinyscreen')) { server <- function(input,output,session) { ## Top-level server function. - server_conf(input,output,session) + rv <- shiny::reactiveValues(dummy=1) # Container for all + # reactive values. + + rf <- list() # Container for all + # reactive functions. + + rv <- react_conf_v(input,output,session,rv=rv,rf=rf) # Config related r. values. + rf <- react_conf_f(input,output,session,rv=rv,rf=rf) # Config related r. functions. + + ## Observers and renderers. + server_conf(input,output,session,rv=rv,rf=rf) session$onSessionEnded(function () { stopApp() }) -- GitLab