`expandChain` not working in modules

I'm making an app with modules in which the user can create as many UI as he wants. Each UI contain one table and I would like to give the possibility to the user to see the code for each of this table separately, not in a unique chunk. Therefore, I included the part of the code with expandChain in my module (module_server).

However, expandChain won't detect the reactive stuff I'm calling because the name of this stuff changes since it is created in a module. Take a look at the app below:

library(dplyr)
library(shiny)
library(shinymeta)
library(WDI)

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")
)

server <- function(input, output, session) {
  
  count <- reactiveValues(value = 0)
  
  observeEvent(input$launch, {
    count$value <- count$value + 1
    insertUI(selector = "#launch",
             where = "afterEnd",
             ui = module_ui(count$value))
    callModule(module_server, count$value)
  })
  
}

shinyApp(ui, server)

When I try to show the code for the table generated, I have the error:

Warning: Error in : <text>:2:2: unexpected input
1: `1_data` <- mtcars
2: 1_
    ^
  133: <Anonymous>

Since the module renames data() by adding a number, data() is not recognized by expandChain. I tried with:

expandChain(paste0(id, "_data()"))

without success (since expandChain does not support character).

Does anybody know how to do it? (also asked on StackOverflow)

Hi @bretauv. The error was caused by the callModule. You use the actionbutton value as the namespace and it is a numeric. And the value add to the front of the name, then violate the naming rule, so you can add other character (I add a 'x') before the actionbutton value will do.

library(dplyr)
library(shiny)
library(shinymeta)
library(WDI)

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")
)

server <- function(input, output, session) {
  
  count <- reactiveValues(value = 0)
  
  observeEvent(input$launch, {
    count$value <- count$value + 1
    insertUI(selector = "#launch",
             where = "afterEnd",
             ui = module_ui(paste0("x", count$value)))
    callModule(module_server, paste0("x", count$value))
  })
  
}

shinyApp(ui, server)
1 Like

@raytong this works, thanks a lot

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