在 conditionalPanel 中对不同条件使用相同的变量

Using the same variable for different conditions in conditionalPanel

我想设置一个最小值、最大值和范围条件面板。选择“min”时,仅显示一个框,选择“max”时显示另一个框,选择“range”时显示两个框。如果用户输入“min”的值,如果他们切换到“range”,我希望它能继续使用。

下面的代码无效。谢谢!


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)

以上代码的问题在于,您正试图创建多个具有相同 ID 的 numericInput。创建一次就可以了,适配JS条件,关于什么时候显示。

请检查以下内容:

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)