How to implement dynamically loading/unloading modules in R(while waiting for one module to finish execution)?
I'm trying to convert a desktop Shiny app that used a driver script and multiple separate apps to a server app running a main app while converting each individual app to modules. Previously, the code used runApp() and stopApp() to communicate information between the driver script and the parts, but I'm running into difficulties making the main server wait for the called module to finish, as well as with loading each module's UI only while that module is running.
This is on R 3.4.4 and I'm developing using RStudio. I've tried using shinyjs delay but that doesn't seem to change anything.
server <- function(input, output, session){
source("module1.R")
source("module2.R")
insertUI(selector = "m1", ui = module1UI("mod1"))
val <- callModule(module1, "mod1")
code reliant on val
stop <- FALSE
while(stop == FALSE){
removeUI(selector= "m1")
insertUI(selector = "m2", ui = module2UI("mod2"))
action <- callModule(module2, "mod2")
if(action == "end"){
stop <- TRUE
}
other stuff down here
}
The issue here is that val is null because I haven't had the chance to finish interacting with module 1, and the server just keeps going.
I'm also not entirely sure that I'm creating the UI correctly (module1UI is a function(id) returning a taglist of inputs/outputs). If I create module1UI("mod1") in ui.R's fluidpage, it loads but doesn't do appear to work at all.
1 Reply
If you can estimate how long it will take for the dependent module to complete, you can try using Sys.sleep:
for (i in 1:N){
instruction 1
Sys.sleep(time_in_seconds)
instruction 2
}
Something to keep in mind with Sys.sleep is that it stops R from executing in general aside from GUI input, so if you're running a module in R alongside, this may not be the best option.
You can also try reconfiguring rshiny. There's an outline here that has a wealth of information regarding rshiny configuration. Another example for the use of rshiny is available here.
Aside from that, use of wait
might be a better option. There are some flags for wait outlined here that you may be able to build into your code.