【问题标题】:Update conditional panels using action buttons使用操作按钮更新条件面板
【发布时间】:2018-01-23 21:01:25
【问题描述】:

我希望能够在 R 中使用 actionButton 更改条件面板。通过阅读相关问题,我想出了以下示例。那是行不通的!但我不知道为什么。

应用代码:

ui = fluidPage(

  conditionalPanel('output.CP_Flag == "1"', 'Condition met'),

  conditionalPanel('output.CP_Flag == "2"', 'Special case: Flag == 2'),

  conditionalPanel('output.CP_Flag != "1"', 'Condition not met'),

  selectInput(inputId = 'Selection',
              label = 'Select value, 1 meets the condition',
              choices = 1:10),

  actionButton(inputId = 'Change_Panel',
               label = 'Change Panel')

)
server = function(input, output, session) {

  CP_Flag = reactive({CP$Flag})
  output$CP_Flag = renderText({CP_Flag()})
  outputOptions(output, 'CP_Flag', suspendWhenHidden=FALSE)

  observeEvent(input$Change_Panel, Fn_CP_Function(as.numeric(input$Selection)))

}

shinyApp(ui, server)

支持代码:

CP = new.env(parent = emptyenv())

CP$Flag = 2

Fn_CP_Function = function(i){
  CP$Flag = i
}

这不是格式的问题,例如数字与字符,或者引号数量不一致,因为在 R 本身中更改 CP$Flag 会产生我期望的结果。只是actionButton没有任何作用。

我将不胜感激 (a) 任何关于为什么这不起作用的见解或 (b) 实现目标的代码 sn-p:根据操作按钮的点击更新条件面板。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    observeEvent 中,计算了一个新值,但它没有分配给任何反应变量。您必须将其分配给 reactiveVal() 并确保名为 CP_FlagrenderText 依赖于它。 请注意,CP$Flag 本身不是反应变量。因此,CP_Flag = reactive({CP$Flag}) 不会在任何时候调用函数 Fn_CP_Function 时更改 CP_Flag 反应式。

    所以这应该有效:

    CP = new.env(parent = emptyenv())
    CP$Flag = 2
    Fn_CP_Function = function(i){
      CP$Flag = i
    }
    
    ui = fluidPage(
    
      conditionalPanel('output.CP_Flag == "1"', 'Condition met'),
    
      conditionalPanel('output.CP_Flag == "2"', 'Special case: Flag == 2'),
    
      conditionalPanel('output.CP_Flag != "1"', 'Condition not met'),
    
      selectInput(inputId = 'Selection',
                  label = 'Select value, 1 meets the condition',
                  choices = 1:10),
    
      actionButton(inputId = 'Change_Panel',
                   label = 'Change Panel')
    
    )
    server = function(input, output, session) {
    
      CP_Flag = reactiveVal(CP$Flag)
      output$CP_Flag = renderText({CP_Flag()})
      outputOptions(output, 'CP_Flag', suspendWhenHidden=FALSE)
    
      observeEvent(input$Change_Panel, CP_Flag(Fn_CP_Function(as.numeric(input$Selection))))
    
    }
    
    shinyApp(ui, server)
    

    此外,您可以使用更少的代码来完成此操作。您的代码可能是大型程序的一部分,并且您可能有充分的理由创建新环境和单独的功能,但我认为无论如何分享它会很好,因为它可能有助于您的理解:

    ui = fluidPage(
    
      conditionalPanel('output.CP_Flag == "1"', 'Condition met'),
      conditionalPanel('output.CP_Flag == "2"', 'Special case: Flag == 2'),
      conditionalPanel('output.CP_Flag != "1"', 'Condition not met'),
      selectInput(inputId = 'Selection',
                  label = 'Select value, 1 meets the condition',
                  choices = 1:10),
    
      actionButton(inputId = 'Change_Panel',
                   label = 'Change Panel')
    
    )
    server = function(input, output, session) {
    
      CP_Flag = reactiveVal(1)
      observeEvent(input$Change_Panel, CP_Flag(input$Selection))
      output$CP_Flag = renderText({CP_Flag()})
      outputOptions(output, 'CP_Flag', suspendWhenHidden=FALSE)
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多