Chaining Shiny gadgets: How to run a Shiny gadget based on return value of another Shiny gadget

Suppose I have a Shiny gadget that returns a value. Based on that value, I may want to run another Shiny gadget. Here is some example code:

library(shiny)

app1 <- shinyApp(
  fluidPage(
    "app1",
    checkboxInput("launch", "launch app2"),
    actionButton("done", "done")
  ),
  function(input, output, session) {
    observeEvent(input$done, {
      stopApp(input$launch)
    })
  }
)

app2 <- shinyApp(
  fluidPage(
    "app2",
    checkboxInput("launch", "launch app1"),
    actionButton("done", "done")
  ),
  function(input, output, session) {
    observeEvent(input$done, {
      stopApp(input$launch)
    })
  }
)

run_app1 <- function() {
  ret <- runGadget(app1, viewer = dialogViewer("app1"))
  if (ret) run_app2()
}

run_app2 <- function() {
  ret <- runGadget(app2, viewer = dialogViewer("app2"))
  if (ret) run_app1()
}

If you run run_app1() and check the box, I want app2 to run after closing. When app2 runs, if the box is checked, then app1 will run after closing. This code does now work.

I can solve it by using rstudioapi::sendToConsole("run_app2()") instead of run_app2(), but I want a solution that isn't specific to RStudio. Does anyone know how to solve this?

I don't want to place this logic inside the gadget itself, it needs to go outside of the gadget

(This is a xpost from StackOverflow, if there is an answer there, I'll update this post)