【发布时间】:2019-12-22 05:53:08
【问题描述】:
我正在创建一个闪亮的应用程序,其中的 selectInput 动态填充了通过 renderUI 创建的反应函数。 selectInput 位于单独的“配置”选项卡面板中。 我注意到在运行应用程序后 input$id 值保持为 NULL,直到我单击 uiOutput() 所在的 tabPanel。然后在渲染之后,为 input$id 分配“选定”值。
我尝试在需要的地方使用 req(input$id) 但它不起作用。如果我尝试在调试控制台中运行 req(input$id) ,则返回的错误只是“错误:”。我也尝试使用 shinydashboard,但得到了相同的行为。
我创建了一个可重现的最小示例。 tabPanel "A" 中的 textOutput 应该显示 selectInput 的选择。但在您点击第二个 tabPanel 之前,它什么也没有显示。
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel("one", textOutput('text')),
tabPanel("two",
uiOutput('select'))
)
)
server <- function(input, output, session) {
c <- reactive({
list("A", "B", "C")
})
output$select <- renderUI({
selectInput(
inputId = "choice",
label = "Select input",
choices = c(),
selected = 1
)
})
output$text <- renderText(
input$choice
)
}
shinyApp(ui, server)
如果我直接在第二个 tabPanel 中放置了一个 selectInput(没有 renderUI),那么 input$choice 会从应用程序开始初始化
例如
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel("one", textOutput('text')),
tabPanel("two",
selectInput(
inputId = "choice",
label = "Select input",
choices = c("A", "B", "C"),
selected = 1
))
)
)
server <- function(input, output, session) {
output$text <- renderText({
input$choice
})
}
shinyApp(ui, server)
有什么方法可以“初始化”renderUI 元素而无需加载它的 tabPanel?
谢谢
【问题讨论】:
-
添加到服务器:
outputOptions(output, "choice", suspendWhenHidden = FALSE) -
谢谢。这有效,但前提是放置在服务器中的输出分配之后。我想我更喜欢这种方法,因为涉及的代码更少。