使用操作按钮更新条件面板

时间:2018-01-23 11:40:50

标签: r shiny

我希望能够使用actionButton更改R shiny中的条件面板。阅读相关问题,我想出了以下示例。这不起作用!但我无法弄清楚原因。

应用代码:

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)实现目标的代码段:根据actionButtons的点击更新条件面板。

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <style> .class1{ background-size: 100% 100%; background-image: url(http://haulihuvila.com/wp-content/uploads/2012/09/hauli-huvila-campgrounds-lg.jpg); } </style> </head> <body> <div class="card" style="height: 20em;width: 17em;"> <div class="class1" style="height:17em;width:17em;background-color: #5f5f5f"> </div> <div class="card-body"> <h3 class="card-title">help me</h3> <a href="#" class="btn btn-primary">More Info</a> </div> </div </body> </html>中,计算新值,但不会将其分配给任何反应变量。您必须将其分配给observeEvent,并确保reactiveVal() renderText依赖于此CP_Flag。 请注意,CP $ Flag本身不是一个反应变量。因此,CP_Flag = reactive({CP$Flag})在调用函数CP_Flag时不会更改Fn_CP_Function被动。

所以这应该有效:

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)