Updating a numeric input reactively in a module based on a value computed in another module

I've an app with two modules (say modules a and b). Module b depends on module a in that it has a field that needs to be updated based on a computation occurring in module a. The app is designed so that users go through module a before module b.

In my actual code, I use an R6 class to run the computation which is basically a shell command calling an external tool that generates a file in a temporary directory. I then use a method to fetch data in that generated file to be passed to module b. I oversimplied the class in my example to keep it simple.

What I'm trying to do is to update the numeric input in module b in a reactive way as soon as the computation in module a is done.

Currently, I manage to do what I want using the gargoyle package as shown in the code below. I was wondering how I can replicate that behaviour without using gargoyle but reactive values directly.

Foo <- R6::R6Class(
  "Foo",
  public = list(
    run = function() {
      Sys.sleep(.5)
      private$n <- 1 + 1
    },
    fetch = function() {
      private$n
    }
  ),
  private = list(
    n = NULL
  )
)

library(shiny)

mod_a_ui <- function(id) {
  ns <- NS(id)
  actionButton(ns("btn"), "Click me!")
}

mod_a_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    o <- Foo$new()
    gargoyle::init("i")
    observeEvent(input$btn, {
      o$run()
      gargoyle::trigger("i")
    })
    o
  })
}

mod_b_ui <- function(id) {
  ns <- NS(id)
  numericInput(ns("n"), "n", value = NULL)
}

mod_b_server <- function(id, r6) {
  moduleServer(id, function(input, output, session) {
    observeEvent(gargoyle::watch("i"), {
      updateNumericInput(inputId = "n", value = r6$fetch())
    })
  })
}

ui <- fluidPage(
  mod_a_ui("a"),
  mod_b_ui("b"),
)

server <- function(input, output, session) {
  o <- mod_a_server("a")
  mod_b_server("b", o)
}


shinyApp(ui, server)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.