有没有办法根据 R Shiny 中选定的 tabPanel 更新 tabsetPanel 中 sliderInput 的输入?

Is there a way to update the input from a sliderInput in a tabsetPanel based on the selected tabPanel in R Shiny?

我们尝试使用 R Shiny 基于三种不同的场景进行 Covid-19 模拟。 所有场景都从 sliderInput 获得不同的默认值,需要根据所选设置和(附加)用户输入将其传递给服务器功能。

我在 UI 中得到了以下 tabSetPanel:

tabsetPanel(id = "inTabset", selected = NULL,
           tabPanel("Großraumbuero",
                    fluidRow(
                      column(4, sliderInput("people",
                                            "Personen im Raum:",
                                            2, 15, 8)) ,
                      
                      column(4, sliderInput("raum",
                                            "Raumgröße (in m²):",
                                            10, 35, 25)),
                      column(4,sliderInput("zeit",
                                           "Aufenthaltsdauer (in Std.):",
                                           1, 16, 8)),
                      
                    )),
           tabPanel("Bar",
                    fluidRow(
                      column(4, sliderInput("people2",
                                            "Personen im Raum:",
                                            2, 15, 15)) ,
                      
                      column(4, sliderInput("raum",
                                            "Raumgröße (in m²):",
                                            10, 35, 23)),
                      column(4,sliderInput("zeit",
                                           "Aufenthaltsdauer (in Std.):",
                                           1, 16, 6)),
                      
                    )),
           tabPanel("Zug",
                    fluidRow(
                      column(4, sliderInput("people3",
                                            "Personen im Raum:",
                                            2, 15, 4)) ,
                      
                      column(4, sliderInput("raum",
                                            "Raumgröße (in m²):",
                                            10, 35, 10)),
                      column(4,sliderInput("zeit",
                                           "Aufenthaltsdauer (in Std.):",
                                           1, 16, 4)),
                      
                    ))

然后在我的 R 服务器中试试:

observeEvent(input$inTabset, {
    if (input$inTabset == "Zug") {
      num_people <- input$people3
    } else if (input$inTabset == "Bar") {
      num_people <- input$people2
    } else if (input$inTabset == "Großraumbuero") {
      num_people <- input$people
    }
      
}
)
paste(num_people)

然而,这并没有提供 UI 所追求的价值。我尝试了几种方法,但都没有将我的 people[2|3] 输入的值传递给用户输入或选项卡选择上的 num_people 变量。

非常感谢,希望有办法实现这一点。

您的变量 num_people 仅在观察者内部可用。如果你想要它在外面,你可以创建一个 reactiveVal 对象。试试这个

server <- function(input, output, session) {
  numpeople <- reactiveVal(0)
  observeEvent(input$inTabset, {
    if (input$inTabset == "Zug") {
      num_people <- input$people3
    } else if (input$inTabset == "Bar") {
      num_people <- input$people2
    } else if (input$inTabset == "Großraumbuero") {
      num_people <- input$people
    }
    numpeople(num_people)
    print(num_people)
  })
  observe(print(numpeople()))
}