Re-use reactive elements defined in modules

I'm making an app in which the user can create as many tables as he/she wants and display the code necessary to remake each individual table using shinymeta. I can generate the code for each of these tables but I have a problem when I want to create a complete modal that shows every code for each table.

To be clearer, here's a reproducible example:

library(shiny)
library(shinymeta)

module_ui <- function(id){
  ns <- NS(id)
  
  tagList(
    fluidRow(
      actionButton(ns("show_table"), "Show table"),
      actionButton(ns("show_code"), "Show code"),
      tableOutput(ns("table"))
    )
  )
}

module_server <- function(input, output, session){
  data <- metaReactive2({
    req(input$show_table)
    
    isolate(metaExpr({
      mtcars 
    }))
  })
  
  output$table <- renderTable({
    data()
  })
  
  observeEvent(input$show_code, {
    showModal(modalDialog(
      renderPrint({
        expandChain(data())
      })
    ))
  })
}

ui <- fluidPage(
  actionButton("launch", "Launch"),
  actionButton("show_full_code", "Show the full code (at least 2 'launch' before)")
)

server <- function(input, output, session) {
  
  count <- reactiveValues(value = 0)
  
  observeEvent(input$launch, {
    count$value <- count$value + 1
    insertUI(selector = "#show_full_code",
             where = "afterEnd",
             ui = module_ui(paste0("x", count$value)))
    callModule(module_server, paste0("x", count$value))
  })
  
  #### "Merge" the single code modals in one big
  observeEvent(input$show_full_code, {
    showModal(modalDialog(
      renderPrint({
        expandChain(x1_data, x2_data)
      })
    ))
  })
  
}

shinyApp(ui, server)

When you click on "Launch", two buttons are generated and you can display a table ("Show table") and the code to remake this table ("Show code"). You can click on "Launch" indefinitely and the table will be named x1_data, x2_data, etc.

However, when I try to generate the code that unites every individual code (by clicking on "Show the full code"), x1_data is not found. Using x1_data() does not work either. I'm not a fan of asking two questions in one post but I will do this now:

  • How can I access the reactive elements created inside modules?
  • How can I "merge" every individual code in a big one?

Also asked on StackOverflow

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