Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive Function Parameters

Tags:

r

shiny

My goal is to make a reactive shiny function in R. There are multiple outputs (e.g. tables) which can be bind to a similar function. However I need the function to react on some parameter, specific to one table. Here is some simple sample code, which isn't working but it makes my idea clear - I hope:

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 is more then one) I wan't to show data with different base parameter (getData(x, y, foo)). So the second table could use "getData(x, y, bar)". I don't want to write every time the same function for another table.

The solution above is not working, since reactive functions do not support parameters.

How would you solve this?

like image 446
michaelsinner Avatar asked Jan 11 '16 15:01

michaelsinner


1 Answers

This should work instead:

getData <- eventReactive(input$funParameter, {
  corrStartDate <- input$StartDate
  corrEndDate <- input$EndDate

  return(someData(corrStartDate, corrEndDate, input$funParameter))
})

eventReactive only updates if arguments stated up front change. Practically speaking, this reactive will not trigger 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 trigger if any of the inputs change

like image 104
Chris Avatar answered Nov 03 '22 07:11

Chris