Shiny 仅在选项卡Panel 1上使用选定输入

Shiny 仅在选项卡Panel 1上使用选定输入,shiny,Shiny,我的闪亮应用程序有多个选项卡面板,我想在每个选项卡面板上都有一个selectizeInput。但是,输入仅在第一个面板上显示为更新-即selectizeInput仅在第一个选项卡上显示为工作。下面的代码有两个相同的SelectizeInput,它们是更新的。道歉,如果这是不可复制的其他地方,因为它似乎是奇怪的行为 d <- c('t','u','o') library(shiny) ui <- fluidPage( tabsetPanel( tabPanel('a',uiOutp

我的闪亮应用程序有多个选项卡面板,我想在每个选项卡面板上都有一个selectizeInput。但是,输入仅在第一个面板上显示为更新-即selectizeInput仅在第一个选项卡上显示为工作。下面的代码有两个相同的SelectizeInput,它们是更新的。道歉,如果这是不可复制的其他地方,因为它似乎是奇怪的行为

d <- c('t','u','o')
library(shiny)

ui <- fluidPage(
tabsetPanel(
 tabPanel('a',uiOutput('a')),
 tabPanel('b',uiOutput('b'))
))

server <- function(input, output, session){

output$a <- renderUI({
req(d)
selectizeInput(
  'a','test1',choices = NULL,
  options = list(placeholder = 'Please select from below'),
  multiple = TRUE)
})

observe({
req(d)
updateSelectizeInput(session, 
                     'a',choices = d,
                     selected = NULL, server = TRUE)
})

output$b <- renderUI({
req(d)
selectizeInput(
  'b','test2', choices = NULL,
  options = list(placeholder = 'Please select from below'),
  multiple = TRUE)
})

observe({
req(d)
updateSelectizeInput(session, 
                     'b',choices = d,selected = NULL,
                     server = TRUE)
})
}

shinyApp(ui, server)
d它是可复制的(谢谢!),答案很简单

问题在于元素的渲染顺序。 (描述可能很混乱,因为您对元素的命名含糊不清。)

说明:您选择的
选项卡面板是
a
,因为它是第一个可用的选项卡。因此,您的
selectizeInput
a
将被呈现。它将按照指定的方式呈现,并使用
choices=NULL

然后,两个观察者都会触发(不是因为他们观察到了什么,而是因为服务器从上到下运行所有命令,包括所有观察者)

这将导致
selectizeInput
a
被更新为
d
中的选项,而
selectizeInput
b
也被更新注意,此时,
selectizeInput
b
甚至还没有渲染

因此,当您单击
选项卡panel
b
selectizeInput
b
将第一次呈现,并且按照您的指定,它将以
choices=NULL
呈现。瞧,别无选择

解决方案:您希望您的观察者进行观察,以便在发生对观察者内容重要的事情时随时运行。在这种情况下,这是一个
选项卡面板
更改。在下面的代码中,我将
id
添加到您的
tabsetPanel
中,让观察者听到它发生的任何事情

d <- c('t','u','o')
library(shiny)

ui <- fluidPage(
  tabsetPanel(id = "x",
    tabPanel('a',uiOutput('a')),
    tabPanel('b',uiOutput('b'))
  ))

server <- function(input, output, session){

  output$a <- renderUI({
    req(d)
    selectizeInput(
      'a','test1',choices = NULL,
      options = list(placeholder = 'Please select from below'),
      multiple = TRUE)
  })

  observe({
    req(d)
    trigger <- input$x
    updateSelectizeInput(session, 
                         'a',choices = d,
                         selected = NULL, server = TRUE)
  })

  output$b <- renderUI({
    req(d)
    selectizeInput(
      'b','test2', choices = NULL,
      options = list(placeholder = 'Please select from below'),
      multiple = TRUE)
  })

  observe({
    req(d)
    trigger <- input$x
    updateSelectizeInput(session, 
                         'b',choices = d,selected = NULL,
                         server = TRUE)
  })
}

shinyApp(ui, server)
d