2016-10-14 51 views
1

我有类似闪亮可变覆盖

step <- 0 

observeEvent(input$myInput, { 
    print(step) 
    if(!is.null(input$myInput) && step == 0 { 
     # do something 
     step <- 1 
     print(step) 
    } else if(!is.null(input$myInput) && step == 1) { 
     # do something 
     print("Been in second condition") 
    } else { 
     # do something else 
    } 
}) 

当我执行我的应用程序,并在第一次observeEvent踢,step如预期0和第一if-condition开始。最后它设置step <- 1。那里的print()显示我step1。但是,当第二次踢observeEvent时,step仍然是0。这就是print声明显示的内容。这是为什么?

+0

@ john-paul的答案是正确的。要修复它,或者使用'<< - '或者'reactiveValues()'对你的用例有意义 –

回答

1

我认为这基本上是一个范围界定问题。你的处理程序表达式 - 括号内的observeEvent中的内容被视为它自己的函数。如果您查看observeEvent的代码,则handlerExpr参数将通过exprToFunction()转换为函数。

在您的处理程序表达式step未定义,所以它从表达式外部获得值0。在表达式step内增加1,但这只是在handlerExpr的那个实例内,它不会在外部将step的原始值更改为1.