如何使用shinny:renderUI和shinny:uioutput根据条件生成不同的输出类型

如何使用shinny:renderUI和shinny:uioutput根据条件生成不同的输出类型,r,shiny,R,Shiny,我希望能够根据先前选择的输出类型从uiOutput生成不同的输出类型,如下所示: ui <- fluidPage( titlePanel("Dynamically generated user interface components"), fluidRow( selectInput("output_type", label = "Select type of output", selected = "table", choices =

我希望能够根据先前选择的输出类型从
uiOutput
生成不同的输出类型,如下所示:

ui <- fluidPage(
  titlePanel("Dynamically generated user interface components"),
  fluidRow(
    selectInput("output_type",
      label = "Select type of output", 
      selected = "table",
      choices = c("table", "barplot", "graph") 
    ),
    uiOutput("diff_outputs")
    # textOutput("choice")
  )
)


server <- function(input, output){

  # output$choice <- renderText({
  #   switch(
  #     input$output_type,
  #     "table" = "you chose table",
  #     "barplot" = "you chose barplot",
  #     "graph" = "you chose graph"
  #   )
  #   
  # })
  get_choice <- reactive({input$choice})
  output$diff_outputs <- renderUI({
    if (is.null(input$output_type))
      return()

    switch(
      # input$output_type,
      get_choice(),
      "table" = renderTable({head(women)}),
      "barplot" = renderPlot({barplot(women$height)}),
      "graph"  = renderPlot({plot(women$height ~ women$weight)})
    )
  })
  # 
  output$output_type <- renderText({input$input_type})

}

shinyApp(ui = ui, server = server)

ui
ui非常感谢。太好了。你能建议一下如何进行模块化吗?@JonMinton你是说每种输出类型都有一个模块吗?还是什么?我会把它设为一个单独的问题,让你知道。这是另一个问题:
ui <- fluidPage(
  titlePanel("Dynamically generated user interface components"),
  fluidRow(
    selectInput("output_type",
                label = "Select type of output", 
                selected = "table",
                choices = c("table", "barplot", "graph") 
    ),
    uiOutput("diff_outputs")
  )
)

server <- function(input, output){

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

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

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

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

}

shinyApp(ui = ui, server = server)