Listening module

Hello guys!

I'm facing an issue with architecting the main server module to effectively listen for calls from the smodData module triggered within the smodUserInpt module.

My current output is: [1] "Drawing..." [1] 619 [1] "Drawing..."
But I expect: [1] "Drawing..." [1] 619 [1] "Drawing..." [1] 324

I understand that a simpler solution might exist by adjusting the logic, but I specifically need the listening capability in this scenario since the actual use case is more intricate.
The code:

library(shiny)
###################################################### Module 1
smodData <- function(id, default) {
  moduleServer(id, function(input, output, session) {
    print("Drawing...")
    return(sample(1:1000, 1))
  })
}
###################################################### Module 2
umodUserInpt <- function(id) {
  ns <- NS(id)
  tagList(
    actionButton(
      inputId = ns("button"),
      label = "Click"
    )
  )
}
smodUserInpt <- function(id, default) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$button, {
      smodData("m1")
    })
  })
}
####################################################### Main
server <- function(input, output, session) {
  smodUserInpt("m2")
  observe({
    x <- smodData("m1")
    print(x)
  })
}
ui <- fluidPage(
  umodUserInpt("m2")
)
shinyApp(ui = ui, server = server)

I apologize in advance if I'm duplicating the topic, but I couldn't find a similar scenario/function :(.

Hi @StellAuror ,

is this replicating you desired output

library(shiny)
###################################################### Module 1
smodData <- function(id, default) {
  moduleServer(id, function(input, output, session) {
    print("Drawing...")
    return(sample(1:1000, 1))
  })
}
###################################################### Module 2
umodUserInpt <- function(id) {
  ns <- NS(id)
  tagList(
    actionButton(
      inputId = ns("button"),
      label = "Click"
    )
  )
}
smodUserInpt <- function(id, default) {
  moduleServer(id, function(input, output, session) {
    # initial call if you need it otherwise use data <- reactiveVal()
    data <- reactiveVal(smodData("m1"))
    observeEvent(input$button, {
      data(smodData("m1"))
    })
    return(data)
  })
}
####################################################### Main
server <- function(input, output, session) {
  x <- smodUserInpt("m2")
  observe({
    print(x())
  })
}
ui <- fluidPage(
  umodUserInpt("m2")
)
shinyApp(ui = ui, server = server)
1 Like

Hi @vedoa, thank you very much!
Just what I was looking for! It seems so obvious now... :slight_smile: Thanks!

This topic was automatically closed 7 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.