Shiny pickerInput - conditionalPanel 仅将值设置为第一个选择

Posted

技术标签:

【中文标题】Shiny pickerInput - conditionalPanel 仅将值设置为第一个选择【英文标题】:Shiny pickerInput - conditionalPanel set value only to the first selection 【发布时间】:2021-01-07 08:16:30 【问题描述】:

我有一个带有三个变量的pickerInput,当我选择一个变量时,我可以为每个变量设置平均值和标准差。

然后我使用这些值来计算它们的分布。

问题是这段代码只将 mean 和 sd 设置为我选择的第一个变量。

提前谢谢你!

这是我的代码的一部分:

library(shiny)
library(shinyWidgets)
ui <-
  fluidPage(
    #3variables are g, m, c
    checkboxInput("checkbox", label = "Use priors"),
    uiOutput("conditionalInput"),
    conditionalPanel(
      condition = "input.priors == 'g'",
      numericInput("mg", "Mean:", NULL, min = 0, max = 1000000),
      numericInput("sdg", "Sd:", NULL , min = 0, max = 1000000)
    ),
    
    
    conditionalPanel(
      condition = "input.priors == 'm'",
      numericInput("mm", "Mean:", NULL, min = 0, max =
                     1000000),
      numericInput("sdm", "Sd:", NULL, min = 0, max = 1000000)
    ),
    
    conditionalPanel(
      condition = "input.priors == 'c'",
      numericInput("mc", "Mean:", NULL, min = 0, max = 1000000),
      numericInput("sdc", "Sd:", NULL, min = 0, max = 1000000)
    )
    
  )

server <- function(input, output, session) 
  output$conditionalInput <- renderUI(
    if (input$checkbox == TRUE) 
      pickerInput("priors",
                  "Select priors",
                  c(
                    "r" = "g",
                    "m" = "m",
                    "K" = "c"
                  ),
                  multiple = T)
    
  )
  

shinyApp(ui = ui, server = server)

【问题讨论】:

预期输出是多少?您想为每个变量设置两个numericInputs,还是想为所选选项赋予相同的均值和标准差? 我想为每个变量(平均值,标准差)设置两个数字输入,以便计算它们的分布。这段代码似乎只将值插入到我输入值的第一个变量中。 【参考方案1】:

这是另一个以(input.priors.indexOf('g') &gt; -1) &amp; input.checkbox == '1' 为条件的解决方案。除了将pickerInput 放入conditionalPanel 之外,启动应用程序时numericInputs 也不会显示。

library(shiny)
library(shinyWidgets)
ui <-
  fluidPage(
    #3variables are g, m, c
    checkboxInput("checkbox", label = "Use priors"),
    
    conditionalPanel(
      condition = "input.checkbox == '1'",
      pickerInput("priors", "Select priors", c("r" = "g", "m" = "m", "K" = "c"),
                  multiple = T) 
    ), 
    
    conditionalPanel(
      condition = "(input.priors.indexOf('g') > -1) & input.checkbox == '1'",
      numericInput("mg", "Mean-r:", NULL, min = 0, max = 1000000),
      numericInput("sdg", "Sd:-r", NULL , min = 0, max = 1000000)
    ),
    
    conditionalPanel(
      condition = "(input.priors.indexOf('m') > -1) & input.checkbox == 1",
      numericInput("mm", "Mean-m:", NULL, min = 0, max =
                     1000000),
      numericInput("sdm", "Sd-m:", NULL, min = 0, max = 1000000)
    ),
    
    conditionalPanel(
      condition = "(input.priors.indexOf('c') > -1) & input.checkbox == '1'",
      numericInput("mc", "Mean-K:", NULL, min = 0, max = 1000000),
      numericInput("sdc", "Sd-K:", NULL, min = 0, max = 1000000)
    )
  )

server <- function(input, output, session) 
    
  

shinyApp(ui = ui, server = server)

编辑:回答@YBS 问题 - 如果pickerInput 需要在服务器端来处理用户提供的数据集,你可以让 numericInputs 在启动应用程序时不显示吗?

我将包含 pickerInput 的条件面板复制到服务器中。似乎仍然隐藏 numericInputs。

library(shiny)
library(shinyWidgets)
ui <-
  fluidPage(
    #3variables are g, m, c
    checkboxInput("checkbox", label = "Use priors"),
    
    uiOutput("conditionalInput"),
    
    conditionalPanel(
      condition = "(input.priors.indexOf('g') > -1 & input.checkbox == 1) ",
      numericInput("mg", "Mean-r:", NULL, min = 0, max = 1000000),
      numericInput("sdg", "Sd:-r", NULL , min = 0, max = 1000000)
    ),
    
    conditionalPanel(
      condition = "(input.priors.indexOf('m') > -1 & input.checkbox == 1) ",
      numericInput("mm", "Mean-m:", NULL, min = 0, max =
                     1000000),
      numericInput("sdm", "Sd-m:", NULL, min = 0, max = 1000000)
    ),
    
    conditionalPanel(
      condition = "(input.priors.indexOf('c') > -1 & input.checkbox == 1) ",
      numericInput("mc", "Mean-K:", NULL, min = 0, max = 1000000),
      numericInput("sdc", "Sd-K:", NULL, min = 0, max = 1000000)
    )
  )

server <- function(input, output, session) 
  

  output$conditionalInput <- renderUI(
    
    conditionalPanel(
      condition = "input.checkbox == '1'",
      pickerInput("priors", "Select priors", c("r" = "g", "m" = "m", "K" = "c"),
                  multiple = T))
    
  )
  
  

shinyApp(ui = ui, server = server)

【讨论】:

很好的答案!如果pickerInput 需要在服务器端处理用户提供的数据集,您可以让numericInputs 在启动应用程序时不显示吗? @YBS 编辑对您有用吗?如果不包括conditionalPanel(...),我无法让pickerInput工作。 效果很好。我没有想到服务器端pickerInput 的条件面板。我会删除我的答案,你的就足够了。 非常感谢!现在,我可以将 m 和 sd 输入到多个值。但我的问题仍然存在。我想使用这些值来计算它们的分布。所以,我在服务器中创建了一个反应函数。问题是,当我运行代码时,应用程序仅将值 (m, sd) 提供给我输入 m 和 sd 的第一个变量。 这是我创建的响应式(服务器): model1

以上是关于Shiny pickerInput - conditionalPanel 仅将值设置为第一个选择的主要内容,如果未能解决你的问题,请参考以下文章

R Shiny pickerInput 选择所有文本

Shiny pickerInput - conditionalPanel 仅将值设置为第一个选择

使用两个相关的 selectInput 过滤 R Shiny 中的数据帧

在 Shiny 中更改 Picker Input 的颜色

在R中使用条件面板和PickerInput

有没有办法从shinyWidgets 的pickerInput 上选择一整组选项?