【问题标题】:Is there a way to update the input from a sliderInput in a tabsetPanel based on the selected tabPanel in R Shiny?有没有办法根据 R Shiny 中选定的 tabPanel 更新 tabsetPanel 中的 sliderInput 的输入?
【发布时间】:2021-07-06 02:24:42
【问题描述】:

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

我的 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)

但是,这并不能提供用户界面所追求的价值。我尝试了几种方法,但都没有将值从我的 people[2|3] 输入传递到用户输入或选项卡选择时的 num_people 变量。

非常感谢提前,希望有办法实现这一目标。

【问题讨论】:

  • 您对 3 个滑块输入使用相同的 ID - 例如 sliderInput("raum",..)。尝试定义唯一 ID。
  • @YBS 的评论和解决方案可能会奏效。作为补充评论,这是一个需要使用module 的案例。
  • 感谢输入的家伙,我会看看我的案例的模块。干杯!

标签: r shiny shinydashboard


【解决方案1】:

您的变量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()))
}

【讨论】:

    猜你喜欢
    • 2013-05-16
    • 2015-08-11
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 2020-04-29
    相关资源
    最近更新 更多