闪亮的观察事件执行功能作为反应

时间:2015-09-28 10:58:07

标签: r shiny

我正在尝试在 <div id="radioGroup"> <h3>Select by Amount: </h3> <input type="radio" name="dialinfo" value="mostdialled"> <label for="mostdialled">Most Dialled Digits</label><br> <input type="radio" name="dialinfo" value="highestcost"> <label for="highestcost">Highest Costing Calls</label> <br> <h3>Select by Location: </h3> <input type="radio" name="callLocation"> <label for="callLocation">Inside the UK</label><br> <input type="radio" name="callLocation"> <label for="callLocation">International</label> <h3>Select by Cost: </h3> <input type="radio" name="costradio"> <label for="costradio">Less than &pound;1</label><br> <input type="radio" name="costradio"> <label for="costradio">More than &pound;1</label><br> <input type="radio" name="costradio"> <label for="costradio">More than &pound;5</label><br> <input type="radio" name="costradio"> <label for="costradio">More than &pound;10</label> <h3>Select Duration: </h3> <input type="radio" name="callDuration"> <label for="callDuration">Less than 60s</label><br> <input type="radio" name="callDuration"> <label for="callDuration">More than 60s</label><br> <input type="radio" name="callDuration"> <label for="callDuration">More than 1hr</label><br> <input type="radio" name="callDuration"> <label for="callDuration">More than 5hrs</label> </div>

中执行一个函数

我有

  • 两个预定义的功能:observeEvent()calculate_value()
  • 存储为反应变量的一个输入:create_my_plot()
  • 从文件中读取一个数据集myreactive()。根据输入值选择文件。
  • a使用a处理并传递给calculate_value()

它是这样的:

create_my_plot()

每当我点击动作按钮时,此事件都会激活并绘制新的绘图。但是,每当myreactive<-reactive({input$myinput}) observeEvent(input$clicks, { filename<-paste(myreactive(),".csv") a<-read.csv(filename) x<-calculate_value(a,b=myreactive()) output$myplot<-renderPlot( create_my_plot(x,myreactive()) }) 更改时,此图都会重新绘制。这很好。但是重新绘制的情节有一个问题:它仍然使用&#34; old&#34; &#34; x&#34;

的版本

每当myreactive()发生变化时,我都需要重新计算函数calculate_value()。它没有发生

我已经尝试了usinig

myreactive()

但是没有任何反应。即根本不执行该函数,并将x保留为null。 对我来说这是不可接受的,因为&#34; myreactive()&#34;正在改变数据集和绘图的描述,如果绘图没有相应地重新绘制,则完全错误。

如果myreactive()发生变化,完全删除情节就足够了,但实际上我想知道发生了什么以及为什么每次x<-reactive({ calculate_value(a,b=myreactive()) }) 重新进行计算calculate_value() {1}}可以做到。

1 个答案:

答案 0 :(得分:1)

就我而言,当我使用observerEvent()时,当某个值发生变化时,此方法中引入的所有内容都会发生变化。 Shiny是一个利用反应式编程的框架。在任何情况下,您都可以使用reactive()来重用代码并获得干净的代码。如果您使用此变量并且值更改,则值将在所有位置更改。

问题的解决方案,可以在observe()内使用observerEvent()。实际上,每次实习代码的值发生变化时,observe()都会执行其中的所有内容。

您可以看到此示例,并尝试使用它:

  testReactive<-reactive({as.numeric(input$numberTest)})

  observeEvent(input$bTest,{
    observe({   
                 x<-10+testReactive()
                 output$testing<-renderText(print(x))

            }) #Closed observe
  }) #Closed observeEvent
相关问题