Access Values in Modules and vice versa

Hi
I extend the example of https://shiny.rstudio.com/articles/modules.html with a tabset. First tab is handled in the 'top-app', the second tab is handle by the module. My question is, how i can access values from the 'top-app' in the module and vice versa from the module in the 'top-app'. For clarification, I added my two questions directly in the code.


counterButton <- function(id, label = "Counter") {
  ns <- NS(id)
  tagList(
    actionButton(ns("button"), label = label),
    verbatimTextOutput(ns("out"))
  )
}

counterServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
    
      ### Question 1: Can I get here the value of 'nInput'

      count <- reactiveVal(0)
      observeEvent(input$button, {
        count(count() + 1)
      })
      output$out <- renderText({
        count()
      })
      count
    }
  )
}

ui <- fluidPage(
        tabsetPanel(
                    tabPanel("tab1",numericInput("nInput","Numeric Input",value=10)),
                    tabPanel("tab2",counterButton("counter1", "Counter #1"))
        )
  
)

server <- function(input, output, session) {
  counterServer("counter1")
  
  ### Question 2: Can I get here the value of 'out'?
}

shinyApp(ui, server)

library(shiny)
counterButton <- function(id, label = "Counter") {
  ns <- NS(id)
  tagList(
    actionButton(ns("button"), label = label),
    verbatimTextOutput(ns("out"))
  )
}

counterServer <- function(id, ninput) {
  moduleServer(
    id,
    function(input, output, session) {

      ### Question 1: Can I get here the value of 'nInput'

      count <- reactiveVal(ninput)
      observeEvent(input$button, {
        count(count() + 1)
      })
      output$out <- renderText({
        count()
      })
      count
    }
  )
}

ui <- fluidPage(
  tabsetPanel(
    tabPanel("tab1",numericInput("nInput","Numeric Input",value=10)),
    tabPanel("tab2",counterButton("counter1", "Counter #1"))
  )
  
)

server <- function(input, output, session) {

               out_from_counter1 <- reactive(
                 counterServer("counter1",
                               ninput = req(input$nInput))
               )
  observeEvent(input$nInput,
               {
                 cat("input$nInput ",input$nInput,"\n")
                 print(out_from_counter1())
                 })
  
  ### Question 2: Can I get here the value of 'out'?
  # the value of out is count from the counterserver
   observeEvent(req(out_from_counter1()),
                {ofc1 <- out_from_counter1()
                cat("out_from_counter1 ",ofc1(),"\n")})
}

shinyApp(ui, server)

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.