简体   繁体   中英

How to use shiny:renderUI and shiny:UiOutput to produce conditional outputs with shiny modules

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.

它有效,但是您有一个错字: taglist应该是tagList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM