首页 > 解决方案 > 如何在选项卡中将两个 uiOutput 提供给 renderUI

问题描述

当我在使用闪亮时尝试喂食uiOutput时,我在第一次运行时遇到错误。切换标签后,应用程序运行正常。renderUItabPanel

这是一个重现错误的最小示例

library(shiny)
ui <- fluidPage(
    tabsetPanel(
        tabPanel("Data", 
                 uiOutput("moreControls") 
        ),
        tabPanel("Research",
                 uiOutput("moreControls2") 
        )
    ),
    plotOutput("plot1")
)

server <- function(input, output) {
    output$moreControls <- renderUI({
        tagList(
            sliderInput("mean", "Mean", -10, 10, 1),
            textInput("label", "Label")
        )
    })

    output$moreControls2 <- renderUI({
        tagList(
            sliderInput("sd", "SD", 1, 50, 10),
            textInput("label2", "Label2")
        )
    })

    output$plot1 <- renderPlot({      
        hist(rnorm(n = 100,input$mean, input$sd) , xlim = c(-100, 100) )
    })

}
shinyApp(ui, server)

标签: rshiny

解决方案


@Vivek 的回答很好,但这是另一种方式:

server <- function(input, output) {
  output$moreControls <- renderUI({
    tagList(
      sliderInput("mean", "Mean", -10, 10, 1),
      textInput("label", "Label")
    )
  })

  output$moreControls2 <- renderUI({
    tagList(
      sliderInput("sd", "SD", 1, 50, 10),
      textInput("label2", "Label2")
    )
  })
  outputOptions(output, "moreControls2", suspendWhenHidden = FALSE)

  output$plot1 <- renderPlot({ 
    req(input$mean, input$sd)
    hist(rnorm(n = 100, input$mean, input$sd) , xlim = c(-100, 100) )
  })

}
shinyApp(ui, server)

在渲染input$mean之前也是不可用的,但是在切换到第二个选项卡之前不可用,因为滑块输入是隐藏的。uiOutputinput$sdinput$sd


推荐阅读