1

This is a slight extension of an earlier question.

Note: This solution now works after a typo was identified in the code and corrected. I hope this is a useful pattern that others can use too.

I would like different output types to be displayed via uiOutput, but in a modular framework.

What I have so far is:

module_ui <- function(id){
  ns <- NS(id)

  tagList(
    selectInput(ns("output_type"),
                label = "Select type of output", 
                selected = "table",
                choices = c("table", "barplot", "graph") 
    ),
    uiOutput(ns("diff_outputs"))
  )
}


module_server <- function(input, output, session){
  ns <- session$ns

  output$table <- renderTable({head(women)}) 

  output$barplot <- renderPlot({barplot(women$height)})

  output$scatterplot <- renderPlot({plot(women$height ~ women$weight)})

  output_selected <- reactive({input$output_type})

  output$diff_outputs <- renderUI({
    if (is.null(output_selected()))
      return()
    switch(
      output_selected(),
      "table" = tableOutput(ns("table")),
      "barplot" = plotOutput(ns("barplot")),
      "graph" = plotOutput(ns("scatterplot"))
    )
  })

}

ui <- fluidPage(
  titlePanel("Dynamically generated user interface components"),
  fluidRow(
    module_ui("module")
  )
)

server <- function(input, output){
  callModule(module_server, "module")
}

shinyApp(ui = ui, server = server)

The problem is that uiOutput is currently blank.

If a solution were found for this kind of problem, it would be very helpful indeed.

I think only very minor modifications are needed, but I am still new to using shiny modules.

JonMinton
  • 1,239
  • 2
  • 8
  • 26

1 Answers1

1

It works but you have a typo: taglist should be tagList.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 1
    This is the first time I have seen ns() being used in the module function. You've compressed virtually all the code in the module function. Thanks for the new way of writing a shiny app. – Lazarus Thurston Feb 11 '20 at 10:37