Shiny pickerInput - conditionalPanel set value only to the first selection

555 views Asked by At

I have a pickerInput with three variables and I want when I select a variable I can set the mean and sd for each variable.

Then I am using these values to compute their distributions.

The problem is that this code sets mean and sd only to the first variable that I select.

Thank you in advance!

Here is a part of my code:

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)
1

There are 1 answers

5
tamtam On

Here is another solution with (input.priors.indexOf('g') > -1) & input.checkbox == '1' as condition. In addition putting the pickerInput into a conditionalPanel the numericInputs won't show when starting the app.

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)

EDIT: Answer to @YBS question - If pickerInput needs to be on the server side for processing user supplied dataset, can you make the numericInputs not show up when starting the app?

I copied the conditionalPanel including the pickerInput into the server. Seems still to hide the 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)