0

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)
bretauv
  • 7,756
  • 2
  • 20
  • 57
K.Maria
  • 1
  • 3
  • What is the expected output? Do you want to have two `numericInput`s for each variable, or do you want to give the same mean and sd to the selected choices? – bretauv Sep 21 '20 at 09:57
  • I would like to have two numericInputs for each variable (mean, sd) in order to compute their distribution. This code seems to insert the values only to the first variable that I enter the values. – K.Maria Sep 21 '20 at 10:08

1 Answers1

2

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)
tamtam
  • 3,541
  • 1
  • 7
  • 21
  • great answer! 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? – YBS Sep 21 '20 at 12:45
  • @YBS does the edit work for you? I couldn't get `pickerInput`to work without including `conditionalPanel(...)`around it. – tamtam Sep 21 '20 at 15:36
  • works great. I didn't think of conditional panel for the `pickerInput` on the server side. I will remove my answer, as yours will suffice. – YBS Sep 21 '20 at 15:58
  • Thank you so much! Now, I can enter m and sd to more than one values. But my problem still exist. I want to use these values to compute their distribution. So, I have created a reactive function in the server. The problem is that when I run the code the app give the values (m, sd) only to the first variable I enter m and sd. – K.Maria Sep 23 '20 at 07:09
  • Here is the reactive I have created (server): model1 <- reactive ({ if (input$checkbox == '1' ){ if(input$priors =='g' ){ inp$priors$logr <- c(log(input$mg^2/sqrt(input$mg^2+input$sdg^2)),sqrt(log(input$sdg^2/input$mg^2+1)),1)} if (input$priors =='m' ) {inp$priors$logm <-c(log(input$mm^2/sqrt(input$mm^2+input$sdm^2)),sqrt(log(input$sdm^2/input$mm^2+1,1)} if (input$priors=='c' ){inp$priors$logK <-c(log(input$mc^2/sqrt(input$mc^2+input$sdc^2)), sqrt(log( input$sdc^2/input$mc^2+1)), 1)} print(inp$priors$logr) print(inp$priors$logm) print(inp$priors$logK) } – K.Maria Sep 23 '20 at 07:13