Chet
Chet

Reputation: 25

Using the same variable for different conditions in conditionalPanel

I would like to set up a min, max, and range conditional panel. When "min" is selected, only the one box is shown, the other when "max" is selected, and both when "range" is selected. If the user enters a value for "min", I would like it to carry over if they toggle to "range".

Not working code below. Thanks!


library('shiny')
library('shinydashboard')

ui <- dashboardPage( title="sample code",
   dashboardHeader( title="sample code",disable=TRUE ),
   dashboardSidebar(
      box( width=12,title="sample sidebar",solidHeader=TRUE,status="warning",background = "black",
           tags$style(".skin-blue .sidebar a:link { color:orange; } a:visited {color:green;}"),
      )
   ),      
   dashboardBody(
      uiOutput("boxes")
   )
)


server <- function(input, output, session) {
   
   output$boxes <- renderUI({
      lapply( 1:5, function( inputRow ) {
         fluidRow(
                  box( width=5,
                       radioButtons( paste0( 'row', inputRow, 'Param' ), 'Set a:' , inline=TRUE, selected = "min",  c("min"="min", "max"="max", "range"="range", "none"="none")),
                       
                       conditionalPanel(condition = paste0( "input.", 'row', inputRow, "Param != 'none'") ),
      
                       conditionalPanel(condition = paste0( "input.", 'row', inputRow, "Param == 'min'" ),
                                        numericInput( paste0(  'row', inputRow,'MinLimit' ), label="Minimum", value = NULL, width = "90%") ),

                       conditionalPanel(condition = paste0( "input.", 'row', inputRow, "Param == 'max'" ),
                                        numericInput( paste0(  'row', inputRow,'MaxLimit' ), label="Maximum", value = NULL, width = "90%") ),
                       
                       conditionalPanel(condition = paste0( "input.", 'row', inputRow, "Param == 'range'" ),
                                        numericInput( paste0(  'row', inputRow,'MinLimit' ), label="Minimum", value = NULL, width = "90%"),
                                        numericInput( paste0(  'row', inputRow,'MaxLimit' ), label="Maximum", value = NULL, width = "90%") )
                  ),
                  box( width=4,
                       renderValueBox({
                          valueBox( eval( paste0(  'input$row', inputRow,'MinLimit' ) ), subtitle="i am stuck", icon = icon("list"), color = "purple" )
                       })                  
                  )
            )
         } )
   })

}

# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 521

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33397

The problem with the above code is, that you are trying to create multiple numericInput's with the same id. It is sufficient to create it once and adapt the JS condition, concerning when to display it.

Please check the following:

library('shiny')
library('shinydashboard')

ui <- dashboardPage( title="sample code",
                     dashboardHeader( title="sample code",disable=TRUE ),
                     dashboardSidebar(
                       box( width=12,title="sample sidebar",solidHeader=TRUE,status="warning",background = "black",
                            tags$style(".skin-blue .sidebar a:link { color:orange; } a:visited {color:green;}"),
                       )
                     ),      
                     dashboardBody(
                       uiOutput("boxes")
                     )
)


server <- function(input, output, session) {
  output$boxes <- renderUI({
    lapply( 1:5, function( inputRow ) {
      fluidRow(
        box( width=5,
             radioButtons( paste0( 'row', inputRow, 'Param' ), 'Set a:' , inline=TRUE, selected = "min",  c("min"="min", "max"="max", "range"="range", "none"="none")),
             conditionalPanel(condition = sprintf( "input.row%sParam != 'none'", inputRow) ),
             conditionalPanel(condition = sprintf("(input.row%sParam == 'min' || input.row%sParam == 'range')", inputRow, inputRow),
                              numericInput( paste0(  'row', inputRow,'MinLimit' ), label="Minimum", value = NULL, width = "90%") ),
             conditionalPanel(condition = sprintf("(input.row%sParam == 'max' || input.row%sParam == 'range')", inputRow, inputRow),
                              numericInput( paste0(  'row', inputRow,'MaxLimit' ), label="Maximum", value = NULL, width = "90%") )
        ),
        box(width=4,
            renderValueBox({
              inputIDmin <- paste0('row', inputRow,'MinLimit')
              inputIDmax <- paste0('row', inputRow,'MaxLimit')
              valueBox(sprintf("Min: %s | Max: %s", input[[inputIDmin]], input[[inputIDmax]]), subtitle="i am no longer stuck", icon = icon("list"), color = "purple" )
            })                  
        )
      )
    } )
  })
}

shinyApp(ui = ui, server = server)

result

Upvotes: 2

Related Questions