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

时间秒杀一切 提交于 2019-12-11 16:23:06

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/56598005/how-to-use-shinyrenderui-and-shinyuioutput-to-produce-conditional-outputs-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!