Reactive Function Parameters

My goal is to make a reactive brilliant function in R. There are several outputs (e.g. tables) that can be associated with a similar function. However, I need a function to respond to some parameter specific to one table. Here are some simple examples of code that doesn't work, but that makes my idea clear - hopefully:

output$tableOne <- DT::renderDataTable({ getData(foo) }) getData <- reactive(function(funParameter){ corrStartDate <- input$StartDate corrEndDate <- input$EndDate return(someData(corrStartDate, corrEndDate, funParameter)) }) 

In all tables (if there are more than one), I will not show data with a different base parameter (getData (x, y, foo)). So the second table can use "getData (x, y, bar)". I do not want to write the same function every time for another table.

The solution above does not work, because reactive functions do not support parameters.

How would you solve this?

+5
source share
1 answer

This should work instead:

 getData <- eventReactive(input$funParameter, { corrStartDate <- input$StartDate corrEndDate <- input$EndDate return(someData(corrStartDate, corrEndDate, input$funParameter)) }) 

eventReactive updated only if the arguments specified before the change are changed. In practice, this reactive signal will not fire if input$StartDate or input$EndDate changes.

If this is not what you want, normal reactive functions should work. I.e:.

 getData <- reactive({ funParameter <- input$funParameter corrStartDate <- input$StartDate corrEndDate <- input$EndDate return(someData(corrStartDate, corrEndDate, funParameter)) }) 

which will fire if any of the inputs changes

+10
source

Source: https://habr.com/ru/post/1240352/


All Articles